Merge "Replace glance client calls with openstack sdk"
[sdnvpn.git] / sdnvpn / test / functest / testcase_13.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2017 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10
11 import logging
12 import os
13 import sys
14
15 from random import randint
16 from sdnvpn.lib import config as sdnvpn_config
17 from sdnvpn.lib import openstack_utils as os_utils
18 from sdnvpn.lib import utils as test_utils
19 from sdnvpn.lib.results import Results
20
21 logger = logging.getLogger(__name__)
22
23 COMMON_CONFIG = sdnvpn_config.CommonConfig()
24 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
25     'sdnvpn.test.functest.testcase_13')
26
27
28 def main():
29     results = Results(COMMON_CONFIG.line_length)
30
31     results.add_to_summary(0, "=")
32     results.add_to_summary(2, "STATUS", "SUBTEST")
33     results.add_to_summary(0, "=")
34
35     if not os.path.isfile(COMMON_CONFIG.ubuntu_image_path):
36         logger.info("Downloading image")
37         image_dest_path = '/'.join(
38                 COMMON_CONFIG.ubuntu_image_path.split('/')[:-1])
39         os_utils.download_url(
40             "http://artifacts.opnfv.org/sdnvpn/"
41             "ubuntu-16.04-server-cloudimg-amd64-disk1.img",
42             image_dest_path)
43     else:
44         logger.info("Using old image")
45
46     nova_client = os_utils.get_nova_client()
47     neutron_client = os_utils.get_neutron_client()
48     conn = os_utils.get_os_connection()
49
50     (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
51      subnet_ids, interfaces, bgpvpn_ids, flavor_ids) = ([] for i in range(9))
52
53     try:
54         image_id = os_utils.create_glance_image(
55             conn,
56             COMMON_CONFIG.ubuntu_image_name,
57             COMMON_CONFIG.ubuntu_image_path,
58             disk="qcow2",
59             container="bare",
60             public="public")
61         image_ids.append(image_id)
62
63         _, flavor_id = test_utils.create_custom_flavor()
64         flavor_ids.append(flavor_id)
65
66         network_1_id, subnet_1_id, router_1_id = test_utils.create_network(
67             neutron_client,
68             TESTCASE_CONFIG.net_1_name,
69             TESTCASE_CONFIG.subnet_1_name,
70             TESTCASE_CONFIG.subnet_1_cidr,
71             TESTCASE_CONFIG.router_1_name)
72
73         interfaces.append(tuple((router_1_id, subnet_1_id)))
74         network_ids.extend([network_1_id])
75         subnet_ids.extend([subnet_1_id])
76         router_ids.extend([router_1_id])
77
78         sg_id = os_utils.create_security_group_full(
79             neutron_client, TESTCASE_CONFIG.secgroup_name,
80             TESTCASE_CONFIG.secgroup_descr)
81
82         compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
83
84         av_zone_1 = "nova:" + compute_nodes[0]
85         av_zone_2 = "nova:" + compute_nodes[1]
86
87         u1 = test_utils.generate_userdata_interface_create(
88             TESTCASE_CONFIG.interface_name,
89             TESTCASE_CONFIG.interface_number,
90             TESTCASE_CONFIG.extra_route_ip,
91             TESTCASE_CONFIG.extra_route_subnet_mask)
92         # boot INTANCES
93         vm_1 = test_utils.create_instance(
94             nova_client,
95             TESTCASE_CONFIG.instance_1_name,
96             image_id,
97             network_1_id,
98             sg_id,
99             flavor=COMMON_CONFIG.custom_flavor_name,
100             secgroup_name=TESTCASE_CONFIG.secgroup_name,
101             compute_node=av_zone_1,
102             userdata=u1)
103         vm_1_ip = test_utils.get_instance_ip(vm_1)
104
105         vm1_port = test_utils.get_port(neutron_client, vm_1.id)
106         test_utils.update_port_allowed_address_pairs(
107             neutron_client,
108             vm1_port['id'],
109             [test_utils.AllowedAddressPair(
110                 TESTCASE_CONFIG.extra_route_cidr,
111                 vm1_port['mac_address'])])
112
113         vm_2 = test_utils.create_instance(
114             nova_client,
115             TESTCASE_CONFIG.instance_2_name,
116             image_id,
117             network_1_id,
118             sg_id,
119             flavor=COMMON_CONFIG.custom_flavor_name,
120             secgroup_name=TESTCASE_CONFIG.secgroup_name,
121             compute_node=av_zone_1,
122             userdata=u1)
123         vm_2_ip = test_utils.get_instance_ip(vm_2)
124
125         vm2_port = test_utils.get_port(neutron_client, vm_2.id)
126         test_utils.update_port_allowed_address_pairs(
127             neutron_client,
128             vm2_port['id'],
129             [test_utils.AllowedAddressPair(
130                 TESTCASE_CONFIG.extra_route_cidr,
131                 vm2_port['mac_address'])])
132
133         test_utils.async_Wait_for_instances([vm_1, vm_2])
134
135         image_2_id = os_utils.create_glance_image(
136             conn, TESTCASE_CONFIG.image_name,
137             COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
138             container="bare", public='public')
139         image_ids.append(image_2_id)
140         # Moved vm_3 creation before associating its network/router with
141         # bgpvpn. If VM is created after its network is associated to bgpvpn
142         # via router, then BGPVPN in ODL uses router's vrf id for newly created
143         # VMs which causes testcase to fail.
144         u3 = test_utils.generate_ping_userdata(
145             [TESTCASE_CONFIG.extra_route_ip])
146         vm_3 = test_utils.create_instance(
147             nova_client,
148             TESTCASE_CONFIG.instance_3_name,
149             image_2_id,
150             network_1_id,
151             sg_id,
152             flavor=COMMON_CONFIG.custom_flavor_name,
153             secgroup_name=TESTCASE_CONFIG.secgroup_name,
154             compute_node=av_zone_2,
155             userdata=u3)
156
157         instance_ids.extend([vm_1.id, vm_2.id, vm_3.id])
158
159         instance_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_3)
160
161         if (not instance_dhcp_up):
162             logger.error("vm_3 instance is down")
163
164         msg = ("Create VPN with multiple RDs")
165         results.record_action(msg)
166         vpn_name = "sdnvpn-" + str(randint(100000, 999999))
167         kwargs = {
168             "import_targets": TESTCASE_CONFIG.targets1,
169             "export_targets": TESTCASE_CONFIG.targets2,
170             "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
171             "name": vpn_name
172         }
173         bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
174         bgpvpn_id = bgpvpn['bgpvpn']['id']
175         logger.debug("VPN created details: %s" % bgpvpn)
176         bgpvpn_ids.append(bgpvpn_id)
177
178         msg = ("Associate router '%s' to the VPN." %
179                TESTCASE_CONFIG.router_1_name)
180         results.record_action(msg)
181         results.add_to_summary(0, "-")
182
183         test_utils.create_router_association(
184             neutron_client, bgpvpn_id, router_1_id)
185
186         test_utils.update_router_extra_route(
187             neutron_client, router_1_id,
188             [test_utils.ExtraRoute(TESTCASE_CONFIG.extra_route_cidr,
189                                    vm_1_ip),
190              test_utils.ExtraRoute(TESTCASE_CONFIG.extra_route_cidr,
191                                    vm_2_ip)])
192
193         logger.info("Waiting for the VMs to connect to each other using the"
194                     " updated network configuration")
195         test_utils.wait_before_subtest()
196
197         results.get_ping_status_target_ip(vm_3,
198                                           TESTCASE_CONFIG.extra_route_name,
199                                           TESTCASE_CONFIG.extra_route_ip,
200                                           expected="PASS",
201                                           timeout=300)
202
203         results.add_to_summary(0, "=")
204         logger.info("\n%s" % results.summary)
205
206     except Exception as e:
207         logger.error("exception occurred while executing testcase_13: %s", e)
208         raise
209     finally:
210         test_utils.update_router_no_extra_route(neutron_client, router_ids)
211         test_utils.cleanup_nova(nova_client, instance_ids, flavor_ids)
212         test_utils.cleanup_glance(conn, image_ids)
213         test_utils.cleanup_neutron(neutron_client, floatingip_ids,
214                                    bgpvpn_ids, interfaces, subnet_ids,
215                                    router_ids, network_ids)
216
217     return results.compile_summary()
218
219
220 if __name__ == '__main__':
221     sys.exit(main())