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