diff --git a/oslo/version/version.py b/oslo/version/version.py index 5ed8be1..2b19719 100644 --- a/oslo/version/version.py +++ b/oslo/version/version.py @@ -169,9 +169,9 @@ class VersionInfo(object): if project_name.startswith('python-'): project_name = project_name[7:] - self._vendor = cfg.get(project_name, "vendor", self._vendor) - self._product = cfg.get(project_name, "product", self._product) - self._suffix = cfg.get(project_name, "package", self._suffix) + self._vendor = cfg.get(project_name, "vendor") + self._product = cfg.get(project_name, "product") + self._suffix = cfg.get(project_name, "package") def _load_vendor_strings(self): """Load default and override vendor strings. diff --git a/test-requirements.txt b/test-requirements.txt index cb38d3f..f0b550b 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -2,6 +2,7 @@ coverage>=3.6 discover fixtures>=0.3.12 flake8==2.0 +mock>=1.0 python-subunit oslo.sphinx sphinx>=1.1.2 diff --git a/tests/__init__.py b/tests/__init__.py index d850631..4c597d6 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -22,6 +22,7 @@ __all__ = [ ] import os +import tempfile import fixtures import testresources @@ -53,3 +54,24 @@ class BaseTestCase(testtools.TestCase, testresources.ResourcedTestCase): self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) self.useFixture(fixtures.NestedTempfile()) + + @staticmethod + def write_to_tempfile(content, suffix='', prefix='tmp'): + """Create temporary file or use existing file. + + This util is needed for creating temporary file with + specified content, suffix and prefix. + + :param content: content for temporary file. + :param suffix: same as parameter 'suffix' for mkstemp + :param prefix: same as parameter 'prefix' for mkstemp + + For example: it can be used in database tests for creating + configuration files. + """ + (fd, path) = tempfile.mkstemp(suffix=suffix, prefix=prefix) + try: + os.write(fd, content.encode('utf-8')) + finally: + os.close(fd) + return path diff --git a/tests/test_version.py b/tests/test_version.py index 9ab9e89..daab1c2 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -18,6 +18,7 @@ import os import fixtures +import mock from oslo.version import version import tests @@ -46,3 +47,40 @@ class FindConfigFilesTestCase(tests.BaseTestCase): self.assertEqual(config_files, version._find_config_files(project='blaa', extension='.json')) + + +class BasicVersionTestCase(tests.BaseTestCase): + + def test_version(self): + with mock.patch.object(version.VersionInfo, + '_get_version_from_pkg_resources', + return_value='5.5.5.5'): + v = version.VersionInfo(None) + self.assertEqual(v.version, '5.5.5.5') + + def test_version_and_release(self): + with mock.patch.object(version.VersionInfo, + '_get_version_from_pkg_resources', + return_value='0.5.21.28.gae25b56'): + v = version.VersionInfo(None) + self.assertEqual(v.release, '0.5.21.28.gae25b56') + self.assertEqual(v.version, '0.5.21.28') + + def test_vendor(self): + with mock.patch.multiple(version.VersionInfo, + _get_provider=mock.DEFAULT, + _load_from_pkg_info=mock.DEFAULT, + _load_from_setup_cfg=mock.DEFAULT): + path = self.write_to_tempfile("""[myfoo] +vendor=bigco +product=product123 +package=mysuffix +""") + with mock.patch.object(version, + '_find_config_files', + return_value=path): + v = version.VersionInfo('myfoo') + self.assertEqual('myfoo', v.package) + self.assertEqual('bigco', v.vendor) + self.assertEqual('product123', v.product) + self.assertEqual('mysuffix', v.suffix)