blob: a79d39db54102d064df469f6bfea7dbb447f2ad9 [file] [log] [blame]
Hanna Arhipova55cc1292019-01-08 14:22:18 +02001import pytest
Hanna Arhipova16e93fb2019-01-23 19:03:01 +02002import utils
Hanna Arhipova55cc1292019-01-08 14:22:18 +02003import random
4import time
Hanna Arhipova16e93fb2019-01-23 19:03:01 +02005from utils import os_client
Hanna Arhipova55cc1292019-01-08 14:22:18 +02006
7@pytest.fixture(scope='session')
8def local_salt_client():
9 return utils.init_salt_client()
10
11
12# TODO: fix
13# should not be executed on any test run
14nodes = utils.get_pairs()
15hw_nodes = utils.get_hw_pairs()
16
17
18@pytest.fixture(scope='session', params=nodes.values(), ids=nodes.keys())
19def pair(request):
20 return request.param
21
22
23@pytest.fixture(scope='session', params=hw_nodes.values(), ids=hw_nodes.keys())
24def hw_pair(request):
25 return request.param
26
27
28@pytest.fixture(scope='session')
29def openstack_clients(local_salt_client):
30 nodes_info = local_salt_client.cmd(
31 'keystone:server', 'pillar.get',
32 ['keystone:server'],
33 expr_form='pillar')
34 if nodes_info.__len__() < 1:
35 pytest.skip("No keystone server found")
36 return False
37 keystone = nodes_info[nodes_info.keys()[0]]
38 url = 'http://{ip}:{port}/'.format(ip=keystone['bind']['public_address'],
39 port=keystone['bind']['public_port'])
40 return os_client.OfficialClientManager(
41 username=keystone['admin_name'],
42 password=keystone['admin_password'],
43 tenant_name=keystone['admin_tenant'],
44 auth_url=url,
45 cert=False,
46 domain='Default',
47 )
48
49
50@pytest.fixture(scope='session')
51def os_resources(openstack_clients):
52 os_actions = os_client.OSCliActions(openstack_clients)
53 os_resource = {}
54 config = utils.get_configuration()
55 image_name = config.get('image_name') or ['Ubuntu']
56
57 os_images_list = [image.id for image in openstack_clients.image.images.list(filters={'name': image_name})]
58 if os_images_list.__len__() == 0:
Hanna Arhipova04ac2002019-03-01 13:12:41 +020059 pytest.skip("No images with name {}. This name can be redefined with 'image_name' env var ".format(image_name))
Hanna Arhipova55cc1292019-01-08 14:22:18 +020060
61 os_resource['image_id'] = str(os_images_list[0])
62
63 os_resource['flavor_id'] = [flavor.id for flavor in openstack_clients.compute.flavors.list() if flavor.name == 'spt-test']
64 if not os_resource['flavor_id']:
65 os_resource['flavor_id'] = os_actions.create_flavor('spt-test', 1536, 1, 3).id
66 else:
67 os_resource['flavor_id'] = str(os_resource['flavor_id'][0])
68
69 os_resource['sec_group'] = os_actions.create_sec_group()
70 os_resource['keypair'] = openstack_clients.compute.keypairs.create('spt-test-{}'.format(random.randrange(100, 999)))
71 os_resource['net1'] = os_actions.create_network_resources()
72 os_resource['ext_net'] = os_actions.get_external_network()
73 adm_tenant = os_actions.get_admin_tenant()
74 os_resource['router'] = os_actions.create_router(os_resource['ext_net'], adm_tenant.id)
75 os_resource['net2'] = os_actions.create_network(adm_tenant.id)
76 os_resource['subnet2'] = os_actions.create_subnet(os_resource['net2'], adm_tenant, '10.2.7.0/24')
77 for subnet in openstack_clients.network.list_subnets()['subnets']:
78 if subnet['network_id'] == os_resource['net1']['id']:
79 os_resource['subnet1'] = subnet['id']
80
81 openstack_clients.network.add_interface_router(os_resource['router']['id'], {'subnet_id': os_resource['subnet1']})
82 openstack_clients.network.add_interface_router(os_resource['router']['id'], {'subnet_id': os_resource['subnet2']['id']})
83 yield os_resource
84 # time.sleep(5)
85 openstack_clients.network.remove_interface_router(os_resource['router']['id'], {'subnet_id': os_resource['subnet1']})
86 openstack_clients.network.remove_interface_router(os_resource['router']['id'], {'subnet_id': os_resource['subnet2']['id']})
87 openstack_clients.network.remove_gateway_router(os_resource['router']['id'])
88 time.sleep(5)
89 openstack_clients.network.delete_router(os_resource['router']['id'])
90 time.sleep(5)
91 # openstack_clients.network.delete_subnet(subnet1['id'])
92 openstack_clients.network.delete_network(os_resource['net1']['id'])
93 openstack_clients.network.delete_network(os_resource['net2']['id'])
94
95 openstack_clients.compute.security_groups.delete(os_resource['sec_group'].id)
96 openstack_clients.compute.keypairs.delete(os_resource['keypair'].name)
97
98 openstack_clients.compute.flavors.delete(os_resource['flavor_id'])