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
14 from functest.utils import openstack_utils as os_utils
15 from random import randint
16 from sdnvpn.lib import config as sdnvpn_config
17 from sdnvpn.lib import utils as test_utils
18 from sdnvpn.lib.results import Results
20 logger = logging.getLogger(__name__)
22 COMMON_CONFIG = sdnvpn_config.CommonConfig()
23 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
24 'sdnvpn.test.functest.testcase_2')
28 results = Results(COMMON_CONFIG.line_length)
30 results.add_to_summary(0, "=")
31 results.add_to_summary(2, "STATUS", "SUBTEST")
32 results.add_to_summary(0, "=")
34 nova_client = os_utils.get_nova_client()
35 neutron_client = os_utils.get_neutron_client()
36 glance_client = os_utils.get_glance_client()
38 (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
39 subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
42 logger.debug("Using private key %s injected to the VMs."
43 % COMMON_CONFIG.keyfile_path)
44 keyfile = open(COMMON_CONFIG.keyfile_path, 'r')
47 files = {"/home/cirros/id_rsa": key}
49 image_id = os_utils.create_glance_image(
50 glance_client, TESTCASE_CONFIG.image_name,
51 COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
52 container="bare", public='public')
53 image_ids.append(image_id)
55 network_1_id = test_utils.create_net(
57 TESTCASE_CONFIG.net_1_name)
58 subnet_1a_id = test_utils.create_subnet(
60 TESTCASE_CONFIG.subnet_1a_name,
61 TESTCASE_CONFIG.subnet_1a_cidr,
63 # TODO: uncomment the commented lines once ODL has
64 # support for mulitple subnets under same neutron network
65 # subnet_1b_id = test_utils.create_subnet(
67 # TESTCASE_CONFIG.subnet_1b_name,
68 # TESTCASE_CONFIG.subnet_1b_cidr,
71 network_2_id = test_utils.create_net(
73 TESTCASE_CONFIG.net_2_name)
74 # subnet_2a_id = test_utils.create_subnet(
76 # TESTCASE_CONFIG.subnet_2a_name,
77 # TESTCASE_CONFIG.subnet_2a_cidr,
79 subnet_2b_id = test_utils.create_subnet(
81 TESTCASE_CONFIG.subnet_2b_name,
82 TESTCASE_CONFIG.subnet_2b_cidr,
84 network_ids.extend([network_1_id, network_2_id])
85 subnet_ids.extend([subnet_1a_id,
90 sg_id = os_utils.create_security_group_full(
91 neutron_client, TESTCASE_CONFIG.secgroup_name,
92 TESTCASE_CONFIG.secgroup_descr)
94 compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
96 av_zone_1 = "nova:" + compute_nodes[0]
97 # av_zone_2 = "nova:" + compute_nodes[1]
100 userdata_common = test_utils.generate_userdata_common()
101 vm_2 = test_utils.create_instance(
103 TESTCASE_CONFIG.instance_2_name,
107 fixed_ip=TESTCASE_CONFIG.instance_2_ip,
108 secgroup_name=TESTCASE_CONFIG.secgroup_name,
109 compute_node=av_zone_1,
110 userdata=userdata_common)
113 # vm_3 = test_utils.create_instance(
115 # TESTCASE_CONFIG.instance_3_name,
119 # fixed_ip=TESTCASE_CONFIG.instance_3_ip,
120 # secgroup_name=TESTCASE_CONFIG.secgroup_name,
121 # compute_node=av_zone_2,
122 # userdata=userdata_common)
124 # vm_5 = test_utils.create_instance(
126 # TESTCASE_CONFIG.instance_5_name,
130 # fixed_ip=TESTCASE_CONFIG.instance_5_ip,
131 # secgroup_name=TESTCASE_CONFIG.secgroup_name,
132 # compute_node=av_zone_2,
133 # userdata=userdata_common)
135 # We boot vm5 first because we need vm5_ip for vm4 userdata
136 u4 = test_utils.generate_userdata_with_ssh(
137 [TESTCASE_CONFIG.instance_1_ip
138 # TESTCASE_CONFIG.instance_3_ip,
139 # TESTCASE_CONFIG.instance_5_ip
141 vm_4 = test_utils.create_instance(
143 TESTCASE_CONFIG.instance_4_name,
147 fixed_ip=TESTCASE_CONFIG.instance_4_ip,
148 secgroup_name=TESTCASE_CONFIG.secgroup_name,
149 compute_node=av_zone_1,
153 # We boot VM1 at the end because we need to get the IPs first
154 # to generate the userdata
155 u1 = test_utils.generate_userdata_with_ssh(
156 [TESTCASE_CONFIG.instance_2_ip,
157 # TESTCASE_CONFIG.instance_3_ip,
158 TESTCASE_CONFIG.instance_4_ip,
159 # TESTCASE_CONFIG.instance_5_ip
161 vm_1 = test_utils.create_instance(
163 TESTCASE_CONFIG.instance_1_name,
167 fixed_ip=TESTCASE_CONFIG.instance_1_ip,
168 secgroup_name=TESTCASE_CONFIG.secgroup_name,
169 compute_node=av_zone_1,
172 instance_ids.extend([vm_1.id,
179 msg = ("Create VPN1 with eRT=iRT")
180 results.record_action(msg)
181 vpn1_name = "sdnvpn-1-" + str(randint(100000, 999999))
183 "import_targets": TESTCASE_CONFIG.targets2,
184 "export_targets": TESTCASE_CONFIG.targets2,
185 "route_targets": TESTCASE_CONFIG.targets2,
186 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers1,
189 bgpvpn1 = test_utils.create_bgpvpn(neutron_client, **kwargs)
190 bgpvpn1_id = bgpvpn1['bgpvpn']['id']
191 logger.debug("VPN1 created details: %s" % bgpvpn1)
192 bgpvpn_ids.append(bgpvpn1_id)
194 msg = ("Associate network '%s' to the VPN." %
195 TESTCASE_CONFIG.net_1_name)
196 results.record_action(msg)
197 results.add_to_summary(0, "-")
199 test_utils.create_network_association(
200 neutron_client, bgpvpn1_id, network_1_id)
202 # Wait for VMs to get ips.
203 instances_up = test_utils.wait_for_instances_up(vm_2)
204 instances_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_1, vm_4)
206 if (not instances_up or not instances_dhcp_up):
207 logger.error("One or more instances are down")
208 # TODO: Handle this appropriately
210 logger.info("Waiting for the VMs to connect to each other using the"
211 " updated network configuration")
212 test_utils.wait_before_subtest()
214 # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
215 results.check_ssh_output(vm_1, vm_2,
216 expected=TESTCASE_CONFIG.instance_2_name,
218 # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
219 # results.check_ssh_output(vm_1, vm_3,
220 # expected=TESTCASE_CONFIG.instance_3_name,
223 results.add_to_summary(0, "-")
224 msg = ("Create VPN2 with eRT=iRT")
225 results.record_action(msg)
226 vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
228 "import_targets": TESTCASE_CONFIG.targets1,
229 "export_targets": TESTCASE_CONFIG.targets1,
230 "route_targets": TESTCASE_CONFIG.targets1,
231 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers2,
234 bgpvpn2 = test_utils.create_bgpvpn(neutron_client, **kwargs)
235 bgpvpn2_id = bgpvpn2['bgpvpn']['id']
236 logger.debug("VPN created details: %s" % bgpvpn2)
237 bgpvpn_ids.append(bgpvpn2_id)
239 msg = ("Associate network '%s' to the VPN2." %
240 TESTCASE_CONFIG.net_2_name)
241 results.record_action(msg)
242 results.add_to_summary(0, "-")
244 test_utils.create_network_association(
245 neutron_client, bgpvpn2_id, network_2_id)
247 test_utils.wait_for_bgp_net_assoc(neutron_client,
248 bgpvpn1_id, network_1_id)
249 test_utils.wait_for_bgp_net_assoc(neutron_client,
250 bgpvpn2_id, network_2_id)
252 logger.info("Waiting for the VMs to connect to each other using the"
253 " updated network configuration")
254 test_utils.wait_before_subtest()
256 # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
257 # results.check_ssh_output(vm_4, vm_5,
258 # expected=TESTCASE_CONFIG.instance_5_name,
261 # 10.10.10.11 should return "not reachable" to sdnvpn-4
262 results.check_ssh_output(vm_4, vm_1,
263 expected="not reachable",
266 except Exception as e:
267 logger.error("exception occurred while executing testcase_2: %s", e)
270 test_utils.cleanup_nova(nova_client, instance_ids, image_ids)
271 test_utils.cleanup_neutron(neutron_client, floatingip_ids,
272 bgpvpn_ids, interfaces, subnet_ids,
273 router_ids, network_ids)
275 return results.compile_summary()
278 if __name__ == '__main__':
279 logging.basicConfig(level=logging.INFO)