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