test_versions.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.runtime import RUNTIME_VARS
  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. salt_dir = RUNTIME_VARS.CODE_DIR
  95. query = 'salt.utils.versions.warn_until('
  96. names = salt.version.SaltStackVersion.NAMES
  97. salt_dir += '/salt/'
  98. cmd = 'grep -lr {0} -A 1 '.format(query) + salt_dir
  99. grep_call = salt.modules.cmdmod.run_stdout(cmd=cmd).split(os.linesep)
  100. for line in grep_call:
  101. num_cmd = salt.modules.cmdmod.run_stdout('grep -c {0} {1}'.format(query, line))
  102. ver_cmd = salt.modules.cmdmod.run_stdout('grep {0} {1} -A 1'.format(query, line))
  103. if 'pyc' in line:
  104. break
  105. match = 0
  106. for key in names:
  107. if key in ver_cmd:
  108. match = match + (ver_cmd.count(key))
  109. if 'utils/__init__.py' in line:
  110. # work around for utils/__init__.py because
  111. # it includes the warn_utils function
  112. match = match + 1
  113. self.assertEqual(match, int(num_cmd), msg='The file: {0} has an '
  114. 'incorrect spelling for the release name in the warn_utils '
  115. 'call: {1}. Expecting one of these release names: '
  116. '{2}'.format(line, ver_cmd, names))
  117. class VersionFuncsTestCase(TestCase):
  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
  252. def test_warn_until_date_warning_raised(self):
  253. # We *always* want *all* warnings thrown on this module
  254. warnings.filterwarnings('always', '', DeprecationWarning, __name__)
  255. _current_date = datetime.date(2000, 1, 1)
  256. # Test warning with datetime.date instance
  257. with warnings.catch_warnings(record=True) as recorded_warnings:
  258. salt.utils.versions.warn_until_date(
  259. datetime.date(2000, 1, 2),
  260. 'Deprecation Message!',
  261. _current_date=_current_date
  262. )
  263. self.assertEqual(
  264. 'Deprecation Message!', six.text_type(recorded_warnings[0].message)
  265. )
  266. # Test warning with datetime.datetime instance
  267. with warnings.catch_warnings(record=True) as recorded_warnings:
  268. salt.utils.versions.warn_until_date(
  269. datetime.datetime(2000, 1, 2),
  270. 'Deprecation Message!',
  271. _current_date=_current_date
  272. )
  273. self.assertEqual(
  274. 'Deprecation Message!', six.text_type(recorded_warnings[0].message)
  275. )
  276. # Test warning with date as a string
  277. with warnings.catch_warnings(record=True) as recorded_warnings:
  278. salt.utils.versions.warn_until_date(
  279. '20000102',
  280. 'Deprecation Message!',
  281. _current_date=_current_date
  282. )
  283. self.assertEqual(
  284. 'Deprecation Message!', six.text_type(recorded_warnings[0].message)
  285. )
  286. # the deprecation warning is not issued because we passed
  287. # _dont_call_warning
  288. with warnings.catch_warnings(record=True) as recorded_warnings:
  289. salt.utils.versions.warn_until_date(
  290. '20000102',
  291. 'Deprecation Message!',
  292. _dont_call_warnings=True,
  293. _current_date=_current_date
  294. )
  295. self.assertEqual(0, len(recorded_warnings))
  296. # Let's test for RuntimeError raise
  297. with self.assertRaisesRegex(
  298. RuntimeError,
  299. r'Deprecation Message! This warning\(now exception\) triggered on '
  300. r'filename \'(.*)test_versions.py\', line number ([\d]+), is '
  301. r'supposed to be shown until ([\d-]+). Today is ([\d-]+). '
  302. r'Please remove the warning.'):
  303. salt.utils.versions.warn_until_date('20000101', 'Deprecation Message!')
  304. # Even though we're calling warn_until_date, we pass _dont_call_warnings
  305. # because we're only after the RuntimeError
  306. with self.assertRaisesRegex(
  307. RuntimeError,
  308. r'Deprecation Message! This warning\(now exception\) triggered on '
  309. r'filename \'(.*)test_versions.py\', line number ([\d]+), is '
  310. r'supposed to be shown until ([\d-]+). Today is ([\d-]+). '
  311. r'Please remove the warning.'):
  312. salt.utils.versions.warn_until_date(
  313. '20000101',
  314. 'Deprecation Message!',
  315. _dont_call_warnings=True,
  316. _current_date=_current_date
  317. )
  318. def test_warn_until_date_bad_strptime_format(self):
  319. # We *always* want *all* warnings thrown on this module
  320. warnings.filterwarnings('always', '', DeprecationWarning, __name__)
  321. # Let's test for RuntimeError raise
  322. with self.assertRaisesRegex(
  323. ValueError,
  324. 'time data \'0022\' does not match format \'%Y%m%d\''):
  325. salt.utils.versions.warn_until_date('0022', 'Deprecation Message!')