Merge "Fix testcase3 (peering subcase) for Apex"
[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 os
15 import argparse
16
17 from sdnvpn.lib import quagga
18 import sdnvpn.lib.utils as test_utils
19 import sdnvpn.lib.config as sdnvpn_config
20
21 import functest.utils.openstack_utils as os_utils
22 import functest.utils.functest_utils as ft_utils
23 import functest.utils.functest_logger as ft_logger
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 = ft_logger.Logger("sdnvpn-testcase-3").getLogger()
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     msg = ("Verify that OpenDaylight can start/communicate with zrpcd/Quagga")
56     results.record_action(msg)
57     results.add_to_summary(0, "-")
58     if not controllers:
59         msg = ("Controller (ODL) list is empty. Skipping rest of tests.")
60         logger.info(msg)
61         results.add_failure(msg)
62         return results.compile_summary()
63     else:
64         msg = ("Controller (ODL) list is ready")
65         logger.info(msg)
66         results.add_success(msg)
67
68     controller = controllers[0]  # We don't handle HA well
69     get_ext_ip_cmd = "sudo ip a | grep br-ex | grep inet | awk '{print $2}'"
70     ext_net_cidr = controller.run_cmd(get_ext_ip_cmd).strip().split('\n')
71     ext_net_mask = ext_net_cidr[0].split('/')[1]
72     controller_ext_ip = ext_net_cidr[0].split('/')[0]
73
74     # TODO This code has to be adapted into Apex quagga rpm spec
75     installer_type = str(os.environ['INSTALLER_TYPE'].lower())
76     if installer_type == "apex":
77         add_quagga_user = 'sudo usermod -a -G quaggavt quagga'
78         test_utils.run_odl_cmd(controller, add_quagga_user)
79
80         create_quagga_run_folder = "sudo mkdir -p /var/run/quagga/"
81         test_utils.run_odl_cmd(controller, create_quagga_run_folder)
82
83         chown_quagga_folder = 'sudo chown quagga:quagga -R /var/run/quagga/'
84         test_utils.run_odl_cmd(controller, chown_quagga_folder)
85
86     logger.info("Starting bgp speaker of controller at IP %s "
87                 % controller_ext_ip)
88     logger.info("Checking if zrpcd is "
89                 "running on the controller node")
90
91     cmd = "systemctl status zrpcd"
92     output = controller.run_cmd(cmd)
93     msg = ("zrpcd is running")
94
95     if not output:
96         logger.info("zrpcd is not running on the controller node")
97         results.add_failure(msg)
98     else:
99         logger.info("zrpcd is running on the controller node")
100         results.add_success(msg)
101
102     results.add_to_summary(0, "-")
103
104     start_quagga = "odl:configure-bgp -op start-bgp-server " \
105                    "--as-num 100 --router-id {0}".format(controller_ext_ip)
106     test_utils.run_odl_cmd(controller, start_quagga)
107
108     logger.info("Checking if bgpd is running"
109                 " on the controller node")
110
111     # Check if there is a non-zombie bgpd process
112     output_bgpd = controller.run_cmd("ps --no-headers -C "
113                                      "bgpd -o state")
114     states = output_bgpd.split()
115     running = any([s != 'Z' for s in states])
116
117     msg = ("bgpd is running")
118     if not running:
119         logger.info("bgpd is not running on the controller node")
120         results.add_failure(msg)
121     else:
122         logger.info("bgpd is running on the controller node")
123         results.add_success(msg)
124
125     results.add_to_summary(0, "-")
126
127     # We should be able to restart the speaker
128     # but the test is disabled because of buggy upstream
129     # https://github.com/6WIND/zrpcd/issues/15
130     # stop_quagga = 'odl:configure-bgp -op stop-bgp-server'
131     # test_utils.run_odl_cmd(controller, stop_quagga)
132
133     # logger.info("Checking if bgpd is still running"
134     #             " on the controller node")
135
136     # output_bgpd = controller.run_cmd("ps --no-headers -C " \
137     #                                  "bgpd -o state")
138     # states = output_bgpd.split()
139     # running = any([s != 'Z' for s in states])
140
141     # msg = ("bgpd is stopped")
142     # if not running:
143     #     logger.info("bgpd is not running on the controller node")
144     #     results.add_success(msg)
145     # else:
146     #     logger.info("bgpd is still running on the controller node")
147     #     results.add_failure(msg)
148
149     # Taken from the sfc tests
150     if not os.path.isfile(COMMON_CONFIG.ubuntu_image_path):
151         logger.info("Downloading image")
152         ft_utils.download_url(
153             "https://cloud-images.ubuntu.com/releases/16.04/"
154             "release/ubuntu-16.04-server-cloudimg-amd64-disk1.img",
155             "/home/opnfv/functest/data/")
156     else:
157         logger.info("Using old image")
158
159     glance_client = os_utils.get_glance_client()
160     nova_client = os_utils.get_nova_client()
161     neutron_client = os_utils.get_neutron_client()
162
163     sg_id = os_utils.create_security_group_full(neutron_client,
164                                                 TESTCASE_CONFIG.secgroup_name,
165                                                 TESTCASE_CONFIG.secgroup_descr)
166     test_utils.open_icmp_ssh(neutron_client, sg_id)
167     test_utils.open_bgp_port(neutron_client, sg_id)
168     net_id, _, _ = test_utils.create_network(neutron_client,
169                                              TESTCASE_CONFIG.net_1_name,
170                                              TESTCASE_CONFIG.subnet_1_name,
171                                              TESTCASE_CONFIG.subnet_1_cidr,
172                                              TESTCASE_CONFIG.router_1_name)
173
174     quagga_net_id, _, _ = test_utils.create_network(
175         neutron_client,
176         TESTCASE_CONFIG.quagga_net_name,
177         TESTCASE_CONFIG.quagga_subnet_name,
178         TESTCASE_CONFIG.quagga_subnet_cidr,
179         TESTCASE_CONFIG.quagga_router_name)
180
181     if installer_type == "fuel":
182         disk = 'raw'
183     elif installer_type == "apex":
184         disk = 'qcow2'
185     else:
186         logger.error("Incompatible installer type")
187
188     ubuntu_image_id = os_utils.create_glance_image(
189         glance_client,
190         COMMON_CONFIG.ubuntu_image_name,
191         COMMON_CONFIG.ubuntu_image_path,
192         disk,
193         container="bare",
194         public="public")
195
196     # NOTE(rski) The order of this seems a bit weird but
197     # there is a reason for this, namely
198     # https://jira.opnfv.org/projects/SDNVPN/issues/SDNVPN-99
199     # so we create the quagga instance using cloud-init
200     # and immediately give it a floating IP.
201     # The cloud-init script should contain a small sleep for
202     # this to work.
203     # We also create the FIP first because it is used in the
204     # cloud-init script.
205     fip = os_utils.create_floating_ip(neutron_client)
206     # fake_fip is needed to bypass NAT
207     # see below for the reason why.
208     fake_fip = os_utils.create_floating_ip(neutron_client)
209     # pin quagga to some compute
210     compute_node = nova_client.hypervisors.list()[0]
211     quagga_compute_node = "nova:" + compute_node.hypervisor_hostname
212     # Map the hypervisor used above to a compute handle
213     # returned by releng's manager
214     for comp in computes:
215         if compute_node.host_ip in comp.run_cmd("sudo ip a"):
216             compute = comp
217             break
218     quagga_bootstrap_script = quagga.gen_quagga_setup_script(
219         controller_ext_ip,
220         fake_fip['fip_addr'],
221         ext_net_mask)
222
223     test_utils.create_custom_flavor()
224
225     quagga_vm = test_utils.create_instance(
226         nova_client,
227         TESTCASE_CONFIG.quagga_instance_name,
228         ubuntu_image_id,
229         quagga_net_id,
230         sg_id,
231         fixed_ip=TESTCASE_CONFIG.quagga_instance_ip,
232         flavor=COMMON_CONFIG.custom_flavor_name,
233         userdata=quagga_bootstrap_script,
234         compute_node=quagga_compute_node)
235
236     fip_added = os_utils.add_floating_ip(nova_client,
237                                          quagga_vm.id,
238                                          fip['fip_addr'])
239
240     msg = "Assign a Floating IP to %s " % TESTCASE_CONFIG.quagga_instance_name
241     if fip_added:
242         results.add_success(msg)
243     else:
244         results.add_failure(msg)
245
246     test_utils.attach_instance_to_ext_br(quagga_vm, compute)
247
248     testcase = "Bootstrap quagga inside an OpenStack instance"
249     cloud_init_success = test_utils.wait_for_cloud_init(quagga_vm)
250     if cloud_init_success:
251         results.add_success(testcase)
252     else:
253         results.add_failure(testcase)
254     results.add_to_summary(0, "=")
255
256     results.add_to_summary(0, '-')
257     results.add_to_summary(1, "Peer Quagga with OpenDaylight")
258     results.add_to_summary(0, '-')
259
260     neighbor = quagga.odl_add_neighbor(fake_fip['fip_addr'],
261                                        controller_ext_ip,
262                                        controller)
263     peer = quagga.check_for_peering(controller)
264
265     if neighbor and peer:
266         results.add_success("Peering with quagga")
267     else:
268         results.add_failure("Peering with quagga")
269
270     return results.compile_summary()
271
272
273 if __name__ == '__main__':
274     main()