Move logging instance creation to create_instance
[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
101     vm_3 = test_utils.create_instance(
102         nova_client,
103         TESTCASE_CONFIG.instance_3_name,
104         image_id,
105         network_1_id,
106         sg_id,
107         fixed_ip=TESTCASE_CONFIG.instance_3_ip,
108         secgroup_name=TESTCASE_CONFIG.secgroup_name,
109         compute_node=av_zone_2,
110         userdata=userdata_common)
111     vm_3_ip = vm_3.networks.itervalues().next()[0]
112
113     vm_5 = test_utils.create_instance(
114         nova_client,
115         TESTCASE_CONFIG.instance_5_name,
116         image_id,
117         network_2_id,
118         sg_id,
119         fixed_ip=TESTCASE_CONFIG.instance_5_ip,
120         secgroup_name=TESTCASE_CONFIG.secgroup_name,
121         compute_node=av_zone_2,
122         userdata=userdata_common)
123     vm_5_ip = vm_5.networks.itervalues().next()[0]
124
125     # We boot vm5 first because we need vm5_ip for vm4 userdata
126     u4 = test_utils.generate_userdata_with_ssh(
127         [TESTCASE_CONFIG.instance_1_ip,
128          TESTCASE_CONFIG.instance_3_ip,
129          TESTCASE_CONFIG.instance_5_ip])
130     vm_4 = test_utils.create_instance(
131         nova_client,
132         TESTCASE_CONFIG.instance_4_name,
133         image_id,
134         network_2_id,
135         sg_id,
136         fixed_ip=TESTCASE_CONFIG.instance_4_ip,
137         secgroup_name=TESTCASE_CONFIG.secgroup_name,
138         compute_node=av_zone_1,
139         userdata=u4,
140         files=files)
141     vm_4_ip = vm_4.networks.itervalues().next()[0]
142
143     # We boot VM1 at the end because we need to get the IPs first to generate
144     # the userdata
145     u1 = test_utils.generate_userdata_with_ssh(
146         [TESTCASE_CONFIG.instance_2_ip,
147          TESTCASE_CONFIG.instance_3_ip,
148          TESTCASE_CONFIG.instance_4_ip,
149          TESTCASE_CONFIG.instance_5_ip])
150     vm_1 = test_utils.create_instance(
151         nova_client,
152         TESTCASE_CONFIG.instance_1_name,
153         image_id,
154         network_1_id,
155         sg_id,
156         fixed_ip=TESTCASE_CONFIG.instance_1_ip,
157         secgroup_name=TESTCASE_CONFIG.secgroup_name,
158         compute_node=av_zone_1,
159         userdata=u1,
160         files=files)
161     vm_1_ip = vm_1.networks.itervalues().next()[0]
162
163     msg = ("Create VPN1 with eRT=iRT")
164     logger.info(msg)
165     results.add_to_summary(1, msg)
166     vpn1_name = "sdnvpn-1-" + str(randint(100000, 999999))
167     kwargs = {"import_targets": TESTCASE_CONFIG.targets2,
168               "export_targets": TESTCASE_CONFIG.targets2,
169               "route_targets": TESTCASE_CONFIG.targets2,
170               "name": vpn1_name}
171     bgpvpn1 = os_utils.create_bgpvpn(neutron_client, **kwargs)
172     bgpvpn1_id = bgpvpn1['bgpvpn']['id']
173     logger.debug("VPN1 created details: %s" % bgpvpn1)
174
175     msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
176     logger.info(msg)
177     results.add_to_summary(1, msg)
178     results.add_to_summary(0, "-")
179
180     os_utils.create_network_association(
181         neutron_client, bgpvpn1_id, network_1_id)
182
183     # Wait for VMs to get ips.
184     instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
185                                                     vm_3, vm_4,
186                                                     vm_5)
187
188     if not instances_up:
189         logger.error("One or more instances is down")
190         sys.exit(-1)
191
192     logger.info("Waiting for the VMs to connect to each other using the"
193                 " updated network configuration")
194     test_utils.wait_before_subtest()
195
196     # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
197     results.check_ssh_output(
198         vm_1, vm_1_ip,
199         vm_2, vm_2_ip,
200         expected=TESTCASE_CONFIG.instance_2_name,
201         timeout=200)
202     # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
203     results.check_ssh_output(
204         vm_1, vm_1_ip,
205         vm_3, vm_3_ip,
206         expected=TESTCASE_CONFIG.instance_3_name,
207         timeout=30)
208
209     results.add_to_summary(0, "-")
210     msg = ("Create VPN2 with eRT=iRT")
211     logger.info(msg)
212     results.add_to_summary(1, msg)
213     vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
214     kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
215               "export_targets": TESTCASE_CONFIG.targets1,
216               "route_targets": TESTCASE_CONFIG.targets1,
217               "name": vpn2_name}
218     bgpvpn2 = os_utils.create_bgpvpn(neutron_client, **kwargs)
219     bgpvpn2_id = bgpvpn2['bgpvpn']['id']
220     logger.debug("VPN created details: %s" % bgpvpn2)
221
222     msg = ("Associate network '%s' to the VPN2." % TESTCASE_CONFIG.net_2_name)
223     logger.info(msg)
224     results.add_to_summary(1, msg)
225     results.add_to_summary(0, "-")
226
227     os_utils.create_network_association(
228         neutron_client, bgpvpn2_id, network_2_id)
229
230     test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn1_id, network_1_id)
231     test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn2_id, network_2_id)
232
233     logger.info("Waiting for the VMs to connect to each other using the"
234                 " updated network configuration")
235     test_utils.wait_before_subtest()
236
237     # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
238     results.check_ssh_output(
239         vm_4, vm_4_ip,
240         vm_5, vm_5_ip,
241         expected=TESTCASE_CONFIG.instance_5_name,
242         timeout=30)
243
244     # 10.10.10.11 should return "not reachable" to sdnvpn-4
245     results.check_ssh_output(
246         vm_4, vm_4_ip, vm_1, vm_1_ip, expected="not reachable", timeout=30)
247
248     return results.compile_summary(TESTCASE_CONFIG.success_criteria)
249
250
251 if __name__ == '__main__':
252     main()