class CreateClusterTemplateTests(OSIntegrationTestCase): """ Test for the OpenStackClusterTemplate class defined in py without any QoS Specs or Encryption """ def setUp(self): """ Instantiates the CreateClusterTemplate object that is responsible for downloading and creating an OS template config file within OpenStack """ super(self.__class__, self).__start__() self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) self.cluster_type_name = self.guid + '-cluster-type' self.magnum = magnum_utils.magnum_client(self.os_creds, self.os_session) metadata = self.image_metadata if not metadata: metadata = dict() if 'extra_properties' not in metadata: metadata['extra_properties'] = dict() metadata['extra_properties']['os_distro'] = 'cirros' os_image_settings = openstack_tests.cirros_image_settings( name=self.guid + '-image', image_metadata=metadata) self.image_creator = OpenStackImage(self.os_creds, os_image_settings) flavor_config = openstack_tests.get_flavor_config( name=self.guid + '-flavor', ram=512, disk=10, vcpus=1, metadata=self.flavor_metadata) self.flavor_creator = OpenStackFlavor(self.os_creds, flavor_config) keypair_priv_filepath = 'tmp/' + self.guid keypair_pub_filepath = keypair_priv_filepath + '.pub' self.keypair_creator = OpenStackKeypair( self.os_creds, KeypairConfig(name=self.guid + '-keypair', public_filepath=keypair_pub_filepath, private_filepath=keypair_priv_filepath)) self.cluster_template_creator = None self.cluster_template_config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, flavor=self.flavor_creator.flavor_settings.name) try: self.image_creator.create() self.flavor_creator.create() self.keypair_creator.create() except: self.tearDown() raise def tearDown(self): """ Cleans the template config """ if self.cluster_template_creator: try: self.cluster_template_creator.clean() except: pass if self.keypair_creator: try: self.keypair_creator.clean() except: pass if self.flavor_creator: try: self.flavor_creator.clean() except: pass if self.image_creator: try: self.image_creator.clean() except: pass super(self.__class__, self).__clean__() def test_create_cluster_template(self): """ Tests the creation of an OpenStack cluster template. """ # Create ClusterTemplate self.cluster_template_creator = OpenStackClusterTemplate( self.os_creds, self.cluster_template_config) created_cluster_template = self.cluster_template_creator.create() self.assertIsNotNone(created_cluster_template) self.assertEqual(self.cluster_template_config.name, created_cluster_template.name) retrieved_cluster_template1 = magnum_utils.get_cluster_template( self.magnum, template_config=self.cluster_template_config) self.assertIsNotNone(retrieved_cluster_template1) self.assertEqual(created_cluster_template, retrieved_cluster_template1) retrieved_cluster_template2 = magnum_utils.get_cluster_template_by_id( self.magnum, created_cluster_template.id) self.assertEqual(created_cluster_template, retrieved_cluster_template2) def test_create_delete_cluster_template(self): """ Tests the creation then deletion of an OpenStack template config to ensure clean() does not raise an Exception. """ # Create ClusterTemplate self.cluster_template_creator = OpenStackClusterTemplate( self.os_creds, self.cluster_template_config) created_cluster_template = self.cluster_template_creator.create() self.assertIsNotNone(created_cluster_template) self.cluster_template_creator.clean() tmplt = magnum_utils.get_cluster_template( self.magnum, template_name=self.cluster_template_config.name) self.assertIsNone(tmplt) def test_create_same_cluster_template(self): """ Tests the creation of an OpenStack cluster_template when one already exists. """ # Create ClusterTemplate self.cluster_template_creator = OpenStackClusterTemplate( self.os_creds, self.cluster_template_config) cluster_template1 = self.cluster_template_creator.create() retrieved_cluster_template = magnum_utils.get_cluster_template( self.magnum, template_config=self.cluster_template_config) self.assertEqual(cluster_template1, retrieved_cluster_template) # Should be retrieving the instance data os_cluster_template_2 = OpenStackClusterTemplate( self.os_creds, self.cluster_template_config) cluster_template2 = os_cluster_template_2.create() self.assertEqual(cluster_template2, cluster_template2) def test_create_cluster_template_bad_flavor(self): """ Tests the creation of an OpenStack cluster template raises an exception with an invalid flavor. """ # Create ClusterTemplate cluster_template_config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, flavor='foo') self.cluster_template_creator = OpenStackClusterTemplate( self.os_creds, cluster_template_config) with self.assertRaises(BadRequest): self.cluster_template_creator.create() def test_create_cluster_template_bad_master_flavor(self): """ Tests the creation of an OpenStack cluster template raises an exception with an invalid master flavor. """ # Create ClusterTemplate cluster_template_config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, flavor=self.flavor_creator.flavor_settings.name, master_flavor='foo') self.cluster_template_creator = OpenStackClusterTemplate( self.os_creds, cluster_template_config) with self.assertRaises(BadRequest): self.cluster_template_creator.create() def test_create_cluster_template_bad_image(self): """ Tests the creation of an OpenStack cluster template raises an exception with an invalid image. """ # Create ClusterTemplate cluster_template_config = ClusterTemplateConfig( name=self.cluster_type_name, image='foo', keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, flavor=self.flavor_creator.flavor_settings.name) self.cluster_template_creator = OpenStackClusterTemplate( self.os_creds, cluster_template_config) with self.assertRaises(BadRequest): self.cluster_template_creator.create() def test_create_cluster_template_bad_network_driver(self): """ Tests the creation of an OpenStack cluster template raises an exception with an invalid keypair. """ # Create ClusterTemplate cluster_template_config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, flavor=self.flavor_creator.flavor_settings.name, network_driver='foo') self.cluster_template_creator = OpenStackClusterTemplate( self.os_creds, cluster_template_config) with self.assertRaises(BadRequest): self.cluster_template_creator.create() def test_create_cluster_template_bad_volume_driver(self): """ Tests the creation of an OpenStack cluster template raises an exception with an invalid keypair. """ # Create ClusterTemplate cluster_template_config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, flavor=self.flavor_creator.flavor_settings.name, volume_driver='foo') self.cluster_template_creator = OpenStackClusterTemplate( self.os_creds, cluster_template_config) with self.assertRaises(BadRequest): self.cluster_template_creator.create()
class HeatUtilsCreateSimpleStackTests(OSComponentTestCase): """ Test basic Heat functionality """ def setUp(self): """ Instantiates OpenStack instances that cannot be spawned by Heat """ guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) stack_name1 = guid + '-stack1' stack_name2 = guid + '-stack2' self.network_name = guid + '-net' self.subnet_name = guid + '-subnet' self.vm_inst_name = guid + '-inst' self.image_creator = OpenStackImage( self.os_creds, openstack_tests.cirros_image_settings( name=guid + '-image', image_metadata=self.image_metadata)) self.image_creator.create() # Create Flavor self.flavor_creator = OpenStackFlavor( self.os_creds, FlavorConfig(name=guid + '-flavor', ram=256, disk=10, vcpus=1)) self.flavor_creator.create() env_values = { 'image_name': self.image_creator.image_settings.name, 'flavor_name': self.flavor_creator.flavor_settings.name, 'net_name': self.network_name, 'subnet_name': self.subnet_name, 'inst_name': self.vm_inst_name } heat_tmplt_path = pkg_resources.resource_filename( 'snaps.openstack.tests.heat', 'test_heat_template.yaml') self.stack_settings1 = StackConfig(name=stack_name1, template_path=heat_tmplt_path, env_values=env_values) self.stack_settings2 = StackConfig(name=stack_name2, template_path=heat_tmplt_path, env_values=env_values) self.stack1 = None self.stack2 = None self.heat_client = heat_utils.heat_client(self.os_creds, self.os_session) def tearDown(self): """ Cleans the stack and image """ if self.stack1: try: heat_utils.delete_stack(self.heat_client, self.stack1) except: pass if self.stack2: try: heat_utils.delete_stack(self.heat_client, self.stack2) except: pass if self.image_creator: try: self.image_creator.clean() except: pass if self.flavor_creator: try: self.flavor_creator.clean() except: pass super(self.__class__, self).__clean__() 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 test_create_stack_x2(self): """ Tests the creation of an OpenStack keypair that does not exist. """ self.stack1 = heat_utils.create_stack(self.heat_client, self.stack_settings1) stack1_query_1 = heat_utils.get_stack( self.heat_client, stack_settings=self.stack_settings1) self.assertEqual(self.stack1, stack1_query_1) stack1_query_2 = heat_utils.get_stack( self.heat_client, stack_name=self.stack_settings1.name) self.assertEqual(self.stack1, stack1_query_2) stack1_query_3 = heat_utils.get_stack_by_id(self.heat_client, self.stack1.id) self.assertEqual(self.stack1, stack1_query_3) self.assertTrue(stack_active(self.heat_client, self.stack1)) self.stack2 = heat_utils.create_stack(self.heat_client, self.stack_settings2) stack2_query_1 = heat_utils.get_stack( self.heat_client, stack_settings=self.stack_settings2) self.assertEqual(self.stack2, stack2_query_1) stack2_query_2 = heat_utils.get_stack( self.heat_client, stack_name=self.stack_settings2.name) self.assertEqual(self.stack2, stack2_query_2) stack2_query_3 = heat_utils.get_stack_by_id(self.heat_client, self.stack2.id) self.assertEqual(self.stack2, stack2_query_3) self.assertTrue(stack_active(self.heat_client, self.stack2))
class MagnumUtilsClusterTypeTests(OSComponentTestCase): """ Tests individual functions within magnum_utils.py """ def setUp(self): self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) self.cluster_type_name = self.guid + '-cluster-type' self.magnum = magnum_utils.magnum_client( self.os_creds, self.os_session) metadata = self.image_metadata if not metadata: metadata = dict() if 'extra_properties' not in metadata: metadata['extra_properties'] = dict() metadata['extra_properties']['os_distro'] = 'cirros' os_image_settings = openstack_tests.cirros_image_settings( name=self.guid + '-image', image_metadata=metadata) self.image_creator = OpenStackImage(self.os_creds, os_image_settings) self.flavor_creator = OpenStackFlavor( self.os_creds, FlavorConfig( name=self.guid + '-flavor', ram=512, disk=10, vcpus=1)) keypair_priv_filepath = 'tmp/' + self.guid keypair_pub_filepath = keypair_priv_filepath + '.pub' self.keypair_creator = OpenStackKeypair( self.os_creds, KeypairConfig( name=self.guid + '-keypair', public_filepath=keypair_pub_filepath, private_filepath=keypair_priv_filepath)) self.cluster_template = None try: self.image_creator.create() self.flavor_creator.create() self.keypair_creator.create() except: self.tearDown() raise def tearDown(self): if self.cluster_template: try: magnum_utils.delete_cluster_template( self.magnum, self.cluster_template.id) except: pass if self.keypair_creator: try: self.keypair_creator.clean() except: pass if self.flavor_creator: try: self.flavor_creator.clean() except: pass if self.image_creator: try: self.image_creator.clean() except: pass super(self.__class__, self).__clean__() def test_create_cluster_template_simple(self): config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, flavor=self.flavor_creator.flavor_settings.name) self.cluster_template = magnum_utils.create_cluster_template( self.magnum, config) self.assertIsNotNone(self.cluster_template) self.assertTrue( validate_cluster_template(config, self.cluster_template)) template_by_name = magnum_utils.get_cluster_template( self.magnum, template_name=config.name) self.assertEqual(self.cluster_template, template_by_name) template_by_id = magnum_utils.get_cluster_template_by_id( self.magnum, self.cluster_template.id) self.assertEqual(self.cluster_template, template_by_id) def test_create_cluster_template_all(self): config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, network_driver='flannel', external_net=self.ext_net_name, floating_ip_enabled=True, docker_volume_size=100, server_type=ServerType.vm, flavor=self.flavor_creator.flavor_settings.name, master_flavor=self.flavor_creator.flavor_settings.name, coe=ContainerOrchestrationEngine.kubernetes, fixed_net='foo', fixed_subnet='bar', registry_enabled=True, insecure_registry='localhost', docker_storage_driver=DockerStorageDriver.overlay, dns_nameserver='8.8.4.4', public=True, tls_disabled=True, http_proxy=None, https_proxy=None, volume_driver='cinder', master_lb_enabled=False, labels={'foo': 'bar'}) self.cluster_template = magnum_utils.create_cluster_template( self.magnum, config) self.assertIsNotNone(self.cluster_template) self.assertTrue( validate_cluster_template(config, self.cluster_template)) template_by_name = magnum_utils.get_cluster_template( self.magnum, template_name=config.name) self.assertEqual(self.cluster_template, template_by_name) template_by_id = magnum_utils.get_cluster_template_by_id( self.magnum, self.cluster_template.id) self.assertEqual(self.cluster_template, template_by_id) def test_create_cluster_template_bad_image(self): config = ClusterTemplateConfig( name=self.cluster_type_name, image='foo', keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, flavor=self.flavor_creator.flavor_settings.name) with self.assertRaises(BadRequest): self.cluster_template = magnum_utils.create_cluster_template( self.magnum, config) def test_create_cluster_template_bad_ext_net(self): config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, external_net='foo', flavor=self.flavor_creator.flavor_settings.name) with self.assertRaises(BadRequest): self.cluster_template = magnum_utils.create_cluster_template( self.magnum, config) def test_create_cluster_template_bad_flavor(self): config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, flavor='foo') with self.assertRaises(BadRequest): self.cluster_template = magnum_utils.create_cluster_template( self.magnum, config) def test_create_cluster_template_bad_master_flavor(self): config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, flavor=self.flavor_creator.flavor_settings.name, master_flavor='foo') with self.assertRaises(BadRequest): self.cluster_template = magnum_utils.create_cluster_template( self.magnum, config) def test_create_cluster_template_bad_network_driver(self): config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, network_driver='foo') with self.assertRaises(BadRequest): self.cluster_template = magnum_utils.create_cluster_template( self.magnum, config) def test_create_cluster_template_bad_volume_driver(self): config = ClusterTemplateConfig( name=self.cluster_type_name, image=self.image_creator.image_settings.name, keypair=self.keypair_creator.keypair_settings.name, external_net=self.ext_net_name, volume_driver='foo') with self.assertRaises(BadRequest): self.cluster_template = magnum_utils.create_cluster_template( self.magnum, config)
class HeatUtilsCreateComplexStackTests(OSComponentTestCase): """ Test basic Heat functionality """ def setUp(self): """ Instantiates OpenStack instances that cannot be spawned by Heat """ guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) stack_name = guid + '-stack' self.network_name = guid + '-net' self.subnet_name = guid + '-subnet' self.vm_inst1_name = guid + '-inst1' self.vm_inst2_name = guid + '-inst2' self.flavor1_name = guid + '-flavor1' self.flavor2_name = guid + '-flavor2' self.keypair_name = guid + '-keypair' self.image_creator1 = OpenStackImage( self.os_creds, openstack_tests.cirros_image_settings( name=guid + '-image1', image_metadata=self.image_metadata)) self.image_creator1.create() self.image_creator2 = OpenStackImage( self.os_creds, openstack_tests.cirros_image_settings( name=guid + '-image2', image_metadata=self.image_metadata)) self.image_creator2.create() env_values = { 'image1_name': self.image_creator1.image_settings.name, 'image2_name': self.image_creator2.image_settings.name, 'flavor1_name': self.flavor1_name, 'flavor2_name': self.flavor2_name, 'net_name': self.network_name, 'subnet_name': self.subnet_name, 'keypair_name': self.keypair_name, 'inst1_name': self.vm_inst1_name, 'inst2_name': self.vm_inst2_name, 'external_net_name': self.ext_net_name } heat_tmplt_path = pkg_resources.resource_filename( 'snaps.openstack.tests.heat', 'floating_ip_heat_template.yaml') stack_settings = StackConfig(name=stack_name, template_path=heat_tmplt_path, env_values=env_values) self.heat_client = heat_utils.heat_client(self.os_creds, self.os_session) self.stack = heat_utils.create_stack(self.heat_client, stack_settings) self.assertTrue(stack_active(self.heat_client, self.stack)) self.keypair1_settings = None self.keypair2_settings = None 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 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)
subnet_settings=[subnet_settings]) network = OpenStackNetwork(os_creds, network_settings) network.create() # Flavors from snaps.config.flavor import FlavorConfig from snaps.openstack.create_flavor import OpenStackFlavor flavor_settings = FlavorConfig(name='test-flavor', ram=256, disk=10, vcpus=2) flavor = OpenStackFlavor(os_creds, flavor_settings) flavor.create() # Instances from snaps.config.network import PortConfig from snaps.openstack.create_instance import OpenStackVmInstance port_settings = PortConfig(name='test-port', network_name=network_settings.name) instance_settings = VmInstanceConfig(name='test-inst', flavor=flavor_settings.name, port_settings=[port_settings]) vm_inst = OpenStackVmInstance(os_creds, instance_settings, image_settings) vm_inst.create(block=True) # Cleanup vm_inst.clean() flavor.clean() network.clean() image.clean()
class CreateVolumeWithImageTests(OSIntegrationTestCase): """ Test cases for the CreateVolume when attempting to associate it to an Image """ def setUp(self): super(self.__class__, self).__start__() self.cinder = cinder_utils.cinder_client(self.os_creds, self.os_session) guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) self.volume_name = guid + '-vol' self.image_name = guid + '-image' os_image_settings = openstack_tests.cirros_image_settings( name=self.image_name, image_metadata=self.image_metadata) # Create Image self.image_creator = OpenStackImage(self.os_creds, os_image_settings) self.image_creator.create() self.volume_creator = None def tearDown(self): if self.volume_creator: try: self.volume_creator.clean() except: pass if self.image_creator: try: self.image_creator.clean() except: pass super(self.__class__, self).__clean__() def test_bad_image_name(self): """ Tests OpenStackVolume#create() method to ensure a volume is NOT created when associating it to an invalid image name """ self.volume_creator = OpenStackVolume( self.os_creds, VolumeConfig(name=self.volume_name, image_name='foo')) with self.assertRaises(BadRequest): self.volume_creator.create(block=True) def test_valid_volume_image(self): """ Tests OpenStackVolume#create() method to ensure a volume is NOT created when associating it to an invalid image name """ self.volume_creator = OpenStackVolume( self.os_creds, VolumeConfig(name=self.volume_name, image_name=self.image_name)) created_volume = self.volume_creator.create(block=True) self.assertIsNotNone(created_volume) self.assertEqual(self.volume_creator.volume_settings.name, created_volume.name) self.assertTrue(self.volume_creator.volume_active()) retrieved_volume = cinder_utils.get_volume_by_id( self.cinder, created_volume.id) self.assertEqual(created_volume, retrieved_volume)
class NovaUtilsInstanceVolumeTests(OSComponentTestCase): """ Tests the creation of VM instances via nova_utils.py """ 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.cinder = cinder_utils.cinder_client( self.os_creds, self.os_session) self.image_creator = None self.network_creator = None self.flavor_creator = None self.volume_creator = None self.instance_creator = 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_settings = openstack_tests.get_flavor_config( name=guid + '-flavor', ram=256, disk=10, vcpus=1, metadata=self.flavor_metadata) self.flavor_creator = OpenStackFlavor( self.os_creds, flavor_settings) self.flavor_creator.create() # Create Volume volume_settings = VolumeConfig( name=self.__class__.__name__ + '-' + str(guid)) self.volume_creator = OpenStackVolume( self.os_creds, volume_settings) self.volume_creator.create(block=True) port_settings = PortConfig( name=guid + '-port', network_name=network_settings.name) instance_settings = VmInstanceConfig( name=guid + '-vm_inst', flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings]) self.instance_creator = OpenStackVmInstance( self.os_creds, instance_settings, image_settings) self.instance_creator.create(block=True) except: self.tearDown() raise def tearDown(self): """ Cleanup deployed resources :return: """ if self.instance_creator: try: self.instance_creator.clean() except: pass if self.volume_creator: try: self.volume_creator.clean() except: pass if self.flavor_creator: try: self.flavor_creator.clean() except: pass if self.network_creator: try: self.network_creator.clean() except: pass if self.image_creator: try: self.image_creator.clean() except: pass super(self.__class__, self).__clean__() def test_add_remove_volume(self): """ Tests the nova_utils.attach_volume() and detach_volume functions with a timeout value :return: """ self.assertIsNotNone(self.volume_creator.get_volume()) self.assertEqual(0, len(self.volume_creator.get_volume().attachments)) # Attach volume to VM neutron = neutron_utils.neutron_client( self.os_creds, self.os_session) keystone = keystone_utils.keystone_client( self.os_creds, self.os_session) self.assertIsNotNone(nova_utils.attach_volume( self.nova, neutron, keystone, self.instance_creator.get_vm_inst(), self.volume_creator.get_volume(), self.os_creds.project_name)) vol_attach = None vol_detach = None attached = False start_time = time.time() while time.time() < start_time + 120: vol_attach = cinder_utils.get_volume_by_id( self.cinder, self.volume_creator.get_volume().id) if len(vol_attach.attachments) > 0: attached = True break time.sleep(3) self.assertTrue(attached) self.assertIsNotNone(vol_attach) keystone = keystone_utils.keystone_client( self.os_creds, self.os_session) vm_attach = nova_utils.get_server_object_by_id( self.nova, neutron, keystone, self.instance_creator.get_vm_inst().id, self.os_creds.project_name) # Validate Attachment self.assertIsNotNone(vol_attach) self.assertEqual(self.volume_creator.get_volume().id, vol_attach.id) self.assertEqual(1, len(vol_attach.attachments)) self.assertEqual(vm_attach.volume_ids[0]['id'], vol_attach.attachments[0]['volume_id']) # Detach volume to VM self.assertIsNotNone(nova_utils.detach_volume( self.nova, neutron, keystone, self.instance_creator.get_vm_inst(), self.volume_creator.get_volume(), self.os_creds.project_name)) start_time = time.time() while time.time() < start_time + 120: vol_detach = cinder_utils.get_volume_by_id( self.cinder, self.volume_creator.get_volume().id) if len(vol_detach.attachments) == 0: attached = False break time.sleep(3) self.assertFalse(attached) self.assertIsNotNone(vol_detach) vm_detach = nova_utils.get_server_object_by_id( self.nova, neutron, keystone, self.instance_creator.get_vm_inst().id, self.os_creds.project_name) # Validate Detachment self.assertIsNotNone(vol_detach) self.assertEqual(self.volume_creator.get_volume().id, vol_detach.id) self.assertEqual(0, len(vol_detach.attachments)) self.assertEqual(0, len(vm_detach.volume_ids)) def test_attach_volume_nowait(self): """ Tests the nova_utils.attach_volume() with a timeout value that is too small to have the volume attachment data to be included on the VmInst object that was supposed to be returned """ self.assertIsNotNone(self.volume_creator.get_volume()) self.assertEqual(0, len(self.volume_creator.get_volume().attachments)) # Attach volume to VM neutron = neutron_utils.neutron_client(self.os_creds, self.os_session) keystone = keystone_utils.keystone_client( self.os_creds, self.os_session) with self.assertRaises(NovaException): nova_utils.attach_volume( self.nova, neutron, keystone, self.instance_creator.get_vm_inst(), self.volume_creator.get_volume(), self.os_creds.project_name, 0) def test_detach_volume_nowait(self): """ Tests the nova_utils.detach_volume() with a timeout value that is too small to have the volume attachment data to be included on the VmInst object that was supposed to be returned """ self.assertIsNotNone(self.volume_creator.get_volume()) self.assertEqual(0, len(self.volume_creator.get_volume().attachments)) # Attach volume to VM neutron = neutron_utils.neutron_client(self.os_creds, self.os_session) keystone = keystone_utils.keystone_client( self.os_creds, self.os_session) nova_utils.attach_volume( self.nova, neutron, keystone, self.instance_creator.get_vm_inst(), self.volume_creator.get_volume(), self.os_creds.project_name) # Check VmInst for attachment keystone = keystone_utils.keystone_client( self.os_creds, self.os_session) latest_vm = nova_utils.get_server_object_by_id( self.nova, neutron, keystone, self.instance_creator.get_vm_inst().id, self.os_creds.project_name) self.assertEqual(1, len(latest_vm.volume_ids)) # Check Volume for attachment vol_attach = None attached = False start_time = time.time() while time.time() < start_time + 120: vol_attach = cinder_utils.get_volume_by_id( self.cinder, self.volume_creator.get_volume().id) if len(vol_attach.attachments) > 0: attached = True break time.sleep(3) self.assertTrue(attached) self.assertIsNotNone(vol_attach) # Detach volume with self.assertRaises(NovaException): nova_utils.detach_volume( self.nova, neutron, keystone, self.instance_creator.get_vm_inst(), self.volume_creator.get_volume(), self.os_creds.project_name, 0)
class NovaUtilsInstanceTests(OSComponentTestCase): """ Tests the creation of VM instances via nova_utils.py """ 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 tearDown(self): """ Cleanup deployed resources :return: """ if self.vm_inst: try: nova_utils.delete_vm_instance(self.nova, self.vm_inst) except: pass if self.port: try: neutron_utils.delete_port(self.neutron, self.port) except: pass if self.flavor_creator: try: self.flavor_creator.clean() except: pass if self.network_creator: try: self.network_creator.clean() except: pass if self.image_creator: try: self.image_creator.clean() except: pass super(self.__class__, self).__clean__() def test_create_instance(self): """ Tests the nova_utils.create_server() method :return: """ self.vm_inst = nova_utils.create_server( self.nova, self.keystone, self.neutron, self.glance, self.instance_settings, self.image_creator.image_settings, self.os_creds.project_name) self.assertIsNotNone(self.vm_inst) # Wait until instance is ACTIVE iters = 0 active = False status = None while iters < 60: status = nova_utils.get_server_status(self.nova, self.vm_inst) if create_instance.STATUS_ACTIVE == status: active = True break time.sleep(3) iters += 1 self.assertTrue(active, msg='VM {} status {} is not {}'.format( self.vm_inst.name, status, create_instance.STATUS_ACTIVE)) vm_inst = nova_utils.get_latest_server_object( self.nova, self.neutron, self.keystone, self.vm_inst, self.os_creds.project_name) self.assertEqual(self.vm_inst.name, vm_inst.name) self.assertEqual(self.vm_inst.id, vm_inst.id)