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