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