test_version.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. # Import Salt libs
  14. from salt.version import SaltStackVersion, versions_report
  15. class VersionTestCase(TestCase):
  16. def test_version_parsing(self):
  17. strip_initial_non_numbers_regex = re.compile(r'(?:[^\d]+)?(?P<vs>.*)')
  18. expect = (
  19. ('v0.12.0-19-g767d4f9', (0, 12, 0, 0, '', 0, 19, 'g767d4f9'), None),
  20. ('v0.12.0-85-g2880105', (0, 12, 0, 0, '', 0, 85, 'g2880105'), None),
  21. ('debian/0.11.1+ds-1-3-ga0afcbd',
  22. (0, 11, 1, 0, '', 0, 3, 'ga0afcbd'), '0.11.1-3-ga0afcbd'),
  23. ('0.12.1', (0, 12, 1, 0, '', 0, 0, None), None),
  24. ('0.12.1', (0, 12, 1, 0, '', 0, 0, None), None),
  25. ('0.17.0rc1', (0, 17, 0, 0, 'rc', 1, 0, None), None),
  26. ('v0.17.0rc1-1-g52ebdfd', (0, 17, 0, 0, 'rc', 1, 1, 'g52ebdfd'), None),
  27. ('v2014.1.4.1', (2014, 1, 4, 1, '', 0, 0, None), None),
  28. ('v2014.1.4.1rc3-n/a-abcdefgh', (2014, 1, 4, 1, 'rc', 3, -1, 'abcdefgh'), None),
  29. ('v3.4.1.1', (3, 4, 1, 1, '', 0, 0, None), None)
  30. )
  31. for vstr, full_info, version in expect:
  32. saltstack_version = SaltStackVersion.parse(vstr)
  33. self.assertEqual(
  34. saltstack_version.full_info, full_info
  35. )
  36. if version is None:
  37. version = strip_initial_non_numbers_regex.search(vstr).group('vs')
  38. self.assertEqual(saltstack_version.string, version)
  39. def test_version_comparison(self):
  40. examples = (
  41. ('debian/0.11.1+ds-1-3-ga0afcbd', '0.11.1+ds-2'),
  42. ('v0.12.0-85-g2880105', 'v0.12.0-19-g767d4f9'),
  43. ('v0.17.0rc1-1-g52ebdfd', '0.17.0rc1'),
  44. ('v0.17.0', 'v0.17.0rc1'),
  45. ('Hydrogen', '0.17.0'),
  46. ('Helium', 'Hydrogen'),
  47. ('v2014.1.4.1-n/a-abcdefgh', 'v2014.1.4.1rc3-n/a-abcdefgh'),
  48. ('v2014.1.4.1-1-abcdefgh', 'v2014.1.4.1-n/a-abcdefgh'),
  49. ('v2016.12.0rc1', 'v2016.12.0b1'),
  50. ('v2016.12.0beta1', 'v2016.12.0alpha1'),
  51. ('v2016.12.0alpha1', 'v2016.12.0alpha0')
  52. )
  53. for higher_version, lower_version in examples:
  54. self.assertTrue(SaltStackVersion.parse(higher_version) > lower_version)
  55. self.assertTrue(SaltStackVersion.parse(lower_version) < higher_version)
  56. def test_unparsable_version(self):
  57. with self.assertRaises(ValueError):
  58. SaltStackVersion.from_name('Drunk')
  59. with self.assertRaises(ValueError):
  60. SaltStackVersion.parse('Drunk')
  61. def test_version_report_lines(self):
  62. '''
  63. Validate padding in versions report is correct
  64. '''
  65. # Get a set of all version report name lenghts including padding
  66. line_lengths = set([
  67. len(line.split(':')[0]) for line in list(versions_report())[4:]
  68. if line != ' ' and line != 'System Versions:'
  69. ])
  70. # Check that they are all the same size (only one element in the set)
  71. assert len(line_lengths) == 1