test_versions.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 datetime
  14. import warnings
  15. # Import Salt Testing libs
  16. from tests.support.unit import TestCase, skipIf
  17. from tests.support.mock import patch
  18. from tests.support.paths import CODE_DIR
  19. # Import Salt libs
  20. import salt.modules.cmdmod
  21. import salt.version
  22. import salt.utils.platform
  23. import salt.utils.versions
  24. from salt.utils.versions import LooseVersion, StrictVersion
  25. # Import 3rd-party libs
  26. from salt.ext import six
  27. if six.PY2:
  28. cmp_method = '__cmp__'
  29. else:
  30. cmp_method = '_cmp'
  31. class VersionTestCase(TestCase):
  32. def test_prerelease(self):
  33. version = StrictVersion('1.2.3a1')
  34. self.assertEqual(version.version, (1, 2, 3))
  35. self.assertEqual(version.prerelease, ('a', 1))
  36. self.assertEqual(six.text_type(version), '1.2.3a1')
  37. version = StrictVersion('1.2.0')
  38. self.assertEqual(six.text_type(version), '1.2')
  39. def test_cmp_strict(self):
  40. versions = (('1.5.1', '1.5.2b2', -1),
  41. ('161', '3.10a', ValueError),
  42. ('8.02', '8.02', 0),
  43. ('3.4j', '1996.07.12', ValueError),
  44. ('3.2.pl0', '3.1.1.6', ValueError),
  45. ('2g6', '11g', ValueError),
  46. ('0.9', '2.2', -1),
  47. ('1.2.1', '1.2', 1),
  48. ('1.1', '1.2.2', -1),
  49. ('1.2', '1.1', 1),
  50. ('1.2.1', '1.2.2', -1),
  51. ('1.2.2', '1.2', 1),
  52. ('1.2', '1.2.2', -1),
  53. ('0.4.0', '0.4', 0),
  54. ('1.13++', '5.5.kw', ValueError),
  55. # Added by us
  56. ('1.1.1a1', '1.1.1', -1)
  57. )
  58. for v1, v2, wanted in versions:
  59. try:
  60. res = getattr(StrictVersion(v1), cmp_method)(StrictVersion(v2))
  61. except ValueError:
  62. if wanted is ValueError:
  63. continue
  64. else:
  65. raise AssertionError(("cmp(%s, %s) "
  66. "shouldn't raise ValueError") % (v1, v2))
  67. self.assertEqual(res, wanted,
  68. 'cmp(%s, %s) should be %s, got %s' %
  69. (v1, v2, wanted, res))
  70. def test_cmp(self):
  71. versions = (('1.5.1', '1.5.2b2', -1),
  72. ('161', '3.10a', 1),
  73. ('8.02', '8.02', 0),
  74. ('3.4j', '1996.07.12', -1),
  75. ('3.2.pl0', '3.1.1.6', 1),
  76. ('2g6', '11g', -1),
  77. ('0.960923', '2.2beta29', -1),
  78. ('1.13++', '5.5.kw', -1),
  79. # Added by us
  80. ('3.10.0-514.el7', '3.10.0-514.6.1.el7', 1),
  81. ('2.2.2', '2.12.1', -1)
  82. )
  83. for v1, v2, wanted in versions:
  84. res = getattr(LooseVersion(v1), cmp_method)(LooseVersion(v2))
  85. self.assertEqual(res, wanted,
  86. 'cmp(%s, %s) should be %s, got %s' %
  87. (v1, v2, wanted, res))
  88. @skipIf(not salt.utils.platform.is_linux(), 'only need to run on linux')
  89. def test_spelling_version_name(self):
  90. '''
  91. check the spelling of the version name for the release
  92. names in the salt.utils.versions.warn_until call
  93. '''
  94. query = 'salt.utils.versions.warn_until('
  95. names = salt.version.SaltStackVersion.NAMES
  96. cmd = 'grep -lr {} -A 1 {}'.format(query, os.path.join(CODE_DIR, 'salt'))
  97. grep_call = salt.modules.cmdmod.run_stdout(cmd=cmd).split(os.linesep)
  98. for line in grep_call:
  99. num_cmd = salt.modules.cmdmod.run_stdout('grep -c {0} {1}'.format(query, line))
  100. ver_cmd = salt.modules.cmdmod.run_stdout('grep {0} {1} -A 1'.format(query, line))
  101. if 'pyc' in line:
  102. break
  103. match = 0
  104. for key in names:
  105. if key in ver_cmd:
  106. match = match + (ver_cmd.count(key))
  107. if 'utils/__init__.py' in line:
  108. # work around for utils/__init__.py because
  109. # it includes the warn_utils function
  110. match = match + 1
  111. self.assertEqual(match, int(num_cmd), msg='The file: {0} has an '
  112. 'incorrect spelling for the release name in the warn_utils '
  113. 'call: {1}. Expecting one of these release names: '
  114. '{2}'.format(line, ver_cmd, names))
  115. class VersionFuncsTestCase(TestCase):
  116. def test_compare(self):
  117. ret = salt.utils.versions.compare('1.0', '==', '1.0')
  118. self.assertTrue(ret)
  119. ret = salt.utils.versions.compare('1.0', '!=', '1.0')
  120. self.assertFalse(ret)
  121. with patch.object(salt.utils.versions, 'log') as log_mock:
  122. ret = salt.utils.versions.compare('1.0', 'HAH I AM NOT A COMP OPERATOR! I AM YOUR FATHER!', '1.0')
  123. self.assertTrue(log_mock.error.called)
  124. def test_kwargs_warn_until(self):
  125. # Test invalid version arg
  126. self.assertRaises(RuntimeError, salt.utils.versions.kwargs_warn_until, {}, [])
  127. def test_warn_until_warning_raised(self):
  128. # We *always* want *all* warnings thrown on this module
  129. warnings.filterwarnings('always', '', DeprecationWarning, __name__)
  130. def raise_warning(_version_info_=(0, 16, 0)):
  131. salt.utils.versions.warn_until(
  132. (0, 17), 'Deprecation Message!',
  133. _version_info_=_version_info_
  134. )
  135. def raise_named_version_warning(_version_info_=(0, 16, 0)):
  136. salt.utils.versions.warn_until(
  137. 'Hydrogen', 'Deprecation Message!',
  138. _version_info_=_version_info_
  139. )
  140. # raise_warning should show warning until version info is >= (0, 17)
  141. with warnings.catch_warnings(record=True) as recorded_warnings:
  142. raise_warning()
  143. self.assertEqual(
  144. 'Deprecation Message!', six.text_type(recorded_warnings[0].message)
  145. )
  146. # raise_warning should show warning until version info is >= (0, 17)
  147. with warnings.catch_warnings(record=True) as recorded_warnings:
  148. raise_named_version_warning()
  149. self.assertEqual(
  150. 'Deprecation Message!', six.text_type(recorded_warnings[0].message)
  151. )
  152. # the deprecation warning is not issued because we passed
  153. # _dont_call_warning
  154. with warnings.catch_warnings(record=True) as recorded_warnings:
  155. salt.utils.versions.warn_until(
  156. (0, 17), 'Foo', _dont_call_warnings=True,
  157. _version_info_=(0, 16)
  158. )
  159. self.assertEqual(0, len(recorded_warnings))
  160. # Let's set version info to (0, 17), a RuntimeError should be raised
  161. with self.assertRaisesRegex(
  162. RuntimeError,
  163. r'The warning triggered on filename \'(.*)test_versions.py\', '
  164. r'line number ([\d]+), is supposed to be shown until version '
  165. r'0.17.0 is released. Current version is now 0.17.0. '
  166. r'Please remove the warning.'):
  167. raise_warning(_version_info_=(0, 17, 0))
  168. # Let's set version info to (0, 17), a RuntimeError should be raised
  169. with self.assertRaisesRegex(
  170. RuntimeError,
  171. r'The warning triggered on filename \'(.*)test_versions.py\', '
  172. r'line number ([\d]+), is supposed to be shown until version '
  173. r'(.*) is released. Current version is now '
  174. r'([\d.]+). Please remove the warning.'):
  175. raise_named_version_warning(_version_info_=(getattr(sys, 'maxint', None) or getattr(sys, 'maxsize'), 16, 0))
  176. # Even though we're calling warn_until, we pass _dont_call_warnings
  177. # because we're only after the RuntimeError
  178. with self.assertRaisesRegex(
  179. RuntimeError,
  180. r'The warning triggered on filename \'(.*)test_versions.py\', '
  181. r'line number ([\d]+), is supposed to be shown until version '
  182. r'0.17.0 is released. Current version is now '
  183. r'(.*). Please remove the warning.'):
  184. salt.utils.versions.warn_until(
  185. (0, 17), 'Foo', _dont_call_warnings=True
  186. )
  187. with self.assertRaisesRegex(
  188. RuntimeError,
  189. r'The warning triggered on filename \'(.*)test_versions.py\', '
  190. r'line number ([\d]+), is supposed to be shown until version '
  191. r'(.*) is released. Current version is now '
  192. r'(.*). Please remove the warning.'):
  193. salt.utils.versions.warn_until(
  194. 'Hydrogen', 'Foo', _dont_call_warnings=True,
  195. _version_info_=(getattr(sys, 'maxint', None) or getattr(sys, 'maxsize'), 16, 0)
  196. )
  197. # version on the deprecation message gets properly formatted
  198. with warnings.catch_warnings(record=True) as recorded_warnings:
  199. vrs = salt.version.SaltStackVersion.from_name('Helium')
  200. salt.utils.versions.warn_until(
  201. 'Helium', 'Deprecation Message until {version}!',
  202. _version_info_=(vrs.major - 1, 0)
  203. )
  204. self.assertEqual(
  205. 'Deprecation Message until {0}!'.format(vrs.formatted_version),
  206. six.text_type(recorded_warnings[0].message)
  207. )
  208. def test_kwargs_warn_until_warning_raised(self):
  209. # We *always* want *all* warnings thrown on this module
  210. warnings.filterwarnings('always', '', DeprecationWarning, __name__)
  211. def raise_warning(**kwargs):
  212. _version_info_ = kwargs.pop('_version_info_', (0, 16, 0))
  213. salt.utils.versions.kwargs_warn_until(
  214. kwargs,
  215. (0, 17),
  216. _version_info_=_version_info_
  217. )
  218. # raise_warning({...}) should show warning until version info is >= (0, 17)
  219. with warnings.catch_warnings(record=True) as recorded_warnings:
  220. raise_warning(foo=42) # with a kwarg
  221. self.assertEqual(
  222. 'The following parameter(s) have been deprecated and '
  223. 'will be removed in \'0.17.0\': \'foo\'.',
  224. six.text_type(recorded_warnings[0].message)
  225. )
  226. # With no **kwargs, should not show warning until version info is >= (0, 17)
  227. with warnings.catch_warnings(record=True) as recorded_warnings:
  228. salt.utils.versions.kwargs_warn_until(
  229. {}, # no kwargs
  230. (0, 17),
  231. _version_info_=(0, 16, 0)
  232. )
  233. self.assertEqual(0, len(recorded_warnings))
  234. # Let's set version info to (0, 17), a RuntimeError should be raised
  235. # regardless of whether or not we pass any **kwargs.
  236. with self.assertRaisesRegex(
  237. RuntimeError,
  238. r'The warning triggered on filename \'(.*)test_versions.py\', '
  239. r'line number ([\d]+), is supposed to be shown until version '
  240. r'0.17.0 is released. Current version is now 0.17.0. '
  241. r'Please remove the warning.'):
  242. raise_warning(_version_info_=(0, 17)) # no kwargs
  243. with self.assertRaisesRegex(
  244. RuntimeError,
  245. r'The warning triggered on filename \'(.*)test_versions.py\', '
  246. r'line number ([\d]+), is supposed to be shown until version '
  247. r'0.17.0 is released. Current version is now 0.17.0. '
  248. r'Please remove the warning.'):
  249. raise_warning(bar='baz', qux='quux', _version_info_=(0, 17)) # some kwargs
  250. def test_warn_until_date_warning_raised(self):
  251. # We *always* want *all* warnings thrown on this module
  252. warnings.filterwarnings('always', '', DeprecationWarning, __name__)
  253. _current_date = datetime.date(2000, 1, 1)
  254. # Test warning with datetime.date instance
  255. with warnings.catch_warnings(record=True) as recorded_warnings:
  256. salt.utils.versions.warn_until_date(
  257. datetime.date(2000, 1, 2),
  258. 'Deprecation Message!',
  259. _current_date=_current_date
  260. )
  261. self.assertEqual(
  262. 'Deprecation Message!', six.text_type(recorded_warnings[0].message)
  263. )
  264. # Test warning with datetime.datetime instance
  265. with warnings.catch_warnings(record=True) as recorded_warnings:
  266. salt.utils.versions.warn_until_date(
  267. datetime.datetime(2000, 1, 2),
  268. 'Deprecation Message!',
  269. _current_date=_current_date
  270. )
  271. self.assertEqual(
  272. 'Deprecation Message!', six.text_type(recorded_warnings[0].message)
  273. )
  274. # Test warning with date as a string
  275. with warnings.catch_warnings(record=True) as recorded_warnings:
  276. salt.utils.versions.warn_until_date(
  277. '20000102',
  278. 'Deprecation Message!',
  279. _current_date=_current_date
  280. )
  281. self.assertEqual(
  282. 'Deprecation Message!', six.text_type(recorded_warnings[0].message)
  283. )
  284. # the deprecation warning is not issued because we passed
  285. # _dont_call_warning
  286. with warnings.catch_warnings(record=True) as recorded_warnings:
  287. salt.utils.versions.warn_until_date(
  288. '20000102',
  289. 'Deprecation Message!',
  290. _dont_call_warnings=True,
  291. _current_date=_current_date
  292. )
  293. self.assertEqual(0, len(recorded_warnings))
  294. # Let's test for RuntimeError raise
  295. with self.assertRaisesRegex(
  296. RuntimeError,
  297. r'Deprecation Message! This warning\(now exception\) triggered on '
  298. r'filename \'(.*)test_versions.py\', line number ([\d]+), is '
  299. r'supposed to be shown until ([\d-]+). Today is ([\d-]+). '
  300. r'Please remove the warning.'):
  301. salt.utils.versions.warn_until_date('20000101', 'Deprecation Message!')
  302. # Even though we're calling warn_until_date, we pass _dont_call_warnings
  303. # because we're only after the RuntimeError
  304. with self.assertRaisesRegex(
  305. RuntimeError,
  306. r'Deprecation Message! This warning\(now exception\) triggered on '
  307. r'filename \'(.*)test_versions.py\', line number ([\d]+), is '
  308. r'supposed to be shown until ([\d-]+). Today is ([\d-]+). '
  309. r'Please remove the warning.'):
  310. salt.utils.versions.warn_until_date(
  311. '20000101',
  312. 'Deprecation Message!',
  313. _dont_call_warnings=True,
  314. _current_date=_current_date
  315. )
  316. def test_warn_until_date_bad_strptime_format(self):
  317. # We *always* want *all* warnings thrown on this module
  318. warnings.filterwarnings('always', '', DeprecationWarning, __name__)
  319. # Let's test for RuntimeError raise
  320. with self.assertRaisesRegex(
  321. ValueError,
  322. 'time data \'0022\' does not match format \'%Y%m%d\''):
  323. salt.utils.versions.warn_until_date('0022', 'Deprecation Message!')