c3778484a3f189f688a1a7cd78d19d1f3aecc057
[sdnvpn.git] / sdnvpn / test / functest / testcase_7.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2015 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 argparse
22
23 import functest.utils.functest_logger as ft_logger
24 import functest.utils.openstack_utils as os_utils
25
26 from sdnvpn.lib import utils as test_utils
27 from sdnvpn.lib import config as sdnvpn_config
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 = ft_logger.Logger("sdnvpn-testcase-7").getLogger()
39
40 COMMON_CONFIG = sdnvpn_config.CommonConfig()
41 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig('testcase_7')
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     image_id = os_utils.create_glance_image(glance_client,
56                                             TESTCASE_CONFIG.image_name,
57                                             COMMON_CONFIG.image_path,
58                                             disk=COMMON_CONFIG.image_format,
59                                             container="bare",
60                                             public=True)
61     network_1_id, _, _ = test_utils.create_network(
62         neutron_client,
63         TESTCASE_CONFIG.net_1_name,
64         TESTCASE_CONFIG.subnet_1_name,
65         TESTCASE_CONFIG.subnet_1_cidr,
66         TESTCASE_CONFIG.router_1_name)
67     network_2_id, _, _ = test_utils.create_network(
68         neutron_client,
69         TESTCASE_CONFIG.net_2_name,
70         TESTCASE_CONFIG.subnet_2_name,
71         TESTCASE_CONFIG.subnet_2_cidr,
72         TESTCASE_CONFIG.router_2_name)
73
74     sg_id = os_utils.create_security_group_full(neutron_client,
75                                                 TESTCASE_CONFIG.secgroup_name,
76                                                 TESTCASE_CONFIG.secgroup_descr)
77     test_utils.open_icmp_ssh(neutron_client, sg_id)
78     vm_2 = test_utils.create_instance(
79         nova_client,
80         TESTCASE_CONFIG.instance_2_name,
81         image_id,
82         network_2_id,
83         sg_id,
84         secgroup_name=TESTCASE_CONFIG.secgroup_name)
85     vm_2_ip = vm_2.networks.itervalues().next()[0]
86
87     u1 = test_utils.generate_ping_userdata([vm_2_ip])
88     vm_1 = test_utils.create_instance(
89         nova_client,
90         TESTCASE_CONFIG.instance_1_name,
91         image_id,
92         network_1_id,
93         sg_id,
94         secgroup_name=TESTCASE_CONFIG.secgroup_name,
95         userdata=u1)
96
97     msg = ("Create VPN with eRT==iRT")
98     results.record_action(msg)
99     vpn_name = "sdnvpn-7"
100     kwargs = {"import_targets": TESTCASE_CONFIG.targets,
101               "export_targets": TESTCASE_CONFIG.targets,
102               "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
103               "name": vpn_name}
104     bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
105     bgpvpn_id = bgpvpn['bgpvpn']['id']
106     logger.debug("VPN created details: %s" % bgpvpn)
107
108     msg = ("Associate networks '%s', '%s' to the VPN."
109            % (TESTCASE_CONFIG.net_1_name,
110               TESTCASE_CONFIG.net_2_name))
111     results.record_action(msg)
112     results.add_to_summary(0, "-")
113
114     os_utils.create_network_association(
115         neutron_client, bgpvpn_id, network_1_id)
116     os_utils.create_network_association(
117         neutron_client, bgpvpn_id, network_2_id)
118
119     test_utils.wait_for_bgp_net_assoc(
120         neutron_client, bgpvpn_id, network_1_id)
121     test_utils.wait_for_bgp_net_assoc(
122         neutron_client, bgpvpn_id, network_2_id)
123
124     instances_up = test_utils.wait_for_instances_up(vm_1, vm_2)
125     if not instances_up:
126         logger.error("One or more instances is down")
127
128     logger.info("Waiting for the VMs to connect to each other using the"
129                 " updated network configuration")
130     test_utils.wait_before_subtest()
131
132     results.get_ping_status(vm_1, vm_2, expected="PASS", timeout=200)
133     results.add_to_summary(0, "=")
134
135     msg = "Assign a Floating IP to %s and ping it" % vm_2.name
136     results.record_action(msg)
137     results.add_to_summary(0, '-')
138
139     fip = os_utils.create_floating_ip(neutron_client)
140     fip_added = os_utils.add_floating_ip(nova_client, vm_2.id, fip['fip_addr'])
141     if fip_added:
142         results.add_success(msg)
143     else:
144         results.add_failure(msg)
145
146     results.ping_ip_test(fip['fip_addr'])
147
148     return results.compile_summary()
149
150
151 if __name__ == '__main__':
152     main()