Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 1 | import pytest |
Hanna Arhipova | 16e93fb | 2019-01-23 19:03:01 +0200 | [diff] [blame] | 2 | import utils |
Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 3 | import random |
| 4 | import time |
Hanna Arhipova | 16e93fb | 2019-01-23 19:03:01 +0200 | [diff] [blame] | 5 | from utils import os_client |
Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 6 | |
| 7 | @pytest.fixture(scope='session') |
| 8 | def local_salt_client(): |
| 9 | return utils.init_salt_client() |
| 10 | |
| 11 | |
| 12 | # TODO: fix |
| 13 | # should not be executed on any test run |
| 14 | nodes = utils.get_pairs() |
| 15 | hw_nodes = utils.get_hw_pairs() |
| 16 | |
| 17 | |
| 18 | @pytest.fixture(scope='session', params=nodes.values(), ids=nodes.keys()) |
| 19 | def pair(request): |
| 20 | return request.param |
| 21 | |
| 22 | |
| 23 | @pytest.fixture(scope='session', params=hw_nodes.values(), ids=hw_nodes.keys()) |
| 24 | def hw_pair(request): |
| 25 | return request.param |
| 26 | |
| 27 | |
| 28 | @pytest.fixture(scope='session') |
| 29 | def openstack_clients(local_salt_client): |
| 30 | nodes_info = local_salt_client.cmd( |
| 31 | 'keystone:server', 'pillar.get', |
Oleksii Zhurba | c8997a4 | 2019-06-03 13:24:53 -0500 | [diff] [blame^] | 32 | ['keystone:client:os_client_config:cfgs:root:content:clouds:admin_identity'], |
Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 33 | expr_form='pillar') |
Oleksii Zhurba | c8997a4 | 2019-06-03 13:24:53 -0500 | [diff] [blame^] | 34 | |
Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 35 | if nodes_info.__len__() < 1: |
| 36 | pytest.skip("No keystone server found") |
| 37 | return False |
| 38 | keystone = nodes_info[nodes_info.keys()[0]] |
Oleksii Zhurba | c8997a4 | 2019-06-03 13:24:53 -0500 | [diff] [blame^] | 39 | # TODO: region? OS_CACERT? |
Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 40 | return os_client.OfficialClientManager( |
Oleksii Zhurba | c8997a4 | 2019-06-03 13:24:53 -0500 | [diff] [blame^] | 41 | username=keystone['auth']['username'], |
| 42 | password=keystone['auth']['password'], |
| 43 | tenant_name=keystone['auth']['project_name'], |
| 44 | auth_url=keystone['auth']['auth_url'], |
Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 45 | cert=False, |
Oleksii Zhurba | c8997a4 | 2019-06-03 13:24:53 -0500 | [diff] [blame^] | 46 | # domain will be used as project_domain_name and user_domain_name |
| 47 | domain=keystone['auth']['project_domain_name'], |
Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 48 | ) |
| 49 | |
| 50 | |
| 51 | @pytest.fixture(scope='session') |
| 52 | def os_resources(openstack_clients): |
| 53 | os_actions = os_client.OSCliActions(openstack_clients) |
| 54 | os_resource = {} |
| 55 | config = utils.get_configuration() |
| 56 | image_name = config.get('image_name') or ['Ubuntu'] |
| 57 | |
| 58 | os_images_list = [image.id for image in openstack_clients.image.images.list(filters={'name': image_name})] |
| 59 | if os_images_list.__len__() == 0: |
Hanna Arhipova | 04ac200 | 2019-03-01 13:12:41 +0200 | [diff] [blame] | 60 | pytest.skip("No images with name {}. This name can be redefined with 'image_name' env var ".format(image_name)) |
Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 61 | |
| 62 | os_resource['image_id'] = str(os_images_list[0]) |
| 63 | |
| 64 | os_resource['flavor_id'] = [flavor.id for flavor in openstack_clients.compute.flavors.list() if flavor.name == 'spt-test'] |
| 65 | if not os_resource['flavor_id']: |
| 66 | os_resource['flavor_id'] = os_actions.create_flavor('spt-test', 1536, 1, 3).id |
| 67 | else: |
| 68 | os_resource['flavor_id'] = str(os_resource['flavor_id'][0]) |
| 69 | |
| 70 | os_resource['sec_group'] = os_actions.create_sec_group() |
| 71 | os_resource['keypair'] = openstack_clients.compute.keypairs.create('spt-test-{}'.format(random.randrange(100, 999))) |
| 72 | os_resource['net1'] = os_actions.create_network_resources() |
| 73 | os_resource['ext_net'] = os_actions.get_external_network() |
| 74 | adm_tenant = os_actions.get_admin_tenant() |
| 75 | os_resource['router'] = os_actions.create_router(os_resource['ext_net'], adm_tenant.id) |
| 76 | os_resource['net2'] = os_actions.create_network(adm_tenant.id) |
Hanna Arhipova | cbf1f3a | 2019-04-09 20:02:10 +0300 | [diff] [blame] | 77 | os_resource['subnet2'] = os_actions.create_subnet(os_resource['net2'], adm_tenant.id, '10.2.7.0/24') |
Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 78 | for subnet in openstack_clients.network.list_subnets()['subnets']: |
| 79 | if subnet['network_id'] == os_resource['net1']['id']: |
| 80 | os_resource['subnet1'] = subnet['id'] |
| 81 | |
| 82 | openstack_clients.network.add_interface_router(os_resource['router']['id'], {'subnet_id': os_resource['subnet1']}) |
| 83 | openstack_clients.network.add_interface_router(os_resource['router']['id'], {'subnet_id': os_resource['subnet2']['id']}) |
| 84 | yield os_resource |
| 85 | # time.sleep(5) |
| 86 | openstack_clients.network.remove_interface_router(os_resource['router']['id'], {'subnet_id': os_resource['subnet1']}) |
| 87 | openstack_clients.network.remove_interface_router(os_resource['router']['id'], {'subnet_id': os_resource['subnet2']['id']}) |
| 88 | openstack_clients.network.remove_gateway_router(os_resource['router']['id']) |
| 89 | time.sleep(5) |
| 90 | openstack_clients.network.delete_router(os_resource['router']['id']) |
| 91 | time.sleep(5) |
| 92 | # openstack_clients.network.delete_subnet(subnet1['id']) |
| 93 | openstack_clients.network.delete_network(os_resource['net1']['id']) |
| 94 | openstack_clients.network.delete_network(os_resource['net2']['id']) |
| 95 | |
| 96 | openstack_clients.compute.security_groups.delete(os_resource['sec_group'].id) |
| 97 | openstack_clients.compute.keypairs.delete(os_resource['keypair'].name) |
| 98 | |
| 99 | openstack_clients.compute.flavors.delete(os_resource['flavor_id']) |