From ed1e27faa1d8634cd8fc89776c36d5a32dd97214 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Wed, 23 Oct 2013 22:15:32 -0400 Subject: Remove bad parameter for cfg.get when loading from cfg file, cfg.get takes just 2 parameters not three. Added some basic tests and test for the specific change as well Change-Id: I633d665f63271b6ada9196a0f08028d404b33110 --- tests/__init__.py | 22 ++++++++++++++++++++++ tests/test_version.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) (limited to 'tests') 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) -- cgit v1.2.1