Refactor config global variables
[sdnvpn.git] / test / functest / testcase_1.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
14 import functest.utils.functest_logger as ft_logger
15 import functest.utils.openstack_utils as os_utils
16
17 import utils as test_utils
18 import config as sdnvpn_config
19 from 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 = ft_logger.Logger("sdnvpn-testcase-1").getLogger()
30
31 COMMON_CONFIG = sdnvpn_config.CommonConfig()
32 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig('testcase_1')
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     image_id = os_utils.create_glance_image(glance_client,
47                                             TESTCASE_CONFIG.image_name,
48                                             COMMON_CONFIG.image_path,
49                                             disk=COMMON_CONFIG.image_format,
50                                             container="bare",
51                                             public=True)
52     network_1_id = test_utils.create_net(neutron_client,
53                                          TESTCASE_CONFIG.net_1_name)
54     test_utils.create_subnet(neutron_client,
55                              TESTCASE_CONFIG.subnet_1_name,
56                              TESTCASE_CONFIG.subnet_1_cidr,
57                              network_1_id)
58
59     network_2_id = test_utils.create_net(neutron_client,
60                                          TESTCASE_CONFIG.net_2_name)
61
62     test_utils.create_subnet(neutron_client,
63                              TESTCASE_CONFIG.subnet_2_name,
64                              TESTCASE_CONFIG.subnet_2_cidr,
65                              network_2_id)
66
67     sg_id = os_utils.create_security_group_full(neutron_client,
68                                                 TESTCASE_CONFIG.secgroup_name,
69                                                 TESTCASE_CONFIG.secgroup_descr)
70
71     compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
72
73     av_zone_1 = "nova:" + compute_nodes[0]
74     av_zone_2 = "nova:" + compute_nodes[1]
75
76     # boot INTANCES
77     vm_2 = test_utils.create_instance(
78         nova_client,
79         TESTCASE_CONFIG.instance_2_name,
80         image_id,
81         network_1_id,
82         sg_id,
83         secgroup_name=TESTCASE_CONFIG.secgroup_name,
84         compute_node=av_zone_1)
85     vm_2_ip = vm_2.networks.itervalues().next()[0]
86     logger.debug("Instance '%s' booted successfully. IP='%s'." %
87                  (TESTCASE_CONFIG.instance_2_name, vm_2_ip))
88
89     vm_3 = test_utils.create_instance(
90         nova_client,
91         TESTCASE_CONFIG.instance_3_name,
92         image_id,
93         network_1_id,
94         sg_id,
95         secgroup_name=TESTCASE_CONFIG.secgroup_name,
96         compute_node=av_zone_2)
97     vm_3_ip = vm_3.networks.itervalues().next()[0]
98     logger.debug("Instance '%s' booted successfully. IP='%s'." %
99                  (TESTCASE_CONFIG.instance_3_name, vm_3_ip))
100
101     vm_5 = test_utils.create_instance(
102         nova_client,
103         TESTCASE_CONFIG.instance_5_name,
104         image_id,
105         network_2_id,
106         sg_id,
107         secgroup_name=TESTCASE_CONFIG.secgroup_name,
108         compute_node=av_zone_2)
109     vm_5_ip = vm_5.networks.itervalues().next()[0]
110     logger.debug("Instance '%s' booted successfully. IP='%s'." %
111                  (TESTCASE_CONFIG.instance_5_name, vm_5_ip))
112
113     # We boot vm5 first because we need vm5_ip for vm4 userdata
114     u4 = test_utils.generate_ping_userdata([vm_5_ip])
115     vm_4 = test_utils.create_instance(
116         nova_client,
117         TESTCASE_CONFIG.instance_4_name,
118         image_id,
119         network_2_id,
120         sg_id,
121         secgroup_name=TESTCASE_CONFIG.secgroup_name,
122         compute_node=av_zone_1,
123         userdata=u4)
124     vm_4_ip = vm_4.networks.itervalues().next()[0]
125     logger.debug("Instance '%s' booted successfully. IP='%s'." %
126                  (TESTCASE_CONFIG.instance_4_name, vm_4_ip))
127
128     # We boot VM1 at the end because we need to get the IPs first to generate
129     # the userdata
130     u1 = test_utils.generate_ping_userdata([vm_2_ip,
131                                             vm_3_ip,
132                                             vm_4_ip,
133                                             vm_5_ip])
134     vm_1 = test_utils.create_instance(
135         nova_client,
136         TESTCASE_CONFIG.instance_1_name,
137         image_id,
138         network_1_id,
139         sg_id,
140         secgroup_name=TESTCASE_CONFIG.secgroup_name,
141         compute_node=av_zone_1,
142         userdata=u1)
143     vm_1_ip = vm_1.networks.itervalues().next()[0]
144     logger.debug("Instance '%s' booted successfully. IP='%s'." %
145                  (TESTCASE_CONFIG.instance_1_name, vm_1_ip))
146
147     msg = ("Create VPN with eRT<>iRT")
148     logger.info(msg)
149     results.add_to_summary(1, msg)
150     vpn_name = "sdnvpn-" + str(randint(100000, 999999))
151     kwargs = {
152         "import_targets": TESTCASE_CONFIG.targets1,
153         "export_targets": TESTCASE_CONFIG.targets2,
154         "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
155         "name": vpn_name
156     }
157     bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
158     bgpvpn_id = bgpvpn['bgpvpn']['id']
159     logger.debug("VPN created details: %s" % bgpvpn)
160
161     msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
162     logger.info(msg)
163     results.add_to_summary(1, msg)
164     results.add_to_summary(0, "-")
165
166     os_utils.create_network_association(
167         neutron_client, bgpvpn_id, network_1_id)
168
169     # Wait for VMs to get ips.
170     instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
171                                                     vm_3, vm_4,
172                                                     vm_5)
173
174     if not instances_up:
175         logger.error("One or more instances is down")
176         # TODO: Handle this appropriately
177
178     # Ping from VM1 to VM2 should work
179     results.get_ping_status(vm_1, vm_1_ip, vm_2, vm_2_ip,
180                             expected="PASS", timeout=200)
181     # Ping from VM1 to VM3 should work
182     results.get_ping_status(vm_1, vm_1_ip, vm_3, vm_3_ip,
183                             expected="PASS", timeout=30)
184     # Ping from VM1 to VM4 should not work
185     results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
186                             expected="FAIL", timeout=30)
187
188     msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_2_name)
189     logger.info(msg)
190     results.add_to_summary(0, "-")
191     results.add_to_summary(1, msg)
192     results.add_to_summary(0, "-")
193     os_utils.create_network_association(
194         neutron_client, bgpvpn_id, network_2_id)
195
196     test_utils.wait_for_bgp_net_assocs(neutron_client,
197                                        bgpvpn_id,
198                                        network_1_id,
199                                        network_2_id)
200
201     logger.info("Waiting for the VMs to connect to each other using the"
202                 " updated network configuration")
203     test_utils.wait_before_subtest()
204
205     # Ping from VM4 to VM5 should work
206     results.get_ping_status(vm_4, vm_4_ip, vm_5, vm_5_ip,
207                             expected="PASS", timeout=30)
208     # Ping from VM1 to VM4 should not work
209     results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
210                             expected="FAIL", timeout=30)
211     # Ping from VM1 to VM5 should not work
212     results.get_ping_status(vm_1, vm_1_ip, vm_5, vm_5_ip,
213                             expected="FAIL", timeout=30)
214
215     msg = ("Update VPN with eRT=iRT ...")
216     logger.info(msg)
217     results.add_to_summary(0, "-")
218     results.add_to_summary(1, msg)
219     results.add_to_summary(0, "-")
220     kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
221               "export_targets": TESTCASE_CONFIG.targets1,
222               "name": vpn_name}
223     bgpvpn = os_utils.update_bgpvpn(neutron_client, bgpvpn_id, **kwargs)
224
225     logger.info("Waiting for the VMs to connect to each other using the"
226                 " updated network configuration")
227     test_utils.wait_before_subtest()
228
229     # Ping from VM1 to VM4 should work
230     results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
231                             expected="PASS", timeout=30)
232     # Ping from VM1 to VM5 should work
233     results.get_ping_status(vm_1, vm_1_ip, vm_5, vm_5_ip,
234                             expected="PASS", timeout=30)
235
236     return results.compile_summary(TESTCASE_CONFIG.success_criteria)
237
238
239 if __name__ == '__main__':
240     main()