SDNVPN-184: resources are not cleaned if tc fails
[sdnvpn.git] / sdnvpn / test / functest / testcase_3.py
1 # Copyright (c) 2017 All rights reserved
2 # This program and the accompanying materials
3 # are made available under the terms of the Apache License, Version 2.0
4 # which accompanies this distribution, and is available at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Tests performed:
9 # - Peering OpenDaylight with Quagga:
10 #   - Set up a Quagga instance in the functest container
11 #   - Start a BGP router with OpenDaylight
12 #   - Add the functest Quagga as a neighbor
13 #   - Verify that the OpenDaylight and gateway Quagga peer
14
15 import logging
16 import os
17 import sys
18
19 from functest.utils import functest_utils as ft_utils
20 from functest.utils import openstack_utils as os_utils
21 from sdnvpn.lib import quagga
22 from sdnvpn.lib import utils as test_utils
23 from sdnvpn.lib import config as sdnvpn_config
24 from sdnvpn.lib.results import Results
25
26
27 logger = logging.getLogger(__name__)
28
29 COMMON_CONFIG = sdnvpn_config.CommonConfig()
30 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
31     "sdnvpn.test.functest.testcase_3")
32
33
34 def main():
35     results = Results(COMMON_CONFIG.line_length)
36     results.add_to_summary(0, "=")
37     results.add_to_summary(2, "STATUS", "SUBTEST")
38     results.add_to_summary(0, "=")
39
40     openstack_nodes = test_utils.get_nodes()
41
42     # node.is_odl() doesn't work in Apex
43     # https://jira.opnfv.org/browse/RELENG-192
44     controllers = [node for node in openstack_nodes
45                    if "running" in
46                    node.run_cmd("sudo systemctl status opendaylight")]
47     computes = [node for node in openstack_nodes if node.is_compute()]
48
49     msg = ("Verify that OpenDaylight can start/communicate with zrpcd/Quagga")
50     results.record_action(msg)
51     results.add_to_summary(0, "-")
52     if not controllers:
53         msg = ("Controller (ODL) list is empty. Skipping rest of tests.")
54         logger.info(msg)
55         results.add_failure(msg)
56         return results.compile_summary()
57     else:
58         msg = ("Controller (ODL) list is ready")
59         logger.info(msg)
60         results.add_success(msg)
61
62     controller = controllers[0]  # We don't handle HA well
63     get_ext_ip_cmd = "sudo ip a | grep br-ex | grep inet | awk '{print $2}'"
64     ext_net_cidr = controller.run_cmd(get_ext_ip_cmd).strip().split('\n')
65     ext_net_mask = ext_net_cidr[0].split('/')[1]
66     controller_ext_ip = ext_net_cidr[0].split('/')[0]
67
68     logger.info("Starting bgp speaker of controller at IP %s "
69                 % controller_ext_ip)
70     logger.info("Checking if zrpcd is "
71                 "running on the controller node")
72
73     cmd = "systemctl status zrpcd |grep -i running"
74     output = controller.run_cmd(cmd)
75     msg = ("zrpcd is running")
76
77     if not output:
78         logger.info("zrpcd is not running on the controller node")
79         results.add_failure(msg)
80     else:
81         logger.info("zrpcd is running on the controller node")
82         results.add_success(msg)
83
84     results.add_to_summary(0, "-")
85
86     start_quagga = "odl:configure-bgp -op start-bgp-server " \
87                    "--as-num 100 --router-id {0}".format(controller_ext_ip)
88     test_utils.run_odl_cmd(controller, start_quagga)
89
90     logger.info("Checking if bgpd is running"
91                 " on the controller node")
92
93     # Check if there is a non-zombie bgpd process
94     output_bgpd = controller.run_cmd("ps --no-headers -C "
95                                      "bgpd -o state")
96     states = output_bgpd.split()
97     running = any([s != 'Z' for s in states])
98
99     msg = ("bgpd is running")
100     if not running:
101         logger.info("bgpd is not running on the controller node")
102         results.add_failure(msg)
103     else:
104         logger.info("bgpd is running on the controller node")
105         results.add_success(msg)
106
107     results.add_to_summary(0, "-")
108
109     # We should be able to restart the speaker
110     # but the test is disabled because of buggy upstream
111     # https://github.com/6WIND/zrpcd/issues/15
112     # stop_quagga = 'odl:configure-bgp -op stop-bgp-server'
113     # test_utils.run_odl_cmd(controller, stop_quagga)
114
115     # logger.info("Checking if bgpd is still running"
116     #             " on the controller node")
117
118     # output_bgpd = controller.run_cmd("ps --no-headers -C " \
119     #                                  "bgpd -o state")
120     # states = output_bgpd.split()
121     # running = any([s != 'Z' for s in states])
122
123     # msg = ("bgpd is stopped")
124     # if not running:
125     #     logger.info("bgpd is not running on the controller node")
126     #     results.add_success(msg)
127     # else:
128     #     logger.info("bgpd is still running on the controller node")
129     #     results.add_failure(msg)
130
131     # Taken from the sfc tests
132     if not os.path.isfile(COMMON_CONFIG.ubuntu_image_path):
133         logger.info("Downloading image")
134         ft_utils.download_url(
135             "http://artifacts.opnfv.org/sdnvpn/"
136             "ubuntu-16.04-server-cloudimg-amd64-disk1.img",
137             "/home/opnfv/functest/data/")
138     else:
139         logger.info("Using old image")
140
141     glance_client = os_utils.get_glance_client()
142     nova_client = os_utils.get_nova_client()
143     neutron_client = os_utils.get_neutron_client()
144
145     (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
146      subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
147
148     try:
149         sg_id = os_utils.create_security_group_full(
150             neutron_client, TESTCASE_CONFIG.secgroup_name,
151             TESTCASE_CONFIG.secgroup_descr)
152         test_utils.open_icmp(neutron_client, sg_id)
153         test_utils.open_http_port(neutron_client, sg_id)
154
155         test_utils.open_bgp_port(neutron_client, sg_id)
156         net_id, subnet_1_id, router_1_id = test_utils.create_network(
157             neutron_client,
158             TESTCASE_CONFIG.net_1_name,
159             TESTCASE_CONFIG.subnet_1_name,
160             TESTCASE_CONFIG.subnet_1_cidr,
161             TESTCASE_CONFIG.router_1_name)
162
163         quagga_net_id, subnet_quagga_id, \
164             router_quagga_id = test_utils.create_network(
165                 neutron_client,
166                 TESTCASE_CONFIG.quagga_net_name,
167                 TESTCASE_CONFIG.quagga_subnet_name,
168                 TESTCASE_CONFIG.quagga_subnet_cidr,
169                 TESTCASE_CONFIG.quagga_router_name)
170
171         interfaces.append(tuple((router_1_id, subnet_1_id)))
172         interfaces.append(tuple((router_quagga_id, subnet_quagga_id)))
173         network_ids.extend([net_id, quagga_net_id])
174         router_ids.extend([router_1_id, router_quagga_id])
175         subnet_ids.extend([subnet_1_id, subnet_quagga_id])
176
177         installer_type = str(os.environ['INSTALLER_TYPE'].lower())
178         if installer_type == "fuel":
179             disk = 'raw'
180         elif installer_type == "apex":
181             disk = 'qcow2'
182         else:
183             logger.error("Incompatible installer type")
184
185         ubuntu_image_id = os_utils.create_glance_image(
186             glance_client,
187             COMMON_CONFIG.ubuntu_image_name,
188             COMMON_CONFIG.ubuntu_image_path,
189             disk,
190             container="bare",
191             public="public")
192
193         image_ids.append(ubuntu_image_id)
194
195         # NOTE(rski) The order of this seems a bit weird but
196         # there is a reason for this, namely
197         # https://jira.opnfv.org/projects/SDNVPN/issues/SDNVPN-99
198         # so we create the quagga instance using cloud-init
199         # and immediately give it a floating IP.
200         # The cloud-init script should contain a small sleep for
201         # this to work.
202         # We also create the FIP first because it is used in the
203         # cloud-init script.
204         fip = os_utils.create_floating_ip(neutron_client)
205         # fake_fip is needed to bypass NAT
206         # see below for the reason why.
207         fake_fip = os_utils.create_floating_ip(neutron_client)
208
209         floatingip_ids.extend([fip['fip_id'], fake_fip['fip_id']])
210         # pin quagga to some compute
211         compute_node = nova_client.hypervisors.list()[0]
212         quagga_compute_node = "nova:" + compute_node.hypervisor_hostname
213         # Map the hypervisor used above to a compute handle
214         # returned by releng's manager
215         for comp in computes:
216             if compute_node.host_ip in comp.run_cmd("sudo ip a"):
217                 compute = comp
218                 break
219         quagga_bootstrap_script = quagga.gen_quagga_setup_script(
220             controller_ext_ip,
221             fake_fip['fip_addr'],
222             ext_net_mask)
223
224         test_utils.create_custom_flavor()
225
226         quagga_vm = test_utils.create_instance(
227             nova_client,
228             TESTCASE_CONFIG.quagga_instance_name,
229             ubuntu_image_id,
230             quagga_net_id,
231             sg_id,
232             fixed_ip=TESTCASE_CONFIG.quagga_instance_ip,
233             flavor=COMMON_CONFIG.custom_flavor_name,
234             userdata=quagga_bootstrap_script,
235             compute_node=quagga_compute_node)
236
237         instance_ids.append(quagga_vm)
238
239         fip_added = os_utils.add_floating_ip(nova_client,
240                                              quagga_vm.id,
241                                              fip['fip_addr'])
242
243         msg = ("Assign a Floating IP to %s " %
244                TESTCASE_CONFIG.quagga_instance_name)
245         if fip_added:
246             results.add_success(msg)
247         else:
248             results.add_failure(msg)
249         test_utils.attach_instance_to_ext_br(quagga_vm, compute)
250
251         try:
252             testcase = "Bootstrap quagga inside an OpenStack instance"
253             cloud_init_success = test_utils.wait_for_cloud_init(quagga_vm)
254             if cloud_init_success:
255                 results.add_success(testcase)
256             else:
257                 results.add_failure(testcase)
258             results.add_to_summary(0, "=")
259
260             results.add_to_summary(0, '-')
261             results.add_to_summary(1, "Peer Quagga with OpenDaylight")
262             results.add_to_summary(0, '-')
263
264             neighbor = quagga.odl_add_neighbor(fake_fip['fip_addr'],
265                                                controller_ext_ip,
266                                                controller)
267             peer = quagga.check_for_peering(controller)
268
269         finally:
270             test_utils.detach_instance_from_ext_br(quagga_vm, compute)
271
272         if neighbor and peer:
273             results.add_success("Peering with quagga")
274         else:
275             results.add_failure("Peering with quagga")
276
277     except Exception as e:
278         logger.error("exception occurred while executing testcase_3: %s", e)
279         raise
280     finally:
281         test_utils.cleanup_nova(nova_client, instance_ids, image_ids)
282         test_utils.cleanup_neutron(neutron_client, floatingip_ids,
283                                    bgpvpn_ids, interfaces, subnet_ids,
284                                    router_ids, network_ids)
285
286     return results.compile_summary()
287
288
289 if __name__ == '__main__':
290     logging.basicConfig(level=logging.INFO)
291     sys.exit(main())