From 5d9acb84f492dca4da863c60e30a5463fe165cb5 Mon Sep 17 00:00:00 2001 From: Juha Kosonen Date: Fri, 24 Aug 2018 10:41:49 +0300 Subject: Create new project/user for snaps tests JIRA: FUNCTEST-1003 Change-Id: Ic55998977386f95f619a355d22bd285782fe81f0 Signed-off-by: Juha Kosonen --- .../openstack/snaps/snaps_test_runner.py | 49 +++++++++++++++++++++- .../opnfv_tests/openstack/snaps/snaps_utils.py | 10 +++-- functest/tests/unit/openstack/snaps/test_snaps.py | 42 +++++++++++++------ 3 files changed, 85 insertions(+), 16 deletions(-) diff --git a/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py b/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py index 6d784d35e..c937ed744 100644 --- a/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py +++ b/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py @@ -11,11 +11,13 @@ """configuration params to run snaps tests""" import logging +import uuid import os_client_config import shade from xtesting.core import unit +from functest.core import tenantnetwork from functest.opnfv_tests.openstack.snaps import snaps_utils from functest.utils import config from functest.utils import functest_utils @@ -30,8 +32,41 @@ class SnapsTestRunner(unit.Suite): def __init__(self, **kwargs): super(SnapsTestRunner, self).__init__(**kwargs) self.logger = logging.getLogger(__name__) - self.os_creds = kwargs.get('os_creds') or snaps_utils.get_credentials() + try: + cloud_config = os_client_config.get_config() + self.orig_cloud = shade.OpenStackCloud(cloud_config=cloud_config) + guid = str(uuid.uuid4()) + self.project = tenantnetwork.NewProject( + self.orig_cloud, self.case_name, guid) + self.project.create() + except Exception: # pylint: disable=broad-except + raise Exception("Cannot create user or project") + + if self.orig_cloud.get_role("admin"): + role_name = "admin" + elif self.orig_cloud.get_role("Admin"): + role_name = "Admin" + else: + raise Exception("Cannot detect neither admin nor Admin") + self.orig_cloud.grant_role( + role_name, user=self.project.user.id, + project=self.project.project.id, + domain=self.project.domain.id) + self.role = None + if not self.orig_cloud.get_role("heat_stack_owner"): + self.role = self.orig_cloud.create_role("heat_stack_owner") + self.orig_cloud.grant_role( + "heat_stack_owner", user=self.project.user.id, + project=self.project.project.id, + domain=self.project.domain.id) + creds_overrides = dict( + username=self.project.user.name, + project_name=self.project.project.name, + project_id=self.project.project.id, + password=self.project.password) + self.os_creds = kwargs.get('os_creds') or snaps_utils.get_credentials( + overrides=creds_overrides) if 'ext_net_name' in kwargs: self.ext_net_name = kwargs['ext_net_name'] else: @@ -55,6 +90,18 @@ class SnapsTestRunner(unit.Suite): if hasattr(config.CONF, 'snaps_images'): self.image_metadata = getattr(config.CONF, 'snaps_images') + def clean(self): + """Cleanup of OpenStack resources. Should be called on completion.""" + try: + super(SnapsTestRunner, self).clean() + assert self.orig_cloud + assert self.project + if self.role: + self.orig_cloud.delete_role(self.role.id) + self.project.clean() + except Exception: # pylint: disable=broad-except + self.__logger.exception("Cannot clean all resources") + def check_requirements(self): """Skip if OpenStack Rocky or newer.""" try: diff --git a/functest/opnfv_tests/openstack/snaps/snaps_utils.py b/functest/opnfv_tests/openstack/snaps/snaps_utils.py index 21d1dd60d..f4a6e2ea0 100644 --- a/functest/opnfv_tests/openstack/snaps/snaps_utils.py +++ b/functest/opnfv_tests/openstack/snaps/snaps_utils.py @@ -46,16 +46,20 @@ def get_active_compute_cnt(os_creds): return len(computes) -def get_credentials(proxy_settings_str=None, ssh_proxy_cmd=None): +def get_credentials(proxy_settings_str=None, ssh_proxy_cmd=None, + overrides=None): """ Returns snaps OSCreds object instance :param: proxy_settings_str: proxy settings string : :param: ssh_proxy_cmd: the SSH proxy command for the environment + :param overrides: dict() values to override in credentials :return: an instance of snaps OSCreds object """ - creds_override = None + creds_override = {} if hasattr(config.CONF, 'snaps_os_creds_override'): - creds_override = getattr(config.CONF, 'snaps_os_creds_override') + creds_override.update(getattr(config.CONF, 'snaps_os_creds_override')) + if overrides: + creds_override.update(overrides) os_creds = openstack_tests.get_credentials( os_env_file=constants.ENV_FILE, proxy_settings_str=proxy_settings_str, ssh_proxy_cmd=ssh_proxy_cmd, overrides=creds_override) diff --git a/functest/tests/unit/openstack/snaps/test_snaps.py b/functest/tests/unit/openstack/snaps/test_snaps.py index a3760445f..14bc38596 100644 --- a/functest/tests/unit/openstack/snaps/test_snaps.py +++ b/functest/tests/unit/openstack/snaps/test_snaps.py @@ -22,7 +22,7 @@ from functest.opnfv_tests.openstack.snaps import smoke class APICheckTesting(unittest.TestCase): """ - Ensures the VPingUserdata class can run in Functest. This test does not + Ensures the ApiCheck class can run in Functest. This test does not actually connect with an OpenStack pod. """ @@ -30,9 +30,15 @@ class APICheckTesting(unittest.TestCase): self.os_creds = OSCreds( username='user', password='pass', auth_url='http://foo.com:5000/v3', project_name='bar') - - self.api_check = api_check.ApiCheck( - os_creds=self.os_creds, ext_net_name='foo') + with mock.patch('os_client_config.get_config') as mock_get_config, \ + mock.patch('shade.OpenStackCloud') as mock_shade, \ + mock.patch('functest.core.tenantnetwork.NewProject') \ + as mock_new_project: + self.api_check = api_check.ApiCheck( + os_creds=self.os_creds, ext_net_name='foo') + mock_get_config.assert_called() + mock_shade.assert_called() + mock_new_project.assert_called() @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_suite_builder.' 'add_openstack_client_tests') @@ -93,7 +99,7 @@ class APICheckTesting(unittest.TestCase): class HealthCheckTesting(unittest.TestCase): """ - Ensures the VPingUserdata class can run in Functest. This test does not + Ensures the HealthCheck class can run in Functest. This test does not actually connect with an OpenStack pod. """ @@ -101,9 +107,15 @@ class HealthCheckTesting(unittest.TestCase): self.os_creds = OSCreds( username='user', password='pass', auth_url='http://foo.com:5000/v3', project_name='bar') - - self.health_check = health_check.HealthCheck( - os_creds=self.os_creds, ext_net_name='foo') + with mock.patch('os_client_config.get_config') as mock_get_config, \ + mock.patch('shade.OpenStackCloud') as mock_shade, \ + mock.patch('functest.core.tenantnetwork.NewProject') \ + as mock_new_project: + self.health_check = health_check.HealthCheck( + os_creds=self.os_creds, ext_net_name='foo') + mock_get_config.assert_called() + mock_shade.assert_called() + mock_new_project.assert_called() @mock.patch('snaps.openstack.tests.os_source_file_test.' 'OSIntegrationTestCase.parameterize') @@ -161,7 +173,7 @@ class HealthCheckTesting(unittest.TestCase): class SmokeTesting(unittest.TestCase): """ - Ensures the VPingUserdata class can run in Functest. This test does not + Ensures the SnapsSmoke class can run in Functest. This test does not actually connect with an OpenStack pod. """ @@ -169,9 +181,15 @@ class SmokeTesting(unittest.TestCase): self.os_creds = OSCreds( username='user', password='pass', auth_url='http://foo.com:5000/v3', project_name='bar') - - self.smoke = smoke.SnapsSmoke( - os_creds=self.os_creds, ext_net_name='foo') + with mock.patch('os_client_config.get_config') as mock_get_config, \ + mock.patch('shade.OpenStackCloud') as mock_shade, \ + mock.patch('functest.core.tenantnetwork.NewProject') \ + as mock_new_project: + self.smoke = smoke.SnapsSmoke( + os_creds=self.os_creds, ext_net_name='foo') + mock_get_config.assert_called() + mock_shade.assert_called() + mock_new_project.assert_called() @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_suite_builder.' 'add_openstack_integration_tests') -- cgit 1.2.3-korg