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