Skip to content

Commit

Permalink
[bgpcfgd]: Implement BBR template test (#5850)
Browse files Browse the repository at this point in the history
Add the test to check that all templates, which use CONFIG_DB__BBR are configured in constants.yml
  • Loading branch information
pavel-shirshov authored Nov 11, 2020
1 parent b5cfc02 commit cdc6879
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
70 changes: 70 additions & 0 deletions src/sonic-bgpcfgd/tests/test_bbr_templates.py
Original file line number Diff line number Diff line change
@@ -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"

4 changes: 2 additions & 2 deletions src/sonic-bgpcfgd/tests/test_ipv6_nexthop_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/sonic-bgpcfgd/tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_")]
Expand Down
12 changes: 8 additions & 4 deletions src/sonic-bgpcfgd/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cdc6879

Please sign in to comment.