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
8 # http://www.apache.org/licenses/LICENSE-2.0
13 from random import randint
15 import functest.utils.openstack_utils as os_utils
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
21 parser = argparse.ArgumentParser()
23 parser.add_argument("-r", "--report",
24 help="Create json result file",
27 args = parser.parse_args()
29 logger = logging.getLogger('sdnvpn-testcase-1')
31 COMMON_CONFIG = sdnvpn_config.CommonConfig()
32 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig('testcase_1')
36 results = Results(COMMON_CONFIG.line_length)
38 results.add_to_summary(0, "=")
39 results.add_to_summary(2, "STATUS", "SUBTEST")
40 results.add_to_summary(0, "=")
42 nova_client = os_utils.get_nova_client()
43 neutron_client = os_utils.get_neutron_client()
44 glance_client = os_utils.get_glance_client()
46 (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
47 subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
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,
55 image_ids.append(image_id)
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,
64 network_2_id = test_utils.create_net(neutron_client,
65 TESTCASE_CONFIG.net_2_name)
67 subnet_2_id = test_utils.create_subnet(neutron_client,
68 TESTCASE_CONFIG.subnet_2_name,
69 TESTCASE_CONFIG.subnet_2_cidr,
71 network_ids.extend([network_1_id, network_2_id])
72 subnet_ids.extend([subnet_1_id, subnet_2_id])
74 sg_id = os_utils.create_security_group_full(neutron_client,
75 TESTCASE_CONFIG.secgroup_name,
76 TESTCASE_CONFIG.secgroup_descr)
78 compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
80 av_zone_1 = "nova:" + compute_nodes[0]
81 av_zone_2 = "nova:" + compute_nodes[1]
84 vm_2 = test_utils.create_instance(
86 TESTCASE_CONFIG.instance_2_name,
90 secgroup_name=TESTCASE_CONFIG.secgroup_name,
91 compute_node=av_zone_1)
92 vm_2_ip = test_utils.get_instance_ip(vm_2)
94 vm_3 = test_utils.create_instance(
96 TESTCASE_CONFIG.instance_3_name,
100 secgroup_name=TESTCASE_CONFIG.secgroup_name,
101 compute_node=av_zone_2)
102 vm_3_ip = test_utils.get_instance_ip(vm_3)
104 vm_5 = test_utils.create_instance(
106 TESTCASE_CONFIG.instance_5_name,
110 secgroup_name=TESTCASE_CONFIG.secgroup_name,
111 compute_node=av_zone_2)
112 vm_5_ip = test_utils.get_instance_ip(vm_5)
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(
118 TESTCASE_CONFIG.instance_4_name,
122 secgroup_name=TESTCASE_CONFIG.secgroup_name,
123 compute_node=av_zone_1,
125 vm_4_ip = test_utils.get_instance_ip(vm_4)
127 # We boot VM1 at the end because we need to get the IPs first to generate
129 u1 = test_utils.generate_ping_userdata([vm_2_ip,
133 vm_1 = test_utils.create_instance(
135 TESTCASE_CONFIG.instance_1_name,
139 secgroup_name=TESTCASE_CONFIG.secgroup_name,
140 compute_node=av_zone_1,
142 instance_ids.extend([vm_1.id, vm_2.id, vm_3.id, vm_4.id, vm_5.id])
144 msg = ("Create VPN with eRT<>iRT")
145 results.record_action(msg)
146 vpn_name = "sdnvpn-" + str(randint(100000, 999999))
148 "import_targets": TESTCASE_CONFIG.targets1,
149 "export_targets": TESTCASE_CONFIG.targets2,
150 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
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)
158 msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
159 results.record_action(msg)
160 results.add_to_summary(0, "-")
162 os_utils.create_network_association(
163 neutron_client, bgpvpn_id, network_1_id)
165 # Wait for VMs to get ips.
166 instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
171 logger.error("One or more instances is down")
172 # TODO: Handle this appropriately
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)
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)
185 test_utils.wait_for_bgp_net_assocs(neutron_client,
190 logger.info("Waiting for the VMs to connect to each other using the"
191 " updated network configuration")
192 test_utils.wait_before_subtest()
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)
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,
206 bgpvpn = os_utils.update_bgpvpn(neutron_client, bgpvpn_id, **kwargs)
208 logger.info("Waiting for the VMs to connect to each other using the"
209 " updated network configuration")
210 test_utils.wait_before_subtest()
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)
215 test_utils.cleanup_nova(nova_client, floatingip_ids, instance_ids,
217 test_utils.cleanup_neutron(neutron_client, bgpvpn_ids, interfaces,
218 subnet_ids, router_ids, network_ids)
220 return results.compile_summary()
223 if __name__ == '__main__':
224 logging.basicConfig(level=logging.INFO)