def test_networks(self): customer_info, _, customer_client = self.create_customer( True, confirmed=True, with_client=True, need_openstack=True, mailtrap_email=True) openstack_client = OpenstackClient.init_by_creds( *self.get_openstack_credentials(customer_info)) networks = openstack_client.network_list()['networks'] self.assertGreater(len(networks), 0) self.search_network_in_list('DefaultPrivateNet', networks) network_name = self.create_name() network = openstack_client.network_create(name=network_name)['network'] try: self.assertEqual(network['name'], network_name) networks = openstack_client.network_list()['networks'] self.search_network_in_list(network_name, networks) finally: openstack_client.network_delete(network['id']) networks = openstack_client.network_list()['networks'] with self.assertRaises(AssertionError): self.search_network_in_list(network_name, networks)
def get_flavor_by_name(self, openstack_client: OpenstackClient, flavor_name: str): """Get opentsack flavor by name, case insensitive.""" flavor_list = openstack_client.flavor_list() return self.assertInList( flavor_list, lambda flavor: flavor.human_id.lower() == flavor_name.lower(), 'Flavor with id="{}" not found in flavor list:{}'.format( flavor_name, flavor_list))
def create_openstack_customer(self, services: list): """Create customer for working in openstack, with authorized openstack client""" tariff_info = self.create_tariff(services, immutable=True) customer_info, _, customer_client = self.create_customer(with_client=True, confirmed=True, need_openstack=True, locale='en', go_prod=True, make_full_prod=True, mailtrap_email=True) self.default_admin_client.customer.update(customer_info['customer_id'], tariff=tariff_info['tariff_id']) openstack_credentials = self.get_openstack_credentials(customer_info) openstack_client = OpenstackClient.init_by_creds(*openstack_credentials) return customer_info, customer_client, openstack_client
def create_openstack_customer(self, services: list): """Create customer for working in openstack, with authorized openstack client""" tariff_info = self.create_tariff(services, immutable=True) customer_info, _, customer_client = self.create_customer( with_client=True, confirmed=True, need_openstack=True, locale='en', go_prod=True, make_full_prod=True, mailtrap_email=True) self.default_admin_client.customer.update( customer_info['customer_id'], tariff=tariff_info['tariff_id']) openstack_credentials = self.get_openstack_credentials(customer_info) openstack_client = OpenstackClient.init_by_creds( *openstack_credentials) return customer_info, customer_client, openstack_client
def test_networks(self): customer_info, _, customer_client = self.create_customer(True, confirmed=True, with_client=True, need_openstack=True, mailtrap_email=True) openstack_client = OpenstackClient.init_by_creds(*self.get_openstack_credentials(customer_info)) networks = openstack_client.network_list()['networks'] self.assertGreater(len(networks), 0) self.search_network_in_list('DefaultPrivateNet', networks) network_name = self.create_name() network = openstack_client.network_create(name=network_name)['network'] try: self.assertEqual(network['name'], network_name) networks = openstack_client.network_list()['networks'] self.search_network_in_list(network_name, networks) finally: openstack_client.network_delete(network['id']) networks = openstack_client.network_list()['networks'] with self.assertRaises(AssertionError): self.search_network_in_list(network_name, networks)
def get_default_network(self, openstack_client: OpenstackClient) -> dict: """Return default network dict for server creation""" network_list = openstack_client.network_list()['networks'] return find_first(network_list, lambda network: network['name'] == 'DefaultPrivateNet') or network_list['networks'][0]
def get_flavor_by_name(self, openstack_client: OpenstackClient, flavor_name:str): """Get opentsack flavor by name, case insensitive.""" flavor_list = openstack_client.flavor_list() return self.assertInList(flavor_list, lambda flavor: flavor.human_id.lower() == flavor_name.lower(), 'Flavor with id="{}" not found in flavor list:{}'.format(flavor_name, flavor_list))
def get_default_image(self, openstack_client: OpenstackClient): """Return default image object for server creation""" image_list = openstack_client.image_list() return image_list[0]
def get_default_network(self, openstack_client: OpenstackClient) -> dict: """Return default network dict for server creation""" network_list = openstack_client.network_list()['networks'] return find_first( network_list, lambda network: network['name'] == 'DefaultPrivateNet') or network_list['networks'][0]
def test_openstack_auth(self): token_info = self.customer_client.customer.get_openstack_auth() openstack_client = OpenstackClient.init_by_token(token_info) image_list = openstack_client.image_list()
def test_server_creation(self): existed_default_tariff = self.check_default_tariff() if existed_default_tariff: self.addCleanupBeforeDelete( self.default_admin_client.tariff.set_default, existed_default_tariff['tariff_id']) self.create_tariff([(self.service_id, '123')], set_default=True, immutable=True) customer_info, _, customer_client = self.create_customer( confirmed=True, with_client=True, need_openstack=True, mailtrap_email=True) openstack_client = OpenstackClient.init_by_creds( *self.get_openstack_credentials(customer_info)) image_list = openstack_client.image_list() if self.image_human_id is None: image = image_list[0] else: image = self.assertInList( image_list, lambda image: image.human_id == self.image_human_id, 'Image with id="{}" not found in image list:{}'.format( self.image_human_id, image_list)) image_id = image.id image_human_id = image.human_id flavor_list = openstack_client.flavor_list() if self.flavor_human_id is None: flavor = flavor_list[0] else: flavor = self.assertInList( flavor_list, lambda flavor: flavor.human_id == self.flavor_human_id, 'Flavor with id="{}" not found in flavor list:{}'.format( self.flavor_human_id, flavor_list)) flavor_id = flavor.id flavor_human_id = flavor.human_id logbook.debug(pformat(flavor.to_dict())) network_list = openstack_client.network_list()['networks'] network_id = network_list[0]['id'] network_human_id = network_list[0]['name'] server_name = self.create_name() logbook.info( 'Creating instance image={} flavor={} network={} name={}'.format( image_human_id, flavor_human_id, network_human_id, server_name)) server = openstack_client.server_create(server_name, image_id, flavor_id, nics=[{ 'net-id': network_id }]) self.addCleanupBeforeDelete(server.delete) for r in self.retries(120): with r: data = openstack_client.nova_client.servers.get( server.id).to_dict() logbook.debug(data) vm_state = data['OS-EXT-STS:vm_state'] if vm_state != 'active': if vm_state == 'error': message = 'Error while creating server' if 'fault' in data: message += ': ' + data['fault']['message'] raise ValueError(message) self.assertEqual(vm_state, 'active', 'Server is not created yet') used = customer_client.customer.quota_used()['used_quotas'] search_in_list = lambda section: next(item[section] for item in used if list(item)[0] == section) get = lambda section, limit_id: next(item for item in search_in_list( section) if item['limit_id'] == limit_id) self.assertEqual(get('server_group', 'instances')['value'], 1) self.assertEqual(get('compute_group', 'cores')['value'], flavor.vcpus) self.assertEqual(get('compute_group', 'ram')['value'], flavor.ram)
def test_server_creation(self): existed_default_tariff = self.check_default_tariff() if existed_default_tariff: self.addCleanupBeforeDelete(self.default_admin_client.tariff.set_default, existed_default_tariff['tariff_id']) self.create_tariff([(self.service_id, '123')], set_default=True, immutable=True) customer_info, _, customer_client = self.create_customer(confirmed=True, with_client=True, need_openstack=True, mailtrap_email=True) openstack_client = OpenstackClient.init_by_creds(*self.get_openstack_credentials(customer_info)) image_list = openstack_client.image_list() if self.image_human_id is None: image = image_list[0] else: image = self.assertInList(image_list, lambda image: image.human_id == self.image_human_id, 'Image with id="{}" not found in image list:{}'.format(self.image_human_id, image_list)) image_id = image.id image_human_id = image.human_id flavor_list = openstack_client.flavor_list() if self.flavor_human_id is None: flavor = flavor_list[0] else: flavor = self.assertInList(flavor_list, lambda flavor: flavor.human_id == self.flavor_human_id, 'Flavor with id="{}" not found in flavor list:{}'.format(self.flavor_human_id, flavor_list)) flavor_id = flavor.id flavor_human_id = flavor.human_id logbook.debug(pformat(flavor.to_dict())) network_list = openstack_client.network_list()['networks'] network_id = network_list[0]['id'] network_human_id = network_list[0]['name'] server_name = self.create_name() logbook.info('Creating instance image={} flavor={} network={} name={}'.format(image_human_id, flavor_human_id, network_human_id, server_name)) server = openstack_client.server_create(server_name, image_id, flavor_id, nics=[{'net-id': network_id}]) self.addCleanupBeforeDelete(server.delete) for r in self.retries(120): with r: data = openstack_client.nova_client.servers.get(server.id).to_dict() logbook.debug(data) vm_state = data['OS-EXT-STS:vm_state'] if vm_state != 'active': if vm_state == 'error': message = 'Error while creating server' if 'fault' in data: message += ': ' + data['fault']['message'] raise ValueError(message) self.assertEqual(vm_state, 'active', 'Server is not created yet') used = customer_client.customer.quota_used()['used_quotas'] search_in_list = lambda section: next(item[section] for item in used if list(item)[0] == section) get = lambda section, limit_id: next(item for item in search_in_list(section) if item['limit_id'] == limit_id) self.assertEqual(get('server_group', 'instances')['value'], 1) self.assertEqual(get('compute_group', 'cores')['value'], flavor.vcpus) self.assertEqual(get('compute_group', 'ram')['value'], flavor.ram)
def test_ips_stats(self): customer_info, _, customer_client = self.create_customer(True, confirmed=True, with_client=True, individual=True, need_openstack=True, mailtrap_email=True) openstack_credentials = self.get_openstack_credentials(customer_info) openstack_client = OpenstackClient.init_by_creds(*openstack_credentials) quotas = self.get_used_quotas(customer_client) if quotas['floatingip']['max_value'] < 2: self.default_admin_client.customer.quota.update(customer_info['customer_id'], {'floatingip': 2}) quotas = self.get_used_quotas(customer_client) self.assertEqual(quotas['floatingip']['max_value'], 2) for r in self.retries(30, 1): with r: quota = openstack_client.neutron_client.show_quota(openstack_credentials[0])['quota'] self.assertEqual(quota['floatingip'], 2) get_stats = lambda: self.default_admin_client.stats.ips() def inc(d:dict, key:str): if key not in d: d[key] = 0 d[key] += 1 def dec(d:dict, key:str): d[key] -= 1 if d[key] == 0: d.pop(key) stats = get_stats() floating_ip = openstack_client.nova_client.floating_ips.create(openstack_client.nova_client.floating_ip_pools.list()[0].name) self.addCleanupBeforeDelete(floating_ip.delete) inc(stats, 'active_customer') inc(stats, 'customer_mode-test') inc(stats, 'customer_type-private') inc(stats, 'total') inc(stats, 'ip_status-DOWN') for r in self.retries(10, 1): with r: self.assertDictEqual(stats, get_stats()) # block test private self.default_admin_client.customer.block(customer_info['customer_id'], True) inc(stats, 'blocked_customer') dec(stats, 'active_customer') for r in self.retries(10, 1): with r: self.assertDictEqual(stats, get_stats()) self.default_admin_client.customer.block(customer_info['customer_id'], False) stats = get_stats() # make prod private customer_client.customer.make_prod() inc(stats, 'customer_mode-production') dec(stats, 'customer_mode-test') for r in self.retries(10, 1): with r: self.assertDictEqual(stats, get_stats()) # wait for customer unblocking task completed for r in self.retries(30, 1, keystoneclient.exceptions.Unauthorized): with r: openstack_client.keystone_client.get_raw_token_from_identity_service( username=openstack_credentials.username, tenant_id=openstack_credentials.tenant_id, password=openstack_credentials.password, auth_url=openstack_credentials.auth_url)