Implement a get VM IP function in utils to be used within test cases
[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 two VMs, one in a subnet with a router
13 - Assoc the two networks in a VPN iRT=eRT
14   One with router assoc, other with net assoc
15 - Try to ping from one VM to the other
16 - Assign a floating IP to the VM in the router assoc network
17 - Ping it
18 """
19 import argparse
20
21 import functest.utils.functest_logger as ft_logger
22 import functest.utils.openstack_utils as os_utils
23
24 import sdnvpn.lib.utils as test_utils
25 from sdnvpn.lib.results import Results
26 import sdnvpn.lib.config as sdnvpn_config
27
28 parser = argparse.ArgumentParser()
29
30 parser.add_argument("-r", "--report",
31                     help="Create json result file",
32                     action="store_true")
33
34 args = parser.parse_args()
35
36 logger = ft_logger.Logger("sdnvpn-testcase-8").getLogger()
37
38 COMMON_CONFIG = sdnvpn_config.CommonConfig()
39 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig('testcase_8')
40
41
42 def main():
43     results = Results(COMMON_CONFIG.line_length)
44
45     results.add_to_summary(0, "=")
46     results.add_to_summary(2, "STATUS", "SUBTEST")
47     results.add_to_summary(0, "=")
48
49     nova_client = os_utils.get_nova_client()
50     neutron_client = os_utils.get_neutron_client()
51     glance_client = os_utils.get_glance_client()
52
53     image_id = os_utils.create_glance_image(glance_client,
54                                             TESTCASE_CONFIG.image_name,
55                                             COMMON_CONFIG.image_path,
56                                             disk=COMMON_CONFIG.image_format,
57                                             container="bare",
58                                             public='public')
59     network_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     test_utils.create_subnet(neutron_client,
69                              TESTCASE_CONFIG.subnet_2_name,
70                              TESTCASE_CONFIG.subnet_2_cidr,
71                              network_2_id)
72
73     sg_id = os_utils.create_security_group_full(neutron_client,
74                                                 TESTCASE_CONFIG.secgroup_name,
75                                                 TESTCASE_CONFIG.secgroup_descr)
76     test_utils.open_icmp_ssh(neutron_client, sg_id)
77     vm_2 = test_utils.create_instance(
78         nova_client,
79         TESTCASE_CONFIG.instance_2_name,
80         image_id,
81         network_2_id,
82         sg_id,
83         secgroup_name=TESTCASE_CONFIG.secgroup_name)
84     vm_2_ip = test_utils.get_instance_ip(vm_2)
85
86     u1 = test_utils.generate_ping_userdata([vm_2_ip])
87     vm_1 = test_utils.create_instance(
88         nova_client,
89         TESTCASE_CONFIG.instance_1_name,
90         image_id,
91         network_1_id,
92         sg_id,
93         secgroup_name=TESTCASE_CONFIG.secgroup_name,
94         userdata=u1)
95
96     results.record_action("Create VPN with eRT==iRT")
97     vpn_name = "sdnvpn-7"
98     kwargs = {"import_targets": TESTCASE_CONFIG.targets,
99               "export_targets": TESTCASE_CONFIG.targets,
100               "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
101               "name": vpn_name}
102     bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
103     bgpvpn_id = bgpvpn['bgpvpn']['id']
104     logger.debug("VPN created details: %s" % bgpvpn)
105
106     msg = ("Associate router '%s' and net '%s' to the VPN."
107            % (TESTCASE_CONFIG.router_1_name,
108               TESTCASE_CONFIG.net_2_name))
109     results.record_action(msg)
110     results.add_to_summary(0, "-")
111
112     os_utils.create_router_association(
113         neutron_client, bgpvpn_id, router_1_id)
114     os_utils.create_network_association(
115         neutron_client, bgpvpn_id, network_2_id)
116
117     test_utils.wait_for_bgp_router_assoc(
118         neutron_client, bgpvpn_id, router_1_id)
119     test_utils.wait_for_bgp_net_assoc(
120         neutron_client, bgpvpn_id, network_2_id)
121
122     instances_up = test_utils.wait_for_instances_up(vm_1, vm_2)
123     if not instances_up:
124         logger.error("One or more instances is down")
125
126     logger.info("Waiting for the VMs to connect to each other using the"
127                 " updated network configuration")
128     test_utils.wait_before_subtest()
129
130     results.get_ping_status(vm_1, vm_2, expected="PASS", timeout=200)
131     results.add_to_summary(0, "=")
132
133     msg = "Assign a Floating IP to %s" % vm_1.name
134     results.record_action(msg)
135
136     fip = os_utils.create_floating_ip(neutron_client)
137     fip_added = os_utils.add_floating_ip(nova_client, vm_1.id, fip['fip_addr'])
138     if fip_added:
139         results.add_success(msg)
140     else:
141         results.add_failure(msg)
142
143     results.record_action("Ping %s via Floating IP" % vm_1.name)
144     results.add_to_summary(0, "-")
145     results.ping_ip_test(fip['fip_addr'])
146
147     return results.compile_summary()
148
149
150 if __name__ == '__main__':
151     main()