test_versions.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. # -*- coding: utf-8 -*-
  2. '''
  3. tests.unit.version_test
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. These tests are copied from python's source `Lib/distutils/tests/test_version.py`
  6. Some new examples were added and some adjustments were made to run tests in python 2 and 3
  7. '''
  8. # pylint: disable=string-substitution-usage-error
  9. # Import python libs
  10. from __future__ import absolute_import, print_function, unicode_literals
  11. import os
  12. import sys
  13. import warnings
  14. # Import Salt Testing libs
  15. import tests.integration as integration
  16. from tests.support.unit import TestCase, skipIf
  17. from tests.support.mock import patch, NO_MOCK, NO_MOCK_REASON
  18. # Import Salt libs
  19. import salt.modules.cmdmod
  20. import salt.version
  21. import salt.utils.platform
  22. import salt.utils.versions
  23. from salt.utils.versions import LooseVersion, StrictVersion
  24. # Import 3rd-party libs
  25. from salt.ext import six
  26. if six.PY2:
  27. cmp_method = '__cmp__'
  28. else:
  29. cmp_method = '_cmp'
  30. class VersionTestCase(TestCase):
  31. def test_prerelease(self):
  32. version = StrictVersion('1.2.3a1')
  33. self.assertEqual(version.version, (1, 2, 3))
  34. self.assertEqual(version.prerelease, ('a', 1))
  35. self.assertEqual(six.text_type(version), '1.2.3a1')
  36. version = StrictVersion('1.2.0')
  37. self.assertEqual(six.text_type(version), '1.2')
  38. def test_cmp_strict(self):
  39. versions = (('1.5.1', '1.5.2b2', -1),
  40. ('161', '3.10a', ValueError),
  41. ('8.02', '8.02', 0),
  42. ('3.4j', '1996.07.12', ValueError),
  43. ('3.2.pl0', '3.1.1.6', ValueError),
  44. ('2g6', '11g', ValueError),
  45. ('0.9', '2.2', -1),
  46. ('1.2.1', '1.2', 1),
  47. ('1.1', '1.2.2', -1),
  48. ('1.2', '1.1', 1),
  49. ('1.2.1', '1.2.2', -1),
  50. ('1.2.2', '1.2', 1),
  51. ('1.2', '1.2.2', -1),
  52. ('0.4.0', '0.4', 0),
  53. ('1.13++', '5.5.kw', ValueError),
  54. # Added by us
  55. ('1.1.1a1', '1.1.1', -1)
  56. )
  57. for v1, v2, wanted in versions:
  58. try:
  59. res = getattr(StrictVersion(v1), cmp_method)(StrictVersion(v2))
  60. except ValueError:
  61. if wanted is ValueError:
  62. continue
  63. else:
  64. raise AssertionError(("cmp(%s, %s) "
  65. "shouldn't raise ValueError") % (v1, v2))
  66. self.assertEqual(res, wanted,
  67. 'cmp(%s, %s) should be %s, got %s' %
  68. (v1, v2, wanted, res))
  69. def test_cmp(self):
  70. versions = (('1.5.1', '1.5.2b2', -1),
  71. ('161', '3.10a', 1),
  72. ('8.02', '8.02', 0),
  73. ('3.4j', '1996.07.12', -1),
  74. ('3.2.pl0', '3.1.1.6', 1),
  75. ('2g6', '11g', -1),
  76. ('0.960923', '2.2beta29', -1),
  77. ('1.13++', '5.5.kw', -1),
  78. # Added by us
  79. ('3.10.0-514.el7', '3.10.0-514.6.1.el7', 1),
  80. ('2.2.2', '2.12.1', -1)
  81. )
  82. for v1, v2, wanted in versions:
  83. res = getattr(LooseVersion(v1), cmp_method)(LooseVersion(v2))
  84. self.assertEqual(res, wanted,
  85. 'cmp(%s, %s) should be %s, got %s' %
  86. (v1, v2, wanted, res))
  87. @skipIf(not salt.utils.platform.is_linux(), 'only need to run on linux')
  88. def test_spelling_version_name(self):
  89. '''
  90. check the spelling of the version name for the release
  91. names in the salt.utils.versions.warn_until call
  92. '''
  93. salt_dir = integration.CODE_DIR
  94. query = 'salt.utils.versions.warn_until'
  95. names = salt.version.SaltStackVersion.NAMES
  96. salt_dir += '/salt/'
  97. cmd = 'grep -lr {0} -A 1 '.format(query) + salt_dir
  98. grep_call = salt.modules.cmdmod.run_stdout(cmd=cmd).split(os.linesep)
  99. for line in grep_call:
  100. num_cmd = salt.modules.cmdmod.run_stdout('grep -c {0} {1}'.format(query, line))
  101. ver_cmd = salt.modules.cmdmod.run_stdout('grep {0} {1} -A 1'.format(query, line))
  102. if 'pyc' in line:
  103. break
  104. match = 0
  105. for key in names:
  106. if key in ver_cmd:
  107. match = match + (ver_cmd.count(key))
  108. if 'utils/__init__.py' in line:
  109. # work around for utils/__init__.py because
  110. # it includes the warn_utils function
  111. match = match + 1
  112. self.assertEqual(match, int(num_cmd), msg='The file: {0} has an '
  113. 'incorrect spelling for the release name in the warn_utils '
  114. 'call: {1}. Expecting one of these release names: '
  115. '{2}'.format(line, ver_cmd, names))
  116. class VersionFuncsTestCase(TestCase):
  117. @skipIf(NO_MOCK, NO_MOCK_REASON)
  118. def test_compare(self):
  119. ret = salt.utils.versions.compare('1.0', '==', '1.0')
  120. self.assertTrue(ret)
  121. ret = salt.utils.versions.compare('1.0', '!=', '1.0')
  122. self.assertFalse(ret)
  123. with patch.object(salt.utils.versions, 'log') as log_mock:
  124. ret = salt.utils.versions.compare('1.0', 'HAH I AM NOT A COMP OPERATOR! I AM YOUR FATHER!', '1.0')
  125. self.assertTrue(log_mock.error.called)
  126. def test_kwargs_warn_until(self):
  127. # Test invalid version arg
  128. self.assertRaises(RuntimeError, salt.utils.versions.kwargs_warn_until, {}, [])
  129. def test_warn_until_warning_raised(self):
  130. # We *always* want *all* warnings thrown on this module
  131. warnings.filterwarnings('always', '', DeprecationWarning, __name__)
  132. def raise_warning(_version_info_=(0, 16, 0)):
  133. salt.utils.versions.warn_until(
  134. (0, 17), 'Deprecation Message!',
  135. _version_info_=_version_info_
  136. )
  137. def raise_named_version_warning(_version_info_=(0, 16, 0)):
  138. salt.utils.versions.warn_until(
  139. 'Hydrogen', 'Deprecation Message!',
  140. _version_info_=_version_info_
  141. )
  142. # raise_warning should show warning until version info is >= (0, 17)
  143. with warnings.catch_warnings(record=True) as recorded_warnings:
  144. raise_warning()
  145. self.assertEqual(
  146. 'Deprecation Message!', six.text_type(recorded_warnings[0].message)
  147. )
  148. # raise_warning should show warning until version info is >= (0, 17)
  149. with warnings.catch_warnings(record=True) as recorded_warnings:
  150. raise_named_version_warning()
  151. self.assertEqual(
  152. 'Deprecation Message!', six.text_type(recorded_warnings[0].message)
  153. )
  154. # the deprecation warning is not issued because we passed
  155. # _dont_call_warning
  156. with warnings.catch_warnings(record=True) as recorded_warnings:
  157. salt.utils.versions.warn_until(
  158. (0, 17), 'Foo', _dont_call_warnings=True,
  159. _version_info_=(0, 16)
  160. )
  161. self.assertEqual(0, len(recorded_warnings))
  162. # Let's set version info to (0, 17), a RuntimeError should be raised
  163. with self.assertRaisesRegex(
  164. RuntimeError,
  165. r'The warning triggered on filename \'(.*)test_versions.py\', '
  166. r'line number ([\d]+), is supposed to be shown until version '
  167. r'0.17.0 is released. Current version is now 0.17.0. '
  168. r'Please remove the warning.'):
  169. raise_warning(_version_info_=(0, 17, 0))
  170. # Let's set version info to (0, 17), a RuntimeError should be raised
  171. with self.assertRaisesRegex(
  172. RuntimeError,
  173. r'The warning triggered on filename \'(.*)test_versions.py\', '
  174. r'line number ([\d]+), is supposed to be shown until version '
  175. r'(.*) is released. Current version is now '
  176. r'([\d.]+). Please remove the warning.'):
  177. raise_named_version_warning(_version_info_=(getattr(sys, 'maxint', None) or getattr(sys, 'maxsize'), 16, 0))
  178. # Even though we're calling warn_until, we pass _dont_call_warnings
  179. # because we're only after the RuntimeError
  180. with self.assertRaisesRegex(
  181. RuntimeError,
  182. r'The warning triggered on filename \'(.*)test_versions.py\', '
  183. r'line number ([\d]+), is supposed to be shown until version '
  184. r'0.17.0 is released. Current version is now '
  185. r'(.*). Please remove the warning.'):
  186. salt.utils.versions.warn_until(
  187. (0, 17), 'Foo', _dont_call_warnings=True
  188. )
  189. with self.assertRaisesRegex(
  190. RuntimeError,
  191. r'The warning triggered on filename \'(.*)test_versions.py\', '
  192. r'line number ([\d]+), is supposed to be shown until version '
  193. r'(.*) is released. Current version is now '
  194. r'(.*). Please remove the warning.'):
  195. salt.utils.versions.warn_until(
  196. 'Hydrogen', 'Foo', _dont_call_warnings=True,
  197. _version_info_=(getattr(sys, 'maxint', None) or getattr(sys, 'maxsize'), 16, 0)
  198. )
  199. # version on the deprecation message gets properly formatted
  200. with warnings.catch_warnings(record=True) as recorded_warnings:
  201. vrs = salt.version.SaltStackVersion.from_name('Helium')
  202. salt.utils.versions.warn_until(
  203. 'Helium', 'Deprecation Message until {version}!',
  204. _version_info_=(vrs.major - 1, 0)
  205. )
  206. self.assertEqual(
  207. 'Deprecation Message until {0}!'.format(vrs.formatted_version),
  208. six.text_type(recorded_warnings[0].message)
  209. )
  210. def test_kwargs_warn_until_warning_raised(self):
  211. # We *always* want *all* warnings thrown on this module
  212. warnings.filterwarnings('always', '', DeprecationWarning, __name__)
  213. def raise_warning(**kwargs):
  214. _version_info_ = kwargs.pop('_version_info_', (0, 16, 0))
  215. salt.utils.versions.kwargs_warn_until(
  216. kwargs,
  217. (0, 17),
  218. _version_info_=_version_info_
  219. )
  220. # raise_warning({...}) should show warning until version info is >= (0, 17)
  221. with warnings.catch_warnings(record=True) as recorded_warnings:
  222. raise_warning(foo=42) # with a kwarg
  223. self.assertEqual(
  224. 'The following parameter(s) have been deprecated and '
  225. 'will be removed in \'0.17.0\': \'foo\'.',
  226. six.text_type(recorded_warnings[0].message)
  227. )
  228. # With no **kwargs, should not show warning until version info is >= (0, 17)
  229. with warnings.catch_warnings(record=True) as recorded_warnings:
  230. salt.utils.versions.kwargs_warn_until(
  231. {}, # no kwargs
  232. (0, 17),
  233. _version_info_=(0, 16, 0)
  234. )
  235. self.assertEqual(0, len(recorded_warnings))
  236. # Let's set version info to (0, 17), a RuntimeError should be raised
  237. # regardless of whether or not we pass any **kwargs.
  238. with self.assertRaisesRegex(
  239. RuntimeError,
  240. r'The warning triggered on filename \'(.*)test_versions.py\', '
  241. r'line number ([\d]+), is supposed to be shown until version '
  242. r'0.17.0 is released. Current version is now 0.17.0. '
  243. r'Please remove the warning.'):
  244. raise_warning(_version_info_=(0, 17)) # no kwargs
  245. with self.assertRaisesRegex(
  246. RuntimeError,
  247. r'The warning triggered on filename \'(.*)test_versions.py\', '
  248. r'line number ([\d]+), is supposed to be shown until version '
  249. r'0.17.0 is released. Current version is now 0.17.0. '
  250. r'Please remove the warning.'):
  251. raise_warning(bar='baz', qux='quux', _version_info_=(0, 17)) # some kwargs