Replace neutron client calls with openstack sdk
[sdnvpn.git] / sdnvpn / test / functest / testcase_7.py
1 #!/usr/bin/env 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 Testcase for router/FloatingIP & net assoc mutual exclusivity
12
13 A testcase for ODL Bug 6962, testing whether a subnet with a router can be
14 network associated:
15 - Create two VMs, each in a subnet with a router
16 - Network assoc the two networks in a VPN iRT=eRT
17 - Try to ping from one VM to the other
18 - Assign a floating IP to a VM
19 - Ping it
20 """
21 import logging
22 import sys
23
24 from sdnvpn.lib import config as sdnvpn_config
25 from sdnvpn.lib import openstack_utils as os_utils
26 from sdnvpn.lib import utils as test_utils
27 from sdnvpn.lib.results import Results
28
29
30 logger = logging.getLogger(__name__)
31
32 COMMON_CONFIG = sdnvpn_config.CommonConfig()
33 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
34     'sdnvpn.test.functest.testcase_7')
35
36
37 def main():
38     conn = os_utils.get_os_connection()
39     results = Results(COMMON_CONFIG.line_length, conn)
40
41     results.add_to_summary(0, "=")
42     results.add_to_summary(2, "STATUS", "SUBTEST")
43     results.add_to_summary(0, "=")
44
45     neutron_client = os_utils.get_neutron_client()
46
47     (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
48      subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
49
50     try:
51         image_id = os_utils.create_glance_image(
52             conn, TESTCASE_CONFIG.image_name,
53             COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
54             container="bare", public='public')
55         image_ids.append(image_id)
56
57         network_1_id, subnet_1_id, router_1_id = test_utils.create_network(
58             conn,
59             TESTCASE_CONFIG.net_1_name,
60             TESTCASE_CONFIG.subnet_1_name,
61             TESTCASE_CONFIG.subnet_1_cidr,
62             TESTCASE_CONFIG.router_1_name)
63
64         network_2_id, subnet_2_id, router_2_id = test_utils.create_network(
65             conn,
66             TESTCASE_CONFIG.net_2_name,
67             TESTCASE_CONFIG.subnet_2_name,
68             TESTCASE_CONFIG.subnet_2_cidr,
69             TESTCASE_CONFIG.router_2_name)
70
71         interfaces.append(tuple((router_1_id, subnet_1_id)))
72         interfaces.append(tuple((router_2_id, subnet_2_id)))
73         network_ids.extend([network_1_id, network_2_id])
74         router_ids.extend([router_1_id, router_2_id])
75         subnet_ids.extend([subnet_1_id, subnet_2_id])
76
77         sg_id = os_utils.create_security_group_full(
78             conn, TESTCASE_CONFIG.secgroup_name,
79             TESTCASE_CONFIG.secgroup_descr)
80         test_utils.open_icmp(conn, sg_id)
81         test_utils.open_http_port(conn, sg_id)
82
83         vm_2 = test_utils.create_instance(
84             conn,
85             TESTCASE_CONFIG.instance_2_name,
86             image_id,
87             network_2_id,
88             sg_id,
89             secgroup_name=TESTCASE_CONFIG.secgroup_name)
90         vm_2_ip = test_utils.get_instance_ip(conn, vm_2)
91
92         u1 = test_utils.generate_ping_userdata([vm_2_ip])
93         vm_1 = test_utils.create_instance(
94             conn,
95             TESTCASE_CONFIG.instance_1_name,
96             image_id,
97             network_1_id,
98             sg_id,
99             secgroup_name=TESTCASE_CONFIG.secgroup_name,
100             userdata=u1)
101
102         instance_ids.extend([vm_1.id, vm_2.id])
103
104         msg = ("Create VPN with eRT==iRT")
105         results.record_action(msg)
106         vpn_name = "sdnvpn-7"
107         kwargs = {
108             "import_targets": TESTCASE_CONFIG.targets,
109             "export_targets": TESTCASE_CONFIG.targets,
110             "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
111             "name": vpn_name
112         }
113         bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
114         bgpvpn_id = bgpvpn['bgpvpn']['id']
115         logger.debug("VPN created details: %s" % bgpvpn)
116         bgpvpn_ids.append(bgpvpn_id)
117
118         msg = ("Associate networks '%s', '%s' to the VPN."
119                % (TESTCASE_CONFIG.net_1_name,
120                   TESTCASE_CONFIG.net_2_name))
121         results.record_action(msg)
122         results.add_to_summary(0, "-")
123
124         test_utils.create_network_association(
125             neutron_client, bgpvpn_id, network_1_id)
126         test_utils.create_network_association(
127             neutron_client, bgpvpn_id, network_2_id)
128
129         test_utils.wait_for_bgp_net_assoc(
130             neutron_client, bgpvpn_id, network_1_id)
131         test_utils.wait_for_bgp_net_assoc(
132             neutron_client, bgpvpn_id, network_2_id)
133
134         # Wait for VMs to get ips.
135         instances_up = test_utils.wait_for_instances_up(vm_2)
136         instances_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_1)
137
138         if (not instances_up or not instances_dhcp_up):
139             logger.error("One or more instances are down")
140             # TODO: Handle this appropriately
141
142         logger.info("Waiting for the VMs to connect to each other using the"
143                     " updated network configuration")
144         test_utils.wait_before_subtest()
145
146         results.get_ping_status(vm_1, vm_2, expected="PASS", timeout=200)
147         results.add_to_summary(0, "=")
148
149         msg = "Assign a Floating IP to %s and ping it" % vm_2.name
150         results.record_action(msg)
151         results.add_to_summary(0, '-')
152
153         vm2_port = test_utils.get_port(conn, vm_2.id)
154         fip_added = os_utils.attach_floating_ip(conn, vm2_port.id)
155         if fip_added:
156             results.add_success(msg)
157         else:
158             results.add_failure(msg)
159
160         results.ping_ip_test(fip_added.floating_ip_address)
161
162         floatingip_ids.append(fip_added.id)
163
164     except Exception as e:
165         logger.error("exception occurred while executing testcase_7: %s", e)
166         raise
167     finally:
168         test_utils.cleanup_nova(conn, instance_ids)
169         test_utils.cleanup_glance(conn, image_ids)
170         test_utils.cleanup_neutron(conn, neutron_client, floatingip_ids,
171                                    bgpvpn_ids, interfaces, subnet_ids,
172                                    router_ids, network_ids)
173
174     return results.compile_summary()
175
176
177 if __name__ == '__main__':
178     sys.exit(main())