Fix most end-to-end issues with testcase 3 and reenable it
[sdnvpn.git] / sdnvpn / test / functest / testcase_3.py
1 #
2 # Copyright (c) 2017 All rights reserved
3 # This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Tests performed:
10 # - Peering OpenDaylight with Quagga:
11 #   - Set up a Quagga instance in the functest container
12 #   - Start a BGP router with OpenDaylight
13 #   - Add the functest Quagga as a neighbor
14 #   - Verify that the OpenDaylight and gateway Quagga peer
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 import functest.utils.functest_logger as ft_logger
25
26 from sdnvpn.lib.results import Results
27
28 COMMON_CONFIG = sdnvpn_config.CommonConfig()
29 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig("testcase_3")
30
31 logger = ft_logger.Logger("sdnvpn-testcase-3").getLogger()
32
33 parser = argparse.ArgumentParser()
34
35 parser.add_argument("-r", "--report",
36                     help="Create json result file",
37                     action="store_true")
38
39 args = parser.parse_args()
40
41
42 def main():
43     results = Results(COMMON_CONFIG.line_length)
44     results.add_to_summary(0, "=")
45     results.add_to_summary(2, "STATUS", "SUBTEST")
46     results.add_to_summary(0, "=")
47
48     openstack_nodes = test_utils.get_nodes()
49
50     controllers = [node for node in openstack_nodes
51                    if node.is_odl()]
52     computes = [node for node in openstack_nodes if node.is_compute()]
53     msg = ("Verify that OpenDaylight can start/communicate with zrpcd/Quagga")
54     results.record_action(msg)
55     results.add_to_summary(0, "-")
56     if not controllers:
57         msg = ("Controller (ODL) list is empty. Skipping rest of tests.")
58         logger.info(msg)
59         results.add_failure(msg)
60         return results.compile_summary()
61     else:
62         msg = ("Controller (ODL) list is ready")
63         logger.info(msg)
64         results.add_success(msg)
65
66     controller = controllers[0]  # We don't handle HA well
67     get_ext_ip_cmd = "ip a | grep br-ex | grep inet | awk '{print $2}'"
68     controller_ext_ip = controller.run_cmd(get_ext_ip_cmd).split("/")[0]
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     cmd = "systemctl status zrpcd"
75     output = controller.run_cmd(cmd)
76     msg = ("zrpcd is running")
77
78     if not output:
79         logger.info("zrpcd is not running on the controller node")
80         results.add_failure(msg)
81     else:
82         logger.info("zrpcd is running on the controller node")
83         results.add_success(msg)
84
85     results.add_to_summary(0, "-")
86
87     start_quagga = "odl:configure-bgp -op start-bgp-server " \
88                    "--as-num 100 --router-id {0}".format(controller_ext_ip)
89     test_utils.run_odl_cmd(controller, start_quagga)
90
91     logger.info("Checking if bgpd is running"
92                 " on the controller node")
93
94     # Check if there is a non-zombie bgpd process
95     output_bgpd = controller.run_cmd("ps --no-headers -C "
96                                      "bgpd -o state")
97     states = output_bgpd.split()
98     running = any([s != 'Z' for s in states])
99
100     msg = ("bgpd is running")
101     if not running:
102         logger.info("bgpd is not running on the controller node")
103         results.add_failure(msg)
104     else:
105         logger.info("bgpd is running on the controller node")
106         results.add_success(msg)
107
108     results.add_to_summary(0, "-")
109
110     # We should be able to restart the speaker
111     # but the test is disabled because of buggy upstream
112     # https://github.com/6WIND/zrpcd/issues/15
113     # stop_quagga = 'odl:configure-bgp -op stop-bgp-server'
114     # test_utils.run_odl_cmd(controller, stop_quagga)
115
116     # logger.info("Checking if bgpd is still running"
117     #             " on the controller node")
118
119     # output_bgpd = controller.run_cmd("ps --no-headers -C " \
120     #                                  "bgpd -o state")
121     # states = output_bgpd.split()
122     # running = any([s != 'Z' for s in states])
123
124     # msg = ("bgpd is stopped")
125     # if not running:
126     #     logger.info("bgpd is not running on the controller node")
127     #     results.add_success(msg)
128     # else:
129     #     logger.info("bgpd is still running on the controller node")
130     #     results.add_failure(msg)
131
132     # Taken from the sfc tests
133     if not os.path.isfile(COMMON_CONFIG.ubuntu_image_path):
134         logger.info("Downloading image")
135         ft_utils.download_url(
136             "https://cloud-images.ubuntu.com/releases/16.04/"
137             "release/ubuntu-16.04-server-cloudimg-amd64-disk1.img",
138             "/home/opnfv/functest/data/")
139     else:
140         logger.info("Using old image")
141
142     glance_client = os_utils.get_glance_client()
143     nova_client = os_utils.get_nova_client()
144     neutron_client = os_utils.get_neutron_client()
145
146     sg_id = os_utils.create_security_group_full(neutron_client,
147                                                 TESTCASE_CONFIG.secgroup_name,
148                                                 TESTCASE_CONFIG.secgroup_descr)
149     test_utils.open_icmp_ssh(neutron_client, sg_id)
150     test_utils.open_bgp_port(neutron_client, sg_id)
151     net_id, _, _ = test_utils.create_network(neutron_client,
152                                              TESTCASE_CONFIG.net_1_name,
153                                              TESTCASE_CONFIG.subnet_1_name,
154                                              TESTCASE_CONFIG.subnet_1_cidr,
155                                              TESTCASE_CONFIG.router_1_name)
156
157     quagga_net_id, _, _ = test_utils.create_network(
158         neutron_client,
159         TESTCASE_CONFIG.quagga_net_name,
160         TESTCASE_CONFIG.quagga_subnet_name,
161         TESTCASE_CONFIG.quagga_subnet_cidr,
162         TESTCASE_CONFIG.quagga_router_name)
163
164     ubuntu_image_id = os_utils.create_glance_image(
165         glance_client,
166         COMMON_CONFIG.ubuntu_image_name,
167         COMMON_CONFIG.ubuntu_image_path,
168         disk="raw",
169         container="bare",
170         public="public")
171
172     # NOTE(rski) The order of this seems a bit weird but
173     # there is a reason for this, namely
174     # https://jira.opnfv.org/projects/SDNVPN/issues/SDNVPN-99
175     # so we create the quagga instance using cloud-init
176     # and immediately give it a floating IP.
177     # The cloud-init script should contain a small sleep for
178     # this to work.
179     # We also create the FIP first because it is used in the
180     # cloud-init script.
181     fip = os_utils.create_floating_ip(neutron_client)
182     # fake_fip is needed to bypass NAT
183     # see below for the reason why.
184     fake_fip = os_utils.create_floating_ip(neutron_client)
185     # pin quagga to some compute
186     compute_node = nova_client.hypervisors.list()[0]
187     quagga_compute_node = "nova:" + compute_node.hypervisor_hostname
188     # Map the hypervisor used above to a compute handle
189     # returned by releng's manager
190     for comp in computes:
191         if compute_node.host_ip in comp.run_cmd("ip a"):
192             compute = comp
193             break
194     # Get the mask of ext net of the compute where quagga is running
195     # TODO check this works on apex
196     ext_cidr = compute.run_cmd(get_ext_ip_cmd).split("/")
197     ext_net_mask = ext_cidr[1]
198     quagga_bootstrap_script = quagga.gen_quagga_setup_script(
199         controller_ext_ip,
200         fake_fip['fip_addr'],
201         ext_net_mask)
202     quagga_vm = test_utils.create_instance(
203         nova_client,
204         TESTCASE_CONFIG.quagga_instance_name,
205         ubuntu_image_id,
206         quagga_net_id,
207         sg_id,
208         fixed_ip=TESTCASE_CONFIG.quagga_instance_ip,
209         flavor=TESTCASE_CONFIG.quagga_instance_flavor,
210         userdata=quagga_bootstrap_script,
211         compute_node=quagga_compute_node)
212
213     fip_added = os_utils.add_floating_ip(nova_client,
214                                          quagga_vm.id,
215                                          fip['fip_addr'])
216
217     msg = "Assign a Floating IP to %s " % TESTCASE_CONFIG.quagga_instance_name
218     if fip_added:
219         results.add_success(msg)
220     else:
221         results.add_failure(msg)
222
223     # This part works around NAT
224     # What we do is attach the instance directly to the OpenStack
225     # external network. This way is is directly accessible from the
226     # controller without NAT. We assign a floating IP for this
227     # to make sure no overlaps happen.
228     libvirt_instance_name = getattr(quagga_vm, "OS-EXT-SRV-ATTR:instance_name")
229     compute.run_cmd("virsh attach-interface %s"
230                     " bridge br-ex" % libvirt_instance_name)
231
232     testcase = "Bootstrap quagga inside an OpenStack instance"
233     cloud_init_success = test_utils.wait_for_cloud_init(quagga_vm)
234     if cloud_init_success:
235         results.add_success(testcase)
236     else:
237         results.add_failure(testcase)
238     results.add_to_summary(0, "=")
239
240     results.add_to_summary(0, '-')
241     results.add_to_summary(1, "Peer Quagga with OpenDaylight")
242     results.add_to_summary(0, '-')
243
244     neighbor = quagga.odl_add_neighbor(fake_fip['fip_addr'],
245                                        controller_ext_ip,
246                                        controller)
247     peer = quagga.check_for_peering(controller)
248
249     if neighbor and peer:
250         results.add_success("Peering with quagga")
251     else:
252         results.add_failure("Peering with quagga")
253
254     return results.compile_summary()
255
256
257 if __name__ == '__main__':
258     main()