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