def test_l2l3dom_sriovvm_delete(self): # skip - TODO(KRIS) to move to separate job eventually self.skipTest('Can\'t do SRIOV vm\'s in CI, skipping test.') port_name = 'sxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-iov-02-20' port = self.sriov_port_create(self.network, port_name) # vm = self._create_server_sriov_port(port,name="sriov-vm2") vmkwargs = { 'name': "sriov-vm2", 'flavorRef': '2', 'imageRef': CONF.compute.image_ref, 'networks': [{ 'port': port['id'] }] } vm = self.os_admin.servers_client.create_server(**vmkwargs) time.sleep(5) # find bridgeport created on the dummy network bridge_port = self.nuage_client.get_l2domain_vports( self.vsd_dummy_network[0]['ID']) # find the gateway port id of the vlan associated vlan_obj = self.nuage_client.get_bridge_port_gateway_vlan( bridge_port[0]) gateway_port_id = vlan_obj[0]['parentID'] # get all the vlans on the gateway port. # should have all the vlans in the port name vlans_in_port = self.nuage_client.get_gateway_vlan( "ports", gateway_port_id) vlan_list = [] for vlan_id in vlans_in_port: vlan_list.append(vlan_id['value']) orig_vlan_list = [0, 22] for id in orig_vlan_list: if id not in vlan_list: raise exceptions.NotFound("vlan " + str(id) + " not in found vlans") # delete vm self.os_admin.servers_client.delete_server(vm['server']['id']) time.sleep(2) # check vlans are deleted vlans_del_vm_port = self.nuage_client.get_gateway_vlan( "ports", gateway_port_id) vlan_list = [] for vlan_id in vlans_del_vm_port: vlan_list.append(vlan_id['value']) orig_vlan_list = [0, 22] for id in orig_vlan_list: if id in vlan_list: raise exceptions.NotFound("vlan " + str(id) + " not in found vlans")
def create_subnet(cls, network, gateway='', cidr=None, mask_bits=None, ip_version=None, client=None, **kwargs): subnet = super(base.BaseAdminNetworkTest, cls).create_subnet(network, gateway, cidr, mask_bits, ip_version, client, **kwargs) dhcp_enabled = subnet['enable_dhcp'] current_time = time.time() if cls.is_dhcp_agent_present() and dhcp_enabled: LOG.info("Waiting for dhcp port resolution") dhcp_subnets = [] while subnet['id'] not in dhcp_subnets: if time.time() - current_time > 30: raise lib_exc.NotFound("DHCP port not resolved within" " allocated time.") time.sleep(0.5) filters = { 'device_owner': 'network:dhcp', 'network_id': subnet['network_id'] } dhcp_ports = cls.ports_client.list_ports(**filters)['ports'] if not dhcp_ports: time.sleep(0.5) continue dhcp_port = dhcp_ports[0] dhcp_subnets = [x['subnet_id'] for x in dhcp_port['fixed_ips']] LOG.info("DHCP port resolved") return subnet
def delete_a_receiver(client, receiver_id, ignore_missing=False): """Utility function that deletes a Senlin receiver.""" res = client.delete_obj('receivers', receiver_id) if res['status'] == 404: if ignore_missing: return raise exceptions.NotFound()
def test_is_resource_deleted_true(self): self.useFixture( mockpatch.Patch( 'tempest.lib.services.compute.floating_ips_client.' 'FloatingIPsClient.show_floating_ip', side_effect=lib_exc.NotFound())) self.assertTrue(self.client.is_resource_deleted('fake-id'))
def wrapper(*args, **kwargs): if args and isinstance(args[0], test.BaseTestCase): test_obj = args[0] else: raise rbac_exceptions.RbacResourceSetupFailed( '`rbac_rule_validation` decorator can only be applied to ' 'an instance of `tempest.test.BaseTestCase`.') allowed = _is_authorized(test_obj, service, rule, extra_target_data, admin_only) expected_exception, irregular_msg = _get_exception_type( expected_error_code) test_status = 'Allowed' try: test_func(*args, **kwargs) except rbac_exceptions.RbacInvalidService as e: msg = ("%s is not a valid service." % service) test_status = ('Error, %s' % (msg)) LOG.error(msg) raise exceptions.NotFound("%s RbacInvalidService was: %s" % (msg, e)) except (expected_exception, rbac_exceptions.RbacConflictingPolicies, rbac_exceptions.RbacMalformedResponse) as e: test_status = 'Denied' if irregular_msg: LOG.warning(irregular_msg.format(rule, service)) if allowed: msg = ("Role %s was not allowed to perform %s." % (role, rule)) LOG.error(msg) raise exceptions.Forbidden("%s Exception was: %s" % (msg, e)) except Exception as e: with excutils.save_and_reraise_exception(): exc_info = sys.exc_info() error_details = six.text_type(exc_info[1]) msg = ("An unexpected exception has occurred during test: " "%s. Exception was: %s" % (test_func.__name__, error_details)) test_status = 'Error, %s' % (error_details) LOG.error(msg) else: if not allowed: LOG.error("Role %s was allowed to perform %s", role, rule) raise rbac_exceptions.RbacOverPermission( "OverPermission: Role %s was allowed to perform %s" % (role, rule)) finally: test_obj.rbac_utils.switch_role(test_obj, toggle_rbac_role=False) if CONF.patrole_log.enable_reporting: RBACLOG.info( "[Service]: %s, [Test]: %s, [Rule]: %s, " "[Expected]: %s, [Actual]: %s", service, test_func.__name__, rule, "Allowed" if allowed else "Denied", test_status)
def get_project_by_name(client, project_name): projects = client.list_projects({'name': project_name})['projects'] for project in projects: if project['name'] == project_name: return project raise lib_exc.NotFound('No such project(%s) in %s' % (project_name, projects))
def get_plugin(cls): plugins = cls.client.plugins.list() plugin_name = plugin_utils.get_default_plugin() for plugin in plugins: if plugin.name == plugin_name: return plugin raise exceptions.NotFound('No available plugins for testing')
def get_image_id(self, image_name): if uuidutils.is_uuid_like(image_name): return image_name for image in self.glance_client.images.list(): if image.name == image_name: return image.id raise exc.NotFound(image_name)
def test_clear_validation_resources_wait_not_found_delete(self): # Test that a not found on delete is not an exception self.mock_kp.mock.side_effect = lib_exc.NotFound('yay') self.mock_sg_network.mock.side_effect = lib_exc.NotFound('yay') self.mock_fip_network.mock.side_effect = lib_exc.NotFound('yay') vr.clear_validation_resources( self.os, floating_ip=FAKE_FIP_NEUTRON['floatingip'], security_group=FAKE_SECURITY_GROUP['security_group'], keypair=FAKE_KEYPAIR['keypair'], use_neutron=True) # Clients calls are still made, but not the wait call self.assertGreater(self.mock_kp.mock.call_count, 0) self.assertGreater(self.mock_sg_network.mock.call_count, 0) self.assertEqual(self.mock_sg_wait_network.mock.call_count, 0) self.assertGreater(self.mock_fip_network.mock.call_count, 0)
def get_security_group_id(self, sg_name): for sg in (self.neutron_client.list_security_groups() ["security_groups"]): if sg['name'] == sg_name: return sg['id'] raise exc.NotFound(sg_name)
def delete_a_profile(base, profile_id, ignore_missing=False): """Utility function that deletes a Senlin profile.""" res = base.client.delete_obj('profiles', profile_id) if res['status'] == 404: if ignore_missing: return raise exceptions.NotFound()
def test_vlan_unaware_l3vm(self): # skip - TODO(KRIS) to move to separate job eventually self.skipTest('Can\'t do SRIOV vm\'s in CI, skipping test.') port_name = "vlanunaware-port1-vlan34" port = self.sriov_port_create(self.network_34, port_name) self._create_server_sriov_port(port, name="vlanunaware-sriov-vm2") time.sleep(2) bridge_port = self.nuage_client.get_l3_subnet_vports( self.vsd_subnet_34[0]['ID']) vlan_obj = self.nuage_client.get_bridge_port_gateway_vlan( bridge_port[0]) gateway_port_id = vlan_obj[0]['parentID'] # get all the vlans on the gateway port. # should have all the vlans in the port name vlans_in_port = self.nuage_client.get_gateway_vlan( "ports", gateway_port_id) vlan_list = [] for vlan_id in vlans_in_port: vlan_list.append(vlan_id['value']) orig_vlan_list = [34] for id in orig_vlan_list: if id not in vlan_list: raise exceptions.NotFound("vlan " + str(id) + " not in found vlans")
def test_l2l3dom_sriovvm_mixvlan(self): # skip - TODO(KRIS) to move to separate job eventually self.skipTest('Can\'t do SRIOV vm\'s in CI, skipping test.') port_name = 'sxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-iov-02-20-33..35' port = self.sriov_port_create(self.network, port_name) self._create_server_sriov_port(port, name="sriov-vm1") time.sleep(5) # find bridgeport created on the dummy network bridge_port = self.nuage_client.get_l2domain_vports( self.vsd_dummy_network[0]['ID']) # find the gateway port id of the vlan associated vlan_obj = self.nuage_client.get_bridge_port_gateway_vlan( bridge_port[0]) gateway_port_id = vlan_obj[0]['parentID'] # get all the vlans on the gateway port. # should have all the vlans in the port name vlans_in_port = self.nuage_client.get_gateway_vlan( "ports", gateway_port_id) vlan_list = [] for vlan_id in vlans_in_port: vlan_list.append(vlan_id['value']) orig_vlan_list = [0, 20, 33, 34, 35] for id in orig_vlan_list: if id not in vlan_list: raise exceptions.NotFound("vlan " + str(id) + " not in found vlans")
def delete_a_policy(base, policy_id, ignore_missing=False): """Utility function that deletes a policy.""" res = base.client.delete_obj('policies', policy_id) if res['status'] == 404: if ignore_missing: return raise exceptions.NotFound() return
def get_network_id(self, network_name): if uuidutils.is_uuid_like(network_name): return network_name networks = self.neutron_client.list_networks(name=network_name) networks = networks['networks'] if len(networks) < 1: raise exc.NotFound(network_name) return networks[0]['id']
def get_private_network_id(cls): net_id = cls._find_network_by_name( TEMPEST_CONF.compute.fixed_network_name) if not net_id: raise exceptions.NotFound( 'Private network \'%s\' not found in network list.' % TEMPEST_CONF.compute.fixed_network_name) return net_id
def delete_a_subnet(base, subnet_id, ignore_missing=False): """Utility function that deletes a Neutron subnet.""" res = base.network_client.delete_obj('subnets', subnet_id) if res['status'] == 404: if ignore_missing is True: return raise exceptions.NotFound()
def fetch_aggregate_by_name(self, name): aggregates = self.aggr_client.list_aggregates()['aggregates'] try: aggr = [aggr for aggr in aggregates if aggr['name'] == name][0] except IndexError: err_msg = "aggregate with name %s doesn't exist." % name raise exceptions.NotFound(err_msg) return aggr
def get_floating_ip_pool_id_for_neutron(cls): net_id = cls._find_network_by_name( TEMPEST_CONF.network.floating_network_name) if not net_id: raise exceptions.NotFound( 'Floating IP pool \'%s\' not found in pool list.' % TEMPEST_CONF.network.floating_network_name) return net_id
def fetch_aggregate_by_name(self, name): aggregates = self.aggr_client.list_aggregates()['aggregates'] try: aggr = next( iter(filter(lambda aggr: aggr['name'] == name, aggregates))) except StopIteration: err_msg = "aggregate with name %s doesn't exist." % name raise exceptions.NotFound(err_msg) return aggr
def _create_subnet(cls, network, gateway='', cidr=None, mask_bits=None, ip_version=None, client=None, **kwargs): """ Copy of tempest/api/network/base.py_create_subnet where we allow NOT passing gateway_ip (!= not passing as parameter and being calculated by create_subnet) """ # allow tests to use admin client if not client: client = cls.subnets_client # The cidr and mask_bits depend on the ip version. ip_version = ip_version if ip_version is not None else cls._ip_version gateway_not_set = gateway == '' if ip_version == 4: cidr = cidr or IPNetwork(CONF.network.tenant_network_cidr) mask_bits = mask_bits or CONF.network.tenant_network_mask_bits elif ip_version == 6: cidr = (cidr or IPNetwork(CONF.network.tenant_network_v6_cidr)) mask_bits = mask_bits or CONF.network.tenant_network_v6_mask_bits # Find a cidr that is not in use yet and create a subnet with it for subnet_cidr in cidr.subnet(mask_bits): if gateway_not_set: gateway_ip = str(IPAddress(subnet_cidr) + 1) else: gateway_ip = gateway try: if gateway_not_set: body = client.create_subnet( network_id=network['id'], cidr=str(subnet_cidr), ip_version=ip_version, # gateway_ip=not passed, **kwargs) break else: body = client.create_subnet(network_id=network['id'], cidr=str(subnet_cidr), ip_version=ip_version, gateway_ip=gateway_ip, **kwargs) break except exceptions.BadRequest as e: is_overlapping_cidr = 'overlaps with another subnet' in str(e) if not is_overlapping_cidr: raise else: message = 'Available CIDR for subnet creation could not be found' raise exceptions.NotFound(message) # raise exceptions.BuildErrorException(message) subnet = body['subnet'] cls.subnets.append(subnet) return subnet
def get_flavor_id(self, flavor_name): if (uuidutils.is_uuid_like(flavor_name) or (isinstance(flavor_name, str) and flavor_name.isdigit())): return flavor_name for flavor in self.nova_client.flavors.list(): if flavor.name == flavor_name: return flavor.id raise exc.NotFound(flavor_name)
def get_user_by_project(users_client, roles_client, project_id, username): users = users_client.list_users(**{'name': username})['users'] users_in_project = roles_client.list_role_assignments( **{'scope.project.id': project_id})['role_assignments'] for user in users: if user['name'] == username: for u in users_in_project: if u['user']['id'] == user['id']: return user raise lib_exc.NotFound('No such user(%s) in %s' % (username, users))
def assign_user_role(self, user, project, role_name): role = self._check_role_exists(role_name) if not role: msg = 'No "%s" role found' % role_name raise lib_exc.NotFound(msg) try: self._assign_user_role(project, user, role) except lib_exc.Conflict: LOG.debug("Role %s already assigned on project %s for user %s" % ( role['id'], project['id'], user['id']))
def get_project_by_name(self, project_name): if self.identity_version == "v2": projects = self.client.list_tenants()['tenants'] else: projects = self.client.list_projects()['projects'] for project in projects: if project['name'] == project_name: return project raise exceptions.NotFound('No such project/tenant (%s) in %s' % (project_name, projects))
def test_create_user_missing_tenant(self): self.useFixture(mockpatch.Patch( 'tempest.common.identity.get_tenant_by_name', side_effect=lib_exc.NotFound("tenant is not found"))) self.useFixture(mockpatch.PatchObject(javelin, "keystone_admin", return_value=self.fake_client)) javelin.create_users([self.fake_object]) mocked_function = self.fake_client.users.create_user self.assertFalse(mocked_function.called)
def wait_until_aggregated(aggregate_name, host_name): for i in xrange(self.MAX_RETRY): try: aggr = self.fetch_aggregate_by_name(aggregate_name) self.assertTrue(host_name in aggr['hosts']) return except Exception: pass time.sleep(self.WAIT_TIME) err_msg = ("hostname %s doesn't exist in aggregate %s." % (host_name, aggregate_name)) raise exceptions.NotFound(err_msg)
def wrapper(*args, **kwargs): if args and isinstance(args[0], test.BaseTestCase): test_obj = args[0] else: raise rbac_exceptions.RbacResourceSetupFailed( '`rbac_rule_validation` decorator can only be applied to ' 'an instance of `tempest.test.BaseTestCase`.') if admin_only: LOG.info("As admin_only is True, only admin role should be " "allowed to perform the API. Skipping oslo.policy " "check for policy action {0}.".format(rule)) allowed = test_obj.rbac_utils.is_admin else: allowed = _is_authorized(test_obj, service, rule, extra_target_data) expected_exception, irregular_msg = _get_exception_type( expected_error_code) try: func(*args, **kwargs) except rbac_exceptions.RbacInvalidService as e: msg = ("%s is not a valid service." % service) LOG.error(msg) raise exceptions.NotFound("%s RbacInvalidService was: %s" % (msg, e)) except (expected_exception, rbac_exceptions.RbacActionFailed) as e: if irregular_msg: LOG.warning(irregular_msg.format(rule, service)) if allowed: msg = ("Role %s was not allowed to perform %s." % (role, rule)) LOG.error(msg) raise exceptions.Forbidden("%s Exception was: %s" % (msg, e)) except Exception as e: exc_info = sys.exc_info() error_details = exc_info[1].__str__() msg = ("%s An unexpected exception has occurred: Expected " "exception was %s, which was not thrown." % (error_details, expected_exception.__name__)) LOG.error(msg) six.reraise(exc_info[0], exc_info[0](msg), exc_info[2]) else: if not allowed: LOG.error("Role %s was allowed to perform %s", role, rule) raise rbac_exceptions.RbacOverPermission( "OverPermission: Role %s was allowed to perform %s" % (role, rule)) finally: test_obj.rbac_utils.switch_role(test_obj, toggle_rbac_role=False)
def delete_a_keypair(base, name, is_admin_manager=True, ignore_missing=False): """Utility function that deletes a Nova keypair.""" if is_admin_manager is True: base.admin_manager.keypairs_client.delete_keypair(name) return res = base.compute_client.delete_obj('os-keypairs', name) if res['status'] == 404: if ignore_missing is True: return raise exceptions.NotFound()
def _get_external_port_id_from_vm(cls, vm_id): # Return external id of the (first) neutron port to which this VM belongs ext_id = 0 port_list = cls.admin_ports_client.list_ports() port_found = False the_port = [] for port in port_list['ports']: if port['device_id'] == vm_id: the_port = port port_found = True break if not port_found: raise exceptions.NotFound("ERROR: this VM (%s) has no port".format( str(vm_id))) else: ext_id = cls.nuage_vsd_client.get_vsd_external_id(the_port['id']) return ext_id