comment multiple subnet configuration
[sdnvpn.git] / sdnvpn / test / functest / testcase_2.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
14 from functest.utils import openstack_utils as os_utils
15 from random import randint
16 from sdnvpn.lib import config as sdnvpn_config
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_1,
204                                                         vm_2,
205                                                         # vm_3,
206                                                         vm_4,
207                                                         # vm_5
208                                                         )
209
210         if not instances_up:
211             logger.error("One or more instances is down")
212             sys.exit(-1)
213
214         logger.info("Waiting for the VMs to connect to each other using the"
215                     " updated network configuration")
216         test_utils.wait_before_subtest()
217
218         # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
219         results.check_ssh_output(vm_1, vm_2,
220                                  expected=TESTCASE_CONFIG.instance_2_name,
221                                  timeout=200)
222         # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
223         # results.check_ssh_output(vm_1, vm_3,
224         #                          expected=TESTCASE_CONFIG.instance_3_name,
225         #                          timeout=30)
226
227         results.add_to_summary(0, "-")
228         msg = ("Create VPN2 with eRT=iRT")
229         results.record_action(msg)
230         vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
231         kwargs = {
232             "import_targets": TESTCASE_CONFIG.targets1,
233             "export_targets": TESTCASE_CONFIG.targets1,
234             "route_targets": TESTCASE_CONFIG.targets1,
235             "route_distinguishers": TESTCASE_CONFIG.route_distinguishers2,
236             "name": vpn2_name
237         }
238         bgpvpn2 = test_utils.create_bgpvpn(neutron_client, **kwargs)
239         bgpvpn2_id = bgpvpn2['bgpvpn']['id']
240         logger.debug("VPN created details: %s" % bgpvpn2)
241         bgpvpn_ids.append(bgpvpn2_id)
242
243         msg = ("Associate network '%s' to the VPN2." %
244                TESTCASE_CONFIG.net_2_name)
245         results.record_action(msg)
246         results.add_to_summary(0, "-")
247
248         test_utils.create_network_association(
249             neutron_client, bgpvpn2_id, network_2_id)
250
251         test_utils.wait_for_bgp_net_assoc(neutron_client,
252                                           bgpvpn1_id, network_1_id)
253         test_utils.wait_for_bgp_net_assoc(neutron_client,
254                                           bgpvpn2_id, network_2_id)
255
256         logger.info("Waiting for the VMs to connect to each other using the"
257                     " updated network configuration")
258         test_utils.wait_before_subtest()
259
260         # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
261         # results.check_ssh_output(vm_4, vm_5,
262         #                          expected=TESTCASE_CONFIG.instance_5_name,
263         #                          timeout=30)
264
265         # 10.10.10.11 should return "not reachable" to sdnvpn-4
266         results.check_ssh_output(vm_4, vm_1,
267                                  expected="not reachable",
268                                  timeout=30)
269
270     except Exception as e:
271         logger.error("exception occurred while executing testcase_2: %s", e)
272         raise
273     finally:
274         test_utils.cleanup_nova(nova_client, instance_ids, image_ids)
275         test_utils.cleanup_neutron(neutron_client, floatingip_ids,
276                                    bgpvpn_ids, interfaces, subnet_ids,
277                                    router_ids, network_ids)
278
279     return results.compile_summary()
280
281
282 if __name__ == '__main__':
283     logging.basicConfig(level=logging.INFO)
284     sys.exit(main())