def test_get_keystoneclient_v3_with_trust_id(self): """test_get_keystoneclient_v3_with_trust_id check that we could retrieve a Session client to work with keystone v3 and using trust_id""" osclients = OpenStackClients() trust_id = "randomid0000000000000000000000001" osclients.set_credential(self.OS_USERNAME, self.OS_PASSWORD, trust_id=trust_id) keystoneClient = osclients.get_keystoneclient() self.assertIsInstance(keystoneClient, keystoneclient.v3.client.Client)
def test_get_keystoneclient_v2_with_tenant_id(self): """test_get_keystoneclient_v2_with_tenant_id check that we could retrieve a Session client to work with keystone v2 and using tenant_id""" osclients = OpenStackClients() osclients.use_v3 = False osclients.set_credential(self.OS_USERNAME, self.OS_PASSWORD, tenant_id=self.OS_TENANT_ID) keystoneClient = osclients.get_keystoneclient() self.assertIsInstance(keystoneClient, keystoneclient.v2_0.client.Client)
def test_get_session_without_username_nor_token(self): """test_get_session_without_username check that we could not retrieve a session without username""" osclients = OpenStackClients() osclients.set_credential("", self.OS_PASSWORD, tenant_id=self.OS_TENANT_ID) # Checking v3 try: osclients.get_session() except Exception as ex: self.assertRaises(ex) # Checking v2 osclients.use_v3 = False try: osclients.get_session() except Exception as ex: self.assertRaises(ex)
def test_set_credential_to_osclients(self): """test_set_credential_to_osclients check that we could set credentials using method set_credential""" username = "******" password = "******" tenant_name = "new_user cloud" tenant_id = "00000000000000000000000000000002" trust_id = "randomid0000000000000000000000001" # FIRST CHECK: Credentials from ENV osclients = OpenStackClients() self.assertEqual(osclients._OpenStackClients__username, self.OS_USERNAME) self.assertEqual(osclients._OpenStackClients__tenant_id, self.OS_TENANT_ID) # SECOND CHECK: updating Credentials with tenant_id osclients.set_credential(username, password, tenant_id=tenant_id) self.assertEqual(osclients._OpenStackClients__tenant_id, tenant_id) # THIRD CHECK: updating Credentials with tenant_name osclients.set_credential(username, password, tenant_name=tenant_name) self.assertEqual(osclients._OpenStackClients__tenant_name, tenant_name) # FOURTH CHECK: updating Credentials with trust_id osclients.set_credential(username, password, trust_id=trust_id) self.assertEqual(osclients._OpenStackClients__trust_id, trust_id) # FIFTH CHECK: updating Credentials without trust_id, tenant_id and tenant_name osclients.set_credential(username, password) self.assertIsNone(osclients._OpenStackClients__trust_id) self.assertIsNone(osclients._OpenStackClients__tenant_name) self.assertIsNone(osclients._OpenStackClients__tenant_id) # Creating a client to check that set_credential destroy the session with v3 novaclient = osclients.get_novaclient() self.assertIsNotNone(osclients._session_v3) osclients.set_credential(username, password) self.assertIsNone(osclients._session_v3) # Creating a client to check that set_credential destroy the session with v2 osclients.use_v3 = False novaclient = osclients.get_novaclient() self.assertIsNotNone(osclients._session_v2) osclients.set_credential(username, password) self.assertIsNone(osclients._session_v2)
def __init__(self): """constructor""" osclients = OpenStackClients() neutron = osclients.get_neutronclient() cinder = osclients.get_cinderclient() glance = osclients.get_glanceclient() nova = osclients.get_novaclient() swift = osclients.get_swiftclient() print('Creating a container') swift.put_container('container', dict()) print('Creating two objects') swift.put_object('container', 'object', 'content') swift.put_object('container', 'object2', 'content2') print('Creating a volume') volume = cinder.volumes.create(name='cindervolume', size=1) external_net = None for net in neutron.list_networks()['networks']: if net['router:external']: external_net = net['id'] break properties = {'key1': 'value1'} print('Creating a private image') glance.images.create(container_format='bare', name='testimage1', disk_format='qcow2', data='aaaaa', properties=properties, is_public=False) if can_create_shared_images: download_images() print('Creating a shared image') cirrosfile = open(img_name) image_shared1 = glance.images.create(container_format='bare', name='testimg2', disk_format='qcow2', data=cirrosfile, properties={'key2': 'value2'}, is_public=True) print('Creating another shared image') cirrosfile2 = open(img_name2) image_shared2 = glance.images.create(container_format='bare', name='testima3', disk_format='qcow2', data=cirrosfile2, properties={'key3': 'val3'}, is_public=True) image_id = image_shared1.id else: image_id = glance.images.find(name=image_name) print('Creating a keypair') nova.keypairs.create(name='testpublickey') print('Allocating a new security group') nova.security_groups.create( 'testsecgroup a security group for testing') print('Reserving a flotaing ip') floatingip = nova.floating_ips.create(pool=external_net) if can_create_networks: print('Creating a router') router = neutron.create_router( {'router': { 'name': 'testrouter', 'admin_state_up': True }})['router'] print('Creating a network') n = neutron.create_network( {'network': { 'name': 'testnetwork', 'admin_state_up': True, }}) network = n['network'] print('Creating a subnet') subnet = neutron.create_subnet({ 'subnet': { 'name': 'testsubnet', 'network_id': network['id'], 'ip_version': 4, 'cidr': '192.168.1.0/24', 'dns_nameservers': ['8.8.8.8'] } })['subnet'] """ Only admin users can create shared networks. network2 = neutron.create_network( {'network': {'name': 'testnetwork_shared', 'admin_state_up': True, 'shared': True}})['network'] subnet2 = neutron.create_subnet( {'subnet': {'name': 'testsubnet_shared', 'network_id': network2['id'], 'ip_version': 4, 'cidr': '192.168.2.0/24', 'dns_nameservers': ['8.8.8.8']}})['subnet'] """ print('Adding interface and gateway to router') neutron.add_interface_router(router['id'], {'subnet_id': subnet['id']}) neutron.add_gateway_router(router['id'], {'network_id': external_net}) else: # use any internal network network = None for net in neutron.list_networks()['networks']: if not net['router:external']: network = net break # The volume must be available before creating the snapshot. time.sleep(3) print('Creating a volume snapshot') cinder.volume_snapshots.create(volume.id) tiny = nova.flavors.find(name='m1.tiny') nova.flavors.find(name='m1.small') nic = {'net-id': network['id']} print('Creating a VM') server = nova.servers.create('vm_testdelete', flavor=tiny, image=image_id, key_name='testpublickey', security_groups=['default'], nics=[nic]) # , files=files, config_drive=True) # give some time before assigning the floating ip time.sleep(10) server.add_floating_ip(floatingip.ip) if can_create_shared_images: # create a VM using another tenant, based in a shared image osclients2 = OpenStackClients() second_user = os.environ['USER2'] second_user_tenant = os.environ['USER2_TENANT'] osclients2.set_credential(second_user, os.environ['PASSWORD_USER2'], tenant_name=second_user_tenant) nova = osclients2.get_novaclient() neutron = osclients2.get_neutronclient() net2 = None for net in neutron.list_networks()['networks']: if not net['router:external']: net2 = net['id'] break nics = [{'net-id': net2}] print('Creating a second VM, with a different user') nova.servers.create('vm_testdelete2', flavor=tiny, image=image_shared2.id, nics=nics)
def __init__(self): """constructor""" osclients = OpenStackClients() neutron = osclients.get_neutronclient() cinder = osclients.get_cinderclient() glance = osclients.get_glanceclient() nova = osclients.get_novaclient() swift = osclients.get_swiftclient() print('Creating a container') swift.put_container('container', dict()) print('Creating two objects') swift.put_object('container', 'object', 'content') swift.put_object('container', 'object2', 'content2') print('Creating a volume') volume = cinder.volumes.create(name='cindervolume', size=1) external_net = None for net in neutron.list_networks()['networks']: if net['router:external']: external_net = net['id'] break properties = {'key1': 'value1'} print('Creating a private image') glance.images.create( container_format='bare', name='testimage1', disk_format='qcow2', data='aaaaa', properties=properties, is_public=False) if can_create_shared_images: download_images() print('Creating a shared image') cirrosfile = open(img_name) image_shared1 = glance.images.create( container_format='bare', name='testimg2', disk_format='qcow2', data=cirrosfile, properties={'key2': 'value2'}, is_public=True) print('Creating another shared image') cirrosfile2 = open(img_name2) image_shared2 = glance.images.create( container_format='bare', name='testima3', disk_format='qcow2', data=cirrosfile2, properties={'key3': 'val3'}, is_public=True) image_id = image_shared1.id else: image_id = glance.images.find(name=image_name) print('Creating a keypair') nova.keypairs.create(name='testpublickey') print('Allocating a new security group') nova.security_groups.create('testsecgroup a security group for testing') print('Reserving a flotaing ip') floatingip = nova.floating_ips.create(pool=external_net) if can_create_networks: print('Creating a router') router = neutron.create_router( {'router': {'name': 'testrouter', 'admin_state_up': True}} )['router'] print('Creating a network') n = neutron.create_network( {'network': {'name': 'testnetwork', 'admin_state_up': True, }}) network = n['network'] print('Creating a subnet') subnet = neutron.create_subnet( {'subnet': {'name': 'testsubnet', 'network_id': network['id'], 'ip_version': 4, 'cidr': '192.168.1.0/24', 'dns_nameservers': ['8.8.8.8']}})['subnet'] """ Only admin users can create shared networks. network2 = neutron.create_network( {'network': {'name': 'testnetwork_shared', 'admin_state_up': True, 'shared': True}})['network'] subnet2 = neutron.create_subnet( {'subnet': {'name': 'testsubnet_shared', 'network_id': network2['id'], 'ip_version': 4, 'cidr': '192.168.2.0/24', 'dns_nameservers': ['8.8.8.8']}})['subnet'] """ print('Adding interface and gateway to router') neutron.add_interface_router(router['id'], {'subnet_id': subnet['id']}) neutron.add_gateway_router(router['id'], {'network_id': external_net}) else: # use any internal network network = None for net in neutron.list_networks()['networks']: if not net['router:external']: network = net break # The volume must be available before creating the snapshot. time.sleep(3) print('Creating a volume snapshot') cinder.volume_snapshots.create(volume.id) tiny = nova.flavors.find(name='m1.tiny') nova.flavors.find(name='m1.small') nic = {'net-id': network['id']} print('Creating a VM') server = nova.servers.create( 'vm_testdelete', flavor=tiny, image=image_id, key_name='testpublickey', security_groups=['default'], nics=[nic]) # , files=files, config_drive=True) # give some time before assigning the floating ip time.sleep(10) server.add_floating_ip(floatingip.ip) if can_create_shared_images: # create a VM using another tenant, based in a shared image osclients2 = OpenStackClients() second_user = os.environ['USER2'] second_user_tenant = os.environ['USER2_TENANT'] osclients2.set_credential(second_user, os.environ['PASSWORD_USER2'], tenant_name=second_user_tenant) nova = osclients2.get_novaclient() neutron = osclients2.get_neutronclient() net2 = None for net in neutron.list_networks()['networks']: if not net['router:external']: net2 = net['id'] break nics = [{'net-id': net2}] print('Creating a second VM, with a different user') nova.servers.create( 'vm_testdelete2', flavor=tiny, image=image_shared2.id, nics=nics)