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