test_chocolatey.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # -*- coding: utf-8 -*-
  2. """
  3. Test for the chocolatey module
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import
  7. import os
  8. # Import Salt Libs
  9. import salt.modules.chocolatey as chocolatey
  10. import salt.utils
  11. import salt.utils.platform
  12. # Import Salt Testing Libs
  13. from tests.support.mixins import LoaderModuleMockMixin
  14. from tests.support.mock import MagicMock, patch
  15. from tests.support.unit import TestCase, skipIf
  16. @skipIf(not salt.utils.platform.is_windows(), "Not a Windows system")
  17. class ChocolateyTestCase(TestCase, LoaderModuleMockMixin):
  18. """
  19. Chocolatey private functions tests
  20. """
  21. @classmethod
  22. def setUpClass(cls):
  23. cls.choco_path = "C:\\path\\to\\chocolatey.exe"
  24. cls.choco_path_pd = os.path.join(
  25. os.environ.get("ProgramData"), "Chocolatey", "bin", "chocolatey.exe"
  26. )
  27. cls.choco_path_sd = os.path.join(
  28. os.environ.get("SystemDrive"), "Chocolatey", "bin", "chocolatey.bat"
  29. )
  30. cls.mock_false = MagicMock(return_value=False)
  31. cls.mock_true = MagicMock(return_value=True)
  32. @classmethod
  33. def tearDownClass(cls):
  34. del cls.choco_path
  35. del cls.choco_path_pd
  36. del cls.choco_path_sd
  37. del cls.mock_false
  38. del cls.mock_true
  39. def setup_loader_modules(self):
  40. return {chocolatey: {"__context__": {}, "__salt__": {}}}
  41. def test__clear_context(self):
  42. """
  43. Tests _clear_context function
  44. """
  45. context = {
  46. "chocolatey._yes": ["--yes"],
  47. "chocolatey._path": self.choco_path,
  48. "chocolatey._version": "0.9.9",
  49. }
  50. with patch.dict(chocolatey.__context__, context):
  51. chocolatey._clear_context()
  52. # Did it clear all chocolatey items from __context__P?
  53. self.assertEqual(chocolatey.__context__, {})
  54. def test__yes_context(self):
  55. """
  56. Tests _yes function when it exists in __context__
  57. """
  58. with patch.dict(chocolatey.__context__, {"chocolatey._yes": ["--yes"]}):
  59. result = chocolatey._yes()
  60. expected = ["--yes"]
  61. # Did it return correctly
  62. self.assertListEqual(result, expected)
  63. # Did it populate __context__
  64. self.assertEqual(chocolatey.__context__["chocolatey._yes"], expected)
  65. def test__yes_version_greater(self):
  66. """
  67. Test _yes when Chocolatey version is greater than 0.9.9
  68. """
  69. mock_version = MagicMock(return_value="10.0.0")
  70. with patch("salt.modules.chocolatey.chocolatey_version", mock_version):
  71. result = chocolatey._yes()
  72. expected = ["--yes"]
  73. # Did it return correctly
  74. self.assertListEqual(result, expected)
  75. # Did it populate __context__
  76. self.assertEqual(chocolatey.__context__["chocolatey._yes"], expected)
  77. def test__yes_version_less_than(self):
  78. """
  79. Test _yes when Chocolatey version is less than 0.9.9
  80. """
  81. mock_version = MagicMock(return_value="0.9.0")
  82. with patch("salt.modules.chocolatey.chocolatey_version", mock_version):
  83. result = chocolatey._yes()
  84. expected = []
  85. # Did it return correctly
  86. self.assertListEqual(result, expected)
  87. # Did it populate __context__
  88. self.assertEqual(chocolatey.__context__["chocolatey._yes"], expected)
  89. def test__find_chocolatey_context(self):
  90. """
  91. Test _find_chocolatey when it exists in __context__
  92. """
  93. with patch.dict(chocolatey.__context__, {"chocolatey._path": self.choco_path}):
  94. result = chocolatey._find_chocolatey()
  95. expected = self.choco_path
  96. self.assertEqual(result, expected)
  97. def test__find_chocolatey_which(self):
  98. """
  99. Test _find_chocolatey when found with `cmd.which`
  100. """
  101. mock_which = MagicMock(return_value=self.choco_path)
  102. with patch.dict(chocolatey.__salt__, {"cmd.which": mock_which}):
  103. result = chocolatey._find_chocolatey()
  104. expected = self.choco_path
  105. # Does it return the correct path
  106. self.assertEqual(result, expected)
  107. # Does it populate __context__
  108. self.assertEqual(chocolatey.__context__["chocolatey._path"], expected)
  109. def test__find_chocolatey_programdata(self):
  110. """
  111. Test _find_chocolatey when found in ProgramData
  112. """
  113. with patch.dict(chocolatey.__salt__, {"cmd.which": self.mock_false}), patch(
  114. "os.path.isfile", self.mock_true
  115. ):
  116. result = chocolatey._find_chocolatey()
  117. expected = self.choco_path_pd
  118. # Does it return the correct path
  119. self.assertEqual(result, expected)
  120. # Does it populate __context__
  121. self.assertEqual(chocolatey.__context__["chocolatey._path"], expected)
  122. def test__find_chocolatey_systemdrive(self):
  123. """
  124. Test _find_chocolatey when found on SystemDrive (older versions)
  125. """
  126. with patch.dict(chocolatey.__salt__, {"cmd.which": self.mock_false}), patch(
  127. "os.path.isfile", MagicMock(side_effect=[False, True])
  128. ):
  129. result = chocolatey._find_chocolatey()
  130. expected = self.choco_path_sd
  131. # Does it return the correct path
  132. self.assertEqual(result, expected)
  133. # Does it populate __context__
  134. self.assertEqual(chocolatey.__context__["chocolatey._path"], expected)
  135. def test_version_check_remote_false(self):
  136. """
  137. Test version when remote is False
  138. """
  139. list_return_value = {"ack": ["3.1.1"]}
  140. with patch.object(chocolatey, "list_", return_value=list_return_value):
  141. expected = {"ack": ["3.1.1"]}
  142. result = chocolatey.version("ack", check_remote=False)
  143. self.assertEqual(result, expected)
  144. def test_version_check_remote_true(self):
  145. """
  146. Test version when remote is True
  147. """
  148. list_side_effect = [
  149. {"ack": ["3.1.1"]},
  150. {"ack": ["3.1.1"], "Wolfpack": ["3.0.17"], "blackbird": ["1.0.79.3"]},
  151. ]
  152. with patch.object(chocolatey, "list_", side_effect=list_side_effect):
  153. expected = {"ack": {"available": ["3.1.1"], "installed": ["3.1.1"]}}
  154. result = chocolatey.version("ack", check_remote=True)
  155. self.assertEqual(result, expected)
  156. def test_version_check_remote_true_not_available(self):
  157. """
  158. Test version when remote is True but remote version is unavailable
  159. """
  160. list_side_effect = [
  161. {"ack": ["3.1.1"]},
  162. {"Wolfpack": ["3.0.17"], "blackbird": ["1.0.79.3"]},
  163. ]
  164. with patch.object(chocolatey, "list_", side_effect=list_side_effect):
  165. expected = {"ack": {"installed": ["3.1.1"]}}
  166. result = chocolatey.version("ack", check_remote=True)
  167. self.assertEqual(result, expected)