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