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