Fix router assoc in Boron
[sdnvpn.git] / test / functest / testcase_4.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2015 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 argparse
12 import os
13 from random import randint
14 import sys
15 import time
16
17 import functest.utils.functest_logger as ft_logger
18 import functest.utils.functest_utils as ft_utils
19 import functest.utils.openstack_utils as os_utils
20
21 import utils as test_utils
22 from results import Results
23
24 parser = argparse.ArgumentParser()
25
26 parser.add_argument("-r", "--report",
27                     help="Create json result file",
28                     action="store_true")
29
30 args = parser.parse_args()
31
32 logger = ft_logger.Logger("sdnvpn-testcase-4").getLogger()
33
34 REPO_PATH = os.environ['repos_dir'] + '/sdnvpn/'
35
36 VM_BOOT_TIMEOUT = 180
37
38 config_file = REPO_PATH + 'test/functest/config.yaml'
39
40 INSTANCE_1_NAME = ft_utils.get_parameter_from_yaml(
41     "testcases.testcase_4.instance_1_name", config_file)
42 INSTANCE_2_NAME = ft_utils.get_parameter_from_yaml(
43     "testcases.testcase_4.instance_2_name", config_file)
44 INSTANCE_3_NAME = ft_utils.get_parameter_from_yaml(
45     "testcases.testcase_4.instance_3_name", config_file)
46 INSTANCE_4_NAME = ft_utils.get_parameter_from_yaml(
47     "testcases.testcase_4.instance_4_name", config_file)
48 INSTANCE_5_NAME = ft_utils.get_parameter_from_yaml(
49     "testcases.testcase_4.instance_5_name", config_file)
50 IMAGE_NAME = ft_utils.get_parameter_from_yaml(
51     "testcases.testcase_4.image_name", config_file)
52 IMAGE_FILENAME = ft_utils.get_functest_config(
53     "general.openstack.image_file_name")
54 IMAGE_FORMAT = ft_utils.get_functest_config(
55     "general.openstack.image_disk_format")
56 IMAGE_PATH = ft_utils.get_functest_config(
57     "general.directories.dir_functest_data") + "/" + IMAGE_FILENAME
58
59 # NEUTRON Private Network parameters
60
61 NET_1_NAME = ft_utils.get_parameter_from_yaml(
62     "testcases.testcase_4.net_1_name", config_file)
63 SUBNET_1_NAME = ft_utils.get_parameter_from_yaml(
64     "testcases.testcase_4.subnet_1_name", config_file)
65 SUBNET_1_CIDR = ft_utils.get_parameter_from_yaml(
66     "testcases.testcase_4.subnet_1_cidr", config_file)
67 ROUTER_1_NAME = ft_utils.get_parameter_from_yaml(
68     "testcases.testcase_4.router_1_name", config_file)
69 NET_2_NAME = ft_utils.get_parameter_from_yaml(
70     "testcases.testcase_4.net_2_name", config_file)
71 SUBNET_2_NAME = ft_utils.get_parameter_from_yaml(
72     "testcases.testcase_4.subnet_2_name", config_file)
73 SUBNET_2_CIDR = ft_utils.get_parameter_from_yaml(
74     "testcases.testcase_4.subnet_2_cidr", config_file)
75 ROUTER_2_NAME = ft_utils.get_parameter_from_yaml(
76     "testcases.testcase_4.router_2_name", config_file)
77 SECGROUP_NAME = ft_utils.get_parameter_from_yaml(
78     "testcases.testcase_4.sdnvpn_sg_name", config_file)
79 SECGROUP_DESCR = ft_utils.get_parameter_from_yaml(
80     "testcases.testcase_4.sdnvpn_sg_descr", config_file)
81 TARGETS_1 = ft_utils.get_parameter_from_yaml(
82     "testcases.testcase_4.targets1", config_file)
83 TARGETS_2 = ft_utils.get_parameter_from_yaml(
84     "testcases.testcase_4.targets2", config_file)
85 ROUTE_DISTINGUISHERS = ft_utils.get_parameter_from_yaml(
86     "testcases.testcase_4.route_distinguishers", config_file)
87 SUCCESS_CRITERIA = ft_utils.get_parameter_from_yaml(
88     "testcases.testcase_4.succes_criteria", config_file)
89 TEST_DB = ft_utils.get_functest_config("results.test_db_url")
90
91 LINE_LENGTH = 60  # length for the summary table
92
93
94 def main():
95     global LINE_LENGTH
96
97     results = Results(LINE_LENGTH)
98
99     results.add_to_summary(0, "=")
100     results.add_to_summary(2, "STATUS", "SUBTEST")
101     results.add_to_summary(0, "=")
102
103     nova_client = os_utils.get_nova_client()
104     neutron_client = os_utils.get_neutron_client()
105     glance_client = os_utils.get_glance_client()
106
107     image_id = os_utils.create_glance_image(glance_client,
108                                             IMAGE_NAME,
109                                             IMAGE_PATH,
110                                             disk=IMAGE_FORMAT,
111                                             container="bare",
112                                             public=True)
113     network_1_id, _, router_1_id = test_utils.create_network(neutron_client,
114                                                              NET_1_NAME,
115                                                              SUBNET_1_NAME,
116                                                              SUBNET_1_CIDR,
117                                                              ROUTER_1_NAME)
118     # the network that is net-assoc'd cannot have a router
119     # because ODL doesn't support it
120     network_2_id = test_utils.create_net(neutron_client,
121                                          NET_2_NAME)
122     test_utils.create_subnet(neutron_client,
123                              SUBNET_2_NAME,
124                              SUBNET_2_CIDR,
125                              network_2_id)
126
127     sg_id = os_utils.create_security_group_full(neutron_client,
128                                                 SECGROUP_NAME, SECGROUP_DESCR)
129
130     # Get hypervisors zones
131     compute_nodes = os_utils.get_hypervisors(nova_client)
132     num_compute_nodes = len(compute_nodes)
133     if num_compute_nodes < 2:
134         logger.error("There are %s compute nodes in the deployment. "
135                      "Minimum number of nodes to complete the test is 2."
136                      % num_compute_nodes)
137         sys.exit(-1)
138
139     logger.debug("Compute nodes: %s" % compute_nodes)
140     av_zone_1 = "nova:" + compute_nodes[0]
141     av_zone_2 = "nova:" + compute_nodes[1]
142
143     # boot INTANCES
144     vm_2 = test_utils.create_instance(nova_client,
145                                       INSTANCE_2_NAME,
146                                       image_id,
147                                       network_1_id,
148                                       sg_id,
149                                       secgroup_name=SECGROUP_NAME,
150                                       compute_node=av_zone_1)
151     vm_2_ip = vm_2.networks.itervalues().next()[0]
152     logger.debug("Instance '%s' booted successfully. IP='%s'." %
153                  (INSTANCE_2_NAME, vm_2_ip))
154
155     vm_3 = test_utils.create_instance(nova_client,
156                                       INSTANCE_3_NAME,
157                                       image_id,
158                                       network_1_id,
159                                       sg_id,
160                                       secgroup_name=SECGROUP_NAME,
161                                       compute_node=av_zone_2)
162     vm_3_ip = vm_3.networks.itervalues().next()[0]
163     logger.debug("Instance '%s' booted successfully. IP='%s'." %
164                  (INSTANCE_3_NAME, vm_3_ip))
165
166     vm_5 = test_utils.create_instance(nova_client,
167                                       INSTANCE_5_NAME,
168                                       image_id,
169                                       network_2_id,
170                                       sg_id,
171                                       secgroup_name=SECGROUP_NAME,
172                                       compute_node=av_zone_2)
173     vm_5_ip = vm_5.networks.itervalues().next()[0]
174     logger.debug("Instance '%s' booted successfully. IP='%s'." %
175                  (INSTANCE_5_NAME, vm_5_ip))
176
177     # We boot vm5 first because we need vm5_ip for vm4 userdata
178     u4 = test_utils.generate_ping_userdata([vm_5_ip])
179     vm_4 = test_utils.create_instance(nova_client,
180                                       INSTANCE_4_NAME,
181                                       image_id,
182                                       network_2_id,
183                                       sg_id,
184                                       secgroup_name=SECGROUP_NAME,
185                                       compute_node=av_zone_1,
186                                       userdata=u4)
187     vm_4_ip = vm_4.networks.itervalues().next()[0]
188     logger.debug("Instance '%s' booted successfully. IP='%s'." %
189                  (INSTANCE_4_NAME, vm_4_ip))
190
191     # We boot VM1 at the end because we need to get the IPs first to generate
192     # the userdata
193     u1 = test_utils.generate_ping_userdata([vm_2_ip,
194                                             vm_3_ip,
195                                             vm_4_ip,
196                                             vm_5_ip])
197     vm_1 = test_utils.create_instance(nova_client,
198                                       INSTANCE_1_NAME,
199                                       image_id,
200                                       network_1_id,
201                                       sg_id,
202                                       secgroup_name=SECGROUP_NAME,
203                                       compute_node=av_zone_1,
204                                       userdata=u1)
205     vm_1_ip = vm_1.networks.itervalues().next()[0]
206     logger.debug("Instance '%s' booted successfully. IP='%s'." %
207                  (INSTANCE_1_NAME, vm_1_ip))
208     msg = ("Create VPN with eRT<>iRT")
209     logger.info(msg)
210     results.add_to_summary(1, msg)
211     vpn_name = "sdnvpn-" + str(randint(100000, 999999))
212     kwargs = {"import_targets": TARGETS_1,
213               "export_targets": TARGETS_2,
214               "route_distinguishers": ROUTE_DISTINGUISHERS,
215               "name": vpn_name}
216     bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
217     bgpvpn_id = bgpvpn['bgpvpn']['id']
218     logger.debug("VPN created details: %s" % bgpvpn)
219
220     msg = ("Associate router '%s' to the VPN." % ROUTER_1_NAME)
221     logger.info(msg)
222     results.add_to_summary(1, msg)
223     results.add_to_summary(0, "-")
224
225     os_utils.create_router_association(
226         neutron_client, bgpvpn_id, router_1_id)
227
228     # Wait for VMs to get ips.
229     instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
230                                                     vm_3, vm_4,
231                                                     vm_5)
232
233     if not instances_up:
234         logger.error("One or more instances is down")
235         # TODO Handle appropriately
236
237     # Ping from VM1 to VM2 should work
238     results.get_ping_status(vm_1, vm_1_ip, vm_2, vm_2_ip,
239                             expected="PASS", timeout=200)
240     # Ping from VM1 to VM3 should work
241     results.get_ping_status(vm_1, vm_1_ip, vm_3, vm_3_ip,
242                             expected="PASS", timeout=30)
243     # Ping from VM1 to VM4 should not work
244     results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
245                             expected="FAIL", timeout=30)
246
247     msg = ("Associate network '%s' to the VPN." % NET_2_NAME)
248     logger.info(msg)
249     results.add_to_summary(0, "-")
250     results.add_to_summary(1, msg)
251     results.add_to_summary(0, "-")
252     os_utils.create_network_association(
253         neutron_client, bgpvpn_id, network_2_id)
254
255     test_utils.wait_for_bgp_router_assoc(
256         neutron_client, bgpvpn_id, router_1_id)
257     test_utils.wait_for_bgp_net_assoc(
258         neutron_client, bgpvpn_id, network_2_id)
259
260     logger.info("Waiting for the VMs to connect to each other using the"
261                 " updated network configuration")
262     time.sleep(30)
263
264     # Ping from VM4 to VM5 should work
265     results.get_ping_status(vm_4, vm_4_ip, vm_5, vm_5_ip,
266                             expected="PASS", timeout=30)
267     # Ping from VM1 to VM4 should not work
268     results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
269                             expected="FAIL", timeout=30)
270     # Ping from VM1 to VM5 should not work
271     results.get_ping_status(vm_1, vm_1_ip, vm_5, vm_5_ip,
272                             expected="FAIL", timeout=30)
273
274     msg = ("Update VPN with eRT=iRT ...")
275     logger.info(msg)
276     results.add_to_summary(0, "-")
277     results.add_to_summary(1, msg)
278     results.add_to_summary(0, "-")
279     kwargs = {"import_targets": TARGETS_1,
280               "export_targets": TARGETS_1,
281               "name": vpn_name}
282     bgpvpn = os_utils.update_bgpvpn(neutron_client, bgpvpn_id, **kwargs)
283
284     logger.info("Waiting for the VMs to connect to each other using the"
285                 " updated network configuration")
286     time.sleep(30)
287
288     # Ping from VM1 to VM4 should work
289     results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
290                             expected="PASS", timeout=30)
291     # Ping from VM1 to VM5 should work
292     results.get_ping_status(vm_1, vm_1_ip, vm_5, vm_5_ip,
293                             expected="PASS", timeout=30)
294
295     results.add_to_summary(0, "=")
296     logger.info("\n%s" % results.summary)
297
298     if results.test_result == "PASS":
299         logger.info("All the ping tests have passed as expected.")
300     else:
301         logger.info("One or more ping tests have failed.")
302
303     status = "PASS"
304     success = 100 - \
305         (100 * int(results.num_tests_failed) / int(results.num_tests_failed))
306     if success < int(SUCCESS_CRITERIA):
307         status = "FAILED"
308
309     return {"status": status, "details": results.details}
310
311
312 if __name__ == '__main__':
313     main()