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