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