From 5f0e8e4aba409982c03d7fbd38115f0beeceb75b 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 (cherry picked from commit 5d9acb84f492dca4da863c60e30a5463fe165cb5) --- .../openstack/snaps/snaps_test_runner.py | 51 +++++++++++++++++++- .../opnfv_tests/openstack/snaps/snaps_utils.py | 10 ++-- functest/tests/unit/openstack/snaps/test_snaps.py | 56 +++++++++++++++------- 3 files changed, 97 insertions(+), 20 deletions(-) diff --git a/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py b/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py index 70327ff89..ad5287c28 100644 --- a/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py +++ b/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py @@ -11,9 +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 @@ -27,8 +31,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.OperatorCloud(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: @@ -51,3 +88,15 @@ class SnapsTestRunner(unit.Suite): self.image_metadata = None 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") diff --git a/functest/opnfv_tests/openstack/snaps/snaps_utils.py b/functest/opnfv_tests/openstack/snaps/snaps_utils.py index fa1005ea8..4b5ab2911 100644 --- a/functest/opnfv_tests/openstack/snaps/snaps_utils.py +++ b/functest/opnfv_tests/openstack/snaps/snaps_utils.py @@ -45,16 +45,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 9da4f2ac8..57e53f32c 100644 --- a/functest/tests/unit/openstack/snaps/test_snaps.py +++ b/functest/tests/unit/openstack/snaps/test_snaps.py @@ -21,7 +21,7 @@ from functest.opnfv_tests.openstack.snaps import ( class ConnectionCheckTesting(unittest.TestCase): """ - Ensures the VPingUserdata class can run in Functest. This test does not + Ensures the ConnectionCheck class can run in Functest. This test does not actually connect with an OpenStack pod. """ @@ -29,9 +29,15 @@ class ConnectionCheckTesting(unittest.TestCase): self.os_creds = OSCreds( username='user', password='pass', auth_url='http://foo.com:5000/v3', project_name='bar') - - self.connection_check = connection_check.ConnectionCheck( - os_creds=self.os_creds, ext_net_name='foo') + with mock.patch('os_client_config.get_config') as mock_get_config, \ + mock.patch('shade.OperatorCloud') as mock_shade, \ + mock.patch('functest.core.tenantnetwork.NewProject') \ + as mock_new_project: + self.connection_check = connection_check.ConnectionCheck( + 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') @@ -86,7 +92,7 @@ class ConnectionCheckTesting(unittest.TestCase): 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. """ @@ -94,9 +100,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.OperatorCloud') 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_api_tests') @@ -151,7 +163,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. """ @@ -159,9 +171,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.OperatorCloud') 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') @@ -219,7 +237,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. """ @@ -227,9 +245,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.OperatorCloud') 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