9905cf3d64482a3ff7a4d1cdbe97fc9c588f0c20
[sdnvpn.git] / sdnvpn / test / functest / testcase_13.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 logging
12 import sys
13
14 from random import randint
15 from sdnvpn.lib import config as sdnvpn_config
16 from sdnvpn.lib import openstack_utils as os_utils
17 from sdnvpn.lib import utils as test_utils
18 from sdnvpn.lib.results import Results
19
20 logger = logging.getLogger(__name__)
21
22 COMMON_CONFIG = sdnvpn_config.CommonConfig()
23 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
24     'sdnvpn.test.functest.testcase_13')
25
26
27 def main():
28     results = Results(COMMON_CONFIG.line_length)
29
30     results.add_to_summary(0, "=")
31     results.add_to_summary(2, "STATUS", "SUBTEST")
32     results.add_to_summary(0, "=")
33
34     nova_client = os_utils.get_nova_client()
35     neutron_client = os_utils.get_neutron_client()
36     glance_client = os_utils.get_glance_client()
37
38     (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
39      subnet_ids, interfaces, bgpvpn_ids, flavor_ids) = ([] for i in range(9))
40
41     try:
42         image_id = os_utils.create_glance_image(
43             glance_client,
44             COMMON_CONFIG.ubuntu_image_name,
45             COMMON_CONFIG.ubuntu_image_path,
46             disk="qcow2",
47             container="bare",
48             public="public")
49         image_ids.append(image_id)
50
51         _, flavor_id = test_utils.create_custom_flavor()
52         flavor_ids.append(flavor_id)
53
54         network_1_id, subnet_1_id, router_1_id = test_utils.create_network(
55             neutron_client,
56             TESTCASE_CONFIG.net_1_name,
57             TESTCASE_CONFIG.subnet_1_name,
58             TESTCASE_CONFIG.subnet_1_cidr,
59             TESTCASE_CONFIG.router_1_name)
60
61         interfaces.append(tuple((router_1_id, subnet_1_id)))
62         network_ids.extend([network_1_id])
63         subnet_ids.extend([subnet_1_id])
64         router_ids.extend([router_1_id])
65
66         sg_id = os_utils.create_security_group_full(
67             neutron_client, TESTCASE_CONFIG.secgroup_name,
68             TESTCASE_CONFIG.secgroup_descr)
69
70         compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
71
72         av_zone_1 = "nova:" + compute_nodes[0]
73         av_zone_2 = "nova:" + compute_nodes[1]
74
75         u1 = test_utils.generate_userdata_interface_create(
76             TESTCASE_CONFIG.interface_name,
77             TESTCASE_CONFIG.interface_number,
78             TESTCASE_CONFIG.extra_route_ip,
79             TESTCASE_CONFIG.extra_route_subnet_mask)
80         # boot INTANCES
81         vm_1 = test_utils.create_instance(
82             nova_client,
83             TESTCASE_CONFIG.instance_1_name,
84             image_id,
85             network_1_id,
86             sg_id,
87             flavor=COMMON_CONFIG.custom_flavor_name,
88             secgroup_name=TESTCASE_CONFIG.secgroup_name,
89             compute_node=av_zone_1,
90             userdata=u1)
91         vm_1_ip = test_utils.get_instance_ip(vm_1)
92
93         vm1_port = test_utils.get_port(neutron_client, vm_1.id)
94         test_utils.update_port_allowed_address_pairs(
95             neutron_client,
96             vm1_port['id'],
97             [test_utils.AllowedAddressPair(
98                 TESTCASE_CONFIG.extra_route_cidr,
99                 vm1_port['mac_address'])])
100
101         vm_2 = test_utils.create_instance(
102             nova_client,
103             TESTCASE_CONFIG.instance_2_name,
104             image_id,
105             network_1_id,
106             sg_id,
107             flavor=COMMON_CONFIG.custom_flavor_name,
108             secgroup_name=TESTCASE_CONFIG.secgroup_name,
109             compute_node=av_zone_1,
110             userdata=u1)
111         vm_2_ip = test_utils.get_instance_ip(vm_2)
112
113         vm2_port = test_utils.get_port(neutron_client, vm_2.id)
114         test_utils.update_port_allowed_address_pairs(
115             neutron_client,
116             vm2_port['id'],
117             [test_utils.AllowedAddressPair(
118                 TESTCASE_CONFIG.extra_route_cidr,
119                 vm2_port['mac_address'])])
120
121         test_utils.async_Wait_for_instances([vm_1, vm_2])
122
123         image_2_id = os_utils.create_glance_image(
124             glance_client, TESTCASE_CONFIG.image_name,
125             COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
126             container="bare", public='public')
127         image_ids.append(image_2_id)
128         # Moved vm_3 creation before associating its network/router with
129         # bgpvpn. If VM is created after its network is associated to bgpvpn
130         # via router, then BGPVPN in ODL uses router's vrf id for newly created
131         # VMs which causes testcase to fail.
132         u3 = test_utils.generate_ping_userdata(
133             [TESTCASE_CONFIG.extra_route_ip])
134         vm_3 = test_utils.create_instance(
135             nova_client,
136             TESTCASE_CONFIG.instance_3_name,
137             image_2_id,
138             network_1_id,
139             sg_id,
140             flavor=COMMON_CONFIG.custom_flavor_name,
141             secgroup_name=TESTCASE_CONFIG.secgroup_name,
142             compute_node=av_zone_2,
143             userdata=u3)
144
145         instance_ids.extend([vm_1.id, vm_2.id, vm_3.id])
146
147         instance_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_3)
148
149         if (not instance_dhcp_up):
150             logger.error("vm_3 instance is down")
151
152         msg = ("Create VPN with multiple RDs")
153         results.record_action(msg)
154         vpn_name = "sdnvpn-" + str(randint(100000, 999999))
155         kwargs = {
156             "import_targets": TESTCASE_CONFIG.targets1,
157             "export_targets": TESTCASE_CONFIG.targets2,
158             "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
159             "name": vpn_name
160         }
161         bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
162         bgpvpn_id = bgpvpn['bgpvpn']['id']
163         logger.debug("VPN created details: %s" % bgpvpn)
164         bgpvpn_ids.append(bgpvpn_id)
165
166         msg = ("Associate router '%s' to the VPN." %
167                TESTCASE_CONFIG.router_1_name)
168         results.record_action(msg)
169         results.add_to_summary(0, "-")
170
171         test_utils.create_router_association(
172             neutron_client, bgpvpn_id, router_1_id)
173
174         test_utils.update_router_extra_route(
175             neutron_client, router_1_id,
176             [test_utils.ExtraRoute(TESTCASE_CONFIG.extra_route_cidr,
177                                    vm_1_ip),
178              test_utils.ExtraRoute(TESTCASE_CONFIG.extra_route_cidr,
179                                    vm_2_ip)])
180
181         logger.info("Waiting for the VMs to connect to each other using the"
182                     " updated network configuration")
183         test_utils.wait_before_subtest()
184
185         results.get_ping_status_target_ip(vm_3,
186                                           TESTCASE_CONFIG.extra_route_name,
187                                           TESTCASE_CONFIG.extra_route_ip,
188                                           expected="PASS",
189                                           timeout=300)
190
191         results.add_to_summary(0, "=")
192         logger.info("\n%s" % results.summary)
193
194     except Exception as e:
195         logger.error("exception occurred while executing testcase_13: %s", e)
196         raise
197     finally:
198         test_utils.update_router_no_extra_route(neutron_client, router_ids)
199         test_utils.cleanup_nova(nova_client, instance_ids)
200         test_utils.cleanup_glance(glance_client, image_ids)
201         test_utils.cleanup_neutron(neutron_client, floatingip_ids,
202                                    bgpvpn_ids, interfaces, subnet_ids,
203                                    router_ids, network_ids)
204
205     return results.compile_summary()
206
207
208 if __name__ == '__main__':
209     logging.basicConfig(level=logging.INFO)
210     sys.exit(main())