def setUp(self): """ Instantiates the CreateImage object that is responsible for downloading and creating an OS image file within OpenStack """ super(self.__class__, self).__start__() guid = uuid.uuid4() self.image_name = self.__class__.__name__ + '-' + str(guid) self.glance = glance_utils.glance_client( self.os_creds, self.os_session) self.image_creator = None if self.image_metadata and 'glance_tests' in self.image_metadata: glance_test_meta = self.image_metadata['glance_tests'] else: glance_test_meta = None self.tmp_dir = 'tmp/' + str(guid) if not os.path.exists(self.tmp_dir): os.makedirs(self.tmp_dir) self.image_settings = openstack_tests.cirros_image_settings( name=self.image_name, image_metadata=glance_test_meta)
def test_glance_connect_success(self): """ Tests to ensure that the proper credentials can connect. """ glance = glance_utils.glance_client(self.os_creds, self.os_session) image = glance_utils.get_image(glance, image_name='foo') self.assertIsNone(image)
def initialize(self): """ Loads the existing Image :return: The Image domain object or None """ super(self.__class__, self).initialize() self.__glance = glance_utils.glance_client(self._os_creds, self._os_session) self.__image = glance_utils.get_image( self.__glance, image_settings=self.image_settings) if self.__image: logger.info('Found image with name - ' + self.image_settings.name) return self.__image elif (self.image_settings.exists and not self.image_settings.url and not self.image_settings.image_file): raise ImageCreationError('Image with does not exist with name - ' + self.image_settings.name) if self.image_settings.kernel_image_settings: self.__kernel_image = glance_utils.get_image( self.__glance, image_settings=self.image_settings.kernel_image_settings) if self.image_settings.ramdisk_image_settings: self.__ramdisk_image = glance_utils.get_image( self.__glance, image_settings=self.image_settings.ramdisk_image_settings) return self.__image
def check_glance(self): """ checks that a simple glance operation works """ try: client = glance_utils.glance_client(self.os_creds) client.images.list() LOGGER.info("Glance service ...OK") except Exception as error: LOGGER.error("Glance service ...FAILED") raise error
def setUp(self): """ Setup objects required by VM instances :return: """ guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) self.nova = nova_utils.nova_client( self.os_creds, self.os_session) self.keystone = keystone_utils.keystone_client( self.os_creds, self.os_session) self.neutron = neutron_utils.neutron_client( self.os_creds, self.os_session) self.glance = glance_utils.glance_client( self.os_creds, self.os_session) self.image_creator = None self.network_creator = None self.flavor_creator = None self.port = None self.vm_inst = None try: image_settings = openstack_tests.cirros_image_settings( name=guid + '-image', image_metadata=self.image_metadata) self.image_creator = OpenStackImage( self.os_creds, image_settings=image_settings) self.image_creator.create() network_settings = openstack_tests.get_priv_net_config( project_name=self.os_creds.project_name, net_name="{}-{}".format(guid, 'net'), subnet_name="{}-{}".format(guid, 'subnet')).network_settings self.network_creator = OpenStackNetwork( self.os_creds, network_settings) self.network_creator.create() flavor_config = openstack_tests.get_flavor_config( name="{}-{}".format(guid, 'flavor-name'), ram=256, disk=10, vcpus=1, metadata=self.flavor_metadata) self.flavor_creator = OpenStackFlavor(self.os_creds, flavor_config) self.flavor_creator.create() port_settings = PortConfig( name=guid + '-port', network_name=network_settings.name) self.port = neutron_utils.create_port( self.neutron, self.os_creds, port_settings) self.instance_settings = VmInstanceConfig( name=guid + '-vm_inst', flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings]) except: self.tearDown() raise
def test_glance_connect_fail(self): """ Tests to ensure that the improper credentials cannot connect. """ from snaps.openstack.os_credentials import OSCreds with self.assertRaises(Exception): glance = glance_utils.glance_client( OSCreds(username='******', password='******', auth_url='url', project_name='project')) glance_utils.get_image(glance, image_name='foo')
def initialize(self): """ Loads the existing VMInst, Port, FloatingIps :return: VMInst domain object """ super(self.__class__, self).initialize() self.__neutron = neutron_utils.neutron_client(self._os_creds, self._os_session) self.__keystone = keystone_utils.keystone_client( self._os_creds, self._os_session) self.__cinder = cinder_utils.cinder_client(self._os_creds, self._os_session) self.__glance = glance_utils.glance_client(self._os_creds, self._os_session) self.__ports = self.__query_ports(self.instance_settings.port_settings) self.__lookup_existing_vm_by_name()
def setUp(self): """ Instantiates the CreateImage object that is responsible for downloading and creating an OS image file within OpenStack """ guid = uuid.uuid4() self.image_name = self.__class__.__name__ + '-' + str(guid) self.image = None self.glance = glance_utils.glance_client(self.os_creds, self.os_session) if self.image_metadata: self.glance_test_meta = self.image_metadata.get('glance_tests') else: self.glance_test_meta = dict() self.tmp_dir = 'tmp/' + str(guid) if not os.path.exists(self.tmp_dir): os.makedirs(self.tmp_dir)
def initialize(self): """ Loads the existing heat stack :return: The Stack domain object or None """ super(self.__class__, self).initialize() self.__neutron = neutron_utils.neutron_client(self._os_creds, self._os_session) self.__nova = nova_utils.nova_client(self._os_creds, self._os_session) self.__glance = glance_utils.glance_client(self._os_creds, self._os_session) self.__cinder = cinder_utils.cinder_client(self._os_creds, self._os_session) self.__heat_cli = heat_utils.heat_client(self._os_creds, self._os_session) self.__stack = heat_utils.get_stack(self.__heat_cli, stack_settings=self.stack_settings) if self.__stack: logger.info('Found stack with name - ' + self.stack_settings.name) return self.__stack
def deploy_vnf(self): start_time = time.time() self.__logger.info("Upload VNFD") cfy_client = self.orchestrator['object'] descriptor = self.vnf['descriptor'] self.deployment_name = descriptor.get('name') vrouter_blueprint_dir = os.path.join(self.data_dir, self.util.blueprint_dir) if not os.path.exists(vrouter_blueprint_dir): Repo.clone_from(descriptor.get('url'), vrouter_blueprint_dir, branch=descriptor.get('version')) cfy_client.blueprints.upload( vrouter_blueprint_dir + self.util.blueprint_file_name, descriptor.get('name')) self.__logger.info("Get or create flavor for vrouter") flavor_settings = FlavorSettings( name=self.vnf['requirements']['flavor']['name'], ram=self.vnf['requirements']['flavor']['ram_min'], disk=25, vcpus=1) flavor_creator = OpenStackFlavor(self.snaps_creds, flavor_settings) flavor = flavor_creator.create() self.created_object.append(flavor_creator) # set image name glance = glance_utils.glance_client(self.snaps_creds) image = glance_utils.get_image(glance, "vyos1.1.7") self.vnf['inputs'].update(dict(target_vnf_image_id=image.id)) self.vnf['inputs'].update(dict(reference_vnf_image_id=image.id)) # set flavor id self.vnf['inputs'].update(dict(target_vnf_flavor_id=flavor.id)) self.vnf['inputs'].update(dict(reference_vnf_flavor_id=flavor.id)) self.vnf['inputs'].update(dict(keystone_username=self.tenant_name)) self.vnf['inputs'].update(dict(keystone_password=self.tenant_name)) self.vnf['inputs'].update(dict(keystone_tenant_name=self.tenant_name)) self.vnf['inputs'].update( dict(keystone_url=os_utils.get_endpoint('identity'))) self.__logger.info("Create VNF Instance") cfy_client.deployments.create(descriptor.get('name'), descriptor.get('name'), self.vnf.get('inputs')) wait_for_execution(cfy_client, get_execution_id(cfy_client, descriptor.get('name')), self.__logger, timeout=7200) self.__logger.info("Start the VNF Instance deployment") execution = cfy_client.executions.start(descriptor.get('name'), 'install') # Show execution log execution = wait_for_execution(cfy_client, execution, self.__logger) duration = time.time() - start_time self.__logger.info(execution) if execution.status == 'terminated': self.details['vnf'].update(status='PASS', duration=duration) result = True else: self.details['vnf'].update(status='FAIL', duration=duration) result = False return result
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__()
def deploy_vnf(self): start_time = time.time() self.__logger.info("Upload VNFD") cfy_client = self.orchestrator['object'] descriptor = self.vnf['descriptor'] self.deployment_name = descriptor.get('name') cfy_client.blueprints.upload(descriptor.get('file_name'), descriptor.get('name')) self.__logger.info("Get or create flavor for vrouter") flavor_settings = FlavorConfig( name=self.vnf['requirements']['flavor']['name'], ram=self.vnf['requirements']['flavor']['ram_min'], disk=25, vcpus=1) flavor_creator = OpenStackFlavor(self.snaps_creds, flavor_settings) flavor = flavor_creator.create() self.created_object.append(flavor_creator) # set image name glance = glance_utils.glance_client(self.snaps_creds) image = glance_utils.get_image(glance, "vyos1.1.7") user_creator = OpenStackUser( self.snaps_creds, UserConfig(name='cloudify_network_bug-{}'.format(self.uuid), password=str(uuid.uuid4()), project_name=self.tenant_name, domain=self.snaps_creds.user_domain_name, roles={'_member_': self.tenant_name})) user_creator.create() self.created_object.append(user_creator) snaps_creds = user_creator.get_os_creds(self.snaps_creds.project_name) self.__logger.debug("snaps creds: %s", snaps_creds) self.vnf['inputs'].update(dict(target_vnf_image_id=image.id)) self.vnf['inputs'].update(dict(reference_vnf_image_id=image.id)) self.vnf['inputs'].update(dict(target_vnf_flavor_id=flavor.id)) self.vnf['inputs'].update(dict(reference_vnf_flavor_id=flavor.id)) self.vnf['inputs'].update(dict(keystone_username=snaps_creds.username)) self.vnf['inputs'].update(dict(keystone_password=snaps_creds.password)) self.vnf['inputs'].update( dict(keystone_tenant_name=snaps_creds.project_name)) self.vnf['inputs'].update( dict(keystone_user_domain_name=snaps_creds.user_domain_name)) self.vnf['inputs'].update( dict(keystone_project_domain_name=snaps_creds.project_domain_name)) self.vnf['inputs'].update(dict(region=snaps_creds.region_name)) self.vnf['inputs'].update( dict(keystone_url=keystone_utils.get_endpoint( snaps_creds, 'identity'))) self.__logger.info("Create VNF Instance") cfy_client.deployments.create(descriptor.get('name'), descriptor.get('name'), self.vnf.get('inputs')) wait_for_execution(cfy_client, get_execution_id(cfy_client, descriptor.get('name')), self.__logger, timeout=7200) self.__logger.info("Start the VNF Instance deployment") execution = cfy_client.executions.start(descriptor.get('name'), 'install') # Show execution log execution = wait_for_execution(cfy_client, execution, self.__logger) duration = time.time() - start_time self.__logger.info(execution) if execution.status == 'terminated': self.details['vnf'].update(status='PASS', duration=duration) result = True else: self.details['vnf'].update(status='FAIL', duration=duration) result = False return result
def setUp(self): """ Instantiates the CreateImage object that is responsible for downloading and creating an OS image file within OpenStack """ self.nova = nova_utils.nova_client(self.os_creds, self.os_session) self.keystone = keystone_utils.keystone_client(self.os_creds, self.os_session) self.glance = glance_utils.glance_client(self.os_creds, self.os_session) self.neutron = neutron_utils.neutron_client(self.os_creds, self.os_session) guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) self.keypair_priv_filepath = 'tmp/' + guid self.keypair_pub_filepath = self.keypair_priv_filepath + '.pub' self.keypair_name = guid + '-kp' self.vm_inst_name = guid + '-inst' self.test_file_local_path = 'tmp/' + guid + '-hello.txt' self.port_1_name = guid + '-port-1' self.port_2_name = guid + '-port-2' self.floating_ip_name = guid + 'fip1' # Setup members to cleanup just in case they don't get created self.inst_creator = None self.keypair_creator = None self.sec_grp_creator = None self.flavor_creator = None self.router_creator = None self.network_creator = None self.image_creator = None try: # Create Image os_image_settings = openstack_tests.cirros_image_settings( name=guid + '-' + '-image', image_metadata=self.image_metadata) self.image_creator = create_image.OpenStackImage( self.os_creds, os_image_settings) self.image_creator.create() # First network is public self.pub_net_config = openstack_tests.get_pub_net_config( project_name=self.os_creds.project_name, net_name=guid + '-pub-net', subnet_name=guid + '-pub-subnet', router_name=guid + '-pub-router', external_net=self.ext_net_name) self.network_creator = create_network.OpenStackNetwork( self.os_creds, self.pub_net_config.network_settings) self.network_creator.create() # Create routers self.router_creator = create_router.OpenStackRouter( self.os_creds, self.pub_net_config.router_settings) self.router_creator.create() # Create Flavor self.flavor_creator = create_flavor.OpenStackFlavor( self.os_creds, FlavorConfig(name=guid + '-flavor-name', ram=256, disk=1, vcpus=1)) self.flavor_creator.create() # Create Key/Pair self.keypair_creator = create_keypairs.OpenStackKeypair( self.os_creds, KeypairConfig(name=self.keypair_name, public_filepath=self.keypair_pub_filepath, private_filepath=self.keypair_priv_filepath)) self.keypair_creator.create() # Create Security Group sec_grp_name = guid + '-sec-grp' rule1 = SecurityGroupRuleConfig(sec_grp_name=sec_grp_name, direction=Direction.ingress, protocol=Protocol.icmp) rule2 = SecurityGroupRuleConfig(sec_grp_name=sec_grp_name, direction=Direction.ingress, protocol=Protocol.tcp, port_range_min=22, port_range_max=22) self.sec_grp_creator = OpenStackSecurityGroup( self.os_creds, SecurityGroupConfig(name=sec_grp_name, rule_settings=[rule1, rule2])) self.sec_grp_creator.create() # Create instance ports_settings = list() ports_settings.append( PortConfig( name=self.port_1_name, network_name=self.pub_net_config.network_settings.name)) instance_settings = VmInstanceConfig( name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=ports_settings, floating_ip_settings=[ FloatingIpConfig( name=self.floating_ip_name, port_name=self.port_1_name, router_name=self.pub_net_config.router_settings.name) ]) self.inst_creator = create_instance.OpenStackVmInstance( self.os_creds, instance_settings, self.image_creator.image_settings, keypair_settings=self.keypair_creator.keypair_settings) except: self.tearDown() raise