diff options
-rw-r--r-- | functest/opnfv_tests/openstack/vping/vping_base.py | 3 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_functest_utils.py | 66 | ||||
-rw-r--r-- | functest/utils/functest_utils.py | 19 |
3 files changed, 87 insertions, 1 deletions
diff --git a/functest/opnfv_tests/openstack/vping/vping_base.py b/functest/opnfv_tests/openstack/vping/vping_base.py index 246b12c97..b1360d7c9 100644 --- a/functest/opnfv_tests/openstack/vping/vping_base.py +++ b/functest/opnfv_tests/openstack/vping/vping_base.py @@ -21,6 +21,7 @@ from xtesting.core import testcase from functest.utils import config from functest.utils import env +from functest.utils import functest_utils class VPingBase(testcase.TestCase): @@ -37,7 +38,7 @@ class VPingBase(testcase.TestCase): self.logger = logging.getLogger(__name__) self.cloud = shade.OperatorCloud( cloud_config=os_client_config.get_config()) - self.ext_net = self.cloud.get_network("ext-net") + self.ext_net = functest_utils.get_external_network(self.cloud) self.logger.debug("ext_net: %s", self.ext_net) self.guid = '-' + str(uuid.uuid4()) self.network = None diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py index d35ed8ced..7debcc1df 100644 --- a/functest/tests/unit/utils/test_functest_utils.py +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -11,9 +11,11 @@ import logging import time +import os import unittest import mock +import munch import pkg_resources from functest.utils import functest_utils @@ -218,6 +220,70 @@ class FunctestUtilsTesting(unittest.TestCase): self.test_file), 'test_image_name') + def test_get_extnetwork_nocloud(self): + with self.assertRaises(AssertionError): + functest_utils.get_external_network(None) + + def test_get_extnetwork_env_ok1(self): + cloud = mock.Mock() + cloud.get_network.return_value = munch.Munch(name="dummy") + os.environ["EXTERNAL_NETWORK"] = 'dummy' + self.assertEqual( + functest_utils.get_external_network(cloud), + cloud.get_network.return_value) + cloud.get_network.assert_called_once_with( + 'dummy', {'router:external': True}) + cloud.list_networks.assert_not_called() + + def test_get_extnetwork_env_ok2(self): + cloud = mock.Mock() + cloud.get_network.return_value = None + cloud.list_networks.return_value = None + os.environ["EXTERNAL_NETWORK"] = 'dummy' + self.assertEqual(functest_utils.get_external_network(cloud), None) + cloud.get_network.assert_called_once_with( + 'dummy', {'router:external': True}) + cloud.list_networks.assert_called_once_with( + {'router:external': True}) + + def test_get_extnetwork_env_ko(self): + cloud = mock.Mock() + cloud.get_network.return_value = None + cloud.list_networks.return_value = [munch.Munch(name="dummy")] + os.environ["EXTERNAL_NETWORK"] = 'dummy' + self.assertEqual( + functest_utils.get_external_network(cloud), + cloud.list_networks.return_value[0]) + cloud.get_network.assert_called_once_with( + 'dummy', {'router:external': True}) + cloud.list_networks.assert_called_once_with( + {'router:external': True}) + + def test_get_extnetwork_noenv_ko(self): + try: + del os.environ["EXTERNAL_NETWORK"] + except Exception: # pylint: disable=broad-except + pass + cloud = mock.Mock() + cloud.list_networks.return_value = None + self.assertEqual(functest_utils.get_external_network(cloud), None) + cloud.get_network.assert_not_called() + cloud.list_networks.assert_called_once_with( + {'router:external': True}) + + def test_get_extnetwork_noenv_ok(self): + try: + del os.environ["EXTERNAL_NETWORK"] + except Exception: # pylint: disable=broad-except + pass + cloud = mock.Mock() + cloud.list_networks.return_value = [munch.Munch(name="dummy")] + self.assertEqual( + functest_utils.get_external_network(cloud), + cloud.list_networks.return_value[0]) + cloud.get_network.assert_not_called() + cloud.list_networks.assert_called_once_with( + {'router:external': True}) if __name__ == "__main__": logging.disable(logging.CRITICAL) diff --git a/functest/utils/functest_utils.py b/functest/utils/functest_utils.py index b614af321..b860828f1 100644 --- a/functest/utils/functest_utils.py +++ b/functest/utils/functest_utils.py @@ -15,6 +15,8 @@ import subprocess import sys import yaml +from functest.utils import env + LOGGER = logging.getLogger(__name__) @@ -72,3 +74,20 @@ def get_parameter_from_yaml(parameter, yfile): raise ValueError("The parameter %s is not defined in" " %s" % (parameter, yfile)) return value + + +def get_external_network(cloud): + """ + Returns the configured external network name or + the first retrieved external network name + """ + assert cloud + if env.get("EXTERNAL_NETWORK"): + network = cloud.get_network( + env.get("EXTERNAL_NETWORK"), {"router:external": True}) + if network: + return network + networks = cloud.list_networks({"router:external": True}) + if networks: + return networks[0] + return None |