test_salt_version.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # -*- coding: utf-8 -*-
  2. """
  3. Unit tests for salt/modules/salt_version.py
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import salt.modules.salt_version as salt_version
  8. import salt.version
  9. # Import Salt libs
  10. from salt.ext import six
  11. from tests.support.mock import MagicMock, patch
  12. # Import Salt Testing libs
  13. from tests.support.unit import TestCase
  14. class SaltVersionTestCase(TestCase):
  15. """
  16. Test cases for salt.modules.salt_version
  17. """
  18. def test_mocked_objects(self):
  19. """
  20. Test that the mocked objects actually have what we expect.
  21. For example, earlier tests incorrectly mocked the
  22. salt.version.SaltStackVersion.LNAMES dict using upper-case indexes
  23. """
  24. assert isinstance(salt.version.SaltStackVersion.LNAMES, dict)
  25. sv = salt.version.SaltStackVersion(*salt.version.__version_info__)
  26. for k, v in salt.version.SaltStackVersion.LNAMES.items():
  27. assert k == k.lower()
  28. assert isinstance(v, tuple)
  29. if sv.new_version(major=v[0]):
  30. assert len(v) == 1
  31. else:
  32. assert len(v) == 2
  33. sv = sv.__str__()
  34. assert isinstance(sv, six.string_types)
  35. with patch("salt.version.SaltStackVersion.LNAMES", {"neon": (2019, 8)}):
  36. sv = salt.version.SaltStackVersion.from_name("Neon")
  37. self.assertEqual(sv.string, "2019.8.0")
  38. # get_release_number tests: 3
  39. def test_get_release_number_no_codename(self):
  40. """
  41. Test that None is returned when the codename isn't found.
  42. """
  43. assert salt_version.get_release_number("foo") is None
  44. @patch("salt.version.SaltStackVersion.LNAMES", {"foo": (12345, 0)})
  45. def test_get_release_number_unassigned(self):
  46. """
  47. Test that a string is returned when a version is found, but unassigned.
  48. """
  49. mock_str = "No version assigned."
  50. assert salt_version.get_release_number("foo") == mock_str
  51. def test_get_release_number_success(self):
  52. """
  53. Test that a version is returned for a released codename
  54. """
  55. assert salt_version.get_release_number("Oxygen") == "2018.3"
  56. def test_get_release_number_success_new_version(self):
  57. """
  58. Test that a version is returned for new versioning (3000)
  59. """
  60. assert salt_version.get_release_number("Neon") == "3000"
  61. # equal tests: 3
  62. @patch("salt.version.SaltStackVersion.LNAMES", {"foo": (1900, 5)})
  63. @patch("salt.version.SaltStackVersion", MagicMock(return_value="1900.5.0"))
  64. def test_equal_success(self):
  65. """
  66. Test that the current version is equal to the codename
  67. """
  68. assert salt_version.equal("foo") is True
  69. @patch("salt.version.SaltStackVersion.LNAMES", {"foo": (3000,)})
  70. @patch("salt.version.SaltStackVersion", MagicMock(return_value="3000.1"))
  71. def test_equal_success_new_version(self):
  72. """
  73. Test that the current version is equal to the codename
  74. while using the new versioning
  75. """
  76. assert salt_version.equal("foo") is True
  77. @patch(
  78. "salt.version.SaltStackVersion.LNAMES",
  79. {"oxygen": (2018, 3), "nitrogen": (2017, 7)},
  80. )
  81. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  82. def test_equal_older_codename(self):
  83. """
  84. Test that when an older codename is passed in, the function returns False.
  85. """
  86. assert salt_version.equal("Nitrogen") is False
  87. @patch(
  88. "salt.version.SaltStackVersion.LNAMES", {"neon": (3000), "nitrogen": (2017, 7)}
  89. )
  90. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  91. def test_equal_older_codename_new_version(self):
  92. """
  93. Test that when an older codename is passed in, the function returns False.
  94. while also testing with the new versioning.
  95. """
  96. assert salt_version.equal("Nitrogen") is False
  97. @patch(
  98. "salt.version.SaltStackVersion.LNAMES",
  99. {"fluorine": (salt.version.MAX_SIZE - 100, 0)},
  100. )
  101. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  102. def test_equal_newer_codename(self):
  103. """
  104. Test that when a newer codename is passed in, the function returns False
  105. """
  106. assert salt_version.equal("Fluorine") is False
  107. # greater_than tests: 4
  108. @patch(
  109. "salt.modules.salt_version.get_release_number", MagicMock(return_value="2017.7")
  110. )
  111. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  112. def test_greater_than_success(self):
  113. """
  114. Test that the current version is newer than the codename
  115. """
  116. assert salt_version.greater_than("Nitrogen") is True
  117. @patch(
  118. "salt.modules.salt_version.get_release_number", MagicMock(return_value="2017.7")
  119. )
  120. @patch("salt.version.SaltStackVersion", MagicMock(return_value="3000"))
  121. def test_greater_than_success_new_version(self):
  122. """
  123. Test that the current version is newer than the codename
  124. """
  125. assert salt_version.greater_than("Nitrogen") is True
  126. @patch("salt.version.SaltStackVersion.LNAMES", {"oxygen": (2018, 3)})
  127. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  128. def test_greater_than_with_equal_codename(self):
  129. """
  130. Test that when an equal codename is passed in, the function returns False.
  131. """
  132. assert salt_version.greater_than("Oxygen") is False
  133. @patch(
  134. "salt.version.SaltStackVersion.LNAMES",
  135. {"fluorine": (2019, 2), "oxygen": (2018, 3)},
  136. )
  137. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  138. def test_greater_than_with_newer_codename(self):
  139. """
  140. Test that when a newer codename is passed in, the function returns False.
  141. """
  142. assert salt_version.greater_than("Fluorine") is False
  143. @patch(
  144. "salt.modules.salt_version.get_release_number",
  145. MagicMock(return_value="No version assigned."),
  146. )
  147. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  148. def test_greater_than_unassigned(self):
  149. """
  150. Test that the unassigned codename is greater than the current version
  151. """
  152. assert salt_version.greater_than("Fluorine") is False
  153. # less_than tests: 4
  154. @patch(
  155. "salt.modules.salt_version.get_release_number", MagicMock(return_value="2019.2")
  156. )
  157. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  158. def test_less_than_success(self):
  159. """
  160. Test that when a newer codename is passed in, the function returns True.
  161. """
  162. assert salt_version.less_than("Fluorine") is True
  163. @patch(
  164. "salt.modules.salt_version.get_release_number", MagicMock(return_value="3000")
  165. )
  166. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  167. def test_less_than_success_new_version(self):
  168. """
  169. Test that when a newer codename is passed in, the function returns True
  170. using new version
  171. """
  172. assert salt_version.less_than("Fluorine") is True
  173. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  174. @patch("salt.version.SaltStackVersion.LNAMES", {"oxygen": (2018, 3)})
  175. def test_less_than_with_equal_codename(self):
  176. """
  177. Test that when an equal codename is passed in, the function returns False.
  178. """
  179. assert salt_version.less_than("Oxygen") is False
  180. @patch(
  181. "salt.modules.salt_version.get_release_number", MagicMock(return_value="2017.7")
  182. )
  183. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  184. def test_less_than_with_older_codename(self):
  185. """
  186. Test that the current version is less than the codename.
  187. """
  188. assert salt_version.less_than("Nitrogen") is False
  189. @patch(
  190. "salt.modules.salt_version.get_release_number",
  191. MagicMock(return_value="No version assigned."),
  192. )
  193. @patch("salt.version.SaltStackVersion", MagicMock(return_value="2018.3.2"))
  194. def test_less_than_with_unassigned_codename(self):
  195. """
  196. Test that when an unassigned codename greater than the current version.
  197. """
  198. assert salt_version.less_than("Fluorine") is True
  199. # _check_release_cmp tests: 2
  200. def test_check_release_cmp_no_codename(self):
  201. """
  202. Test that None is returned when the codename isn't found.
  203. """
  204. assert salt_version._check_release_cmp("foo") is None
  205. def test_check_release_cmp_success(self):
  206. """
  207. Test that an int is returned from the version compare
  208. """
  209. assert isinstance(salt_version._check_release_cmp("Oxygen"), int)