aedf0ce4371b5742e08175f8fd8c33b51b22ee25
[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 COMMON_CONFIG = sdnvpn_config.CommonConfig()
33 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig("testcase_3")
34
35 logger = ft_logger.Logger("sdnvpn-testcase-3").getLogger()
36
37 parser = argparse.ArgumentParser()
38
39 parser.add_argument("-r", "--report",
40                     help="Create json result file",
41                     action="store_true")
42
43 args = parser.parse_args()
44
45
46 def main():
47     results = Results(COMMON_CONFIG.line_length)
48     results.add_to_summary(0, "=")
49     results.add_to_summary(2, "STATUS", "SUBTEST")
50     results.add_to_summary(0, "=")
51
52     openstack_nodes = test_utils.get_nodes()
53
54     controllers = [node for node in openstack_nodes
55                    if node.is_odl()]
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     for controller in controllers:
70         logger.info("Starting bgp speaker of controller at IP %s "
71                     % controller.ip)
72         logger.info("Checking if zrpcd is "
73                     "running on the controller node")
74
75         cmd = "systemctl status zrpcd"
76         output = controller.run_cmd(cmd)
77         msg = ("zrpcd is running")
78
79         if not output:
80             logger.info("zrpcd is not running on the controller node")
81             results.add_failure(msg)
82         else:
83             logger.info("zrpcd is running on the controller node")
84             results.add_success(msg)
85
86         results.add_to_summary(0, "-")
87
88         # TODO here we need the external ip of the controller
89         start_quagga = "odl:configure-bgp -op start-bgp-server " \
90                        "--as-num 100 --router-id {0}".format(controller.ip)
91         test_utils.run_odl_cmd(controller, start_quagga)
92
93         logger.info("Checking if bgpd is running"
94                     " on the controller node")
95
96         # Check if there is a non-zombie bgpd process
97         output_bgpd = controller.run_cmd("ps --no-headers -C "
98                                          "bgpd -o state")
99         states = output_bgpd.split()
100         running = any([s != 'Z' for s in states])
101
102         msg = ("bgpd is running")
103         if not running:
104             logger.info("bgpd is not running on the controller node")
105             results.add_failure(msg)
106         else:
107             logger.info("bgpd is running on the controller node")
108             results.add_success(msg)
109
110         results.add_to_summary(0, "-")
111
112         stop_quagga = 'odl:configure-bgp -op stop-bgp-server'
113
114         test_utils.run_odl_cmd(controller, stop_quagga)
115
116         # disabled because of buggy upstream
117         # https://github.com/6WIND/zrpcd/issues/15
118         # logger.info("Checking if bgpd is still running"
119         #             " on the controller node")
120
121         # output_bgpd = controller.run_cmd("ps --no-headers -C " \
122         #                                  "bgpd -o state")
123         # states = output_bgpd.split()
124         # running = any([s != 'Z' for s in states])
125
126         # msg = ("bgpd is stopped")
127         # if not running:
128         #     logger.info("bgpd is not running on the controller node")
129         #     results.add_success(msg)
130         # else:
131         #     logger.info("bgpd is still running on the controller node")
132         #     results.add_failure(msg)
133
134     # Taken from the sfc tests
135     if not os.path.isfile(COMMON_CONFIG.ubuntu_image_path):
136         logger.info("Downloading image")
137         ft_utils.download_url(
138             "https://cloud-images.ubuntu.com/releases/16.04/"
139             "release/ubuntu-16.04-server-cloudimg-amd64-disk1.img",
140             "/home/opnfv/functest/data/")
141     else:
142         logger.info("Using old image")
143
144     glance_client = os_utils.get_glance_client()
145     nova_client = os_utils.get_nova_client()
146     neutron_client = os_utils.get_neutron_client()
147
148     sg_id = os_utils.create_security_group_full(neutron_client,
149                                                 TESTCASE_CONFIG.secgroup_name,
150                                                 TESTCASE_CONFIG.secgroup_descr)
151     test_utils.open_icmp_ssh(neutron_client, sg_id)
152     test_utils.open_bgp_port(neutron_client, sg_id)
153     net_id, _, _ = test_utils.create_network(neutron_client,
154                                              TESTCASE_CONFIG.net_1_name,
155                                              TESTCASE_CONFIG.subnet_1_name,
156                                              TESTCASE_CONFIG.subnet_1_cidr,
157                                              TESTCASE_CONFIG.router_1_name)
158
159     quagga_net_id, _, _ = test_utils.create_network(
160         neutron_client,
161         TESTCASE_CONFIG.quagga_net_name,
162         TESTCASE_CONFIG.quagga_subnet_name,
163         TESTCASE_CONFIG.quagga_subnet_cidr,
164         TESTCASE_CONFIG.quagga_router_name)
165
166     ubuntu_image_id = os_utils.create_glance_image(
167         glance_client,
168         COMMON_CONFIG.ubuntu_image_name,
169         COMMON_CONFIG.ubuntu_image_path,
170         disk="qcow2",
171         container="bare",
172         public="public")
173
174     # NOTE(rski) The order of this seems a bit weird but
175     # there is a reason for this, namely
176     # https://jira.opnfv.org/projects/SDNVPN/issues/SDNVPN-99
177     # so we create the quagga instance using cloud-init
178     # and immediately give it a floating IP.
179     # The cloud-init script should contain a small sleep for
180     # this to work.
181     # We also create the FIP first because it is used in the
182     # cloud-init script.
183     fip = os_utils.create_floating_ip(neutron_client)
184
185     quagga_bootstrap_script = quagga.gen_quagga_setup_script(
186         controllers[0].ip, fip['fip_addr'])
187     quagga_vm = test_utils.create_instance(
188         nova_client,
189         TESTCASE_CONFIG.quagga_instance_name,
190         ubuntu_image_id,
191         quagga_net_id,
192         sg_id,
193         fixed_ip=TESTCASE_CONFIG.quagga_instance_ip,
194         flavor=TESTCASE_CONFIG.quagga_instance_flavor,
195         userdata=quagga_bootstrap_script)
196     fip_added = os_utils.add_floating_ip(nova_client,
197                                          quagga_vm.id,
198                                          fip['fip_addr'])
199
200     msg = "Assign a Floating IP to %s " % TESTCASE_CONFIG.quagga_instance_name
201     if fip_added:
202         results.add_success(msg)
203     else:
204         results.add_failure(msg)
205
206     testcase = "Bootstrap quagga inside an OpenStack instance"
207     success = False
208     if success:
209         results.add_success(testcase)
210     else:
211         results.add_failure(testcase)
212     results.add_to_summary(0, "=")
213
214     results.add_to_summary(0, '-')
215     results.add_to_summary(1, "Peer Quagga with OpenDaylight")
216     results.add_to_summary(0, '-')
217
218     neighbor = quagga.odl_add_neighbor(fip['fip_addr'], controller)
219     peer = quagga.check_for_peering(controller)
220
221     image_id = os_utils.create_glance_image(glance_client,
222                                             TESTCASE_CONFIG.image_name,
223                                             COMMON_CONFIG.image_path,
224                                             disk=COMMON_CONFIG.image_format,
225                                             container="bare",
226                                             public=True)
227
228     instance = test_utils.create_instance(
229         nova_client,
230         TESTCASE_CONFIG.instance_1_name,
231         image_id,
232         net_id,
233         sg_id,
234         fixed_ip=TESTCASE_CONFIG.instance_1_ip,
235         secgroup_name=TESTCASE_CONFIG.secgroup_name)
236
237     kwargs = {"import_targets": TESTCASE_CONFIG.import_targets,
238               "export_targets": TESTCASE_CONFIG.export_targets,
239               "route_targets": TESTCASE_CONFIG.export_targets,
240               "name": "bgpvpn-3-1"}
241
242     bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
243     bgpvpn_id = bgpvpn['bgpvpn']['id']
244     os_utils.create_network_association(
245         neutron_client, bgpvpn_id, net_id)
246
247     test_utils.wait_for_instance(instance)
248
249     exchange = quagga.check_for_route_exchange(fip['fip_addr'])
250     if neighbor and peer and exchange:
251         results.add_success("Peering with quagga")
252     else:
253         results.add_failure("Peering with quagga")
254
255     return results.compile_summary()
256
257
258 if __name__ == '__main__':
259     main()