Merge "Set unlimited network and subnet quotas to fix CI"
[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 from opnfv.deployment.factory import Factory as DeploymentFactory
33
34
35 COMMON_CONFIG = sdnvpn_config.CommonConfig()
36 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig("testcase_3")
37
38 logger = ft_logger.Logger("sdnvpn-testcase-3").getLogger()
39
40 parser = argparse.ArgumentParser()
41
42 parser.add_argument("-r", "--report",
43                     help="Create json result file",
44                     action="store_true")
45
46 args = parser.parse_args()
47
48 logger = ft_logger.Logger("sdnvpn-testcase-3").getLogger()
49
50 COMMON_CONFIG = sdnvpn_config.CommonConfig()
51 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig("testcase_3")
52
53
54 def main():
55     results = Results(COMMON_CONFIG.line_length)
56     results.add_to_summary(0, "=")
57     results.add_to_summary(2, "STATUS", "SUBTEST")
58     results.add_to_summary(0, "=")
59
60     # TODO unhardcode this to work with apex
61     deploymentHandler = DeploymentFactory.get_handler(
62         'fuel',
63         '10.20.0.2',
64         'root',
65         'r00tme')
66
67     openstack_nodes = deploymentHandler.get_nodes()
68
69     controllers = [node for node in openstack_nodes
70                    if node.is_odl()]
71     msg = ("Verify that OpenDaylight can start/communicate with zrpcd/Quagga")
72     results.record_action(msg)
73     results.add_to_summary(0, "-")
74
75     if not controllers:
76         msg = ("Controller (ODL) list is empty. Skipping rest of tests.")
77         logger.info(msg)
78         results.add_failure(msg)
79         return results.compile_summary()
80     else:
81         msg = ("Controller (ODL) list is ready")
82         logger.info(msg)
83         results.add_success(msg)
84
85     for controller in controllers:
86         logger.info("Starting bgp speaker of controller at IP %s "
87                     % controller.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         # TODO here we need the external ip of the controller
105         start_quagga = "odl:configure-bgp -op start-bgp-server " \
106                        "--as-num 100 --router-id {0}".format(controller.ip)
107
108         test_utils.run_odl_cmd(controller, start_quagga)
109
110         logger.info("Checking if bgpd is running"
111                     " on the controller node")
112
113         # Check if there is a non-zombie bgpd process
114         output_bgpd = controller.run_cmd("ps --no-headers -C bgpd -o state")
115         states = output_bgpd.split()
116         running = any([s != 'Z' for s in states])
117
118         msg = ("bgpd is running")
119         if not running:
120             logger.info("bgpd is not running on the controller node")
121             results.add_failure(msg)
122         else:
123             logger.info("bgpd is running on the controller node")
124             results.add_success(msg)
125
126         results.add_to_summary(0, "-")
127
128         stop_quagga = 'odl:configure-bgp -op stop-bgp-server'
129
130         test_utils.run_odl_cmd(controller, stop_quagga)
131
132         # disabled because of buggy upstream
133         # https://github.com/6WIND/zrpcd/issues/15
134         # logger.info("Checking if bgpd is still running"
135         #             " on the controller node")
136
137         # output_bgpd = controller.run_cmd("ps --no-headers -C 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     ubuntu_image_id = os_utils.create_glance_image(
182         glance_client,
183         COMMON_CONFIG.ubuntu_image_name,
184         COMMON_CONFIG.ubuntu_image_path,
185         disk="qcow2",
186         container="bare",
187         public="public")
188
189     # NOTE(rski) The order of this seems a bit weird but
190     # there is a reason for this, namely
191     # https://jira.opnfv.org/projects/SDNVPN/issues/SDNVPN-99
192     # so we create the quagga instance using cloud-init
193     # and immediately give it a floating IP.
194     # The cloud-init script should contain a small sleep for
195     # this to work.
196     # We also create the FIP first because it is used in the
197     # cloud-init script.
198     fip = os_utils.create_floating_ip(neutron_client)
199
200     quagga_bootstrap_script = quagga.gen_quagga_setup_script(
201         controllers[0].ip, fip['fip_addr'])
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     fip_added = os_utils.add_floating_ip(nova_client,
212                                          quagga_vm.id,
213                                          fip['fip_addr'])
214
215     msg = "Assign a Floating IP to %s " % TESTCASE_CONFIG.quagga_instance_name
216     if fip_added:
217         results.add_success(msg)
218     else:
219         results.add_failure(msg)
220
221     testcase = "Bootstrap quagga inside an OpenStack instance"
222     success = False
223     if success:
224         results.add_success(testcase)
225     else:
226         results.add_failure(testcase)
227     results.add_to_summary(0, "=")
228
229     results.add_to_summary(0, '-')
230     results.add_to_summary(1, "Peer Quagga with OpenDaylight")
231     results.add_to_summary(0, '-')
232
233     neighbor = quagga.odl_add_neighbor(fip['fip_addr'], controller)
234     peer = quagga.check_for_peering(controller)
235
236     image_id = os_utils.create_glance_image(glance_client,
237                                             TESTCASE_CONFIG.image_name,
238                                             COMMON_CONFIG.image_path,
239                                             disk=COMMON_CONFIG.image_format,
240                                             container="bare",
241                                             public=True)
242
243     instance = test_utils.create_instance(
244         nova_client,
245         TESTCASE_CONFIG.instance_1_name,
246         image_id,
247         net_id,
248         sg_id,
249         fixed_ip=TESTCASE_CONFIG.instance_1_ip,
250         secgroup_name=TESTCASE_CONFIG.secgroup_name)
251
252     kwargs = {"import_targets": TESTCASE_CONFIG.import_targets,
253               "export_targets": TESTCASE_CONFIG.export_targets,
254               "route_targets": TESTCASE_CONFIG.export_targets,
255               "name": "bgpvpn-3-1"}
256
257     bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
258     bgpvpn_id = bgpvpn['bgpvpn']['id']
259     os_utils.create_network_association(
260         neutron_client, bgpvpn_id, net_id)
261
262     test_utils.wait_for_instance(instance)
263
264     exchange = quagga.check_for_route_exchange(fip['fip_addr'])
265     if neighbor and peer and exchange:
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()