TestVersion.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. '''
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. '''
  16. from unittest import TestCase
  17. import os
  18. class TestVersion(TestCase):
  19. """
  20. Class that tests the method of the version.py file used to format and compare version numbers
  21. of both Ambari (which use 3 digits separated by dots) and stacks (which use 4 digits separated by dots).
  22. """
  23. def setUp(self):
  24. import imp
  25. self.test_directory = os.path.dirname(os.path.abspath(__file__))
  26. test_file_path = os.path.join(self.test_directory, '../../../../ambari-common/src/main/python/resource_management/libraries/functions/version.py')
  27. with open(test_file_path, 'rb') as fp:
  28. self.version_module = imp.load_module('version', fp, test_file_path, ('.py', 'rb', imp.PY_SOURCE))
  29. def test_format(self):
  30. l = [("2.2", "2.2.0.0"),
  31. ("2.2.1", "2.2.1.0"),
  32. ("2.2.1.3", "2.2.1.3")]
  33. for input, expected in l:
  34. actual = self.version_module.format_hdp_stack_version(input)
  35. self.assertEqual(expected, actual)
  36. gluster_fs_actual = self.version_module.format_hdp_stack_version("GlusterFS")
  37. self.assertEqual("", gluster_fs_actual)
  38. def test_comparison(self):
  39. # All versions to compare, from 1.0.0.0 to 3.0.0.0, and only include elements that are a multiple of 7.
  40. versions = range(1000, 3000, 7)
  41. versions = [".".join(list(str(elem))) for elem in versions]
  42. for idx, x in enumerate(versions):
  43. for idy, y in enumerate(versions):
  44. # Expected value will either be -1, 0, 1, and it relies on the fact
  45. # that an increasing index implies a greater version number.
  46. expected_value = cmp(idx, idy)
  47. actual_value = self.version_module.compare_versions(x, y)
  48. self.assertEqual(expected_value, actual_value)
  49. # Try something fancier
  50. self.assertEqual(0, self.version_module.compare_versions("2.10", "2.10.0"))
  51. self.assertEqual(0, self.version_module.compare_versions("2.10", "2.10.0.0"))
  52. self.assertEqual(0, self.version_module.compare_versions("2.10.0", "2.10.0.0"))
  53. try:
  54. self.version_module.compare_versions("", "GlusterFS")
  55. except ValueError:
  56. pass
  57. else:
  58. self.fail("Did not raise exception")