From d589e4e5345ed82c68d9a011ac89f8cdbefe2ca3 Mon Sep 17 00:00:00 2001 From: Cédric Ollivier Date: Wed, 14 Feb 2018 16:02:53 +0100 Subject: Get properly env vars or their default values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It defines env.get() as an unique way to get Functest env vars or their default values. It can be considered as a wrapper above os.environ. It enforces backward compatibility via CONST which mustn't be used for that purpose. It should be noted that it also stops using CONST for getting OpenStack env vars. Change-Id: I333dc1afbc0123166a7eaff8b551370098efa341 Signed-off-by: Cédric Ollivier --- .../tests/unit/openstack/tempest/test_tempest.py | 10 +-- functest/tests/unit/utils/test_env.py | 96 ++++++++++++++++++++++ functest/tests/unit/utils/test_functest_utils.py | 2 +- 3 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 functest/tests/unit/utils/test_env.py (limited to 'functest/tests') diff --git a/functest/tests/unit/openstack/tempest/test_tempest.py b/functest/tests/unit/openstack/tempest/test_tempest.py index 5d543ffc..060a8a01 100644 --- a/functest/tests/unit/openstack/tempest/test_tempest.py +++ b/functest/tests/unit/openstack/tempest/test_tempest.py @@ -8,6 +8,7 @@ # pylint: disable=missing-docstring import logging +import os import unittest import mock @@ -15,7 +16,6 @@ import mock from functest.core import testcase from functest.opnfv_tests.openstack.tempest import tempest from functest.opnfv_tests.openstack.tempest import conf_utils -from functest.utils.constants import CONST from snaps.openstack.os_credentials import OSCreds @@ -115,8 +115,8 @@ class OSTempestTesting(unittest.TestCase): mock.patch.object(self.tempestcommon, 'read_file', return_value=['test1', 'test2']): conf_utils.TEMPEST_BLACKLIST = Exception - CONST.__setattr__('INSTALLER_TYPE', 'installer_type') - CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario') + os.environ['INSTALLER_TYPE'] = 'installer_type' + os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario' self.tempestcommon.apply_tempest_blacklist() obj = mock_open() obj.write.assert_any_call('test1\n') @@ -131,8 +131,8 @@ class OSTempestTesting(unittest.TestCase): return_value=['test1', 'test2']), \ mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'yaml.safe_load', return_value=item_dict): - CONST.__setattr__('INSTALLER_TYPE', 'installer_type') - CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario') + os.environ['INSTALLER_TYPE'] = 'installer_type' + os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario' self.tempestcommon.apply_tempest_blacklist() obj = mock_open() obj.write.assert_any_call('test1\n') diff --git a/functest/tests/unit/utils/test_env.py b/functest/tests/unit/utils/test_env.py new file mode 100644 index 00000000..064ff988 --- /dev/null +++ b/functest/tests/unit/utils/test_env.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python + +# Copyright (c) 2018 Orange and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +# pylint: disable=missing-docstring + +import logging +import os +import unittest + +from six.moves import reload_module + +from functest.utils import env +from functest.utils import constants + + +class EnvTesting(unittest.TestCase): + # pylint: disable=missing-docstring + + def setUp(self): + os.environ['FOO'] = 'foo' + os.environ['BUILD_TAG'] = 'master' + os.environ['CI_LOOP'] = 'weekly' + + def test_get_unset_unknown_env(self): + del os.environ['FOO'] + self.assertEqual(env.get('FOO'), None) + # Backward compatibilty (waiting for SDNVPN and SFC) + reload_module(env) + with self.assertRaises(AttributeError): + getattr(env.ENV, 'FOO') + reload_module(constants) + with self.assertRaises(AttributeError): + getattr(constants.CONST, 'FOO') + + def test_get_unknown_env(self): + self.assertEqual(env.get('FOO'), 'foo') + reload_module(env) + # Backward compatibilty (waiting for SDNVPN and SFC) + with self.assertRaises(AttributeError): + getattr(env.ENV, 'FOO') + reload_module(constants) + with self.assertRaises(AttributeError): + getattr(constants.CONST, 'FOO') + + def test_get_unset_env(self): + del os.environ['CI_LOOP'] + self.assertEqual( + env.get('CI_LOOP'), env.INPUTS['CI_LOOP']) + # Backward compatibilty (waiting for SDNVPN and SFC) + reload_module(env) + self.assertEqual( + getattr(env.ENV, 'CI_LOOP'), env.INPUTS['CI_LOOP']) + reload_module(constants) + self.assertEqual( + getattr(constants.CONST, 'CI_LOOP'), + env.INPUTS['CI_LOOP']) + + def test_get_env(self): + self.assertEqual( + env.get('CI_LOOP'), 'weekly') + # Backward compatibilty (waiting for SDNVPN and SFC) + reload_module(env) + self.assertEqual(getattr(env.ENV, 'CI_LOOP'), 'weekly') + reload_module(constants) + self.assertEqual(getattr(constants.CONST, 'CI_LOOP'), 'weekly') + + def test_get_unset_env2(self): + del os.environ['BUILD_TAG'] + self.assertEqual( + env.get('BUILD_TAG'), env.INPUTS['BUILD_TAG']) + # Backward compatibilty (waiting for SDNVPN and SFC) + reload_module(env) + self.assertEqual( + getattr(env.ENV, 'BUILD_TAG'), env.INPUTS['BUILD_TAG']) + reload_module(constants) + self.assertEqual( + getattr(constants.CONST, 'BUILD_TAG'), env.INPUTS['BUILD_TAG']) + + def test_get_env2(self): + self.assertEqual(env.get('BUILD_TAG'), 'master') + # Backward compatibilty (waiting for SDNVPN and SFC) + reload_module(env) + self.assertEqual(getattr(env.ENV, 'BUILD_TAG'), 'master') + reload_module(env) + self.assertEqual(getattr(constants.CONST, 'BUILD_TAG'), 'master') + + +if __name__ == "__main__": + logging.disable(logging.CRITICAL) + unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py index dd34c90d..9f8733bb 100644 --- a/functest/tests/unit/utils/test_functest_utils.py +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -128,7 +128,7 @@ class FunctestUtilsTesting(unittest.TestCase): self.assertEqual(functest_utils.get_resolvconf_ns(), self.test_ip[1:]) - def _get_environ(self, var): + def _get_environ(self, var, *args): # pylint: disable=unused-argument if var == 'INSTALLER_TYPE': return self.installer elif var == 'DEPLOY_SCENARIO': -- cgit 1.2.3-korg