0253444093f252d8db67f1292f6bb1bb4945ad96
[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     computes = [node for node in openstack_nodes if node.is_compute()]
57     msg = ("Verify that OpenDaylight can start/communicate with zrpcd/Quagga")
58     results.record_action(msg)
59     results.add_to_summary(0, "-")
60     if not controllers:
61         msg = ("Controller (ODL) list is empty. Skipping rest of tests.")
62         logger.info(msg)
63         results.add_failure(msg)
64         return results.compile_summary()
65     else:
66         msg = ("Controller (ODL) list is ready")
67         logger.info(msg)
68         results.add_success(msg)
69
70     for controller in controllers:
71         logger.info("Starting bgp speaker of controller at IP %s "
72                     % controller.ip)
73         logger.info("Checking if zrpcd is "
74                     "running on the controller node")
75
76         cmd = "systemctl status zrpcd"
77         output = controller.run_cmd(cmd)
78         msg = ("zrpcd is running")
79
80         if not output:
81             logger.info("zrpcd is not running on the controller node")
82             results.add_failure(msg)
83         else:
84             logger.info("zrpcd is running on the controller node")
85             results.add_success(msg)
86
87         results.add_to_summary(0, "-")
88
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="raw",
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     # fake_fip is needed to bypass NAT
185     # see below for the reason why.
186     fake_fip = os_utils.create_floating_ip(neutron_client)
187     # pin quagga to some compute
188     compute_node = nova_client.hypervisors.list()[0]
189     quagga_compute_node = "nova:" + compute_node.hypervisor_hostname
190     # Map the hypervisor used above to a compute handle
191     # returned by releng's manager
192     for comp in computes:
193         if compute_node.host_ip in comp.run_cmd("ip a"):
194             compute = comp
195             break
196     # Get the mask of ext net of the compute where quagga is running
197     # TODO check this works on apex
198     cmd = "ip a | grep br-ex | grep inet | awk '{print $2}'"
199     ext_cidr = compute.run_cmd(cmd).split("/")
200     ext_net_mask = ext_cidr[1]
201     quagga_bootstrap_script = quagga.gen_quagga_setup_script(
202         controllers[0].ip,
203         fake_fip['fip_addr'],
204         ext_net_mask)
205     quagga_vm = test_utils.create_instance(
206         nova_client,
207         TESTCASE_CONFIG.quagga_instance_name,
208         ubuntu_image_id,
209         quagga_net_id,
210         sg_id,
211         fixed_ip=TESTCASE_CONFIG.quagga_instance_ip,
212         flavor=TESTCASE_CONFIG.quagga_instance_flavor,
213         userdata=quagga_bootstrap_script,
214         compute_node=quagga_compute_node)
215
216     fip_added = os_utils.add_floating_ip(nova_client,
217                                          quagga_vm.id,
218                                          fip['fip_addr'])
219
220     msg = "Assign a Floating IP to %s " % TESTCASE_CONFIG.quagga_instance_name
221     if fip_added:
222         results.add_success(msg)
223     else:
224         results.add_failure(msg)
225
226     testcase = "Bootstrap quagga inside an OpenStack instance"
227     success = False
228     if success:
229         results.add_success(testcase)
230     else:
231         results.add_failure(testcase)
232     results.add_to_summary(0, "=")
233
234     # This part works around NAT
235     # What we do is attach the instance directly to the OpenStack
236     # external network. This way is is directly accessible from the
237     # controller without NAT. We assign a floating IP for this
238     # to make sure no overlaps happen.
239     libvirt_instance_name = getattr(quagga_vm, "OS-EXT-SRV-ATTR:instance_name")
240     compute.run_cmd("virsh attach-interface %s"
241                     " bridge br-ex" % libvirt_instance_name)
242
243     results.add_to_summary(0, '-')
244     results.add_to_summary(1, "Peer Quagga with OpenDaylight")
245     results.add_to_summary(0, '-')
246
247     neighbor = quagga.odl_add_neighbor(fip['fip_addr'], controller)
248     peer = quagga.check_for_peering(controller)
249
250     image_id = os_utils.create_glance_image(glance_client,
251                                             TESTCASE_CONFIG.image_name,
252                                             COMMON_CONFIG.image_path,
253                                             disk=COMMON_CONFIG.image_format,
254                                             container="bare",
255                                             public="public")
256
257     instance = test_utils.create_instance(
258         nova_client,
259         TESTCASE_CONFIG.instance_1_name,
260         image_id,
261         net_id,
262         sg_id,
263         fixed_ip=TESTCASE_CONFIG.instance_1_ip,
264         secgroup_name=TESTCASE_CONFIG.secgroup_name)
265
266     kwargs = {"import_targets": TESTCASE_CONFIG.import_targets,
267               "export_targets": TESTCASE_CONFIG.export_targets,
268               "route_targets": TESTCASE_CONFIG.export_targets,
269               "name": "bgpvpn-3-1"}
270
271     bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
272     bgpvpn_id = bgpvpn['bgpvpn']['id']
273     os_utils.create_network_association(
274         neutron_client, bgpvpn_id, net_id)
275
276     test_utils.wait_for_instance(instance)
277
278     exchange = quagga.check_for_route_exchange(fip['fip_addr'])
279     if neighbor and peer and exchange:
280         results.add_success("Peering with quagga")
281     else:
282         results.add_failure("Peering with quagga")
283
284     return results.compile_summary()
285
286
287 if __name__ == '__main__':
288     main()