Refactor imports and invocation of main function
[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 argparse
22 import logging
23 import sys
24
25 from functest.utils import openstack_utils as os_utils
26 from sdnvpn.lib import config as sdnvpn_config
27 from sdnvpn.lib import utils as test_utils
28 from sdnvpn.lib.results import Results
29
30 parser = argparse.ArgumentParser()
31
32 parser.add_argument("-r", "--report",
33                     help="Create json result file",
34                     action="store_true")
35
36 args = parser.parse_args()
37
38 logger = logging.getLogger('sdnvpn-testcase-8')
39
40 COMMON_CONFIG = sdnvpn_config.CommonConfig()
41 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig('testcase_8')
42
43
44 def main():
45     results = Results(COMMON_CONFIG.line_length)
46
47     results.add_to_summary(0, "=")
48     results.add_to_summary(2, "STATUS", "SUBTEST")
49     results.add_to_summary(0, "=")
50
51     nova_client = os_utils.get_nova_client()
52     neutron_client = os_utils.get_neutron_client()
53     glance_client = os_utils.get_glance_client()
54
55     (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
56      subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
57
58     image_id = os_utils.create_glance_image(glance_client,
59                                             TESTCASE_CONFIG.image_name,
60                                             COMMON_CONFIG.image_path,
61                                             disk=COMMON_CONFIG.image_format,
62                                             container="bare",
63                                             public='public')
64     image_ids.append(image_id)
65
66     network_1_id, subnet_1_id, router_1_id = test_utils.create_network(
67         neutron_client,
68         TESTCASE_CONFIG.net_1_name,
69         TESTCASE_CONFIG.subnet_1_name,
70         TESTCASE_CONFIG.subnet_1_cidr,
71         TESTCASE_CONFIG.router_1_name)
72     network_2_id = test_utils.create_net(
73         neutron_client,
74         TESTCASE_CONFIG.net_2_name)
75
76     subnet_2_id = test_utils.create_subnet(
77         neutron_client,
78         TESTCASE_CONFIG.subnet_2_name,
79         TESTCASE_CONFIG.subnet_2_cidr,
80         network_2_id)
81
82     interfaces.append(tuple((router_1_id, subnet_1_id)))
83     network_ids.extend([network_1_id, network_2_id])
84     router_ids.append(router_1_id)
85     subnet_ids.extend([subnet_1_id, subnet_2_id])
86
87     sg_id = os_utils.create_security_group_full(neutron_client,
88                                                 TESTCASE_CONFIG.secgroup_name,
89                                                 TESTCASE_CONFIG.secgroup_descr)
90     test_utils.open_icmp(neutron_client, sg_id)
91     test_utils.open_http_port(neutron_client, sg_id)
92
93     vm_2 = test_utils.create_instance(
94         nova_client,
95         TESTCASE_CONFIG.instance_2_name,
96         image_id,
97         network_2_id,
98         sg_id,
99         secgroup_name=TESTCASE_CONFIG.secgroup_name)
100     vm_2_ip = test_utils.get_instance_ip(vm_2)
101
102     u1 = test_utils.generate_ping_userdata([vm_2_ip])
103     vm_1 = test_utils.create_instance(
104         nova_client,
105         TESTCASE_CONFIG.instance_1_name,
106         image_id,
107         network_1_id,
108         sg_id,
109         secgroup_name=TESTCASE_CONFIG.secgroup_name,
110         userdata=u1)
111     instance_ids.extend([vm_1.id, vm_2.id])
112
113     results.record_action("Create VPN with eRT==iRT")
114     vpn_name = "sdnvpn-8"
115     kwargs = {"import_targets": TESTCASE_CONFIG.targets,
116               "export_targets": TESTCASE_CONFIG.targets,
117               "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
118               "name": vpn_name}
119     bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
120     bgpvpn_id = bgpvpn['bgpvpn']['id']
121     logger.debug("VPN created details: %s" % bgpvpn)
122     bgpvpn_ids.append(bgpvpn_id)
123
124     msg = ("Associate router '%s' and net '%s' to the VPN."
125            % (TESTCASE_CONFIG.router_1_name,
126               TESTCASE_CONFIG.net_2_name))
127     results.record_action(msg)
128     results.add_to_summary(0, "-")
129
130     test_utils.create_router_association(
131         neutron_client, bgpvpn_id, router_1_id)
132     test_utils.create_network_association(
133         neutron_client, bgpvpn_id, network_2_id)
134
135     test_utils.wait_for_bgp_router_assoc(
136         neutron_client, bgpvpn_id, router_1_id)
137     test_utils.wait_for_bgp_net_assoc(
138         neutron_client, bgpvpn_id, network_2_id)
139
140     instances_up = test_utils.wait_for_instances_up(vm_1, vm_2)
141     if not instances_up:
142         logger.error("One or more instances is down")
143
144     logger.info("Waiting for the VMs to connect to each other using the"
145                 " updated network configuration")
146     test_utils.wait_before_subtest()
147
148     results.get_ping_status(vm_1, vm_2, expected="PASS", timeout=200)
149     results.add_to_summary(0, "=")
150
151     msg = "Assign a Floating IP to %s" % vm_1.name
152     results.record_action(msg)
153
154     fip = os_utils.create_floating_ip(neutron_client)
155
156     fip_added = os_utils.add_floating_ip(nova_client, vm_1.id, fip['fip_addr'])
157     if fip_added:
158         results.add_success(msg)
159     else:
160         results.add_failure(msg)
161
162     results.add_to_summary(0, "=")
163     results.record_action("Ping %s via Floating IP" % vm_1.name)
164     results.add_to_summary(0, "-")
165     results.ping_ip_test(fip['fip_addr'])
166
167     floatingip_ids.append(fip['fip_id'])
168
169     test_utils.cleanup_nova(nova_client, instance_ids, image_ids)
170     test_utils.cleanup_neutron(neutron_client, floatingip_ids, bgpvpn_ids,
171                                interfaces, subnet_ids, router_ids,
172                                network_ids)
173
174     return results.compile_summary()
175
176
177 if __name__ == '__main__':
178     logging.basicConfig(level=logging.INFO)
179     sys.exit(main())