1
0

test_version.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.unit.version_test
  5. ~~~~~~~~~~~~~~~~~~~~~~~
  6. Test salt's regex git describe version parsing
  7. '''
  8. # Import python libs
  9. from __future__ import absolute_import
  10. import re
  11. # Import Salt Testing libs
  12. from tests.support.unit import TestCase
  13. from tests.support.mock import MagicMock, patch
  14. # Import Salt libs
  15. from salt.version import SaltStackVersion, versions_report
  16. import salt.version
  17. class VersionTestCase(TestCase):
  18. def test_version_parsing(self):
  19. strip_initial_non_numbers_regex = re.compile(r'(?:[^\d]+)?(?P<vs>.*)')
  20. expect = (
  21. ('v0.12.0-19-g767d4f9', (0, 12, 0, 0, '', 0, 19, 'g767d4f9'), None),
  22. ('v0.12.0-85-g2880105', (0, 12, 0, 0, '', 0, 85, 'g2880105'), None),
  23. ('debian/0.11.1+ds-1-3-ga0afcbd',
  24. (0, 11, 1, 0, '', 0, 3, 'ga0afcbd'), '0.11.1-3-ga0afcbd'),
  25. ('0.12.1', (0, 12, 1, 0, '', 0, 0, None), None),
  26. ('0.12.1', (0, 12, 1, 0, '', 0, 0, None), None),
  27. ('0.17.0rc1', (0, 17, 0, 0, 'rc', 1, 0, None), None),
  28. ('v0.17.0rc1-1-g52ebdfd', (0, 17, 0, 0, 'rc', 1, 1, 'g52ebdfd'), None),
  29. ('v2014.1.4.1', (2014, 1, 4, 1, '', 0, 0, None), None),
  30. ('v2014.1.4.1rc3-n/a-abcdefff', (2014, 1, 4, 1, 'rc', 3, -1, 'abcdefff'), None),
  31. ('v3.4.1.1', (3, 4, 1, 1, '', 0, 0, None), None),
  32. ('v3000', (3000, None, None, 0, '', 0, 0, None), '3000'),
  33. ('v3000rc1', (3000, None, None, 0, 'rc', 1, 0, None), '3000rc1'),
  34. )
  35. for vstr, full_info, version in expect:
  36. saltstack_version = SaltStackVersion.parse(vstr)
  37. self.assertEqual(
  38. saltstack_version.full_info, full_info
  39. )
  40. if version is None:
  41. version = strip_initial_non_numbers_regex.search(vstr).group('vs')
  42. self.assertEqual(saltstack_version.string, version)
  43. def test_version_comparison(self):
  44. examples = (
  45. ('debian/0.11.1+ds-1-3-ga0afcbd', '0.11.1+ds-2'),
  46. ('v0.12.0-85-g2880105', 'v0.12.0-19-g767d4f9'),
  47. ('v0.17.0rc1-1-g52ebdfd', '0.17.0rc1'),
  48. ('v0.17.0', 'v0.17.0rc1'),
  49. ('Hydrogen', '0.17.0'),
  50. ('Helium', 'Hydrogen'),
  51. ('v2014.1.4.1-n/a-abcdefff', 'v2014.1.4.1rc3-n/a-abcdefff'),
  52. ('v2014.1.4.1-1-abcdefff', 'v2014.1.4.1-n/a-abcdefff'),
  53. ('v2016.12.0rc1', 'v2016.12.0b1'),
  54. ('v2016.12.0beta1', 'v2016.12.0alpha1'),
  55. ('v2016.12.0alpha1', 'v2016.12.0alpha0'),
  56. ('v3000.1', 'v3000'),
  57. ('v3000rc2', 'v3000rc1'),
  58. ('v3001', 'v3000'),
  59. ('v4023rc1', 'v4022rc1'),
  60. ('v3000', 'v3000rc1'),
  61. ('v3000', 'v2019.2.1'),
  62. ('v3000.1', 'v2019.2.1'),
  63. # we created v3000.0rc1 tag on repo
  64. # but we should not be using this
  65. # version scheme in the future
  66. # but still adding test for it
  67. ('v3000', 'v3000.0rc1'),
  68. )
  69. for higher_version, lower_version in examples:
  70. self.assertTrue(SaltStackVersion.parse(higher_version) > lower_version)
  71. self.assertTrue(SaltStackVersion.parse(lower_version) < higher_version)
  72. assert SaltStackVersion.parse(lower_version) != higher_version
  73. def test_unparsable_version(self):
  74. with self.assertRaises(ValueError):
  75. SaltStackVersion.from_name('Drunk')
  76. with self.assertRaises(ValueError):
  77. SaltStackVersion.parse('Drunk')
  78. def test_sha(self):
  79. '''
  80. test matching sha's
  81. '''
  82. exp_ret = (
  83. ('d6cd1e2bd19e03a81132a23b2025920577f84e37', True),
  84. ('2880105', True),
  85. ('v3000.0.1', False),
  86. ('v0.12.0-85-g2880105', False)
  87. )
  88. for commit, exp in exp_ret:
  89. ret = SaltStackVersion.git_sha_regex.match(commit)
  90. if exp:
  91. assert ret
  92. else:
  93. assert not ret
  94. def test_version_report_lines(self):
  95. '''
  96. Validate padding in versions report is correct
  97. '''
  98. # Get a set of all version report name lenghts including padding
  99. line_lengths = set([
  100. len(line.split(':')[0]) for line in list(versions_report())[4:]
  101. if line != ' ' and line != 'System Versions:'
  102. ])
  103. # Check that they are all the same size (only one element in the set)
  104. assert len(line_lengths) == 1
  105. def test_string_new_version(self):
  106. '''
  107. Validate string property method
  108. using new versioning scheme
  109. '''
  110. maj_ver = '3000'
  111. ver = SaltStackVersion(major=maj_ver)
  112. assert not ver.minor
  113. assert not ver.bugfix
  114. assert maj_ver == ver.string
  115. def test_string_new_version_minor(self):
  116. '''
  117. Validate string property method
  118. using new versioning scheme alongside
  119. minor version
  120. '''
  121. maj_ver = 3000
  122. min_ver = 1
  123. ver = SaltStackVersion(major=maj_ver, minor=min_ver)
  124. assert ver.minor == min_ver
  125. assert not ver.bugfix
  126. assert ver.string == '{0}.{1}'.format(maj_ver, min_ver)
  127. def test_string_old_version(self):
  128. '''
  129. Validate string property method
  130. using old versioning scheme alongside
  131. minor version
  132. '''
  133. maj_ver = '2019'
  134. min_ver = '2'
  135. ver = SaltStackVersion(major=maj_ver, minor=min_ver)
  136. assert ver.bugfix == 0
  137. assert ver.string == '{0}.{1}.0'.format(maj_ver, min_ver)
  138. def test_discover_version(self):
  139. '''
  140. Test call to __discover_version
  141. when using different versions
  142. '''
  143. version = {('3000', None):
  144. {(b'v3000.0rc2-12-g44fe283a77\n', '3000rc2-12-g44fe283a77'),
  145. (b'v3000', '3000'),
  146. (b'1234567', '3000-n/a-1234567'), },
  147. (2019, 2):
  148. {(b'v2019.2.0rc2-12-g44fe283a77\n', '2019.2.0rc2-12-g44fe283a77'),
  149. (b'v2019.2.0', '2019.2.0'),
  150. (b'afc9830198dj', '2019.2.0-n/a-afc9830198dj'), },
  151. }
  152. for maj_min, test_v in version.items():
  153. for tag_ver, exp in version[maj_min]:
  154. salt_ver = SaltStackVersion(major=maj_min[0], minor=maj_min[1], bugfix=None)
  155. attrs = {'communicate.return_value': (tag_ver, b''),
  156. 'returncode.return_value': 0}
  157. proc_ret = MagicMock(**attrs)
  158. proc_mock = patch('subprocess.Popen', return_value=proc_ret)
  159. patch_os = patch('os.path.exists', return_value=True)
  160. with proc_mock, patch_os:
  161. ret = getattr(salt.version, '__discover_version')(salt_ver)
  162. assert ret == exp