Fix the config.yaml including for each testcase
[sdnvpn.git] / sdnvpn / test / functest / testcase_8.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 Test whether router assoc can coexist with floating IP
12 - Create VM1 in net1 with a subnet which is connected to a router
13     which is connected with the gateway
14 - Create VM2 in net2 with a subnet without a router attached.
15 - Create bgpvpn with iRT=eRT
16 - Assoc the router of net1 with bgpvpn and assoc net 2 with the bgpvpn
17 - Try to ping from one VM to the other
18 - Assign a floating IP to the VM in the router assoc network
19 - Ping it the floating ip
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_8')
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     image_id = os_utils.create_glance_image(glance_client,
52                                             TESTCASE_CONFIG.image_name,
53                                             COMMON_CONFIG.image_path,
54                                             disk=COMMON_CONFIG.image_format,
55                                             container="bare",
56                                             public='public')
57     image_ids.append(image_id)
58
59     network_1_id, subnet_1_id, router_1_id = test_utils.create_network(
60         neutron_client,
61         TESTCASE_CONFIG.net_1_name,
62         TESTCASE_CONFIG.subnet_1_name,
63         TESTCASE_CONFIG.subnet_1_cidr,
64         TESTCASE_CONFIG.router_1_name)
65     network_2_id = test_utils.create_net(
66         neutron_client,
67         TESTCASE_CONFIG.net_2_name)
68
69     subnet_2_id = test_utils.create_subnet(
70         neutron_client,
71         TESTCASE_CONFIG.subnet_2_name,
72         TESTCASE_CONFIG.subnet_2_cidr,
73         network_2_id)
74
75     interfaces.append(tuple((router_1_id, subnet_1_id)))
76     network_ids.extend([network_1_id, network_2_id])
77     router_ids.append(router_1_id)
78     subnet_ids.extend([subnet_1_id, subnet_2_id])
79
80     sg_id = os_utils.create_security_group_full(neutron_client,
81                                                 TESTCASE_CONFIG.secgroup_name,
82                                                 TESTCASE_CONFIG.secgroup_descr)
83     test_utils.open_icmp(neutron_client, sg_id)
84     test_utils.open_http_port(neutron_client, sg_id)
85
86     vm_2 = test_utils.create_instance(
87         nova_client,
88         TESTCASE_CONFIG.instance_2_name,
89         image_id,
90         network_2_id,
91         sg_id,
92         secgroup_name=TESTCASE_CONFIG.secgroup_name)
93     vm_2_ip = test_utils.get_instance_ip(vm_2)
94
95     u1 = test_utils.generate_ping_userdata([vm_2_ip])
96     vm_1 = test_utils.create_instance(
97         nova_client,
98         TESTCASE_CONFIG.instance_1_name,
99         image_id,
100         network_1_id,
101         sg_id,
102         secgroup_name=TESTCASE_CONFIG.secgroup_name,
103         userdata=u1)
104     instance_ids.extend([vm_1.id, vm_2.id])
105
106     results.record_action("Create VPN with eRT==iRT")
107     vpn_name = "sdnvpn-8"
108     kwargs = {"import_targets": TESTCASE_CONFIG.targets,
109               "export_targets": TESTCASE_CONFIG.targets,
110               "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
111               "name": vpn_name}
112     bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
113     bgpvpn_id = bgpvpn['bgpvpn']['id']
114     logger.debug("VPN created details: %s" % bgpvpn)
115     bgpvpn_ids.append(bgpvpn_id)
116
117     msg = ("Associate router '%s' and net '%s' to the VPN."
118            % (TESTCASE_CONFIG.router_1_name,
119               TESTCASE_CONFIG.net_2_name))
120     results.record_action(msg)
121     results.add_to_summary(0, "-")
122
123     test_utils.create_router_association(
124         neutron_client, bgpvpn_id, router_1_id)
125     test_utils.create_network_association(
126         neutron_client, bgpvpn_id, network_2_id)
127
128     test_utils.wait_for_bgp_router_assoc(
129         neutron_client, bgpvpn_id, router_1_id)
130     test_utils.wait_for_bgp_net_assoc(
131         neutron_client, bgpvpn_id, network_2_id)
132
133     instances_up = test_utils.wait_for_instances_up(vm_1, vm_2)
134     if not instances_up:
135         logger.error("One or more instances is down")
136
137     logger.info("Waiting for the VMs to connect to each other using the"
138                 " updated network configuration")
139     test_utils.wait_before_subtest()
140
141     results.get_ping_status(vm_1, vm_2, expected="PASS", timeout=200)
142     results.add_to_summary(0, "=")
143
144     msg = "Assign a Floating IP to %s" % vm_1.name
145     results.record_action(msg)
146
147     fip = os_utils.create_floating_ip(neutron_client)
148
149     fip_added = os_utils.add_floating_ip(nova_client, vm_1.id, fip['fip_addr'])
150     if fip_added:
151         results.add_success(msg)
152     else:
153         results.add_failure(msg)
154
155     results.add_to_summary(0, "=")
156     results.record_action("Ping %s via Floating IP" % vm_1.name)
157     results.add_to_summary(0, "-")
158     results.ping_ip_test(fip['fip_addr'])
159
160     floatingip_ids.append(fip['fip_id'])
161
162     test_utils.cleanup_nova(nova_client, instance_ids, image_ids)
163     test_utils.cleanup_neutron(neutron_client, floatingip_ids, bgpvpn_ids,
164                                interfaces, subnet_ids, router_ids,
165                                network_ids)
166
167     return results.compile_summary()
168
169
170 if __name__ == '__main__':
171     logging.basicConfig(level=logging.INFO)
172     sys.exit(main())