Merge "remove flavor at the end of the test"
[sdnvpn.git] / sdnvpn / test / functest / testcase_7.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 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 functest.utils import openstack_utils as os_utils
25 from sdnvpn.lib import config as sdnvpn_config
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     results = Results(COMMON_CONFIG.line_length)
39
40     results.add_to_summary(0, "=")
41     results.add_to_summary(2, "STATUS", "SUBTEST")
42     results.add_to_summary(0, "=")
43
44     nova_client = os_utils.get_nova_client()
45     neutron_client = os_utils.get_neutron_client()
46     glance_client = os_utils.get_glance_client()
47
48     (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
49      subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
50
51     try:
52         image_id = os_utils.create_glance_image(
53             glance_client, TESTCASE_CONFIG.image_name,
54             COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
55             container="bare", public='public')
56         image_ids.append(image_id)
57
58         network_1_id, subnet_1_id, router_1_id = test_utils.create_network(
59             neutron_client,
60             TESTCASE_CONFIG.net_1_name,
61             TESTCASE_CONFIG.subnet_1_name,
62             TESTCASE_CONFIG.subnet_1_cidr,
63             TESTCASE_CONFIG.router_1_name)
64
65         network_2_id, subnet_2_id, router_2_id = test_utils.create_network(
66             neutron_client,
67             TESTCASE_CONFIG.net_2_name,
68             TESTCASE_CONFIG.subnet_2_name,
69             TESTCASE_CONFIG.subnet_2_cidr,
70             TESTCASE_CONFIG.router_2_name)
71
72         interfaces.append(tuple((router_1_id, subnet_1_id)))
73         interfaces.append(tuple((router_2_id, subnet_2_id)))
74         network_ids.extend([network_1_id, network_2_id])
75         router_ids.extend([router_1_id, router_2_id])
76         subnet_ids.extend([subnet_1_id, subnet_2_id])
77
78         sg_id = os_utils.create_security_group_full(
79             neutron_client, TESTCASE_CONFIG.secgroup_name,
80             TESTCASE_CONFIG.secgroup_descr)
81         test_utils.open_icmp(neutron_client, sg_id)
82         test_utils.open_http_port(neutron_client, sg_id)
83
84         vm_2 = test_utils.create_instance(
85             nova_client,
86             TESTCASE_CONFIG.instance_2_name,
87             image_id,
88             network_2_id,
89             sg_id,
90             secgroup_name=TESTCASE_CONFIG.secgroup_name)
91         vm_2_ip = test_utils.get_instance_ip(vm_2)
92
93         u1 = test_utils.generate_ping_userdata([vm_2_ip])
94         vm_1 = test_utils.create_instance(
95             nova_client,
96             TESTCASE_CONFIG.instance_1_name,
97             image_id,
98             network_1_id,
99             sg_id,
100             secgroup_name=TESTCASE_CONFIG.secgroup_name,
101             userdata=u1)
102
103         instance_ids.extend([vm_1.id, vm_2.id])
104
105         msg = ("Create VPN with eRT==iRT")
106         results.record_action(msg)
107         vpn_name = "sdnvpn-7"
108         kwargs = {
109             "import_targets": TESTCASE_CONFIG.targets,
110             "export_targets": TESTCASE_CONFIG.targets,
111             "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
112             "name": vpn_name
113         }
114         bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
115         bgpvpn_id = bgpvpn['bgpvpn']['id']
116         logger.debug("VPN created details: %s" % bgpvpn)
117         bgpvpn_ids.append(bgpvpn_id)
118
119         msg = ("Associate networks '%s', '%s' to the VPN."
120                % (TESTCASE_CONFIG.net_1_name,
121                   TESTCASE_CONFIG.net_2_name))
122         results.record_action(msg)
123         results.add_to_summary(0, "-")
124
125         test_utils.create_network_association(
126             neutron_client, bgpvpn_id, network_1_id)
127         test_utils.create_network_association(
128             neutron_client, bgpvpn_id, network_2_id)
129
130         test_utils.wait_for_bgp_net_assoc(
131             neutron_client, bgpvpn_id, network_1_id)
132         test_utils.wait_for_bgp_net_assoc(
133             neutron_client, bgpvpn_id, network_2_id)
134
135         # Wait for VMs to get ips.
136         instances_up = test_utils.wait_for_instances_up(vm_2)
137         instances_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_1)
138
139         if (not instances_up or not instances_dhcp_up):
140             logger.error("One or more instances are down")
141             # TODO: Handle this appropriately
142
143         logger.info("Waiting for the VMs to connect to each other using the"
144                     " updated network configuration")
145         test_utils.wait_before_subtest()
146
147         results.get_ping_status(vm_1, vm_2, expected="PASS", timeout=200)
148         results.add_to_summary(0, "=")
149
150         msg = "Assign a Floating IP to %s and ping it" % vm_2.name
151         results.record_action(msg)
152         results.add_to_summary(0, '-')
153
154         fip = os_utils.create_floating_ip(neutron_client)
155         fip_added = os_utils.add_floating_ip(nova_client, vm_2.id,
156                                              fip['fip_addr'])
157         if fip_added:
158             results.add_success(msg)
159         else:
160             results.add_failure(msg)
161
162         results.ping_ip_test(fip['fip_addr'])
163
164         floatingip_ids.append(fip['fip_id'])
165
166     except Exception as e:
167         logger.error("exception occurred while executing testcase_7: %s", e)
168         raise
169     finally:
170         test_utils.cleanup_nova(nova_client, instance_ids)
171         test_utils.cleanup_glance(glance_client, image_ids)
172         test_utils.cleanup_neutron(neutron_client, floatingip_ids,
173                                    bgpvpn_ids, interfaces, subnet_ids,
174                                    router_ids, network_ids)
175
176     return results.compile_summary()
177
178
179 if __name__ == '__main__':
180     logging.basicConfig(level=logging.INFO)
181     sys.exit(main())