e1b68631781f75a0ee785e7e241d175669d91422
[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 from random import randint
13 import sys
14
15 import functest.utils.functest_logger as ft_logger
16 import functest.utils.openstack_utils as os_utils
17
18 import utils as test_utils
19 from results import Results
20 import config as sdnvpn_config
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-2").getLogger()
31
32 COMMON_CONFIG = sdnvpn_config.CommonConfig()
33 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig('testcase_2')
34
35
36 def main():
37     results = Results(COMMON_CONFIG.line_length)
38
39     results.add_to_summary(0, "=")
40     results.add_to_summary(2, "STATUS", "SUBTEST")
41     results.add_to_summary(0, "=")
42
43     nova_client = os_utils.get_nova_client()
44     neutron_client = os_utils.get_neutron_client()
45     glance_client = os_utils.get_glance_client()
46
47     logger.debug("Using private key %s injected to the VMs."
48                  % COMMON_CONFIG.keyfile_path)
49     keyfile = open(COMMON_CONFIG.keyfile_path, 'r')
50     key = keyfile.read()
51     keyfile.close()
52     files = {"/home/cirros/id_rsa": key}
53
54     image_id = os_utils.create_glance_image(glance_client,
55                                             TESTCASE_CONFIG.image_name,
56                                             COMMON_CONFIG.image_path,
57                                             disk=COMMON_CONFIG.image_format,
58                                             container="bare",
59                                             public=True)
60     network_1_id, _, _ = test_utils.create_network(
61         neutron_client,
62         TESTCASE_CONFIG.net_1_name,
63         TESTCASE_CONFIG.subnet_1a_name,
64         TESTCASE_CONFIG.subnet_1a_cidr,
65         TESTCASE_CONFIG.router_1_name,
66         TESTCASE_CONFIG.subnet_1b_name,
67         TESTCASE_CONFIG.subnet_1b_cidr)
68
69     network_2_id, _, _ = test_utils.create_network(
70         neutron_client,
71         TESTCASE_CONFIG.net_2_name,
72         TESTCASE_CONFIG.subnet_2a_name,
73         TESTCASE_CONFIG.subnet_2a_cidr,
74         TESTCASE_CONFIG.router_2_name,
75         TESTCASE_CONFIG.subnet_2b_name,
76         TESTCASE_CONFIG.subnet_2b_cidr)
77
78     sg_id = os_utils.create_security_group_full(neutron_client,
79                                                 TESTCASE_CONFIG.secgroup_name,
80                                                 TESTCASE_CONFIG.secgroup_descr)
81
82     compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
83
84     av_zone_1 = "nova:" + compute_nodes[0]
85     av_zone_2 = "nova:" + compute_nodes[1]
86
87     # boot INTANCES
88     userdata_common = test_utils.generate_userdata_common()
89     vm_2 = test_utils.create_instance(
90         nova_client,
91         TESTCASE_CONFIG.instance_2_name,
92         image_id,
93         network_1_id,
94         sg_id,
95         fixed_ip=TESTCASE_CONFIG.instance_2_ip,
96         secgroup_name=TESTCASE_CONFIG.secgroup_name,
97         compute_node=av_zone_1,
98         userdata=userdata_common)
99     vm_2_ip = vm_2.networks.itervalues().next()[0]
100     logger.debug("Instance '%s' booted successfully. IP='%s'." %
101                  (TESTCASE_CONFIG.instance_2_name, vm_2_ip))
102
103     vm_3 = test_utils.create_instance(
104         nova_client,
105         TESTCASE_CONFIG.instance_3_name,
106         image_id,
107         network_1_id,
108         sg_id,
109         fixed_ip=TESTCASE_CONFIG.instance_3_ip,
110         secgroup_name=TESTCASE_CONFIG.secgroup_name,
111         compute_node=av_zone_2,
112         userdata=userdata_common)
113     vm_3_ip = vm_3.networks.itervalues().next()[0]
114     logger.debug("Instance '%s' booted successfully. IP='%s'." %
115                  (TESTCASE_CONFIG.instance_3_name, vm_3_ip))
116
117     vm_5 = test_utils.create_instance(
118         nova_client,
119         TESTCASE_CONFIG.instance_5_name,
120         image_id,
121         network_2_id,
122         sg_id,
123         fixed_ip=TESTCASE_CONFIG.instance_5_ip,
124         secgroup_name=TESTCASE_CONFIG.secgroup_name,
125         compute_node=av_zone_2,
126         userdata=userdata_common)
127     vm_5_ip = vm_5.networks.itervalues().next()[0]
128     logger.debug("Instance '%s' booted successfully. IP='%s'." %
129                  (TESTCASE_CONFIG.instance_5_name, vm_5_ip))
130
131     # We boot vm5 first because we need vm5_ip for vm4 userdata
132     u4 = test_utils.generate_userdata_with_ssh(
133         [TESTCASE_CONFIG.instance_1_ip,
134          TESTCASE_CONFIG.instance_3_ip,
135          TESTCASE_CONFIG.instance_5_ip])
136     vm_4 = test_utils.create_instance(
137         nova_client,
138         TESTCASE_CONFIG.instance_4_name,
139         image_id,
140         network_2_id,
141         sg_id,
142         fixed_ip=TESTCASE_CONFIG.instance_4_ip,
143         secgroup_name=TESTCASE_CONFIG.secgroup_name,
144         compute_node=av_zone_1,
145         userdata=u4,
146         files=files)
147     vm_4_ip = vm_4.networks.itervalues().next()[0]
148     logger.debug("Instance '%s' booted successfully. IP='%s'." %
149                  (TESTCASE_CONFIG.instance_4_name, vm_4_ip))
150
151     # We boot VM1 at the end because we need to get the IPs first to generate
152     # the userdata
153     u1 = test_utils.generate_userdata_with_ssh(
154         [TESTCASE_CONFIG.instance_2_ip,
155          TESTCASE_CONFIG.instance_3_ip,
156          TESTCASE_CONFIG.instance_4_ip,
157          TESTCASE_CONFIG.instance_5_ip])
158     vm_1 = test_utils.create_instance(
159         nova_client,
160         TESTCASE_CONFIG.instance_1_name,
161         image_id,
162         network_1_id,
163         sg_id,
164         fixed_ip=TESTCASE_CONFIG.instance_1_ip,
165         secgroup_name=TESTCASE_CONFIG.secgroup_name,
166         compute_node=av_zone_1,
167         userdata=u1,
168         files=files)
169     vm_1_ip = vm_1.networks.itervalues().next()[0]
170     logger.debug("Instance '%s' booted successfully. IP='%s'." %
171                  (TESTCASE_CONFIG.instance_1_name, vm_1_ip))
172
173     msg = ("Create VPN1 with eRT=iRT")
174     logger.info(msg)
175     results.add_to_summary(1, msg)
176     vpn1_name = "sdnvpn-1-" + str(randint(100000, 999999))
177     kwargs = {"import_targets": TESTCASE_CONFIG.targets2,
178               "export_targets": TESTCASE_CONFIG.targets2,
179               "route_targets": TESTCASE_CONFIG.targets2,
180               "name": vpn1_name}
181     bgpvpn1 = os_utils.create_bgpvpn(neutron_client, **kwargs)
182     bgpvpn1_id = bgpvpn1['bgpvpn']['id']
183     logger.debug("VPN1 created details: %s" % bgpvpn1)
184
185     msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
186     logger.info(msg)
187     results.add_to_summary(1, msg)
188     results.add_to_summary(0, "-")
189
190     os_utils.create_network_association(
191         neutron_client, bgpvpn1_id, network_1_id)
192
193     # Wait for VMs to get ips.
194     instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
195                                                     vm_3, vm_4,
196                                                     vm_5)
197
198     if not instances_up:
199         logger.error("One or more instances is down")
200         sys.exit(-1)
201
202     logger.info("Waiting for the VMs to connect to each other using the"
203                 " updated network configuration")
204     test_utils.wait_before_subtest()
205
206     # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
207     results.check_ssh_output(
208         vm_1, vm_1_ip,
209         vm_2, vm_2_ip,
210         expected=TESTCASE_CONFIG.instance_2_name,
211         timeout=200)
212     # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
213     results.check_ssh_output(
214         vm_1, vm_1_ip,
215         vm_3, vm_3_ip,
216         expected=TESTCASE_CONFIG.instance_3_name,
217         timeout=30)
218
219     results.add_to_summary(0, "-")
220     msg = ("Create VPN2 with eRT=iRT")
221     logger.info(msg)
222     results.add_to_summary(1, msg)
223     vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
224     kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
225               "export_targets": TESTCASE_CONFIG.targets1,
226               "route_targets": TESTCASE_CONFIG.targets1,
227               "name": vpn2_name}
228     bgpvpn2 = os_utils.create_bgpvpn(neutron_client, **kwargs)
229     bgpvpn2_id = bgpvpn2['bgpvpn']['id']
230     logger.debug("VPN created details: %s" % bgpvpn2)
231
232     msg = ("Associate network '%s' to the VPN2." % TESTCASE_CONFIG.net_2_name)
233     logger.info(msg)
234     results.add_to_summary(1, msg)
235     results.add_to_summary(0, "-")
236
237     os_utils.create_network_association(
238         neutron_client, bgpvpn2_id, network_2_id)
239
240     test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn1_id, network_1_id)
241     test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn2_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     # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
248     results.check_ssh_output(
249         vm_4, vm_4_ip,
250         vm_5, vm_5_ip,
251         expected=TESTCASE_CONFIG.instance_5_name,
252         timeout=30)
253
254     # 10.10.10.11 should return "not reachable" to sdnvpn-4
255     results.check_ssh_output(
256         vm_4, vm_4_ip, vm_1, vm_1_ip, expected="not reachable", timeout=30)
257
258     return results.compile_summary(TESTCASE_CONFIG.success_criteria)
259
260
261 if __name__ == '__main__':
262     main()