diff options
author | Romanos Skiadas <rski@intracom-telecom.com> | 2016-09-08 16:23:40 +0300 |
---|---|---|
committer | Romanos Skiadas <rski@intracom-telecom.com> | 2017-03-02 15:54:33 +0200 |
commit | be5cd25ed85bb42f4115b49aec13ddfec20c1b97 (patch) | |
tree | 656520216279d026ad7c1bd3c8eb013deb810648 | |
parent | 71273a2b8d51725be2743010fb65fb46229dd10c (diff) |
Implement quagga peering
- Create an openstack instance
- Assign it a floating IP
- Execute a quagga bootstrap script using cloud init
- Tell ODL to peer with the instance
- Start an instance in a VPN
- make sure the route was exchanged between the peered quagga and ODL
Change-Id: I73bcaec5425df2b953c2bceaca7d4f09ff28f3d0
Signed-off-by: Romanos Skiadas <rski@intracom-telecom.com>
-rw-r--r-- | sdnvpn/artifacts/quagga_setup.sh | 68 | ||||
-rw-r--r-- | sdnvpn/lib/config.py | 12 | ||||
-rw-r--r-- | sdnvpn/lib/quagga.py | 76 | ||||
-rw-r--r-- | sdnvpn/lib/results.py | 6 | ||||
-rw-r--r-- | sdnvpn/lib/utils.py | 62 | ||||
-rw-r--r-- | sdnvpn/test/functest/config.yaml | 22 | ||||
-rw-r--r-- | sdnvpn/test/functest/testcase_3.py | 166 |
7 files changed, 397 insertions, 15 deletions
diff --git a/sdnvpn/artifacts/quagga_setup.sh b/sdnvpn/artifacts/quagga_setup.sh new file mode 100644 index 0000000..6da50e1 --- /dev/null +++ b/sdnvpn/artifacts/quagga_setup.sh @@ -0,0 +1,68 @@ +#! /bin/bash + +set -xe + +# change the password because this script is run on a passwordless cloud-image +echo 'ubuntu:opnfv' | chpasswd + +# Wait for a floating IP +# as a workaround to NAT breakage +sleep 20 + +# Variables to be filled in with python +NEIGHBOR_IP=%s +OWN_IP=%s + +ZEBRA_CONFIG_LOCATION="/etc/quagga/zebra.conf" +DAEMONS_FILE_LOCATION="/etc/quagga/daemons" +BGPD_CONFIG_LOCATION="/etc/quagga/daemons" +BGPD_LOG_FILE="/var/log/bgpd.log" + +DEBIAN_FONTEND=noninteractive apt-get update +DEBIAN_FRONTEND=noninteractive apt-get install quagga -y + +touch $BGPD_LOG_FILE +chown quagga:quagga $BGPD_LOG_FILE + +chown quagga:quagga $DAEMONS_FILE_LOCATION +cat <<CATEOF > $DAEMONS_FILE_LOCATION +zebra=yes +bgpd=yes +ospfd=no +ospf6d=no +ripd=no +ripngd=no +isisd=no +babeld=no +CATEOF + +touch $ZEBRA_CONFIG_LOCATION +chown quagga:quagga $ZEBRA_CONFIG_LOCATION + +cat <<CATEOF > $BGPD_CONFIG_LOCATION +! -*- bgp -*- + +hostname bgpd +password sdncbgpc + +router bgp 200 + bgp router-id ${OWN_IP} + neighbor ${NEIGHBOR_IP} remote-as 100 + no neighbor ${NEIGHBOR_IP} activate +! + address-family vpnv4 unicast + neighbor ${NEIGHBOR_IP} activate + exit-address-family +! +line vty + exec-timeout 0 0 +! +debug bgp events +debug bgp updates +log file ${BGPD_LOG_FILE} +end +CATEOF +chown quagga:quagga $BGPD_CONFIG_LOCATION + +pgrep bgpd +pgrep zebra diff --git a/sdnvpn/lib/config.py b/sdnvpn/lib/config.py index 7fc0cd4..4fd40ed 100644 --- a/sdnvpn/lib/config.py +++ b/sdnvpn/lib/config.py @@ -21,7 +21,6 @@ class CommonConfig(object): """ Common configuration parameters across testcases """ - def __init__(self): self.repo_path = CONST.dir_repo_sdnvpn self.config_file = os.path.join(self.repo_path, @@ -29,6 +28,9 @@ class CommonConfig(object): self.keyfile_path = os.path.join(self.repo_path, 'sdnvpn/artifacts/id_rsa') self.test_db = CONST.results_test_db_url + self.quagga_setup_script_path = os.path.join( + self.repo_path, + "sdnvpn/artifacts/quagga_setup.sh") self.line_length = 90 # length for the summary table self.vm_boot_timeout = 180 self.default_flavor = ft_utils.get_parameter_from_yaml( @@ -37,6 +39,14 @@ class CommonConfig(object): self.image_format = CONST.openstack_image_disk_format self.image_path = '{0}/{1}'.format(CONST.dir_functest_data, self.image_filename) + # This is the ubuntu image used by sfc + # Basically vanilla ubuntu + some scripts in there + # We can use it to setup a quagga instance + # TODO does functest have an ubuntu image somewhere? + self.ubuntu_image_name = "sdnvpn-ubuntu" + self.ubuntu_image_path = '{0}/{1}'.format( + CONST.dir_functest_data, + "ubuntu-16.04-server-cloudimg-amd64-disk1.img") class TestcaseConfig(object): diff --git a/sdnvpn/lib/quagga.py b/sdnvpn/lib/quagga.py new file mode 100644 index 0000000..e2885c2 --- /dev/null +++ b/sdnvpn/lib/quagga.py @@ -0,0 +1,76 @@ +"""Utilities for setting up quagga peering""" + +import re +import time + +import functest.utils.functest_logger as ft_logger +import functest.utils.functest_utils as ft_utils +import sdnvpn.lib.config as config +from sdnvpn.lib.utils import run_odl_cmd, exec_cmd + +logger = ft_logger.Logger("sdnvpn-quagga").getLogger() + +COMMON_CONFIG = config.CommonConfig() + + +def odl_add_neighbor(neighbor_ip, controller): + command = 'configure-bgp -op add-neighbor --as-num 200' + command += ' --ip %s --use-source-ip %s' % (neighbor_ip, controller.ip) + success = run_odl_cmd(controller, command) + return success + + +def bootstrap_quagga(fip_addr, controller_ip): + script = gen_quagga_setup_script( + controller_ip, + fip_addr) + cmd = "sshpass -popnfv ssh opnfv@%s << EOF %s EOF" % (fip_addr, script) + rc = ft_utils.execute_command(cmd) + return rc == 0 + + +def gen_quagga_setup_script(controller_ip, instance_floating_ip): + with open(COMMON_CONFIG.quagga_setup_script_path) as f: + template = f.read() + script = template % (controller_ip, instance_floating_ip) + return script + + +def check_for_peering(controller): + cmd = 'show-bgp --cmd "ip bgp neighbors"' + tries = 20 + neighbors = None + bgp_state_regex = re.compile("(BGP state =.*)") + opens_regex = re.compile("Opens:(.*)") + while tries > 0: + if neighbors and 'Established' in neighbors: + break + neighbors = run_odl_cmd(controller, cmd) + logger.info("Output of %s: %s", cmd, neighbors) + if neighbors: + opens = opens_regex.search(neighbors) + if opens: + logger.info("Opens sent/received: %s", opens.group(1)) + state = bgp_state_regex.search(neighbors) + if state: + logger.info("Peering state: %s", state.group(1)) + tries -= 1 + time.sleep(1) + + if not neighbors or 'Established' not in neighbors: + logger.error("Quagga failed to peer with OpenDaylight") + logger.error("OpenDaylight status: %s", neighbors) + return False + + logger.info("Quagga peered with OpenDaylight") + return True + + +def check_for_route_exchange(ip): + """Check that Quagga has learned the route to an IP""" + logger.debug("Checking that '%s' is in the Zebra routing table", ip) + routes, success = exec_cmd("vtysh -c 'show ip route'", verbose=True) + if not success: + return False + logger.debug("Zebra routing table: %s", routes) + return ip in routes diff --git a/sdnvpn/lib/results.py b/sdnvpn/lib/results.py index 5661d07..9f4fd19 100644 --- a/sdnvpn/lib/results.py +++ b/sdnvpn/lib/results.py @@ -114,6 +114,12 @@ class Results(object): def add_success(self, test): self.add_to_summary(2, "PASS", test) + def add_subtest(self, test, successful): + if successful: + self.add_success(test) + else: + self.add_failure(test) + def check_ssh_output(self, vm_source, vm_target, expected, timeout=30): console_log = vm_source.get_console_output() diff --git a/sdnvpn/lib/utils.py b/sdnvpn/lib/utils.py index 00d0fa7..149a37e 100644 --- a/sdnvpn/lib/utils.py +++ b/sdnvpn/lib/utils.py @@ -9,10 +9,12 @@ # import sys import time +import requests +import re +import subprocess import functest.utils.functest_logger as ft_logger import functest.utils.openstack_utils as os_utils -import re from sdnvpn.lib import config as sdnvpn_config @@ -20,6 +22,9 @@ logger = ft_logger.Logger("sndvpn_test_utils").getLogger() common_config = sdnvpn_config.CommonConfig() +ODL_USER = 'admin' +ODL_PASS = 'admin' + def create_net(neutron_client, name): logger.debug("Creating network %s", name) @@ -301,3 +306,58 @@ def open_icmp_ssh(neutron_client, security_group_id): security_group_id, 'tcp', 80, 80) + + +def open_bgp_port(neutron_client, security_group_id): + os_utils.create_secgroup_rule(neutron_client, + security_group_id, + 'tcp', + 179, 179) + + +def exec_cmd(cmd, verbose): + success = True + logger.debug("Executing '%s'" % cmd) + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + output = "" + for line in iter(p.stdout.readline, b''): + output += line + + if verbose: + logger.debug(output) + + p.stdout.close() + returncode = p.wait() + if returncode != 0: + logger.error("Command %s failed to execute." % cmd) + success = False + + return output, success + + +def check_odl_fib(ip, controller_ip): + """Check that there is an entry in the ODL Fib for `ip`""" + url = "http://" + controller_ip + \ + ":8181/restconf/config/odl-fib:fibEntries/" + logger.debug("Querring '%s' for FIB entries", url) + res = requests.get(url, auth=(ODL_USER, ODL_PASS)) + if res.status_code != 200: + logger.error("OpenDaylight response status code: %s", res.status_code) + return False + logger.debug("Checking whether '%s' is in the OpenDaylight FIB" + % controller_ip) + logger.debug("OpenDaylight FIB: \n%s" % res.text) + return ip in res.text + + +def run_odl_cmd(odl_node, cmd): + ''' + Run a command in the OpenDaylight Karaf shell + + This is a bit flimsy because of shell quote escaping, make sure that + the cmd passed does not have any top level double quotes or this + function will break. + ''' + karaf_cmd = '/opt/opendaylight/bin/client "%s" ' % cmd + return odl_node.run_cmd(karaf_cmd) diff --git a/sdnvpn/test/functest/config.yaml b/sdnvpn/test/functest/config.yaml index 4a0628b..dd46dde 100644 --- a/sdnvpn/test/functest/config.yaml +++ b/sdnvpn/test/functest/config.yaml @@ -67,9 +67,29 @@ testcases: route_distinguishers2: '222:222' testcase_3: - enabled: true + enabled: false description: Data center gateway integration testname_db: functest_testcase_3 + secgroup_name: sdnvpn-sg + secgroup_descr: Security group for SDNVPN test cases + image_name: sdnvpn-image + ubuntu_image_name: sdnvpn-ubuntu-image + net_1_name: sdnvpn-3-1-net + subnet_1_name: sdnvpn-3-1-subnet + subnet_1_cidr: 10.10.10.0/24 + router_1_name: sdnvpn-3-1-router + quagga_net_name: sdnvpn-3-2-quagga-net + quagga_subnet_name: sdnvpn-3-2-quagga-subnet + quagga_subnet_cidr: 10.10.11.0/24 + quagga_router_name: sdnvpn-3-2-quagga-router + quagga_instance_name: sdnvpn-3-2-quagga + quagga_instance_ip: 10.10.11.5 + quagga_instance_flavor: m1.small + instance_1_name: sdnvpn-3-1 + instance_1_ip: 10.10.10.5 + import_targets: '31:31' + export_targets: '32:32' + testcase_4: enabled: true diff --git a/sdnvpn/test/functest/testcase_3.py b/sdnvpn/test/functest/testcase_3.py index 42b672a..26593f8 100644 --- a/sdnvpn/test/functest/testcase_3.py +++ b/sdnvpn/test/functest/testcase_3.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # # Copyright (c) 2017 All rights reserved # This program and the accompanying materials @@ -7,13 +6,37 @@ # # http://www.apache.org/licenses/LICENSE-2.0 # - +# Tests performed: +# - Peering OpenDaylight with Quagga: +# - Set up a Quagga instance in the functest container +# - Start a BGP router with OpenDaylight +# - Add the functest Quagga as a neighbor +# - Verify that the OpenDaylight and functest Quaggas peer +# - Exchange routing information with Quagga: +# - Create a network, instance and BGPVPN in OpenStack +# - Verify the route to the instance is present in the OpenDaylight FIB +# - Verify that the functest Quagga also learns these routes +import os import argparse + +from sdnvpn.lib import quagga +import sdnvpn.lib.utils as test_utils +import sdnvpn.lib.config as sdnvpn_config + +import functest.utils.openstack_utils as os_utils +import functest.utils.functest_utils as ft_utils import functest.utils.functest_logger as ft_logger -from sdnvpn.lib import config as sdnvpn_config + from sdnvpn.lib.results import Results + from opnfv.deployment.factory import Factory as DeploymentFactory + +COMMON_CONFIG = sdnvpn_config.CommonConfig() +TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig("testcase_3") + +logger = ft_logger.Logger("sdnvpn-testcase-3").getLogger() + parser = argparse.ArgumentParser() parser.add_argument("-r", "--report", @@ -45,15 +68,15 @@ def main(): controllers = [node for node in openstack_nodes if node.is_odl()] - msg = ("Verify that OpenDaylight can start/communicate with zrpcd/Quagga") results.record_action(msg) results.add_to_summary(0, "-") if not controllers: - msg = ("Controller (ODL) list is empty") + msg = ("Controller (ODL) list is empty. Skipping rest of tests.") logger.info(msg) results.add_failure(msg) + return results.compile_summary() else: msg = ("Controller (ODL) list is ready") logger.info(msg) @@ -79,11 +102,10 @@ def main(): results.add_to_summary(0, "-") # TODO here we need the external ip of the controller - cmd_start_quagga = '/opt/opendaylight/bin/client "odl:configure-bgp ' \ - '-op start-bgp-server --as-num 100 ' \ - '--router-id {0}"'.format(controller.ip) + start_quagga = "odl:configure-bgp -op start-bgp-server " \ + "--as-num 100 --router-id {0}".format(controller.ip) - controller.run_cmd(cmd_start_quagga) + test_utils.run_odl_cmd(controller, start_quagga) logger.info("Checking if bgpd is running" " on the controller node") @@ -103,10 +125,9 @@ def main(): results.add_to_summary(0, "-") - cmd_stop_quagga = '/opt/opendaylight/bin/client -v "odl:configure' \ - '-bgp -op stop-bgp-server"' + stop_quagga = 'odl:configure-bgp -op stop-bgp-server' - controller.run_cmd(cmd_stop_quagga) + test_utils.run_odl_cmd(controller, stop_quagga) # disabled because of buggy upstream # https://github.com/6WIND/zrpcd/issues/15 @@ -125,6 +146,127 @@ def main(): # logger.info("bgpd is still running on the controller node") # results.add_failure(msg) + # Taken from the sfc tests + if not os.path.isfile(COMMON_CONFIG.ubuntu_image_path): + logger.info("Downloading image") + ft_utils.download_url( + "https://cloud-images.ubuntu.com/releases/16.04/" + "release/ubuntu-16.04-server-cloudimg-amd64-disk1.img", + "/home/opnfv/functest/data/") + else: + logger.info("Using old image") + + glance_client = os_utils.get_glance_client() + nova_client = os_utils.get_nova_client() + neutron_client = os_utils.get_neutron_client() + + sg_id = os_utils.create_security_group_full(neutron_client, + TESTCASE_CONFIG.secgroup_name, + TESTCASE_CONFIG.secgroup_descr) + test_utils.open_icmp_ssh(neutron_client, sg_id) + test_utils.open_bgp_port(neutron_client, sg_id) + net_id, _, _ = test_utils.create_network(neutron_client, + TESTCASE_CONFIG.net_1_name, + TESTCASE_CONFIG.subnet_1_name, + TESTCASE_CONFIG.subnet_1_cidr, + TESTCASE_CONFIG.router_1_name) + + quagga_net_id, _, _ = test_utils.create_network( + neutron_client, + TESTCASE_CONFIG.quagga_net_name, + TESTCASE_CONFIG.quagga_subnet_name, + TESTCASE_CONFIG.quagga_subnet_cidr, + TESTCASE_CONFIG.quagga_router_name) + + ubuntu_image_id = os_utils.create_glance_image( + glance_client, + COMMON_CONFIG.ubuntu_image_name, + COMMON_CONFIG.ubuntu_image_path, + disk="qcow2", + container="bare", + public="public") + + # NOTE(rski) The order of this seems a bit weird but + # there is a reason for this, namely + # https://jira.opnfv.org/projects/SDNVPN/issues/SDNVPN-99 + # so we create the quagga instance using cloud-init + # and immediately give it a floating IP. + # The cloud-init script should contain a small sleep for + # this to work. + # We also create the FIP first because it is used in the + # cloud-init script. + fip = os_utils.create_floating_ip(neutron_client) + + quagga_bootstrap_script = quagga.gen_quagga_setup_script( + controllers[0].ip, fip['fip_addr']) + quagga_vm = test_utils.create_instance( + nova_client, + TESTCASE_CONFIG.quagga_instance_name, + ubuntu_image_id, + quagga_net_id, + sg_id, + fixed_ip=TESTCASE_CONFIG.quagga_instance_ip, + flavor=TESTCASE_CONFIG.quagga_instance_flavor, + userdata=quagga_bootstrap_script) + fip_added = os_utils.add_floating_ip(nova_client, + quagga_vm.id, + fip['fip_addr']) + + msg = "Assign a Floating IP to %s " % TESTCASE_CONFIG.quagga_instance_name + if fip_added: + results.add_success(msg) + else: + results.add_failure(msg) + + testcase = "Bootstrap quagga inside an OpenStack instance" + success = False + if success: + results.add_success(testcase) + else: + results.add_failure(testcase) + results.add_to_summary(0, "=") + + results.add_to_summary(0, '-') + results.add_to_summary(1, "Peer Quagga with OpenDaylight") + results.add_to_summary(0, '-') + + neighbor = quagga.odl_add_neighbor(fip['fip_addr'], controller) + peer = quagga.check_for_peering(controller) + + image_id = os_utils.create_glance_image(glance_client, + TESTCASE_CONFIG.image_name, + COMMON_CONFIG.image_path, + disk=COMMON_CONFIG.image_format, + container="bare", + public=True) + + instance = test_utils.create_instance( + nova_client, + TESTCASE_CONFIG.instance_1_name, + image_id, + net_id, + sg_id, + fixed_ip=TESTCASE_CONFIG.instance_1_ip, + secgroup_name=TESTCASE_CONFIG.secgroup_name) + + kwargs = {"import_targets": TESTCASE_CONFIG.import_targets, + "export_targets": TESTCASE_CONFIG.export_targets, + "route_targets": TESTCASE_CONFIG.export_targets, + "name": "bgpvpn-3-1"} + + bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs) + bgpvpn_id = bgpvpn['bgpvpn']['id'] + os_utils.create_network_association( + neutron_client, bgpvpn_id, net_id) + + test_utils.wait_for_instance(instance) + + exchange = quagga.check_for_route_exchange(fip['fip_addr']) + if neighbor and peer and exchange: + results.add_success("Peering with quagga") + else: + results.add_failure("Peering with quagga") + return results.compile_summary() |