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
15 from random import randint
16 from sdnvpn.lib import config as sdnvpn_config
17 from sdnvpn.lib import openstack_utils as os_utils
18 from sdnvpn.lib import utils as test_utils
19 from sdnvpn.lib.results import Results
21 logger = logging.getLogger(__name__)
23 COMMON_CONFIG = sdnvpn_config.CommonConfig()
24 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
25 'sdnvpn.test.functest.testcase_13')
29 results = Results(COMMON_CONFIG.line_length)
31 results.add_to_summary(0, "=")
32 results.add_to_summary(2, "STATUS", "SUBTEST")
33 results.add_to_summary(0, "=")
35 if not os.path.isfile(COMMON_CONFIG.ubuntu_image_path):
36 logger.info("Downloading image")
37 image_dest_path = '/'.join(
38 COMMON_CONFIG.ubuntu_image_path.split('/')[:-1])
39 os_utils.download_url(
40 "http://artifacts.opnfv.org/sdnvpn/"
41 "ubuntu-16.04-server-cloudimg-amd64-disk1.img",
44 logger.info("Using old image")
46 nova_client = os_utils.get_nova_client()
47 neutron_client = os_utils.get_neutron_client()
48 conn = os_utils.get_os_connection()
50 (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
51 subnet_ids, interfaces, bgpvpn_ids, flavor_ids) = ([] for i in range(9))
54 image_id = os_utils.create_glance_image(
56 COMMON_CONFIG.ubuntu_image_name,
57 COMMON_CONFIG.ubuntu_image_path,
61 image_ids.append(image_id)
63 _, flavor_id = test_utils.create_custom_flavor()
64 flavor_ids.append(flavor_id)
66 network_1_id, subnet_1_id, router_1_id = test_utils.create_network(
68 TESTCASE_CONFIG.net_1_name,
69 TESTCASE_CONFIG.subnet_1_name,
70 TESTCASE_CONFIG.subnet_1_cidr,
71 TESTCASE_CONFIG.router_1_name)
73 interfaces.append(tuple((router_1_id, subnet_1_id)))
74 network_ids.extend([network_1_id])
75 subnet_ids.extend([subnet_1_id])
76 router_ids.extend([router_1_id])
78 sg_id = os_utils.create_security_group_full(
79 neutron_client, TESTCASE_CONFIG.secgroup_name,
80 TESTCASE_CONFIG.secgroup_descr)
82 compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
84 av_zone_1 = "nova:" + compute_nodes[0]
85 av_zone_2 = "nova:" + compute_nodes[1]
87 u1 = test_utils.generate_userdata_interface_create(
88 TESTCASE_CONFIG.interface_name,
89 TESTCASE_CONFIG.interface_number,
90 TESTCASE_CONFIG.extra_route_ip,
91 TESTCASE_CONFIG.extra_route_subnet_mask)
93 vm_1 = test_utils.create_instance(
95 TESTCASE_CONFIG.instance_1_name,
99 flavor=COMMON_CONFIG.custom_flavor_name,
100 secgroup_name=TESTCASE_CONFIG.secgroup_name,
101 compute_node=av_zone_1,
103 vm_1_ip = test_utils.get_instance_ip(vm_1)
105 vm1_port = test_utils.get_port(neutron_client, vm_1.id)
106 test_utils.update_port_allowed_address_pairs(
109 [test_utils.AllowedAddressPair(
110 TESTCASE_CONFIG.extra_route_cidr,
111 vm1_port['mac_address'])])
113 vm_2 = test_utils.create_instance(
115 TESTCASE_CONFIG.instance_2_name,
119 flavor=COMMON_CONFIG.custom_flavor_name,
120 secgroup_name=TESTCASE_CONFIG.secgroup_name,
121 compute_node=av_zone_1,
123 vm_2_ip = test_utils.get_instance_ip(vm_2)
125 vm2_port = test_utils.get_port(neutron_client, vm_2.id)
126 test_utils.update_port_allowed_address_pairs(
129 [test_utils.AllowedAddressPair(
130 TESTCASE_CONFIG.extra_route_cidr,
131 vm2_port['mac_address'])])
133 test_utils.async_Wait_for_instances([vm_1, vm_2])
135 image_2_id = os_utils.create_glance_image(
136 conn, TESTCASE_CONFIG.image_name,
137 COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
138 container="bare", public='public')
139 image_ids.append(image_2_id)
140 # Moved vm_3 creation before associating its network/router with
141 # bgpvpn. If VM is created after its network is associated to bgpvpn
142 # via router, then BGPVPN in ODL uses router's vrf id for newly created
143 # VMs which causes testcase to fail.
144 u3 = test_utils.generate_ping_userdata(
145 [TESTCASE_CONFIG.extra_route_ip])
146 vm_3 = test_utils.create_instance(
148 TESTCASE_CONFIG.instance_3_name,
152 flavor=COMMON_CONFIG.custom_flavor_name,
153 secgroup_name=TESTCASE_CONFIG.secgroup_name,
154 compute_node=av_zone_2,
157 instance_ids.extend([vm_1.id, vm_2.id, vm_3.id])
159 instance_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_3)
161 if (not instance_dhcp_up):
162 logger.error("vm_3 instance is down")
164 msg = ("Create VPN with multiple RDs")
165 results.record_action(msg)
166 vpn_name = "sdnvpn-" + str(randint(100000, 999999))
168 "import_targets": TESTCASE_CONFIG.targets1,
169 "export_targets": TESTCASE_CONFIG.targets2,
170 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
173 bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
174 bgpvpn_id = bgpvpn['bgpvpn']['id']
175 logger.debug("VPN created details: %s" % bgpvpn)
176 bgpvpn_ids.append(bgpvpn_id)
178 msg = ("Associate router '%s' to the VPN." %
179 TESTCASE_CONFIG.router_1_name)
180 results.record_action(msg)
181 results.add_to_summary(0, "-")
183 test_utils.create_router_association(
184 neutron_client, bgpvpn_id, router_1_id)
186 test_utils.update_router_extra_route(
187 neutron_client, router_1_id,
188 [test_utils.ExtraRoute(TESTCASE_CONFIG.extra_route_cidr,
190 test_utils.ExtraRoute(TESTCASE_CONFIG.extra_route_cidr,
193 logger.info("Waiting for the VMs to connect to each other using the"
194 " updated network configuration")
195 test_utils.wait_before_subtest()
197 results.get_ping_status_target_ip(vm_3,
198 TESTCASE_CONFIG.extra_route_name,
199 TESTCASE_CONFIG.extra_route_ip,
203 results.add_to_summary(0, "=")
204 logger.info("\n%s" % results.summary)
206 except Exception as e:
207 logger.error("exception occurred while executing testcase_13: %s", e)
210 test_utils.update_router_no_extra_route(neutron_client, router_ids)
211 test_utils.cleanup_nova(nova_client, instance_ids, flavor_ids)
212 test_utils.cleanup_glance(conn, image_ids)
213 test_utils.cleanup_neutron(neutron_client, floatingip_ids,
214 bgpvpn_ids, interfaces, subnet_ids,
215 router_ids, network_ids)
217 return results.compile_summary()
220 if __name__ == '__main__':