Fix functest failing in apex case- testcase3
[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 functest Quaggas peer
15 # - Exchange routing information with Quagga:
16 #   - Create a network, instance and BGPVPN in OpenStack
17 #   - Verify the route to the instance is present in the OpenDaylight FIB
18 #   - Verify that the functest Quagga also learns these routes
19 import os
20 import argparse
21
22 from sdnvpn.lib import quagga
23 import sdnvpn.lib.utils as test_utils
24 import sdnvpn.lib.config as sdnvpn_config
25
26 import functest.utils.openstack_utils as os_utils
27 import functest.utils.functest_utils as ft_utils
28 import functest.utils.functest_logger as ft_logger
29
30 from sdnvpn.lib.results import Results
31
32
33 COMMON_CONFIG = sdnvpn_config.CommonConfig()
34 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig("testcase_3")
35
36 logger = ft_logger.Logger("sdnvpn-testcase-3").getLogger()
37
38 parser = argparse.ArgumentParser()
39
40 parser.add_argument("-r", "--report",
41                     help="Create json result file",
42                     action="store_true")
43
44 args = parser.parse_args()
45
46 logger = ft_logger.Logger("sdnvpn-testcase-3").getLogger()
47
48 COMMON_CONFIG = sdnvpn_config.CommonConfig()
49 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig("testcase_3")
50
51
52 def main():
53     results = Results(COMMON_CONFIG.line_length)
54     results.add_to_summary(0, "=")
55     results.add_to_summary(2, "STATUS", "SUBTEST")
56     results.add_to_summary(0, "=")
57
58     openstack_nodes = test_utils.get_nodes()
59
60     controllers = [node for node in openstack_nodes
61                    if node.is_odl()]
62     msg = ("Verify that OpenDaylight can start/communicate with zrpcd/Quagga")
63     results.record_action(msg)
64     results.add_to_summary(0, "-")
65     if not controllers:
66         msg = ("Controller (ODL) list is empty. Skipping rest of tests.")
67         logger.info(msg)
68         results.add_failure(msg)
69         return results.compile_summary()
70     else:
71         msg = ("Controller (ODL) list is ready")
72         logger.info(msg)
73         results.add_success(msg)
74
75     for controller in controllers:
76         logger.info("Starting bgp speaker of controller at IP %s "
77                     % controller.ip)
78         logger.info("Checking if zrpcd is "
79                     "running on the controller node")
80
81         cmd = "systemctl status zrpcd"
82         output = controller.run_cmd(cmd)
83         msg = ("zrpcd is running")
84
85         if not output:
86             logger.info("zrpcd is not running on the controller node")
87             results.add_failure(msg)
88         else:
89             logger.info("zrpcd is running on the controller node")
90             results.add_success(msg)
91
92         results.add_to_summary(0, "-")
93
94         # TODO here we need the external ip of the controller
95         start_quagga = "odl:configure-bgp -op start-bgp-server " \
96                        "--as-num 100 --router-id {0}".format(controller.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         stop_quagga = 'odl:configure-bgp -op stop-bgp-server'
119
120         test_utils.run_odl_cmd(controller, stop_quagga)
121
122         # disabled because of buggy upstream
123         # https://github.com/6WIND/zrpcd/issues/15
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         ft_utils.download_url(
144             "https://cloud-images.ubuntu.com/releases/16.04/"
145             "release/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     sg_id = os_utils.create_security_group_full(neutron_client,
155                                                 TESTCASE_CONFIG.secgroup_name,
156                                                 TESTCASE_CONFIG.secgroup_descr)
157     test_utils.open_icmp_ssh(neutron_client, sg_id)
158     test_utils.open_bgp_port(neutron_client, sg_id)
159     net_id, _, _ = test_utils.create_network(neutron_client,
160                                              TESTCASE_CONFIG.net_1_name,
161                                              TESTCASE_CONFIG.subnet_1_name,
162                                              TESTCASE_CONFIG.subnet_1_cidr,
163                                              TESTCASE_CONFIG.router_1_name)
164
165     quagga_net_id, _, _ = test_utils.create_network(
166         neutron_client,
167         TESTCASE_CONFIG.quagga_net_name,
168         TESTCASE_CONFIG.quagga_subnet_name,
169         TESTCASE_CONFIG.quagga_subnet_cidr,
170         TESTCASE_CONFIG.quagga_router_name)
171
172     ubuntu_image_id = os_utils.create_glance_image(
173         glance_client,
174         COMMON_CONFIG.ubuntu_image_name,
175         COMMON_CONFIG.ubuntu_image_path,
176         disk="qcow2",
177         container="bare",
178         public="public")
179
180     # NOTE(rski) The order of this seems a bit weird but
181     # there is a reason for this, namely
182     # https://jira.opnfv.org/projects/SDNVPN/issues/SDNVPN-99
183     # so we create the quagga instance using cloud-init
184     # and immediately give it a floating IP.
185     # The cloud-init script should contain a small sleep for
186     # this to work.
187     # We also create the FIP first because it is used in the
188     # cloud-init script.
189     fip = os_utils.create_floating_ip(neutron_client)
190
191     quagga_bootstrap_script = quagga.gen_quagga_setup_script(
192         controllers[0].ip, fip['fip_addr'])
193     quagga_vm = test_utils.create_instance(
194         nova_client,
195         TESTCASE_CONFIG.quagga_instance_name,
196         ubuntu_image_id,
197         quagga_net_id,
198         sg_id,
199         fixed_ip=TESTCASE_CONFIG.quagga_instance_ip,
200         flavor=TESTCASE_CONFIG.quagga_instance_flavor,
201         userdata=quagga_bootstrap_script)
202     fip_added = os_utils.add_floating_ip(nova_client,
203                                          quagga_vm.id,
204                                          fip['fip_addr'])
205
206     msg = "Assign a Floating IP to %s " % TESTCASE_CONFIG.quagga_instance_name
207     if fip_added:
208         results.add_success(msg)
209     else:
210         results.add_failure(msg)
211
212     testcase = "Bootstrap quagga inside an OpenStack instance"
213     success = False
214     if success:
215         results.add_success(testcase)
216     else:
217         results.add_failure(testcase)
218     results.add_to_summary(0, "=")
219
220     results.add_to_summary(0, '-')
221     results.add_to_summary(1, "Peer Quagga with OpenDaylight")
222     results.add_to_summary(0, '-')
223
224     neighbor = quagga.odl_add_neighbor(fip['fip_addr'], controller)
225     peer = quagga.check_for_peering(controller)
226
227     image_id = os_utils.create_glance_image(glance_client,
228                                             TESTCASE_CONFIG.image_name,
229                                             COMMON_CONFIG.image_path,
230                                             disk=COMMON_CONFIG.image_format,
231                                             container="bare",
232                                             public=True)
233
234     instance = test_utils.create_instance(
235         nova_client,
236         TESTCASE_CONFIG.instance_1_name,
237         image_id,
238         net_id,
239         sg_id,
240         fixed_ip=TESTCASE_CONFIG.instance_1_ip,
241         secgroup_name=TESTCASE_CONFIG.secgroup_name)
242
243     kwargs = {"import_targets": TESTCASE_CONFIG.import_targets,
244               "export_targets": TESTCASE_CONFIG.export_targets,
245               "route_targets": TESTCASE_CONFIG.export_targets,
246               "name": "bgpvpn-3-1"}
247
248     bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
249     bgpvpn_id = bgpvpn['bgpvpn']['id']
250     os_utils.create_network_association(
251         neutron_client, bgpvpn_id, net_id)
252
253     test_utils.wait_for_instance(instance)
254
255     exchange = quagga.check_for_route_exchange(fip['fip_addr'])
256     if neighbor and peer and exchange:
257         results.add_success("Peering with quagga")
258     else:
259         results.add_failure("Peering with quagga")
260
261     return results.compile_summary()
262
263
264 if __name__ == '__main__':
265     main()