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