diff --git a/tacker/tests/functional/base.py b/tacker/tests/functional/base.py index c8ff61680..9319c5c32 100644 --- a/tacker/tests/functional/base.py +++ b/tacker/tests/functional/base.py @@ -103,14 +103,16 @@ class BaseTackerTest(base.BaseTestCase): cls.cinder_client = cls.cinderclient() @classmethod - def get_credentials(cls): - vim_params = yaml.safe_load(read_file('local-vim.yaml')) + def get_credentials(cls, vim_conf_file=None): + if vim_conf_file is None: + vim_conf_file = 'local-vim.yaml' + vim_params = yaml.safe_load(read_file(vim_conf_file)) vim_params['auth_url'] += '/v3' return vim_params @classmethod - def get_auth_session(cls): - vim_params = cls.get_credentials() + def get_auth_session(cls, vim_conf_file=None): + vim_params = cls.get_credentials(vim_conf_file) auth = v3.Password( auth_url=vim_params['auth_url'], username=vim_params['username'], @@ -123,20 +125,20 @@ class BaseTackerTest(base.BaseTestCase): return auth_ses @classmethod - def tacker_http_client(cls): - auth_session = cls.get_auth_session() + def tacker_http_client(cls, vim_conf_file=None): + auth_session = cls.get_auth_session(vim_conf_file) return SessionClient(session=auth_session, service_type='nfv-orchestration', region_name='RegionOne') @classmethod - def tackerclient(cls): - auth_session = cls.get_auth_session() + def tackerclient(cls, vim_conf_file=None): + auth_session = cls.get_auth_session(vim_conf_file) return tacker_client.Client(session=auth_session, retries=5) @classmethod - def novaclient(cls): - vim_params = cls.get_credentials() + def novaclient(cls, vim_conf_file=None): + vim_params = cls.get_credentials(vim_conf_file) auth = v3.Password(auth_url=vim_params['auth_url'], username=vim_params['username'], password=vim_params['password'], @@ -149,8 +151,8 @@ class BaseTackerTest(base.BaseTestCase): session=auth_ses) @classmethod - def neutronclient(cls): - vim_params = cls.get_credentials() + def neutronclient(cls, vim_conf_file=None): + vim_params = cls.get_credentials(vim_conf_file) auth = v3.Password(auth_url=vim_params['auth_url'], username=vim_params['username'], password=vim_params['password'], @@ -162,16 +164,18 @@ class BaseTackerTest(base.BaseTestCase): return neutron_client.Client(session=auth_ses) @classmethod - def heatclient(cls): - data = yaml.safe_load(read_file('local-vim.yaml')) + def heatclient(cls, vim_conf_file=None): + if vim_conf_file is None: + vim_conf_file = 'local-vim.yaml' + data = yaml.safe_load(read_file(vim_conf_file)) data['auth_url'] = data['auth_url'] + '/v3' domain_name = data.pop('domain_name') data['user_domain_name'] = domain_name data['project_domain_name'] = domain_name return clients.OpenstackClients(auth_attr=data).heat - def blazarclient(cls): - data = cls.get_credentials() + def blazarclient(cls, vim_conf_file=None): + data = cls.get_credentials(vim_conf_file) domain_name = data.pop('domain_name') data['user_domain_name'] = domain_name data['project_domain_name'] = domain_name @@ -186,8 +190,8 @@ class BaseTackerTest(base.BaseTestCase): return client @classmethod - def glanceclient(cls): - vim_params = cls.get_credentials() + def glanceclient(cls, vim_conf_file=None): + vim_params = cls.get_credentials(vim_conf_file) auth = v3.Password(auth_url=vim_params['auth_url'], username=vim_params['username'], password=vim_params['password'], @@ -199,15 +203,15 @@ class BaseTackerTest(base.BaseTestCase): return glance_client.Client(session=auth_ses) @classmethod - def aodh_http_client(cls): - auth_session = cls.get_auth_session() + def aodh_http_client(cls, vim_conf_file=None): + auth_session = cls.get_auth_session(vim_conf_file) return SessionClient(session=auth_session, service_type='alarming', region_name='RegionOne') @classmethod - def cinderclient(cls): - vim_params = cls.get_credentials() + def cinderclient(cls, vim_conf_file=None): + vim_params = cls.get_credentials(vim_conf_file) auth = v3.Password(auth_url=vim_params['auth_url'], username=vim_params['username'], password=vim_params['password'], diff --git a/tacker/tests/functional/sol/vnflcm/base.py b/tacker/tests/functional/sol/vnflcm/base.py index 1ec9a47ae..afbcd65fa 100644 --- a/tacker/tests/functional/sol/vnflcm/base.py +++ b/tacker/tests/functional/sol/vnflcm/base.py @@ -38,6 +38,7 @@ VNF_HEAL_TIMEOUT = 600 VNF_LCM_DONE_TIMEOUT = 1200 RETRY_WAIT_TIME = 5 FAKE_SERVER_MANAGER = FakeServerManager.get_instance() +FAKE_SERVER_PORT = 9990 MOCK_NOTIFY_CALLBACK_URL = '/notification/callback' UUID_RE = r'\w{8}-\w{4}-\w{4}-\w{4}-\w{12}' @@ -215,8 +216,8 @@ class BaseVnfLcmTest(base.BaseTackerTest): we set up fake NFVO server for test at here. ''' super(BaseVnfLcmTest, cls).setUpClass() - FAKE_SERVER_MANAGER.prepare_http_server() - FAKE_SERVER_MANAGER.start_server() + cls._prepare_start_fake_server(FAKE_SERVER_MANAGER, + FAKE_SERVER_PORT) @classmethod def tearDownClass(cls): @@ -232,17 +233,8 @@ class BaseVnfLcmTest(base.BaseTackerTest): callback_url = os.path.join( MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) - FAKE_SERVER_MANAGER.clear_history(callback_url) - FAKE_SERVER_MANAGER.set_callback( - 'POST', - callback_url, - status_code=204 - ) - FAKE_SERVER_MANAGER.set_callback( - 'GET', - callback_url, - status_code=204 - ) + self._clear_history_and_set_callback(FAKE_SERVER_MANAGER, + callback_url) self.tacker_client = base.BaseTackerTest.tacker_http_client() @@ -254,62 +246,110 @@ class BaseVnfLcmTest(base.BaseTackerTest): self.vim = self.get_vim(vim_list, 'VIM0') if not self.vim: assert False, "vim_list is Empty: Default VIM is missing" + result = self._create_network_settings() + self.ext_networks = result.get('ext_networks') + self.ext_vl = result.get('ext_vl') + self.ext_mngd_networks = result.get('ext_mngd_networks') + self.ext_link_ports = result.get('ext_link_ports') + self.ext_subnets = result.get('ext_subnets') + self.changed_ext_networks = result.get('changed_ext_networks') + self.changed_ext_subnets = result.get('changed_ext_subnets') + @classmethod + def _prepare_start_fake_server(cls, fake_server, + fake_server_port): + fake_server.prepare_http_server(address='localhost', + port=fake_server_port) + fake_server.start_server() + + def _clear_history_and_set_callback(self, + fake_server_manager, + callback_url): + fake_server_manager.clear_history(callback_url) + fake_server_manager.set_callback( + 'POST', + callback_url, + status_code=204 + ) + fake_server_manager.set_callback( + 'GET', + callback_url, + status_code=204 + ) + + def _create_network_settings(self, neutron_client=None): + if neutron_client is None: + neutron_client = self.neutronclient() # Create external external. - self.ext_networks = list() + ext_networks = list() # Create external managed networks - self.ext_mngd_networks = list() # Store ids for cleaning. + ext_mngd_networks = list() # Store ids for cleaning. # Create external link ports in net0 - self.ext_link_ports = list() + ext_link_ports = list() # Create external subnet in net1 - self.ext_subnets = list() # Store ids for cleaning. + ext_subnets = list() # Store ids for cleaning. # Create external networks to change. - self.changed_ext_networks = list() - self.changed_ext_subnets = list() # Store ids for cleaning. + changed_ext_networks = list() + changed_ext_subnets = list() # Store ids for cleaning. - networks = self.neutronclient().list_networks() + networks = neutron_client.list_networks() for nw in networks.get('networks'): if nw['name'] == 'net0': - self.ext_networks.append(nw['id']) - self.ext_vl = _get_external_virtual_links(nw['id']) - self.ext_subnets.append(nw['subnets'][0]) - self.ext_link_ports.append(self._create_port(nw['id'])) - self.ext_link_ports.append(self._create_port(nw['id'])) + ext_networks.append(nw['id']) + ext_vl = _get_external_virtual_links(nw['id']) + ext_subnets.append(nw['subnets'][0]) + ext_link_ports.append(self._create_port(nw['id'], + neutron_client)) + ext_link_ports.append(self._create_port(nw['id'], + neutron_client)) elif nw['name'] == 'net1': - self.ext_mngd_networks.append(nw['id']) + ext_mngd_networks.append(nw['id']) # create new network. ext_net_id, ext_net_name = \ - self._create_network("external_net") - self.ext_networks.append(ext_net_id) + self._create_network("external_net", neutron_client) + ext_networks.append(ext_net_id) ext_mngd_net_id, _ = \ - self._create_network("external_managed_internal_net") - self.ext_mngd_networks.append(ext_mngd_net_id) + self._create_network("external_managed_internal_net", + neutron_client) + ext_mngd_networks.append(ext_mngd_net_id) changed_ext_net_id, changed_ext_net_name = \ - self._create_network("changed_external_net") - self.changed_ext_networks.append(changed_ext_net_id) + self._create_network("changed_external_net", neutron_client) + changed_ext_networks.append(changed_ext_net_id) # Chack how many networks are created. - networks = self.neutronclient().list_networks() + networks = neutron_client.list_networks() for nw in networks.get('networks'): if nw['name'] not in [ext_net_name, changed_ext_net_name]: continue elif nw['name'] == ext_net_name: - self.ext_subnets.append( + ext_subnets.append( self._create_subnet(nw, cidr="22.22.1.0/24", - gateway="22.22.1.1")) + gateway="22.22.1.1", + neutron_client=neutron_client)) elif nw['name'] == changed_ext_net_name: - self.changed_ext_subnets.append( + changed_ext_subnets.append( self._create_subnet(nw, cidr="22.22.2.0/24", - gateway="22.22.2.1")) + gateway="22.22.2.1", + neutron_client=neutron_client)) + return {'ext_networks': ext_networks, + 'ext_vl': ext_vl, + 'ext_mngd_networks': ext_mngd_networks, + 'ext_link_ports': ext_link_ports, + 'ext_subnets': ext_subnets, + 'changed_ext_networks': changed_ext_networks, + 'changed_ext_subnets': changed_ext_subnets} @classmethod - def _list_glance_image(cls, filter_name='cirros-0.5.2-x86_64-disk'): + def _list_glance_image(cls, filter_name='cirros-0.5.2-x86_64-disk', + glance_client=None): + if glance_client is None: + glance_client = cls.glance_client try: - images = cls.glance_client.images.list() + images = glance_client.images.list() except Exception: print("glance-image does not exists.", flush=True) return [] @@ -320,9 +360,11 @@ class BaseVnfLcmTest(base.BaseTackerTest): return list(filter(lambda image: image.name == filter_name, images)) @classmethod - def _get_glance_image(cls, image_id): + def _get_glance_image(cls, image_id, glance_client=None): + if glance_client is None: + glance_client = cls.glance_client try: - image = cls.glance_client.images.get(image_id) + image = glance_client.images.get(image_id) except Exception: print("glance-image does not exists.", image_id, flush=True) return None @@ -330,17 +372,23 @@ class BaseVnfLcmTest(base.BaseTackerTest): return image @classmethod - def _create_glance_image(cls, image_data, file_url): - image = cls.glance_client.images.create(**image_data) - cls.glance_client.images.upload(image.id, file_url) + def _create_glance_image(cls, image_data, file_url, glance_client=None): + if glance_client is None: + glance_client = cls.glance_client + image = glance_client.images.create(**image_data) + glance_client.images.upload(image.id, file_url) return image.id def _get_glance_image_list_from_stack_resource( - self, stack_id, stack_resource_name): + self, stack_id, stack_resource_name, h_client=None): + if h_client is None: + h_client = self.h_client image_id_list = [] for resource_name in stack_resource_name: - resource_details = self._get_heat_resource(stack_id, resource_name) + resource_details = self._get_heat_resource(stack_id, + resource_name, + h_client) image = self._get_image_id_from_resource_attributes( resource_details) if image: @@ -348,44 +396,56 @@ class BaseVnfLcmTest(base.BaseTackerTest): return image_id_list - def _register_subscription(self, request_body): - resp, response_body = self.http_client.do_request( + def _register_subscription(self, request_body, http_client=None): + if http_client is None: + http_client = self.http_client + resp, response_body = http_client.do_request( self.base_subscriptions_url, "POST", body=jsonutils.dumps(request_body)) return resp, response_body - def _delete_subscription(self, subscription_id): + def _delete_subscription(self, subscription_id, tacker_client=None): + if tacker_client is None: + tacker_client = self.tacker_client delete_url = os.path.join(self.base_subscriptions_url, subscription_id) - resp, body = self.tacker_client.do_request(delete_url, "DELETE") + resp, body = tacker_client.do_request(delete_url, "DELETE") return resp, body - def _show_subscription(self, subscription_id): + def _show_subscription(self, subscription_id, tacker_client=None): + if tacker_client is None: + tacker_client = self.tacker_client show_url = os.path.join(self.base_subscriptions_url, subscription_id) - resp, body = self.tacker_client.do_request(show_url, "GET") + resp, body = tacker_client.do_request(show_url, "GET") return resp, body - def _list_subscription(self): - resp, body = self.tacker_client.do_request( + def _list_subscription(self, tacker_client=None): + if tacker_client is None: + tacker_client = self.tacker_client + resp, body = tacker_client.do_request( self.base_subscriptions_url, "GET") return resp, body - def _list_subscription_filter(self, **kwargs): + def _list_subscription_filter(self, http_client=None, **kwargs): + if http_client is None: + http_client = self.http_client params = kwargs.get('params', {}) filter_variable = params['filter'] subscriptions_list_filter_url = "%s?%s" % ( self.base_subscriptions_url, filter_variable) - resp, subscription_body = self.http_client.do_request( + resp, subscription_body = http_client.do_request( subscriptions_list_filter_url, "GET") return resp, subscription_body def _create_vnf_instance(self, vnfd_id, vnf_instance_name=None, - vnf_instance_description=None): + vnf_instance_description=None, http_client=None): + if http_client is None: + http_client = self.http_client request_body = {'vnfdId': vnfd_id} if vnf_instance_name: request_body['vnfInstanceName'] = vnf_instance_name @@ -393,41 +453,52 @@ class BaseVnfLcmTest(base.BaseTackerTest): if vnf_instance_description: request_body['vnfInstanceDescription'] = vnf_instance_description - return self._create_vnf_instance_from_body(request_body) + return self._create_vnf_instance_from_body(request_body, http_client) - def _create_vnf_instance_from_body(self, request_body): + def _create_vnf_instance_from_body(self, request_body, http_client=None): + if http_client is None: + http_client = self.http_client request_body['vnfInstanceName'] = self._testMethodName - resp, response_body = self.http_client.do_request( + resp, response_body = http_client.do_request( self.base_vnf_instances_url, "POST", body=jsonutils.dumps(request_body)) return resp, response_body - def _delete_vnf_instance(self, id): + def _delete_vnf_instance(self, id, http_client=None): + if http_client is None: + http_client = self.http_client url = os.path.join(self.base_vnf_instances_url, id) - resp, body = self.http_client.do_request(url, "DELETE") + resp, body = http_client.do_request(url, "DELETE") return resp, body - def _show_vnf_instance(self, id): + def _show_vnf_instance(self, id, http_client=None): + if http_client is None: + http_client = self.http_client show_url = os.path.join(self.base_vnf_instances_url, id) - resp, vnf_instance = self.http_client.do_request(show_url, "GET") + resp, vnf_instance = http_client.do_request(show_url, "GET") return resp, vnf_instance - def _list_vnf_instance(self, **kwargs): - resp, vnf_instances = self.http_client.do_request( + def _list_vnf_instance(self, http_client=None, **kwargs): + if http_client is None: + http_client = self.http_client + resp, vnf_instances = http_client.do_request( self.base_vnf_instances_url, "GET", **kwargs) return resp, vnf_instances def _wait_vnf_instance(self, id, + http_client=None, instantiation_state=fields.VnfInstanceState.INSTANTIATED, timeout=VNF_INSTANTIATE_TIMEOUT): + if http_client is None: + http_client = self.http_client start_time = int(time.time()) while True: - resp, body = self._show_vnf_instance(id) + resp, body = self._show_vnf_instance(id, http_client) if body['instantiationState'] == instantiation_state: break @@ -440,97 +511,124 @@ class BaseVnfLcmTest(base.BaseTackerTest): time.sleep(5) - def _instantiate_vnf_instance(self, id, request_body): + def _instantiate_vnf_instance(self, id, request_body, http_client=None): + if http_client is None: + http_client = self.http_client url = os.path.join(self.base_vnf_instances_url, id, "instantiate") - resp, body = self.http_client.do_request(url, "POST", + resp, body = http_client.do_request(url, "POST", body=jsonutils.dumps(request_body)) return resp, body - def _heal_vnf_instance(self, vnf_instance_id, request_body): + def _heal_vnf_instance(self, vnf_instance_id, request_body, + http_client=None): + if http_client is None: + http_client = self.http_client url = os.path.join( self.base_vnf_instances_url, vnf_instance_id, "heal") - resp, body = self.http_client.do_request(url, "POST", + resp, body = http_client.do_request(url, "POST", body=jsonutils.dumps(request_body)) return resp, body - def _scale_vnf_instance(self, vnf_instance_id, request_body): + def _scale_vnf_instance(self, vnf_instance_id, request_body, + http_client=None): + if http_client is None: + http_client = self.http_client url = os.path.join( self.base_vnf_instances_url, vnf_instance_id, "scale") - resp, body = self.http_client.do_request(url, "POST", + resp, body = http_client.do_request(url, "POST", body=jsonutils.dumps(request_body)) return resp, body - def _terminate_vnf_instance(self, id, request_body): + def _terminate_vnf_instance(self, id, request_body, http_client=None): + if http_client is None: + http_client = self.http_client url = os.path.join(self.base_vnf_instances_url, id, "terminate") - resp, body = self.http_client.do_request(url, "POST", + resp, body = http_client.do_request(url, "POST", body=jsonutils.dumps(request_body)) return resp, body - def _update_vnf_instance(self, vnf_instance_id, request_body): + def _update_vnf_instance(self, vnf_instance_id, request_body, + http_client=None): + if http_client is None: + http_client = self.http_client url = os.path.join(self.base_vnf_instances_url, vnf_instance_id) - resp, body = self.http_client.do_request(url, "PATCH", + resp, body = http_client.do_request(url, "PATCH", body=jsonutils.dumps(request_body)) return resp, body - def _change_ext_conn_vnf_instance(self, vnf_instance_id, request_body): + def _change_ext_conn_vnf_instance(self, vnf_instance_id, request_body, + http_client=None): + if http_client is None: + http_client = self.http_client url = os.path.join( self.base_vnf_instances_url, vnf_instance_id, "change_ext_conn") - resp, body = self.http_client.do_request(url, "POST", + resp, body = http_client.do_request(url, "POST", body=jsonutils.dumps(request_body)) return resp, body - def _rollback_op_occs(self, vnf_lcm_op_occs_id): + def _rollback_op_occs(self, vnf_lcm_op_occs_id, http_client=None): + if http_client is None: + http_client = self.http_client rollback_url = os.path.join( self.base_vnf_lcm_op_occs_url, vnf_lcm_op_occs_id, 'rollback') - resp, response_body = self.http_client.do_request( + resp, response_body = http_client.do_request( rollback_url, "POST") return resp, response_body - def _fail_op_occs(self, vnf_lcm_op_occs_id): + def _fail_op_occs(self, vnf_lcm_op_occs_id, http_client=None): + if http_client is None: + http_client = self.http_client fail_url = os.path.join( self.base_vnf_lcm_op_occs_url, vnf_lcm_op_occs_id, 'fail') - resp, response_body = self.http_client.do_request( + resp, response_body = http_client.do_request( fail_url, "POST") return resp, response_body - def _retry_op_occs(self, vnf_lcm_op_occs_id): + def _retry_op_occs(self, vnf_lcm_op_occs_id, http_client=None): + if http_client is None: + http_client = self.http_client retry_url = os.path.join( self.base_vnf_lcm_op_occs_url, vnf_lcm_op_occs_id, 'retry') - resp, response_body = self.http_client.do_request( + resp, response_body = http_client.do_request( retry_url, "POST") return resp, response_body - def _show_op_occs(self, vnf_lcm_op_occs_id): + def _show_op_occs(self, vnf_lcm_op_occs_id, http_client=None): + if http_client is None: + http_client = self.http_client show_url = os.path.join( self.base_vnf_lcm_op_occs_url, vnf_lcm_op_occs_id) - resp, response_body = self.http_client.do_request( + resp, response_body = http_client.do_request( show_url, "GET") return resp, response_body - def _wait_terminate_vnf_instance(self, id, timeout=None): + def _wait_terminate_vnf_instance(self, id, timeout=None, http_client=None): + if http_client is None: + http_client = self.http_client start_time = int(time.time()) self._wait_vnf_instance(id, + http_client, instantiation_state=fields.VnfInstanceState.NOT_INSTANTIATED, timeout=timeout) @@ -546,9 +644,12 @@ class BaseVnfLcmTest(base.BaseTackerTest): # wait for status completion time.sleep(VNF_DELETE_COMPLETION_WAIT) - def _get_heat_stack(self, vnf_instance_id, prefix_id='vnflcm_'): + def _get_heat_stack(self, vnf_instance_id, h_client=None, + prefix_id='vnflcm_'): + if h_client is None: + h_client = self.h_client try: - stacks = self.h_client.stacks.list() + stacks = h_client.stacks.list() except Exception: return None @@ -563,17 +664,22 @@ class BaseVnfLcmTest(base.BaseTackerTest): return target_stakcs[0] - def _delete_heat_stack(self, stack_id): - self.h_client.stacks.delete(stack_id) + def _delete_heat_stack(self, stack_id, h_client=None): + if h_client is None: + h_client = self.h_client + h_client.stacks.delete(stack_id) - def _wait_until_stack_ready(self, stack_id, expected_status): + def _wait_until_stack_ready(self, stack_id, expected_status, + h_client=None): + if h_client is None: + h_client = self.h_client start_time = time.time() callback_url = os.path.join( MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) while True: - stack = self.h_client.stacks.get(stack_id) + stack = h_client.stacks.get(stack_id) actual_status = stack.stack_status print( ("Wait:callback_url=<%s>, " + @@ -601,27 +707,35 @@ class BaseVnfLcmTest(base.BaseTackerTest): time.sleep(RETRY_WAIT_TIME) - def _get_heat_resource_list(self, stack_id, nested_depth=0): + def _get_heat_resource_list(self, stack_id, nested_depth=0, + h_client=None): + if h_client is None: + h_client = self.h_client try: - resources = self.h_client.resources.list( + resources = h_client.resources.list( stack_id, nested_depth=nested_depth) except Exception: return None return resources - def _get_heat_resource(self, stack_id, resource_name): + def _get_heat_resource(self, stack_id, resource_name, h_client=None): + if h_client is None: + h_client = self.h_client try: - resource = self.h_client.resources.get( + resource = h_client.resources.get( stack_id, resource_name) except Exception: return None return resource - def _get_heat_stack_template(self, stack_id, nested_depth=0): + def _get_heat_stack_template(self, stack_id, nested_depth=0, + h_client=None): + if h_client is None: + h_client = self.h_client try: - template = self.h_client.stacks.template(stack_id) + template = h_client.stacks.template(stack_id) except Exception: return None @@ -640,9 +754,12 @@ class BaseVnfLcmTest(base.BaseTackerTest): stack_id, resource_type='OS::Nova::Server', nested_depth=2, - limit=2): + limit=2, + h_client=None): + if h_client is None: + h_client = self.h_client resources = self._get_heat_resource_list( - stack_id, nested_depth=nested_depth) + stack_id, nested_depth=nested_depth, h_client=h_client) if resources is None: return None @@ -715,8 +832,11 @@ class BaseVnfLcmTest(base.BaseTackerTest): def assert_heat_stack_status( self, vnf_instance_id, + h_client=None, expected_stack_status=infra_cnst.STACK_CREATE_COMPLETE): - stack = self._get_heat_stack(vnf_instance_id) + if h_client is None: + h_client = self.h_client + stack = self._get_heat_stack(vnf_instance_id, h_client) self.assertEqual( expected_stack_status, stack.stack_status) @@ -724,18 +844,25 @@ class BaseVnfLcmTest(base.BaseTackerTest): def assert_heat_resource_status( self, vnf_instance, + h_client=None, expected_glance_image=None, expected_resource_status=None): - def assert_glance_image(stack_id, resource_name): - resource_details = self._get_heat_resource(stack_id, resource_name) + if h_client is None: + h_client = self.h_client + + def assert_glance_image(stack_id, resource_name, h_client): + resource_details = self._get_heat_resource(stack_id, + resource_name, + h_client) image = self._get_image_id_from_resource_attributes( resource_details) if image: self.assertEqual(expected_glance_image, image.status) - stack = self._get_heat_stack(vnf_instance['id']) - resources = self._get_heat_resource_list(stack.id, 2) + stack = self._get_heat_stack(vnf_instance['id'], h_client) + resources = self._get_heat_resource_list(stack.id, + nested_depth=2, h_client=h_client) self.assertIsNotNone(resources) for resource in resources: @@ -745,26 +872,39 @@ class BaseVnfLcmTest(base.BaseTackerTest): # FT-checkpoint: Glance-image if expected_glance_image: - assert_glance_image(stack.id, resource.resource_name) + assert_glance_image(stack.id, resource.resource_name, + h_client) def assert_heat_resource_status_is_none( self, stack_id, + h_client=None, + glance_client=None, resources_name_list=None, glance_image_id_list=None): + if h_client is None: + h_client = self.h_client + if glance_client is None: + glance_client = self.glance_client resources_name_list = resources_name_list or [] for resource_name in resources_name_list: - resource = self._get_heat_resource(stack_id, resource_name) + resource = self._get_heat_resource(stack_id, + resource_name, + h_client) self.assertIsNone(resource) glance_image_id_list = glance_image_id_list or [] for glance_image_id in glance_image_id_list: - image = self._get_glance_image(glance_image_id) + image = self._get_glance_image(glance_image_id, + glance_client) self.assertIsNone(image) def _wait_lcm_done(self, expected_operation_status=None, - vnf_instance_id=None): + vnf_instance_id=None, + fake_server_manager=None): + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER start_time = int(time.time()) callback_url = os.path.join( MOCK_NOTIFY_CALLBACK_URL, @@ -773,7 +913,7 @@ class BaseVnfLcmTest(base.BaseTackerTest): while True: actual_status = None vnf_lcm_op_occ_id = None - notify_mock_responses = FAKE_SERVER_MANAGER.get_history( + notify_mock_responses = fake_server_manager.get_history( callback_url) print( ("Wait:callback_url=<%s>, " + @@ -810,11 +950,14 @@ class BaseVnfLcmTest(base.BaseTackerTest): time.sleep(RETRY_WAIT_TIME) - def _wait_stack_update(self, vnf_instance_id, expected_status): + def _wait_stack_update(self, vnf_instance_id, expected_status, + h_client=None): + if h_client is None: + h_client = self.h_client timeout = VNF_HEAL_TIMEOUT start_time = int(time.time()) while True: - stack = self._get_heat_stack(vnf_instance_id) + stack = self._get_heat_stack(vnf_instance_id, h_client) if stack.stack_status == expected_status: break @@ -827,7 +970,10 @@ class BaseVnfLcmTest(base.BaseTackerTest): time.sleep(RETRY_WAIT_TIME) - def assert_create_vnf(self, resp, vnf_instance): + def assert_create_vnf(self, resp, vnf_instance, + fake_server_manager=None): + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER self.assertEqual(201, resp.status_code) self.assert_http_header_location_for_create(resp.headers) @@ -840,17 +986,24 @@ class BaseVnfLcmTest(base.BaseTackerTest): MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) notify_mock_responses = self._filter_notify_history(callback_url, - vnf_instance.get('id')) + vnf_instance.get('id'), + fake_server_manager=fake_server_manager) self.assertEqual(1, len(notify_mock_responses)) self.assert_notification_mock_response( notify_mock_responses[0], 'VnfIdentifierCreationNotification') - def assert_delete_vnf(self, resp, vnf_instance_id): + def assert_delete_vnf(self, resp, vnf_instance_id, http_client=None, + fake_server_manager=None): self.assertEqual(204, resp.status_code) - resp, _ = self._show_vnf_instance(vnf_instance_id) + if http_client is None: + http_client = self.http_client + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER + + resp, _ = self._show_vnf_instance(vnf_instance_id, http_client) self.assertEqual(404, resp.status_code) # FT-checkpoint: Notification @@ -858,7 +1011,7 @@ class BaseVnfLcmTest(base.BaseTackerTest): MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) notify_mock_responses = self._filter_notify_history(callback_url, - vnf_instance_id) + vnf_instance_id, fake_server_manager=fake_server_manager) self.assertEqual(1, len(notify_mock_responses)) self.assert_notification_mock_response( @@ -868,14 +1021,26 @@ class BaseVnfLcmTest(base.BaseTackerTest): def assert_instantiate_vnf( self, resp, - vnf_instance_id): + vnf_instance_id, + http_client=None, + h_client=None, + fake_server_manager=None): + if http_client is None: + http_client = self.http_client + if h_client is None: + h_client = self.h_client + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER + self.assertEqual(202, resp.status_code) - resp, vnf_instance = self._show_vnf_instance(vnf_instance_id) + resp, vnf_instance = self._show_vnf_instance(vnf_instance_id, + http_client) self.assert_vnf_state(vnf_instance) - self.assert_heat_stack_status(vnf_instance['id']) + self.assert_heat_stack_status(vnf_instance['id'], h_client) self.assert_heat_resource_status( vnf_instance, + h_client, expected_glance_image='active', expected_resource_status='CREATE_COMPLETE') @@ -884,7 +1049,7 @@ class BaseVnfLcmTest(base.BaseTackerTest): MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) notify_mock_responses = self._filter_notify_history(callback_url, - vnf_instance_id) + vnf_instance_id, fake_server_manager=fake_server_manager) self.assertEqual(3, len(notify_mock_responses)) self.assert_notification_mock_response( @@ -906,15 +1071,27 @@ class BaseVnfLcmTest(base.BaseTackerTest): self, resp, vnf_instance_id, - expected_stack_status='UPDATE_COMPLETE'): + expected_stack_status='UPDATE_COMPLETE', + http_client=None, + h_client=None, + fake_server_manager=None): + if http_client is None: + http_client = self.http_client + if h_client is None: + h_client = self.h_client + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER + self.assertEqual(202, resp.status_code) - resp, vnf_instance = self._show_vnf_instance(vnf_instance_id) + resp, vnf_instance = self._show_vnf_instance(vnf_instance_id, + http_client) self.assert_vnf_state(vnf_instance) self.assert_instantiation_state(vnf_instance) self.assert_heat_stack_status( vnf_instance['id'], + h_client, expected_stack_status=expected_stack_status) # FT-checkpoint: Notification @@ -922,7 +1099,7 @@ class BaseVnfLcmTest(base.BaseTackerTest): MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) notify_mock_responses = self._filter_notify_history(callback_url, - vnf_instance_id) + vnf_instance_id, fake_server_manager=fake_server_manager) self.assertEqual(3, len(notify_mock_responses)) self.assert_notification_mock_response( @@ -946,20 +1123,36 @@ class BaseVnfLcmTest(base.BaseTackerTest): vnf_instance_id, stack_id, resource_name_list, - glance_image_id_list): + glance_image_id_list, + http_client=None, + h_client=None, + glance_client=None, + fake_server_manager=None): + if http_client is None: + http_client = self.http_client + if h_client is None: + h_client = self.h_client + if glance_client is None: + glance_client = self.glance_client + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER + self.assertEqual(202, resp.status_code) - resp, vnf_instance = self._show_vnf_instance(vnf_instance_id) + resp, vnf_instance = self._show_vnf_instance(vnf_instance_id, + http_client) self.assert_instantiation_state( vnf_instance, fields.VnfInstanceState.NOT_INSTANTIATED) # FT-checkpoint: Heat stack status. - stack = self._get_heat_stack(vnf_instance_id) + stack = self._get_heat_stack(vnf_instance_id, h_client) self.assertIsNone(stack) self.assert_heat_resource_status_is_none( stack_id, + h_client, + glance_client, resources_name_list=resource_name_list, glance_image_id_list=glance_image_id_list) @@ -968,7 +1161,7 @@ class BaseVnfLcmTest(base.BaseTackerTest): MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) notify_mock_responses = self._filter_notify_history(callback_url, - vnf_instance_id) + vnf_instance_id, fake_server_manager=fake_server_manager) self.assertEqual(3, len(notify_mock_responses)) self.assert_notification_mock_response( @@ -993,11 +1186,21 @@ class BaseVnfLcmTest(base.BaseTackerTest): pre_stack_resource_list, post_stack_resource_list, scale_type='SCALE_OUT', - expected_stack_status='CREATE_COMPLETE'): + expected_stack_status='CREATE_COMPLETE', + http_client=None, + h_client=None, + fake_server_manager=None): + if http_client is None: + http_client = self.http_client + if h_client is None: + h_client = self.h_client + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER self.assertEqual(202, resp.status_code) self.assert_http_header_location_for_lcm_op_occs(resp.headers) - resp, vnf_instance = self._show_vnf_instance(vnf_instance_id) + resp, vnf_instance = self._show_vnf_instance(vnf_instance_id, + http_client) self.assert_vnf_state(vnf_instance) self.assert_instantiation_state(vnf_instance) @@ -1018,6 +1221,7 @@ class BaseVnfLcmTest(base.BaseTackerTest): self.assert_heat_stack_status( vnf_instance['id'], + h_client, expected_stack_status=expected_stack_status) # FT-checkpoint: Notification @@ -1025,7 +1229,7 @@ class BaseVnfLcmTest(base.BaseTackerTest): MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) notify_mock_responses = self._filter_notify_history(callback_url, - vnf_instance_id) + vnf_instance_id, fake_server_manager=fake_server_manager) self.assertEqual(3, len(notify_mock_responses)) self.assert_notification_mock_response( @@ -1043,15 +1247,18 @@ class BaseVnfLcmTest(base.BaseTackerTest): 'VnfLcmOperationOccurrenceNotification', 'COMPLETED') - def assert_rollback_vnf(self, resp, vnf_instance_id): + def assert_rollback_vnf(self, resp, vnf_instance_id, + fake_server_manager=None): self.assertEqual(202, resp.status_code) + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER # FT-checkpoint: Notification callback_url = os.path.join( MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) notify_mock_responses = self._filter_notify_history(callback_url, - vnf_instance_id) + vnf_instance_id, fake_server_manager=fake_server_manager) self.assertEqual(2, len(notify_mock_responses)) self.assert_notification_mock_response( @@ -1064,15 +1271,18 @@ class BaseVnfLcmTest(base.BaseTackerTest): 'VnfLcmOperationOccurrenceNotification', 'ROLLED_BACK') - def assert_fail_vnf(self, resp, vnf_instance_id): + def assert_fail_vnf(self, resp, vnf_instance_id, + fake_server_manager=None): self.assertEqual(200, resp.status_code) + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER # FT-checkpoint: Notification callback_url = os.path.join( MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) notify_mock_responses = self._filter_notify_history(callback_url, - vnf_instance_id) + vnf_instance_id, fake_server_manager=fake_server_manager) self.assertEqual(1, len(notify_mock_responses)) self.assert_notification_mock_response( @@ -1080,15 +1290,18 @@ class BaseVnfLcmTest(base.BaseTackerTest): 'VnfLcmOperationOccurrenceNotification', 'FAILED') - def assert_retry_vnf(self, resp, vnf_instance_id): + def assert_retry_vnf(self, resp, vnf_instance_id, + fake_server_manager=None): self.assertEqual(202, resp.status_code) + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER # FT-checkpoint: Notification callback_url = os.path.join( MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) notify_mock_responses = self._filter_notify_history(callback_url, - vnf_instance_id) + vnf_instance_id, fake_server_manager=fake_server_manager) self.assertEqual(2, len(notify_mock_responses)) self.assert_notification_mock_response( @@ -1105,11 +1318,21 @@ class BaseVnfLcmTest(base.BaseTackerTest): self, resp, vnf_instance_id, - expected_stack_status='CREATE_COMPLETE'): + expected_stack_status='CREATE_COMPLETE', + http_client=None, + h_client=None, + fake_server_manager=None): + if http_client is None: + http_client = self.http_client + if h_client is None: + h_client = self.h_client + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER self.assertEqual(202, resp.status_code) self.assert_http_header_location_for_lcm_op_occs(resp.headers) - resp, vnf_instance = self._show_vnf_instance(vnf_instance_id) + resp, vnf_instance = self._show_vnf_instance(vnf_instance_id, + http_client) self.assertEqual(200, resp.status_code) self.assert_vnf_state(vnf_instance) @@ -1117,6 +1340,7 @@ class BaseVnfLcmTest(base.BaseTackerTest): self.assert_heat_stack_status( vnf_instance['id'], + h_client, expected_stack_status=expected_stack_status) # FT-checkpoint: Notification @@ -1124,7 +1348,7 @@ class BaseVnfLcmTest(base.BaseTackerTest): MOCK_NOTIFY_CALLBACK_URL, self._testMethodName) notify_mock_responses = self._filter_notify_history(callback_url, - vnf_instance_id) + vnf_instance_id, fake_server_manager=fake_server_manager) self.assertEqual(2, len(notify_mock_responses)) self.assert_notification_mock_response( @@ -1137,10 +1361,13 @@ class BaseVnfLcmTest(base.BaseTackerTest): 'VnfLcmOperationOccurrenceNotification', 'COMPLETED') - def assert_notification_get(self, callback_url): - notify_mock_responses = FAKE_SERVER_MANAGER.get_history( + def assert_notification_get(self, callback_url, + fake_server_manager=None): + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER + notify_mock_responses = fake_server_manager.get_history( callback_url) - FAKE_SERVER_MANAGER.clear_history( + fake_server_manager.clear_history( callback_url) self.assertEqual(1, len(notify_mock_responses)) self.assertEqual(204, notify_mock_responses[0].status_code) @@ -1161,10 +1388,13 @@ class BaseVnfLcmTest(base.BaseTackerTest): expected_operation_status, notify_mock_response.request_body['operationState']) - def _create_network(self, name): + def _create_network(self, name, neutron_client=None): # First, we have to check network name passed by caller is # already exists or not. # OK, we can create this. + + if neutron_client is None: + neutron_client = self.neutronclient() try: uniq_name = name + '-' + uuidutils.generate_uuid() net = \ @@ -1178,7 +1408,9 @@ class BaseVnfLcmTest(base.BaseTackerTest): self.fail("Failed, create network=<%s>, %s" % (uniq_name, e)) - def _create_subnet(self, network, cidr, gateway): + def _create_subnet(self, network, cidr, gateway, neutron_client=None): + if neutron_client is None: + neutron_client = self.neutronclient() body = {'subnet': {'network_id': network['id'], 'name': "subnet-%s" % uuidutils.generate_uuid(), 'cidr': "{}".format(cidr), @@ -1187,39 +1419,47 @@ class BaseVnfLcmTest(base.BaseTackerTest): "enable_dhcp": True}} try: - subnet = self.neutronclient().create_subnet(body=body)["subnet"] - self.addCleanup(self._delete_subnet, subnet['id']) + subnet = neutron_client.create_subnet(body=body)["subnet"] + self.addCleanup(self._delete_subnet, subnet['id'], neutron_client) print("Create subnet success, %s" % subnet['id'], flush=True) return subnet['id'] except Exception as e: self.fail("Failed, create subnet for net_id=<%s>, %s" % (network['id'], e)) - def _create_port(self, network_id): + def _create_port(self, network_id, neutron_client=None): + if neutron_client is None: + neutron_client = self.neutronclient() body = {'port': {'network_id': network_id}} try: - port = self.neutronclient().create_port(body=body)["port"] - self.addCleanup(self._delete_port, port['id']) + port = neutron_client.create_port(body=body)["port"] + self.addCleanup(self._delete_port, port['id'], neutron_client) return port['id'] except Exception as e: self.fail("Failed, create port for net_id=<%s>, %s" % (network_id, e)) - def _delete_network(self, network_id): + def _delete_network(self, network_id, neutron_client=None): + if neutron_client is None: + neutron_client = self.neutronclient() try: - self.neutronclient().delete_network(network_id) + neutron_client.delete_network(network_id) except Exception: print("Failed, delete network.", network_id, flush=True) - def _delete_subnet(self, subnet_id): + def _delete_subnet(self, subnet_id, neutron_client=None): + if neutron_client is None: + neutron_client = self.neutronclient() try: - self.neutronclient().delete_subnet(subnet_id) + neutron_client.delete_subnet(subnet_id) except Exception: print("Failed, delete subnet.", subnet_id, flush=True) - def _delete_port(self, port_id): + def _delete_port(self, port_id, neutron_client): + if neutron_client is None: + neutron_client = self.neutronclient() try: - self.neutronclient().delete_port(port_id) + neutron_client.delete_port(port_id) except Exception: print("Failed, delete port.", port_id, flush=True) @@ -1238,20 +1478,26 @@ class BaseVnfLcmTest(base.BaseTackerTest): self.assertIsNotNone(_links.get('self')) self.assertIsNotNone(_links.get('self').get('href')) - def _filter_notify_history(self, callback_url, vnf_instance_id): - notify_histories = FAKE_SERVER_MANAGER.get_history( + def _filter_notify_history(self, callback_url, vnf_instance_id, + fake_server_manager=None): + if fake_server_manager is None: + fake_server_manager = FAKE_SERVER_MANAGER + notify_histories = fake_server_manager.get_history( callback_url) - FAKE_SERVER_MANAGER.clear_history(callback_url) + fake_server_manager.clear_history(callback_url) return [ h for h in notify_histories if h.request_body.get('vnfInstanceId') == vnf_instance_id] - def _get_heat_stack_show(self, vnf_instance_id, resource_name): + def _get_heat_stack_show(self, vnf_instance_id, resource_name, + h_client=None): """Retrieve image name of the resource from stack""" + if h_client is None: + h_client = self.h_client try: - stack = self._get_heat_stack(vnf_instance_id) - stack_info = self.h_client.stacks.get(stack.id) + stack = self._get_heat_stack(vnf_instance_id, h_client) + stack_info = h_client.stacks.get(stack.id) stack_dict = stack_info.to_dict() resource_dict = json.loads(stack_dict['parameters']['nfv']) except Exception: