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