Replace neutron client calls with openstack sdk
[sdnvpn.git] / sdnvpn / test / functest / testcase_2.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 base64
12 import logging
13 import sys
14
15 from random import randint
16 from sdnvpn.lib import config as sdnvpn_config
17 from sdnvpn.lib import openstack_utils as os_utils
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_2')
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         logger.debug("Using private key %s injected to the VMs."
43                      % COMMON_CONFIG.keyfile_path)
44         keyfile = open(COMMON_CONFIG.keyfile_path, 'r')
45         key = keyfile.read()
46         keyfile.close()
47         files = [{'path': '/home/cirros/id_rsa',
48                   'contents': base64.b64encode(key)}]
49
50         image_id = os_utils.create_glance_image(
51             conn, TESTCASE_CONFIG.image_name,
52             COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
53             container="bare", public='public')
54         image_ids.append(image_id)
55
56         network_1_id = test_utils.create_net(
57             conn,
58             TESTCASE_CONFIG.net_1_name)
59         subnet_1a_id = test_utils.create_subnet(
60             conn,
61             TESTCASE_CONFIG.subnet_1a_name,
62             TESTCASE_CONFIG.subnet_1a_cidr,
63             network_1_id)
64         # TODO: uncomment the commented lines once ODL has
65         # support for mulitple subnets under same neutron network
66         # subnet_1b_id = test_utils.create_subnet(
67         #     conn,
68         #     TESTCASE_CONFIG.subnet_1b_name,
69         #     TESTCASE_CONFIG.subnet_1b_cidr,
70         #     network_1_id)
71
72         network_2_id = test_utils.create_net(
73             conn,
74             TESTCASE_CONFIG.net_2_name)
75         # subnet_2a_id = test_utils.create_subnet(
76         #     conn,
77         #     TESTCASE_CONFIG.subnet_2a_name,
78         #     TESTCASE_CONFIG.subnet_2a_cidr,
79         #     network_2_id)
80         subnet_2b_id = test_utils.create_subnet(
81             conn,
82             TESTCASE_CONFIG.subnet_2b_name,
83             TESTCASE_CONFIG.subnet_2b_cidr,
84             network_2_id)
85         network_ids.extend([network_1_id, network_2_id])
86         subnet_ids.extend([subnet_1a_id,
87                            # subnet_1b_id,
88                            # subnet_2a_id,
89                            subnet_2b_id])
90
91         sg_id = os_utils.create_security_group_full(
92             conn, TESTCASE_CONFIG.secgroup_name,
93             TESTCASE_CONFIG.secgroup_descr)
94
95         compute_nodes = test_utils.assert_and_get_compute_nodes(conn)
96
97         av_zone_1 = "nova:" + compute_nodes[0]
98         # av_zone_2 = "nova:" + compute_nodes[1]
99
100         # boot INTANCES
101         userdata_common = test_utils.generate_userdata_common()
102         vm_2 = test_utils.create_instance(
103             conn,
104             TESTCASE_CONFIG.instance_2_name,
105             image_id,
106             network_1_id,
107             sg_id,
108             fixed_ip=TESTCASE_CONFIG.instance_2_ip,
109             secgroup_name=TESTCASE_CONFIG.secgroup_name,
110             compute_node=av_zone_1,
111             userdata=userdata_common)
112
113
114 #         vm_3 = test_utils.create_instance(
115 #             conn,
116 #             TESTCASE_CONFIG.instance_3_name,
117 #             image_id,
118 #             network_1_id,
119 #             sg_id,
120 #             fixed_ip=TESTCASE_CONFIG.instance_3_ip,
121 #             secgroup_name=TESTCASE_CONFIG.secgroup_name,
122 #             compute_node=av_zone_2,
123 #             userdata=userdata_common)
124 #
125 #         vm_5 = test_utils.create_instance(
126 #             conn,
127 #             TESTCASE_CONFIG.instance_5_name,
128 #             image_id,
129 #             network_2_id,
130 #             sg_id,
131 #             fixed_ip=TESTCASE_CONFIG.instance_5_ip,
132 #             secgroup_name=TESTCASE_CONFIG.secgroup_name,
133 #             compute_node=av_zone_2,
134 #             userdata=userdata_common)
135
136         # We boot vm5 first because we need vm5_ip for vm4 userdata
137         u4 = test_utils.generate_userdata_with_ssh(
138             [TESTCASE_CONFIG.instance_1_ip
139              # TESTCASE_CONFIG.instance_3_ip,
140              # TESTCASE_CONFIG.instance_5_ip
141              ])
142         vm_4 = test_utils.create_instance(
143             conn,
144             TESTCASE_CONFIG.instance_4_name,
145             image_id,
146             network_2_id,
147             sg_id,
148             fixed_ip=TESTCASE_CONFIG.instance_4_ip,
149             secgroup_name=TESTCASE_CONFIG.secgroup_name,
150             compute_node=av_zone_1,
151             userdata=u4,
152             files=files)
153
154         # We boot VM1 at the end because we need to get the IPs first
155         # to generate the userdata
156         u1 = test_utils.generate_userdata_with_ssh(
157             [TESTCASE_CONFIG.instance_2_ip,
158              # TESTCASE_CONFIG.instance_3_ip,
159              TESTCASE_CONFIG.instance_4_ip,
160              # TESTCASE_CONFIG.instance_5_ip
161              ])
162         vm_1 = test_utils.create_instance(
163             conn,
164             TESTCASE_CONFIG.instance_1_name,
165             image_id,
166             network_1_id,
167             sg_id,
168             fixed_ip=TESTCASE_CONFIG.instance_1_ip,
169             secgroup_name=TESTCASE_CONFIG.secgroup_name,
170             compute_node=av_zone_1,
171             userdata=u1,
172             files=files)
173         instance_ids.extend([vm_1.id,
174                              vm_2.id,
175                              # vm_3.id,
176                              vm_4.id,
177                              #  vm_5.id
178                              ])
179
180         msg = ("Create VPN1 with eRT=iRT")
181         results.record_action(msg)
182         vpn1_name = "sdnvpn-1-" + str(randint(100000, 999999))
183         kwargs = {
184             "import_targets": TESTCASE_CONFIG.targets2,
185             "export_targets": TESTCASE_CONFIG.targets2,
186             "route_targets": TESTCASE_CONFIG.targets2,
187             "route_distinguishers": TESTCASE_CONFIG.route_distinguishers1,
188             "name": vpn1_name
189         }
190         bgpvpn1 = test_utils.create_bgpvpn(neutron_client, **kwargs)
191         bgpvpn1_id = bgpvpn1['bgpvpn']['id']
192         logger.debug("VPN1 created details: %s" % bgpvpn1)
193         bgpvpn_ids.append(bgpvpn1_id)
194
195         msg = ("Associate network '%s' to the VPN." %
196                TESTCASE_CONFIG.net_1_name)
197         results.record_action(msg)
198         results.add_to_summary(0, "-")
199
200         test_utils.create_network_association(
201             neutron_client, bgpvpn1_id, network_1_id)
202
203         # Wait for VMs to get ips.
204         instances_up = test_utils.wait_for_instances_up(vm_2)
205         instances_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_1, vm_4)
206
207         if (not instances_up or not instances_dhcp_up):
208             logger.error("One or more instances are down")
209             # TODO: Handle this appropriately
210
211         logger.info("Waiting for the VMs to connect to each other using the"
212                     " updated network configuration")
213         test_utils.wait_before_subtest()
214
215         # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
216         results.check_ssh_output(vm_1, vm_2,
217                                  expected=TESTCASE_CONFIG.instance_2_name,
218                                  timeout=200)
219         # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
220         # results.check_ssh_output(vm_1, vm_3,
221         #                          expected=TESTCASE_CONFIG.instance_3_name,
222         #                          timeout=30)
223
224         results.add_to_summary(0, "-")
225         msg = ("Create VPN2 with eRT=iRT")
226         results.record_action(msg)
227         vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
228         kwargs = {
229             "import_targets": TESTCASE_CONFIG.targets1,
230             "export_targets": TESTCASE_CONFIG.targets1,
231             "route_targets": TESTCASE_CONFIG.targets1,
232             "route_distinguishers": TESTCASE_CONFIG.route_distinguishers2,
233             "name": vpn2_name
234         }
235         bgpvpn2 = test_utils.create_bgpvpn(neutron_client, **kwargs)
236         bgpvpn2_id = bgpvpn2['bgpvpn']['id']
237         logger.debug("VPN created details: %s" % bgpvpn2)
238         bgpvpn_ids.append(bgpvpn2_id)
239
240         msg = ("Associate network '%s' to the VPN2." %
241                TESTCASE_CONFIG.net_2_name)
242         results.record_action(msg)
243         results.add_to_summary(0, "-")
244
245         test_utils.create_network_association(
246             neutron_client, bgpvpn2_id, network_2_id)
247
248         test_utils.wait_for_bgp_net_assoc(neutron_client,
249                                           bgpvpn1_id, network_1_id)
250         test_utils.wait_for_bgp_net_assoc(neutron_client,
251                                           bgpvpn2_id, network_2_id)
252
253         logger.info("Waiting for the VMs to connect to each other using the"
254                     " updated network configuration")
255         test_utils.wait_before_subtest()
256
257         # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
258         # results.check_ssh_output(vm_4, vm_5,
259         #                          expected=TESTCASE_CONFIG.instance_5_name,
260         #                          timeout=30)
261
262         # 10.10.10.11 should return "not reachable" to sdnvpn-4
263         results.check_ssh_output(vm_4, vm_1,
264                                  expected="not reachable",
265                                  timeout=30)
266
267     except Exception as e:
268         logger.error("exception occurred while executing testcase_2: %s", e)
269         raise
270     finally:
271         test_utils.cleanup_nova(conn, instance_ids)
272         test_utils.cleanup_glance(conn, image_ids)
273         test_utils.cleanup_neutron(conn, neutron_client, floatingip_ids,
274                                    bgpvpn_ids, interfaces, subnet_ids,
275                                    router_ids, network_ids)
276
277     return results.compile_summary()
278
279
280 if __name__ == '__main__':
281     sys.exit(main())