From 0e12e2e5ac8f1ae46a1c19436675da5075ea8f44 Mon Sep 17 00:00:00 2001 From: boucherv Date: Thu, 20 Apr 2017 16:20:57 +0200 Subject: [VNF_base] Support existing tenant and user Support existing tenant and user with a get_or_create_tenant method. If the tenant and user already exist it only get the id of those JIRA: FUNCTEST-799 Change-Id: Id5daf2904592e42199471bdd3547e861e6db6e32 Signed-off-by: boucherv --- functest/core/vnf_base.py | 107 ++++++++++++++++---------------------- functest/utils/openstack_utils.py | 18 +++++++ 2 files changed, 63 insertions(+), 62 deletions(-) diff --git a/functest/core/vnf_base.py b/functest/core/vnf_base.py index 6bd6e4311..2de28c124 100644 --- a/functest/core/vnf_base.py +++ b/functest/core/vnf_base.py @@ -27,6 +27,12 @@ class VnfOnBoardingBase(base.TestCase): self.cmd = kwargs.get('cmd', '') self.details = {} self.result_dir = CONST.dir_results + self.details_step_mapping = dict( + deploy_orchestrator='orchestrator', + deploy_vnf='vnf', + test_vnf='test_vnf', + prepare='prepare_env') + self.details['prepare_env'] = {} self.details['orchestrator'] = {} self.details['vnf'] = {} self.details['test_vnf'] = {} @@ -118,48 +124,45 @@ class VnfOnBoardingBase(base.TestCase): self.logger.info("Prepare OpenStack plateform(create tenant and user)") admin_user_id = os_utils.get_user_id(self.keystone_client, self.creds['username']) - if admin_user_id == '': - self.step_failure("Failed to get id of " + - self.creds['username']) + if not admin_user_id: + self.step_failure("Failed to get id of {0}".format( + self.creds['username'])) - tenant_id = os_utils.create_tenant( - self.keystone_client, self.tenant_name, self.tenant_description) + tenant_id = os_utils.get_tenant_id(self.keystone_client, + self.tenant_name) if not tenant_id: - self.step_failure("Failed to create " + - self.tenant_name + " tenant") - - roles_name = ["admin", "Admin"] - role_id = '' - for role_name in roles_name: - if role_id == '': - role_id = os_utils.get_role_id(self.keystone_client, role_name) - - if role_id == '': - self.logger.error("Failed to get id for %s role" % role_name) - self.step_failure("Failed to get role id of " + role_name) - - if not os_utils.add_role_user(self.keystone_client, admin_user_id, - role_id, tenant_id): - self.logger.error("Failed to add %s on tenant" % - self.creds['username']) - self.step_failure("Failed to add %s on tenant" % - self.creds['username']) - - user_id = os_utils.create_user(self.keystone_client, - self.tenant_name, - self.tenant_name, - None, - tenant_id) + tenant_id = os_utils.create_tenant(self.keystone_client, + self.tenant_name, + self.tenant_description) + if not tenant_id: + self.step_failure("Failed to get or create {0} tenant".format( + self.tenant_name)) + roles_name = ["admin", "Admin"] + role_id = '' + for role_name in roles_name: + if not role_id: + role_id = os_utils.get_role_id(self.keystone_client, + role_name) + + if not role_id: + self.step_failure("Failed to get id for {0} role".format( + role_name)) + + if not os_utils.add_role_user(self.keystone_client, admin_user_id, + role_id, tenant_id): + self.step_failure("Failed to add {0} on tenant".format( + self.creds['username'])) + + user_id = os_utils.get_or_create_user(self.keystone_client, + self.tenant_name, + self.tenant_name, + tenant_id) if not user_id: - self.logger.error("Failed to create %s user" % self.tenant_name) - self.step_failure("Failed to create user ") + self.step_failure("Failed to get or create {0} user".format( + self.tenant_name)) - if not os_utils.add_role_user(self.keystone_client, user_id, - role_id, tenant_id): - self.logger.error("Failed to add %s on tenant" % - self.tenant_name) - self.step_failure("Failed to add %s on tenant" % - self.tenant_name) + os_utils.add_role_user(self.keystone_client, user_id, + role_id, tenant_id) self.logger.info("Update OpenStack creds informations") self.admin_creds = self.creds.copy() @@ -187,31 +190,10 @@ class VnfOnBoardingBase(base.TestCase): self.logger.error("VNF must be tested") raise Exception("VNF not tested") + # clean before openstack clean run def clean(self): self.logger.info("test cleaning") - self.logger.info("Removing %s tenant .." % self.tenant_name) - tenant_id = os_utils.get_tenant_id(self.keystone_client, - self.tenant_name) - if tenant_id == '': - self.logger.error("Error : Failed to get id of %s tenant" % - self.tenant_name) - else: - if not os_utils.delete_tenant(self.keystone_client, tenant_id): - self.logger.error("Error : Failed to remove %s tenant" % - self.tenant_name) - - self.logger.info("Removing %s user .." % self.tenant_name) - user_id = os_utils.get_user_id( - self.keystone_client, self.tenant_name) - if user_id == '': - self.logger.error("Error : Failed to get id of %s user" % - self.tenant_name) - else: - if not os_utils.delete_user(self.keystone_client, user_id): - self.logger.error("Error : Failed to remove %s user" % - self.tenant_name) - def parse_results(self): exit_code = self.EX_OK self.criteria = "PASS" @@ -231,9 +213,10 @@ class VnfOnBoardingBase(base.TestCase): def step_failure(self, error_msg): part = inspect.stack()[1][3] - self.logger.error("Step '%s' failed: %s", part, error_msg) + self.logger.error("Step {0} failed: {1}".format(part, error_msg)) try: - part_info = self.details[part] + step_name = self.details_step_mapping[part] + part_info = self.details[step_name] except KeyError: self.details[part] = {} part_info = self.details[part] diff --git a/functest/utils/openstack_utils.py b/functest/utils/openstack_utils.py index 4663f7ba6..929a761e0 100644 --- a/functest/utils/openstack_utils.py +++ b/functest/utils/openstack_utils.py @@ -1394,6 +1394,15 @@ def create_tenant(keystone_client, tenant_name, tenant_description): return None +def get_or_create_tenant(keystone_client, tenant_name, tenant_description): + tenant_id = get_tenant_id(keystone_client, tenant_name) + if not tenant_id: + tenant_id = create_tenant(keystone_client, tenant_name, + tenant_description) + + return tenant_id + + def create_user(keystone_client, user_name, user_password, user_email, tenant_id): try: @@ -1417,6 +1426,15 @@ def create_user(keystone_client, user_name, user_password, return None +def get_or_create_user(keystone_client, user_name, user_password, + tenant_id, user_email=None): + user_id = get_user_id(keystone_client, user_name) + if not user_id: + user_id = create_user(keystone_client, user_name, user_password, + user_email, tenant_id) + return user_id + + def add_role_user(keystone_client, user_id, role_id, tenant_id): try: if is_keystone_v3(): -- cgit 1.2.3-korg