[bgpcfgd]: Implement BBR template test (#5850)
Add the test to check that all templates, which use CONFIG_DB__BBR are configured in constants.yml
This commit is contained in:
parent
b5cfc02552
commit
cdc6879c3e
70
src/sonic-bgpcfgd/tests/test_bbr_templates.py
Normal file
70
src/sonic-bgpcfgd/tests/test_bbr_templates.py
Normal file
@ -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"
|
||||
|
@ -2,7 +2,7 @@ import os
|
||||
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:
|
||||
|
@ -4,14 +4,14 @@ import json
|
||||
|
||||
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_")]
|
||||
|
@ -3,14 +3,18 @@ import yaml
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user