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