From cdc6879c3ec1bc6523fd678d10ef084dbefc785d Mon Sep 17 00:00:00 2001 From: pavel-shirshov Date: Tue, 10 Nov 2020 18:44:20 -0800 Subject: [PATCH] [bgpcfgd]: Implement BBR template test (#5850) Add the test to check that all templates, which use CONFIG_DB__BBR are configured in constants.yml --- src/sonic-bgpcfgd/tests/test_bbr_templates.py | 70 +++++++++++++++++++ .../tests/test_ipv6_nexthop_global.py | 4 +- src/sonic-bgpcfgd/tests/test_templates.py | 4 +- src/sonic-bgpcfgd/tests/util.py | 12 ++-- 4 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 src/sonic-bgpcfgd/tests/test_bbr_templates.py diff --git a/src/sonic-bgpcfgd/tests/test_bbr_templates.py b/src/sonic-bgpcfgd/tests/test_bbr_templates.py new file mode 100644 index 000000000000..989734d79714 --- /dev/null +++ b/src/sonic-bgpcfgd/tests/test_bbr_templates.py @@ -0,0 +1,70 @@ +import itertools +import os +import re + +from .util import load_constants_dir_mappings, load_constants + + +TEMPLATE_PATH = os.path.abspath('../../dockers/docker-fpm-frr/frr/bgpd/templates') + + +def find_all_files(path): + paths_to_check = [path] + res = [] + while paths_to_check: + path = paths_to_check[0] + paths_to_check = paths_to_check[1:] + for name in os.listdir(path): + full_path = "%s/%s" % (path, name) + if os.path.isfile(full_path): + res.append(full_path) + elif os.path.isdir(full_path): + paths_to_check.append(full_path) + return res + +def get_files_to_check(): + directories = load_constants_dir_mappings() + general_path = "%s/%s" % (TEMPLATE_PATH, directories['general']) + files = find_all_files(general_path) + return files + +def get_peer_groups_with_bbr(filename): + re_bbr = re.compile(r".+CONFIG_DB__BGP_BBR.+") #\['status'\] == 'enabled'") + re_endif = re.compile(r'^\s*{% +endif +%}\s*$') + re_peer = re.compile(r'^\s*neighbor\s+(\S+)\s+allowas-in\s+1\s*$') + inside_bbr = False + res = [] + with open(filename) as fp: + for line in fp: + s_line = line.strip() + if s_line == '': + continue + elif s_line.startswith('!'): + continue + elif re_bbr.match(s_line): + inside_bbr = True + elif re_peer.match(s_line) and inside_bbr: + m = re_peer.match(s_line) + pg = m.group(1) + res.append(pg) + elif re_endif.match(s_line) and inside_bbr: + inside_bbr = False + return res + +def load_constants_bbr(): + data = load_constants() + assert "bgp" in data["constants"], "'bgp' key not found in constants.yml" + assert "peers" in data["constants"]["bgp"], "'peers' key not found in constants.yml" + assert "general" in data["constants"]["bgp"]['peers'], "'general' key not found in constants.yml" + return data["constants"]["bgp"]["peers"]['general'] + +def test_bbr_templates(): + files_to_check = get_files_to_check() + pg_with_bbr_per_file = [ get_peer_groups_with_bbr(name) for name in files_to_check ] + pg_with_bbr = set(itertools.chain.from_iterable(pg_with_bbr_per_file)) + general = load_constants_bbr() + if pg_with_bbr: + assert 'bbr' in general, "BBR is not defined in 'general', but BBR is enabled for %s" % pg_with_bbr + for pg in pg_with_bbr: + assert pg in general['bbr'], "peer-group '%s' has BBR enabled, but it is not configured in constants.yml" + diff --git a/src/sonic-bgpcfgd/tests/test_ipv6_nexthop_global.py b/src/sonic-bgpcfgd/tests/test_ipv6_nexthop_global.py index 849b1f8d36be..d1bb67ee9cd4 100644 --- a/src/sonic-bgpcfgd/tests/test_ipv6_nexthop_global.py +++ b/src/sonic-bgpcfgd/tests/test_ipv6_nexthop_global.py @@ -2,7 +2,7 @@ import re from bgpcfgd.template import TemplateFabric -from .util import load_constants +from .util import load_constants_dir_mappings TEMPLATE_PATH = os.path.abspath('../../dockers/docker-fpm-frr/frr') @@ -110,7 +110,7 @@ def check_routemap(path, route_map_name): assert checked, "route-map %s wasn't found" % route_map_name def test_v6_next_hop_global(): - paths = ["tests/data/%s" % value for value in load_constants().values()] + paths = ["tests/data/%s" % value for value in load_constants_dir_mappings().values()] for path in paths: test_cases = process_instances(path) for test_case in test_cases: diff --git a/src/sonic-bgpcfgd/tests/test_templates.py b/src/sonic-bgpcfgd/tests/test_templates.py index 6c8204a28ec8..7d3bc773ad16 100644 --- a/src/sonic-bgpcfgd/tests/test_templates.py +++ b/src/sonic-bgpcfgd/tests/test_templates.py @@ -4,14 +4,14 @@ from bgpcfgd.template import TemplateFabric from bgpcfgd.config import ConfigMgr -from .util import load_constants +from .util import load_constants_dir_mappings TEMPLATE_PATH = os.path.abspath('../../dockers/docker-fpm-frr/frr') def load_tests(peer_type, template_name): - constants = load_constants() + constants = load_constants_dir_mappings() path = "tests/data/%s/%s" % (constants[peer_type], template_name) param_files = [name for name in os.listdir(path) if os.path.isfile(os.path.join(path, name)) and name.startswith("param_")] diff --git a/src/sonic-bgpcfgd/tests/util.py b/src/sonic-bgpcfgd/tests/util.py index aa6c62a56b5f..a328a272c836 100644 --- a/src/sonic-bgpcfgd/tests/util.py +++ b/src/sonic-bgpcfgd/tests/util.py @@ -3,14 +3,18 @@ CONSTANTS_PATH = os.path.abspath('../../files/image_config/constants/constants.yml') -def load_constants(): - with open(CONSTANTS_PATH) as f: - data = yaml.load(f) # FIXME" , Loader=yaml.FullLoader) +def load_constants_dir_mappings(): + data = load_constants() result = {} - assert "constants" in data, "'constants' key not found in constants.yml" assert "bgp" in data["constants"], "'bgp' key not found in constants.yml" assert "peers" in data["constants"]["bgp"], "'peers' key not found in constants.yml" for name, value in data["constants"]["bgp"]["peers"].items(): assert "template_dir" in value, "'template_dir' key not found for peer '%s'" % name result[name] = value["template_dir"] return result + +def load_constants(): + with open(CONSTANTS_PATH) as f: + data = yaml.load(f) # FIXME" , Loader=yaml.FullLoader) + assert "constants" in data, "'constants' key not found in constants.yml" + return data