Merge "Move logging instance creation to create_instance"
[sdnvpn.git] / 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 import utils as test_utils
18 import config as sdnvpn_config
19 from 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     vm_1_ip = vm_1.networks.itervalues().next()[0]
136
137     msg = ("Create VPN with eRT<>iRT")
138     logger.info(msg)
139     results.add_to_summary(1, msg)
140     vpn_name = "sdnvpn-" + str(randint(100000, 999999))
141     kwargs = {
142         "import_targets": TESTCASE_CONFIG.targets1,
143         "export_targets": TESTCASE_CONFIG.targets2,
144         "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
145         "name": vpn_name
146     }
147     bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
148     bgpvpn_id = bgpvpn['bgpvpn']['id']
149     logger.debug("VPN created details: %s" % bgpvpn)
150
151     msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
152     logger.info(msg)
153     results.add_to_summary(1, msg)
154     results.add_to_summary(0, "-")
155
156     os_utils.create_network_association(
157         neutron_client, bgpvpn_id, network_1_id)
158
159     # Wait for VMs to get ips.
160     instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
161                                                     vm_3, vm_4,
162                                                     vm_5)
163
164     if not instances_up:
165         logger.error("One or more instances is down")
166         # TODO: Handle this appropriately
167
168     # Ping from VM1 to VM2 should work
169     results.get_ping_status(vm_1, vm_1_ip, vm_2, vm_2_ip,
170                             expected="PASS", timeout=200)
171     # Ping from VM1 to VM3 should work
172     results.get_ping_status(vm_1, vm_1_ip, vm_3, vm_3_ip,
173                             expected="PASS", timeout=30)
174     # Ping from VM1 to VM4 should not work
175     results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
176                             expected="FAIL", timeout=30)
177
178     msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_2_name)
179     logger.info(msg)
180     results.add_to_summary(0, "-")
181     results.add_to_summary(1, msg)
182     results.add_to_summary(0, "-")
183     os_utils.create_network_association(
184         neutron_client, bgpvpn_id, network_2_id)
185
186     test_utils.wait_for_bgp_net_assocs(neutron_client,
187                                        bgpvpn_id,
188                                        network_1_id,
189                                        network_2_id)
190
191     logger.info("Waiting for the VMs to connect to each other using the"
192                 " updated network configuration")
193     test_utils.wait_before_subtest()
194
195     # Ping from VM4 to VM5 should work
196     results.get_ping_status(vm_4, vm_4_ip, vm_5, vm_5_ip,
197                             expected="PASS", timeout=30)
198     # Ping from VM1 to VM4 should not work
199     results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
200                             expected="FAIL", timeout=30)
201     # Ping from VM1 to VM5 should not work
202     results.get_ping_status(vm_1, vm_1_ip, vm_5, vm_5_ip,
203                             expected="FAIL", timeout=30)
204
205     msg = ("Update VPN with eRT=iRT ...")
206     logger.info(msg)
207     results.add_to_summary(0, "-")
208     results.add_to_summary(1, msg)
209     results.add_to_summary(0, "-")
210     kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
211               "export_targets": TESTCASE_CONFIG.targets1,
212               "name": vpn_name}
213     bgpvpn = os_utils.update_bgpvpn(neutron_client, bgpvpn_id, **kwargs)
214
215     logger.info("Waiting for the VMs to connect to each other using the"
216                 " updated network configuration")
217     test_utils.wait_before_subtest()
218
219     # Ping from VM1 to VM4 should work
220     results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
221                             expected="PASS", timeout=30)
222     # Ping from VM1 to VM5 should work
223     results.get_ping_status(vm_1, vm_1_ip, vm_5, vm_5_ip,
224                             expected="PASS", timeout=30)
225
226     return results.compile_summary(TESTCASE_CONFIG.success_criteria)
227
228
229 if __name__ == '__main__':
230     main()