test_win_dotnet.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.utils.platform
  6. import salt.utils.win_dotnet as win_dotnet
  7. # Import Salt Testing Libs
  8. from tests.support.mock import MagicMock, patch
  9. from tests.support.unit import TestCase, skipIf
  10. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  11. class WinDotNetTestCase(TestCase):
  12. """
  13. Test cases for salt.utils.win_dotnet
  14. """
  15. def setUp(self):
  16. self.mock_reg_list = MagicMock(
  17. return_value=["CDF", "v2.0.50727", "v3.0", "v3.5", "v4", "v4.0"]
  18. )
  19. self.mock_reg_exists = MagicMock(
  20. side_effect=[
  21. True, # Version exists in v2.0.50727
  22. True, # Version exists in v3.0
  23. True, # Version exists in v3.5
  24. False, # Version not in v4
  25. True, # Release exists in v4
  26. False, # Version not in v4.0
  27. False, # Release not in v4.0
  28. ]
  29. )
  30. self.mock_reg_read = MagicMock(
  31. side_effect=[
  32. # v2.0.50727
  33. {"vdata": 1},
  34. {"vdata": "2.0.50727.4927"},
  35. {"vdata": 2},
  36. # v3.0
  37. {"vdata": 1},
  38. {"vdata": "3.0.30729.4926"},
  39. {"vdata": 2},
  40. # v3.5
  41. {"vdata": 1},
  42. {"vdata": "3.5.30729.4926"},
  43. {"vdata": 1},
  44. # v4
  45. {"vdata": 1},
  46. {"vdata": 461814},
  47. ]
  48. )
  49. def test_versions(self):
  50. """
  51. Test the versions function
  52. """
  53. expected = {
  54. "details": {
  55. "v2.0.50727": {
  56. "full": "2.0.50727.4927 SP2",
  57. "service_pack": 2,
  58. "version": "2.0.50727.4927",
  59. },
  60. "v3.0": {
  61. "full": "3.0.30729.4926 SP2",
  62. "service_pack": 2,
  63. "version": "3.0.30729.4926",
  64. },
  65. "v3.5": {
  66. "full": "3.5.30729.4926 SP1",
  67. "service_pack": 1,
  68. "version": "3.5.30729.4926",
  69. },
  70. "v4": {"full": "4.7.2", "service_pack": "N/A", "version": "4.7.2"},
  71. },
  72. "versions": ["2.0.50727.4927", "3.0.30729.4926", "3.5.30729.4926", "4.7.2"],
  73. }
  74. with patch("salt.utils.win_reg.list_keys", self.mock_reg_list), patch(
  75. "salt.utils.win_reg.value_exists", self.mock_reg_exists
  76. ), patch("salt.utils.win_reg.read_value", self.mock_reg_read):
  77. result = win_dotnet.versions()
  78. self.assertDictEqual(result, expected)
  79. def test_versions_list(self):
  80. expected = ["2.0.50727.4927", "3.0.30729.4926", "3.5.30729.4926", "4.7.2"]
  81. with patch("salt.utils.win_reg.list_keys", self.mock_reg_list), patch(
  82. "salt.utils.win_reg.value_exists", self.mock_reg_exists
  83. ), patch("salt.utils.win_reg.read_value", self.mock_reg_read):
  84. result = win_dotnet.versions_list()
  85. self.assertListEqual(result, expected)
  86. def test_versions_details(self):
  87. """
  88. Test the versions function
  89. """
  90. expected = {
  91. "v2.0.50727": {
  92. "full": "2.0.50727.4927 SP2",
  93. "service_pack": 2,
  94. "version": "2.0.50727.4927",
  95. },
  96. "v3.0": {
  97. "full": "3.0.30729.4926 SP2",
  98. "service_pack": 2,
  99. "version": "3.0.30729.4926",
  100. },
  101. "v3.5": {
  102. "full": "3.5.30729.4926 SP1",
  103. "service_pack": 1,
  104. "version": "3.5.30729.4926",
  105. },
  106. "v4": {"full": "4.7.2", "service_pack": "N/A", "version": "4.7.2"},
  107. }
  108. with patch("salt.utils.win_reg.list_keys", self.mock_reg_list), patch(
  109. "salt.utils.win_reg.value_exists", self.mock_reg_exists
  110. ), patch("salt.utils.win_reg.read_value", self.mock_reg_read):
  111. result = win_dotnet.versions_details()
  112. self.assertDictEqual(result, expected)
  113. def test_version_atleast_35(self):
  114. with patch("salt.utils.win_reg.list_keys", self.mock_reg_list), patch(
  115. "salt.utils.win_reg.value_exists", self.mock_reg_exists
  116. ), patch("salt.utils.win_reg.read_value", self.mock_reg_read):
  117. self.assertTrue(win_dotnet.version_at_least("3.5"))
  118. def test_version_atleast_47(self):
  119. with patch("salt.utils.win_reg.list_keys", self.mock_reg_list), patch(
  120. "salt.utils.win_reg.value_exists", self.mock_reg_exists
  121. ), patch("salt.utils.win_reg.read_value", self.mock_reg_read):
  122. self.assertTrue(win_dotnet.version_at_least("4.7"))
  123. def test_version_atleast_472(self):
  124. with patch("salt.utils.win_reg.list_keys", self.mock_reg_list), patch(
  125. "salt.utils.win_reg.value_exists", self.mock_reg_exists
  126. ), patch("salt.utils.win_reg.read_value", self.mock_reg_read):
  127. self.assertTrue(win_dotnet.version_at_least("4.7.2"))
  128. def test_version_not_atleast_473(self):
  129. with patch("salt.utils.win_reg.list_keys", self.mock_reg_list), patch(
  130. "salt.utils.win_reg.value_exists", self.mock_reg_exists
  131. ), patch("salt.utils.win_reg.read_value", self.mock_reg_read):
  132. self.assertFalse(win_dotnet.version_at_least("4.7.3"))