Merge "fix for sdnvpn CI test failure"
[sdnvpn.git] / sdnvpn / test / functest / testcase_4.py
1 #!/usr/bin/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 sys
13 import time
14
15 from functest.utils import openstack_utils as os_utils
16 from random import randint
17 from sdnvpn.lib import config as sdnvpn_config
18 from sdnvpn.lib import utils as test_utils
19 from sdnvpn.lib.results import Results
20
21
22 logger = logging.getLogger(__name__)
23
24 COMMON_CONFIG = sdnvpn_config.CommonConfig()
25 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
26     'sdnvpn.test.functest.testcase_4')
27
28
29 def main():
30     results = Results(COMMON_CONFIG.line_length)
31
32     results.add_to_summary(0, "=")
33     results.add_to_summary(2, "STATUS", "SUBTEST")
34     results.add_to_summary(0, "=")
35
36     nova_client = os_utils.get_nova_client()
37     neutron_client = os_utils.get_neutron_client()
38     glance_client = os_utils.get_glance_client()
39
40     (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
41      subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
42
43     try:
44         image_id = os_utils.create_glance_image(
45             glance_client, TESTCASE_CONFIG.image_name,
46             COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
47             container="bare", public='public')
48         image_ids.append(image_id)
49
50         network_1_id, subnet_1_id, router_1_id = test_utils.create_network(
51             neutron_client,
52             TESTCASE_CONFIG.net_1_name,
53             TESTCASE_CONFIG.subnet_1_name,
54             TESTCASE_CONFIG.subnet_1_cidr,
55             TESTCASE_CONFIG.router_1_name)
56
57         network_2_id = test_utils.create_net(
58             neutron_client,
59             TESTCASE_CONFIG.net_2_name)
60
61         subnet_2_id = test_utils.create_subnet(
62             neutron_client,
63             TESTCASE_CONFIG.subnet_2_name,
64             TESTCASE_CONFIG.subnet_2_cidr,
65             network_2_id)
66         interfaces.append(tuple((router_1_id, subnet_1_id)))
67         network_ids.extend([network_1_id, network_2_id])
68         router_ids.append(router_1_id)
69         subnet_ids.extend([subnet_1_id, subnet_2_id])
70
71         sg_id = os_utils.create_security_group_full(
72             neutron_client,
73             TESTCASE_CONFIG.secgroup_name,
74             TESTCASE_CONFIG.secgroup_descr)
75
76         compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
77
78         av_zone_1 = "nova:" + compute_nodes[0]
79         av_zone_2 = "nova:" + compute_nodes[1]
80
81         # boot INTANCES
82         vm_2 = test_utils.create_instance(
83             nova_client,
84             TESTCASE_CONFIG.instance_2_name,
85             image_id,
86             network_1_id,
87             sg_id,
88             secgroup_name=TESTCASE_CONFIG.secgroup_name,
89             compute_node=av_zone_1)
90         vm_2_ip = test_utils.get_instance_ip(vm_2)
91
92         vm_3 = test_utils.create_instance(
93             nova_client,
94             TESTCASE_CONFIG.instance_3_name,
95             image_id,
96             network_1_id,
97             sg_id,
98             secgroup_name=TESTCASE_CONFIG.secgroup_name,
99             compute_node=av_zone_2)
100         vm_3_ip = test_utils.get_instance_ip(vm_3)
101
102         vm_5 = test_utils.create_instance(
103             nova_client,
104             TESTCASE_CONFIG.instance_5_name,
105             image_id,
106             network_2_id,
107             sg_id,
108             secgroup_name=TESTCASE_CONFIG.secgroup_name,
109             compute_node=av_zone_2)
110         vm_5_ip = test_utils.get_instance_ip(vm_5)
111
112         # We boot vm5 first because we need vm5_ip for vm4 userdata
113         u4 = test_utils.generate_ping_userdata([vm_5_ip])
114         vm_4 = test_utils.create_instance(
115             nova_client,
116             TESTCASE_CONFIG.instance_4_name,
117             image_id,
118             network_2_id,
119             sg_id,
120             secgroup_name=TESTCASE_CONFIG.secgroup_name,
121             compute_node=av_zone_1,
122             userdata=u4)
123         vm_4_ip = test_utils.get_instance_ip(vm_4)
124
125         # We boot VM1 at the end because we need to get the IPs
126         # first to generate the userdata
127         u1 = test_utils.generate_ping_userdata([vm_2_ip,
128                                                 vm_3_ip,
129                                                 vm_4_ip,
130                                                 vm_5_ip])
131         vm_1 = test_utils.create_instance(
132             nova_client,
133             TESTCASE_CONFIG.instance_1_name,
134             image_id,
135             network_1_id,
136             sg_id,
137             secgroup_name=TESTCASE_CONFIG.secgroup_name,
138             compute_node=av_zone_1,
139             userdata=u1)
140
141         instance_ids.extend([vm_1.id, vm_2.id, vm_3.id, vm_4.id, vm_5.id])
142
143         msg = ("Create VPN with eRT<>iRT")
144         results.record_action(msg)
145         vpn_name = "sdnvpn-" + str(randint(100000, 999999))
146         kwargs = {
147             "import_targets": TESTCASE_CONFIG.targets1,
148             "export_targets": TESTCASE_CONFIG.targets2,
149             "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
150             "name": vpn_name
151         }
152         bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
153         bgpvpn_id = bgpvpn['bgpvpn']['id']
154         logger.debug("VPN created details: %s" % bgpvpn)
155         bgpvpn_ids.append(bgpvpn_id)
156
157         msg = ("Associate router '%s' to the VPN." %
158                TESTCASE_CONFIG.router_1_name)
159         results.record_action(msg)
160         results.add_to_summary(0, "-")
161
162         test_utils.create_router_association(
163             neutron_client, bgpvpn_id, router_1_id)
164
165         # Wait for VMs to get ips.
166         instances_up = test_utils.wait_for_instances_up(vm_2, vm_3, vm_5)
167         instances_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_1, vm_4)
168
169         if (not instances_up or not instances_dhcp_up):
170             logger.error("One or more instances are down")
171             # TODO: Handle this appropriately
172
173         results.get_ping_status(vm_1, vm_2, expected="PASS", timeout=200)
174         results.get_ping_status(vm_1, vm_3, expected="PASS", timeout=30)
175         results.get_ping_status(vm_1, vm_4, expected="FAIL", timeout=30)
176
177         msg = ("Associate network '%s' to the VPN." %
178                TESTCASE_CONFIG.net_2_name)
179         results.add_to_summary(0, "-")
180         results.record_action(msg)
181         results.add_to_summary(0, "-")
182         test_utils.create_network_association(
183             neutron_client, bgpvpn_id, network_2_id)
184
185         test_utils.wait_for_bgp_router_assoc(
186             neutron_client, bgpvpn_id, router_1_id)
187         test_utils.wait_for_bgp_net_assoc(
188             neutron_client, bgpvpn_id, network_2_id)
189
190         logger.info("Waiting for the VMs to connect to each other using the"
191                     " updated network configuration")
192         test_utils.wait_before_subtest()
193
194         results.get_ping_status(vm_4, vm_5, expected="PASS", timeout=30)
195         # TODO enable again when isolation in VPN with iRT != eRT works
196         # results.get_ping_status(vm_1, vm_4, expected="FAIL", timeout=30)
197         # results.get_ping_status(vm_1, vm_5, expected="FAIL", timeout=30)
198
199         msg = ("Update VPN with eRT=iRT ...")
200         results.add_to_summary(0, "-")
201         results.record_action(msg)
202         results.add_to_summary(0, "-")
203
204         # use bgpvpn-create instead of update till NETVIRT-1067 bug is fixed
205         # kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
206         #           "export_targets": TESTCASE_CONFIG.targets1,
207         #           "name": vpn_name}
208         # bgpvpn = test_utils.update_bgpvpn(neutron_client,
209         #                                   bgpvpn_id, **kwargs)
210
211         test_utils.delete_bgpvpn(neutron_client, bgpvpn_id)
212         bgpvpn_ids.remove(bgpvpn_id)
213         kwargs = {
214             "import_targets": TESTCASE_CONFIG.targets1,
215             "export_targets": TESTCASE_CONFIG.targets1,
216             "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
217             "name": vpn_name
218         }
219
220         test_utils.wait_before_subtest()
221
222         bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
223         bgpvpn_id = bgpvpn['bgpvpn']['id']
224         logger.debug("VPN re-created details: %s" % bgpvpn)
225         bgpvpn_ids.append(bgpvpn_id)
226
227         msg = ("Associate again network '%s' and router '%s 'to the VPN."
228                % (TESTCASE_CONFIG.net_2_name,
229                   TESTCASE_CONFIG.router_1_name))
230         results.add_to_summary(0, "-")
231         results.record_action(msg)
232         results.add_to_summary(0, "-")
233
234         test_utils.create_router_association(
235             neutron_client, bgpvpn_id, router_1_id)
236
237         test_utils.create_network_association(
238             neutron_client, bgpvpn_id, network_2_id)
239
240         test_utils.wait_for_bgp_router_assoc(
241             neutron_client, bgpvpn_id, router_1_id)
242         test_utils.wait_for_bgp_net_assoc(
243             neutron_client, bgpvpn_id, network_2_id)
244         # The above code has to be removed after re-enabling bgpvpn-update
245
246         logger.info("Waiting for the VMs to connect to each other using the"
247                     " updated network configuration")
248         test_utils.wait_before_subtest()
249
250         results.get_ping_status(vm_1, vm_4, expected="PASS", timeout=30)
251         results.get_ping_status(vm_1, vm_5, expected="PASS", timeout=30)
252
253         results.add_to_summary(0, "=")
254         logger.info("\n%s" % results.summary)
255
256     except Exception as e:
257         logger.error("exception occurred while executing testcase_4: %s", e)
258         raise
259     finally:
260         test_utils.cleanup_nova(nova_client, instance_ids)
261         test_utils.cleanup_glance(glance_client, image_ids)
262         test_utils.cleanup_neutron(neutron_client, floatingip_ids,
263                                    bgpvpn_ids, interfaces, subnet_ids,
264                                    router_ids, network_ids)
265
266     return results.compile_summary()
267
268
269 if __name__ == '__main__':
270     logging.basicConfig(level=logging.INFO)
271     sys.exit(main())