def test_create_stack(self): """ Tests the creation of an OpenStack Heat stack1 that does not exist. """ self.stack1 = heat_utils.create_stack(self.heat_client, self.stack_settings1) stack_query_1 = heat_utils.get_stack( self.heat_client, stack_settings=self.stack_settings1) self.assertEqual(self.stack1, stack_query_1) stack_query_2 = heat_utils.get_stack( self.heat_client, stack_name=self.stack_settings1.name) self.assertEqual(self.stack1, stack_query_2) stack_query_3 = heat_utils.get_stack_by_id(self.heat_client, self.stack1.id) self.assertEqual(self.stack1, stack_query_3) resources = heat_utils.get_resources(self.heat_client, self.stack1.id) self.assertIsNotNone(resources) self.assertEqual(4, len(resources)) outputs = heat_utils.get_outputs(self.heat_client, self.stack1) self.assertIsNotNone(outputs) self.assertEqual(0, len(outputs)) self.assertTrue(stack_active(self.heat_client, self.stack1)) neutron = neutron_utils.neutron_client(self.os_creds, self.os_session) networks = heat_utils.get_stack_networks(self.heat_client, neutron, self.stack1) self.assertIsNotNone(networks) self.assertEqual(1, len(networks)) self.assertEqual(self.network_name, networks[0].name) subnets = neutron_utils.get_subnets_by_network(neutron, networks[0]) self.assertEqual(1, len(subnets)) self.assertEqual(self.subnet_name, subnets[0].name) nova = nova_utils.nova_client(self.os_creds, self.os_session) keystone = keystone_utils.keystone_client(self.os_creds, self.os_session) servers = heat_utils.get_stack_servers(self.heat_client, nova, neutron, keystone, self.stack1, self.os_creds.project_name) self.assertIsNotNone(servers) self.assertEqual(1, len(servers)) self.assertEqual(self.vm_inst_name, servers[0].name)
def assign_floating_ip_vnfs(self, router, ips=None): ''' Assign floating ips to all the SFs. Optionally specify the subnet IPs that a floating IP should be assigned to, assuming that the SF is connected to a single subnet globally and per port. ''' stacks = self.heat.stacks.list() fips = [] project_name = 'admin' for stack in stacks: servers = heat_utils.get_stack_servers(self.heat, self.nova, self.neutron, self.keystone, stack, project_name) sf_creator = cr_inst.generate_creator(self.os_creds, servers[0], self.image_settings, project_name) name = servers[0].name + "-float" if ips is None: port_name = servers[0].ports[0].name else: port_name = None for port in servers[0].ports: if port.ips[0]['ip_address'] in ips: port_name = port.name break if port_name is None: err_msg = "The VNF {} does not have any suitable port {} " \ "for floating IP assignment".format( name, 'with ip any of ' + str(ips) if ips else '') logger.error(err_msg) raise Exception(err_msg) float_ip = FloatingIpConfig(name=name, port_name=port_name, router_name=router.name) ip = sf_creator.add_floating_ip(float_ip) self.creators.append(sf_creator) fips.append(ip.ip) return fips
def get_vm_inst_creators(self, heat_keypair_option=None): """ Returns a list of VM Instance creator objects as configured by the heat template :return: list() of OpenStackVmInstance objects """ out = list() stack_servers = heat_utils.get_stack_servers( self.__heat_cli, self.__nova, self.__neutron, self._keystone, self.__stack, self._os_creds.project_name) workers = [] for stack_server in stack_servers: worker = worker_pool().apply_async( self.__create_vm_inst, (heat_keypair_option, stack_server)) workers.append(worker) for worker in workers: out.append(worker.get()) return out
def test_get_settings_from_stack(self): """ Tests that a heat template with floating IPs and can have the proper settings derived from settings_utils.py. """ resources = heat_utils.get_resources(self.heat_client, self.stack.id) self.assertIsNotNone(resources) self.assertEqual(13, len(resources)) options = heat_utils.get_outputs(self.heat_client, self.stack) self.assertIsNotNone(options) self.assertEqual(1, len(options)) neutron = neutron_utils.neutron_client(self.os_creds, self.os_session) networks = heat_utils.get_stack_networks(self.heat_client, neutron, self.stack) self.assertIsNotNone(networks) self.assertEqual(1, len(networks)) self.assertEqual(self.network_name, networks[0].name) network_settings = settings_utils.create_network_config( neutron, networks[0]) self.assertIsNotNone(network_settings) self.assertEqual(self.network_name, network_settings.name) nova = nova_utils.nova_client(self.os_creds, self.os_session) glance = glance_utils.glance_client(self.os_creds, self.os_session) keystone = keystone_utils.keystone_client(self.os_creds, self.os_session) servers = heat_utils.get_stack_servers(self.heat_client, nova, neutron, keystone, self.stack, self.os_creds.project_name) self.assertIsNotNone(servers) self.assertEqual(2, len(servers)) image_settings = settings_utils.determine_image_config( glance, servers[0], [ self.image_creator1.image_settings, self.image_creator2.image_settings ]) self.assertIsNotNone(image_settings) if image_settings.name.endswith('1'): self.assertEqual(self.image_creator1.image_settings.name, image_settings.name) else: self.assertEqual(self.image_creator2.image_settings.name, image_settings.name) image_settings = settings_utils.determine_image_config( glance, servers[1], [ self.image_creator1.image_settings, self.image_creator2.image_settings ]) if image_settings.name.endswith('1'): self.assertEqual(self.image_creator1.image_settings.name, image_settings.name) else: self.assertEqual(self.image_creator2.image_settings.name, image_settings.name) self.keypair1_settings = settings_utils.determine_keypair_config( self.heat_client, self.stack, servers[0], priv_key_key='private_key') self.assertIsNotNone(self.keypair1_settings) self.assertEqual(self.keypair_name, self.keypair1_settings.name) self.keypair2_settings = settings_utils.determine_keypair_config( self.heat_client, self.stack, servers[1], priv_key_key='private_key') self.assertIsNotNone(self.keypair2_settings) self.assertEqual(self.keypair_name, self.keypair2_settings.name)
def tearDown(self): """ Cleans the stack and image """ if self.stack: try: heat_utils.delete_stack(self.heat_client, self.stack) # Wait until stack deployment has completed end_time = (time.time() + stack_config.STACK_COMPLETE_TIMEOUT) is_deleted = False while time.time() < end_time: status = heat_utils.get_stack_status( self.heat_client, self.stack.id) if status == stack_config.STATUS_DELETE_COMPLETE: is_deleted = True break elif status == stack_config.STATUS_DELETE_FAILED: is_deleted = False break time.sleep(3) if not is_deleted: nova = nova_utils.nova_client(self.os_creds, self.os_session) keystone = keystone_utils.keystone_client( self.os_creds, self.os_session) neutron = neutron_utils.neutron_client( self.os_creds, self.os_session) glance = glance_utils.glance_client( self.os_creds, self.os_session) servers = heat_utils.get_stack_servers( self.heat_client, nova, neutron, keystone, self.stack, self.os_creds.project_name) for server in servers: vm_settings = settings_utils.create_vm_inst_config( nova, keystone, neutron, server, self.os_creds.project_name) img_settings = settings_utils.determine_image_config( glance, server, [ self.image_creator1.image_settings, self.image_creator2.image_settings ]) vm_creator = OpenStackVmInstance( self.os_creds, vm_settings, img_settings) vm_creator.initialize() vm_creator.clean() vm_creator.vm_deleted(block=True) heat_utils.delete_stack(self.heat_client, self.stack) time.sleep(20) except: raise if self.image_creator1: try: self.image_creator1.clean() except: pass if self.image_creator2: try: self.image_creator2.clean() except: pass if self.keypair1_settings: expanded_path = os.path.expanduser( self.keypair1_settings.private_filepath) os.chmod(expanded_path, 0o755) os.remove(expanded_path) if self.keypair2_settings: expanded_path = os.path.expanduser( self.keypair2_settings.private_filepath) os.chmod(expanded_path, 0o755) os.remove(expanded_path) super(self.__class__, self).__clean__()