Merge "Fix table formatting in testcase 7"
[sdnvpn.git] / sdnvpn / test / functest / testcase_1.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 import argparse
12 from random import randint
13
14 import functest.utils.functest_logger as ft_logger
15 import functest.utils.openstack_utils as os_utils
16
17 from sdnvpn.lib import utils as test_utils
18 from sdnvpn.lib import config as sdnvpn_config
19 from sdnvpn.lib.results import Results
20
21 parser = argparse.ArgumentParser()
22
23 parser.add_argument("-r", "--report",
24                     help="Create json result file",
25                     action="store_true")
26
27 args = parser.parse_args()
28
29 logger = ft_logger.Logger("sdnvpn-testcase-1").getLogger()
30
31 COMMON_CONFIG = sdnvpn_config.CommonConfig()
32 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig('testcase_1')
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     glance_client = os_utils.get_glance_client()
45
46     image_id = os_utils.create_glance_image(glance_client,
47                                             TESTCASE_CONFIG.image_name,
48                                             COMMON_CONFIG.image_path,
49                                             disk=COMMON_CONFIG.image_format,
50                                             container="bare",
51                                             public=True)
52     network_1_id = test_utils.create_net(neutron_client,
53                                          TESTCASE_CONFIG.net_1_name)
54     test_utils.create_subnet(neutron_client,
55                              TESTCASE_CONFIG.subnet_1_name,
56                              TESTCASE_CONFIG.subnet_1_cidr,
57                              network_1_id)
58
59     network_2_id = test_utils.create_net(neutron_client,
60                                          TESTCASE_CONFIG.net_2_name)
61
62     test_utils.create_subnet(neutron_client,
63                              TESTCASE_CONFIG.subnet_2_name,
64                              TESTCASE_CONFIG.subnet_2_cidr,
65                              network_2_id)
66
67     sg_id = os_utils.create_security_group_full(neutron_client,
68                                                 TESTCASE_CONFIG.secgroup_name,
69                                                 TESTCASE_CONFIG.secgroup_descr)
70
71     compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
72
73     av_zone_1 = "nova:" + compute_nodes[0]
74     av_zone_2 = "nova:" + compute_nodes[1]
75
76     # boot INTANCES
77     vm_2 = test_utils.create_instance(
78         nova_client,
79         TESTCASE_CONFIG.instance_2_name,
80         image_id,
81         network_1_id,
82         sg_id,
83         secgroup_name=TESTCASE_CONFIG.secgroup_name,
84         compute_node=av_zone_1)
85     vm_2_ip = vm_2.networks.itervalues().next()[0]
86
87     vm_3 = test_utils.create_instance(
88         nova_client,
89         TESTCASE_CONFIG.instance_3_name,
90         image_id,
91         network_1_id,
92         sg_id,
93         secgroup_name=TESTCASE_CONFIG.secgroup_name,
94         compute_node=av_zone_2)
95     vm_3_ip = vm_3.networks.itervalues().next()[0]
96
97     vm_5 = test_utils.create_instance(
98         nova_client,
99         TESTCASE_CONFIG.instance_5_name,
100         image_id,
101         network_2_id,
102         sg_id,
103         secgroup_name=TESTCASE_CONFIG.secgroup_name,
104         compute_node=av_zone_2)
105     vm_5_ip = vm_5.networks.itervalues().next()[0]
106
107     # We boot vm5 first because we need vm5_ip for vm4 userdata
108     u4 = test_utils.generate_ping_userdata([vm_5_ip])
109     vm_4 = test_utils.create_instance(
110         nova_client,
111         TESTCASE_CONFIG.instance_4_name,
112         image_id,
113         network_2_id,
114         sg_id,
115         secgroup_name=TESTCASE_CONFIG.secgroup_name,
116         compute_node=av_zone_1,
117         userdata=u4)
118     vm_4_ip = vm_4.networks.itervalues().next()[0]
119
120     # We boot VM1 at the end because we need to get the IPs first to generate
121     # the userdata
122     u1 = test_utils.generate_ping_userdata([vm_2_ip,
123                                             vm_3_ip,
124                                             vm_4_ip,
125                                             vm_5_ip])
126     vm_1 = test_utils.create_instance(
127         nova_client,
128         TESTCASE_CONFIG.instance_1_name,
129         image_id,
130         network_1_id,
131         sg_id,
132         secgroup_name=TESTCASE_CONFIG.secgroup_name,
133         compute_node=av_zone_1,
134         userdata=u1)
135
136     msg = ("Create VPN with eRT<>iRT")
137     results.record_action(msg)
138     vpn_name = "sdnvpn-" + str(randint(100000, 999999))
139     kwargs = {
140         "import_targets": TESTCASE_CONFIG.targets1,
141         "export_targets": TESTCASE_CONFIG.targets2,
142         "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
143         "name": vpn_name
144     }
145     bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
146     bgpvpn_id = bgpvpn['bgpvpn']['id']
147     logger.debug("VPN created details: %s" % bgpvpn)
148
149     msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
150     results.record_action(msg)
151     results.add_to_summary(0, "-")
152
153     os_utils.create_network_association(
154         neutron_client, bgpvpn_id, network_1_id)
155
156     # Wait for VMs to get ips.
157     instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
158                                                     vm_3, vm_4,
159                                                     vm_5)
160
161     if not instances_up:
162         logger.error("One or more instances is down")
163         # TODO: Handle this appropriately
164
165     results.get_ping_status(vm_1, vm_2, expected="PASS", timeout=200)
166     results.get_ping_status(vm_1, vm_3, expected="PASS", timeout=30)
167     results.get_ping_status(vm_1, vm_4, expected="FAIL", timeout=30)
168
169     msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_2_name)
170     results.add_to_summary(0, "-")
171     results.record_action(msg)
172     results.add_to_summary(0, "-")
173     os_utils.create_network_association(
174         neutron_client, bgpvpn_id, network_2_id)
175
176     test_utils.wait_for_bgp_net_assocs(neutron_client,
177                                        bgpvpn_id,
178                                        network_1_id,
179                                        network_2_id)
180
181     logger.info("Waiting for the VMs to connect to each other using the"
182                 " updated network configuration")
183     test_utils.wait_before_subtest()
184
185     results.get_ping_status(vm_4, vm_5, expected="PASS", timeout=30)
186     # TODO enable again when isolation in VPN with iRT != eRT works
187     # results.get_ping_status(vm_1, vm_4, expected="FAIL", timeout=30)
188     # results.get_ping_status(vm_1, vm_5, expected="FAIL", timeout=30)
189
190     msg = ("Update VPN with eRT=iRT ...")
191     results.add_to_summary(0, "-")
192     results.record_action(msg)
193     results.add_to_summary(0, "-")
194     kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
195               "export_targets": TESTCASE_CONFIG.targets1,
196               "name": vpn_name}
197     bgpvpn = os_utils.update_bgpvpn(neutron_client, bgpvpn_id, **kwargs)
198
199     logger.info("Waiting for the VMs to connect to each other using the"
200                 " updated network configuration")
201     test_utils.wait_before_subtest()
202
203     results.get_ping_status(vm_1, vm_4, expected="PASS", timeout=30)
204     results.get_ping_status(vm_1, vm_5, expected="PASS", timeout=30)
205
206     return results.compile_summary()
207
208
209 if __name__ == '__main__':
210     main()