Fix incompatibility with new tempest functest API
[sdnvpn.git] / sdnvpn / 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 sdnvpn.lib.utils as test_utils
19 from sdnvpn.lib.results import Results
20 import sdnvpn.lib.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='public')
60     network_1_id = test_utils.create_net(
61         neutron_client,
62         TESTCASE_CONFIG.net_1_name)
63     test_utils.create_subnet(
64         neutron_client,
65         TESTCASE_CONFIG.subnet_1a_name,
66         TESTCASE_CONFIG.subnet_1a_cidr,
67         network_1_id)
68     test_utils.create_subnet(
69         neutron_client,
70         TESTCASE_CONFIG.subnet_1b_name,
71         TESTCASE_CONFIG.subnet_1b_cidr,
72         network_1_id)
73
74     network_2_id = test_utils.create_net(
75         neutron_client,
76         TESTCASE_CONFIG.net_2_name)
77     test_utils.create_subnet(
78         neutron_client,
79         TESTCASE_CONFIG.subnet_2a_name,
80         TESTCASE_CONFIG.subnet_2a_cidr,
81         network_2_id)
82     test_utils.create_subnet(
83         neutron_client,
84         TESTCASE_CONFIG.subnet_2b_name,
85         TESTCASE_CONFIG.subnet_2b_cidr,
86         network_2_id)
87
88     sg_id = os_utils.create_security_group_full(neutron_client,
89                                                 TESTCASE_CONFIG.secgroup_name,
90                                                 TESTCASE_CONFIG.secgroup_descr)
91
92     compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
93
94     av_zone_1 = "nova:" + compute_nodes[0]
95     av_zone_2 = "nova:" + compute_nodes[1]
96
97     # boot INTANCES
98     userdata_common = test_utils.generate_userdata_common()
99     vm_2 = test_utils.create_instance(
100         nova_client,
101         TESTCASE_CONFIG.instance_2_name,
102         image_id,
103         network_1_id,
104         sg_id,
105         fixed_ip=TESTCASE_CONFIG.instance_2_ip,
106         secgroup_name=TESTCASE_CONFIG.secgroup_name,
107         compute_node=av_zone_1,
108         userdata=userdata_common)
109
110     vm_3 = test_utils.create_instance(
111         nova_client,
112         TESTCASE_CONFIG.instance_3_name,
113         image_id,
114         network_1_id,
115         sg_id,
116         fixed_ip=TESTCASE_CONFIG.instance_3_ip,
117         secgroup_name=TESTCASE_CONFIG.secgroup_name,
118         compute_node=av_zone_2,
119         userdata=userdata_common)
120
121     vm_5 = test_utils.create_instance(
122         nova_client,
123         TESTCASE_CONFIG.instance_5_name,
124         image_id,
125         network_2_id,
126         sg_id,
127         fixed_ip=TESTCASE_CONFIG.instance_5_ip,
128         secgroup_name=TESTCASE_CONFIG.secgroup_name,
129         compute_node=av_zone_2,
130         userdata=userdata_common)
131
132     # We boot vm5 first because we need vm5_ip for vm4 userdata
133     u4 = test_utils.generate_userdata_with_ssh(
134         [TESTCASE_CONFIG.instance_1_ip,
135          TESTCASE_CONFIG.instance_3_ip,
136          TESTCASE_CONFIG.instance_5_ip])
137     vm_4 = test_utils.create_instance(
138         nova_client,
139         TESTCASE_CONFIG.instance_4_name,
140         image_id,
141         network_2_id,
142         sg_id,
143         fixed_ip=TESTCASE_CONFIG.instance_4_ip,
144         secgroup_name=TESTCASE_CONFIG.secgroup_name,
145         compute_node=av_zone_1,
146         userdata=u4,
147         files=files)
148
149     # We boot VM1 at the end because we need to get the IPs first to generate
150     # the userdata
151     u1 = test_utils.generate_userdata_with_ssh(
152         [TESTCASE_CONFIG.instance_2_ip,
153          TESTCASE_CONFIG.instance_3_ip,
154          TESTCASE_CONFIG.instance_4_ip,
155          TESTCASE_CONFIG.instance_5_ip])
156     vm_1 = test_utils.create_instance(
157         nova_client,
158         TESTCASE_CONFIG.instance_1_name,
159         image_id,
160         network_1_id,
161         sg_id,
162         fixed_ip=TESTCASE_CONFIG.instance_1_ip,
163         secgroup_name=TESTCASE_CONFIG.secgroup_name,
164         compute_node=av_zone_1,
165         userdata=u1,
166         files=files)
167
168     msg = ("Create VPN1 with eRT=iRT")
169     results.record_action(msg)
170     vpn1_name = "sdnvpn-1-" + str(randint(100000, 999999))
171     kwargs = {"import_targets": TESTCASE_CONFIG.targets2,
172               "export_targets": TESTCASE_CONFIG.targets2,
173               "route_targets": TESTCASE_CONFIG.targets2,
174               "route_distinguishers": TESTCASE_CONFIG.route_distinguishers1,
175               "name": vpn1_name}
176     bgpvpn1 = os_utils.create_bgpvpn(neutron_client, **kwargs)
177     bgpvpn1_id = bgpvpn1['bgpvpn']['id']
178     logger.debug("VPN1 created details: %s" % bgpvpn1)
179
180     msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
181     results.record_action(msg)
182     results.add_to_summary(0, "-")
183
184     os_utils.create_network_association(
185         neutron_client, bgpvpn1_id, network_1_id)
186
187     # Wait for VMs to get ips.
188     instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
189                                                     vm_3, vm_4,
190                                                     vm_5)
191
192     if not instances_up:
193         logger.error("One or more instances is down")
194         sys.exit(-1)
195
196     logger.info("Waiting for the VMs to connect to each other using the"
197                 " updated network configuration")
198     test_utils.wait_before_subtest()
199
200     # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
201     results.check_ssh_output(vm_1, vm_2,
202                              expected=TESTCASE_CONFIG.instance_2_name,
203                              timeout=200)
204     # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
205     results.check_ssh_output(vm_1, vm_3,
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     results.record_action(msg)
212     vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
213     kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
214               "export_targets": TESTCASE_CONFIG.targets1,
215               "route_targets": TESTCASE_CONFIG.targets1,
216               "route_distinguishers": TESTCASE_CONFIG.route_distinguishers2,
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     results.record_action(msg)
224     results.add_to_summary(0, "-")
225
226     os_utils.create_network_association(
227         neutron_client, bgpvpn2_id, network_2_id)
228
229     test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn1_id, network_1_id)
230     test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn2_id, network_2_id)
231
232     logger.info("Waiting for the VMs to connect to each other using the"
233                 " updated network configuration")
234     test_utils.wait_before_subtest()
235
236     # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
237     results.check_ssh_output(vm_4, vm_5,
238                              expected=TESTCASE_CONFIG.instance_5_name,
239                              timeout=30)
240
241     # 10.10.10.11 should return "not reachable" to sdnvpn-4
242     results.check_ssh_output(vm_4, vm_1,
243                              expected="not reachable",
244                              timeout=30)
245
246     return results.compile_summary()
247
248
249 if __name__ == '__main__':
250     main()