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