Skip to content

Commit

Permalink
adding unit-tests, checking for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ghooo committed Feb 17, 2022
1 parent 6a374e3 commit 21b748e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
19 changes: 13 additions & 6 deletions generic_config_updater/gu_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,22 +772,29 @@ def _get_uses_leaf_model(self, model, table, token):
return None

# a model can be a single dict or a list of dictionaries, unify to a list of dictionaries
if isinstance(uses_s, dict):
if not isinstance(uses_s, list):
uses_s = [uses_s]

sy = self._create_sonic_yang_with_loaded_models()
# find yang module for current table
table_module = sy.confDbYangMap[table]['yangModule']
# uses Example: "@name": "bgpcmn:sonic-bgp-cmn"
for uses in uses_s:
if not isinstance(uses, dict):
raise GenericConfigUpdaterError(f"'uses' is expected to be a dictionary found '{type(uses)}'.\n" \
f" uses: {uses}\n model: {model}\n table: {table}\n token: {token}")

# Assume ':' means reference to another module
if ':' in uses['@name']:
prefix = uses['@name'].split(':')[0].strip()
uses_module = sy._findYangModuleFromPrefix(prefix, table_module)
name_parts = uses['@name'].split(':')
prefix = name_parts[0].strip()
uses_module_name = sy._findYangModuleFromPrefix(prefix, table_module)
grouping = name_parts[-1].strip()
else:
uses_module = table_module
grouping = uses['@name'].split(':')[-1].strip()
leafs = sy.preProcessedYang['grouping'][uses_module][grouping]
uses_module_name = table_module['@name']
grouping = uses['@name']

leafs = sy.preProcessedYang['grouping'][uses_module_name][grouping]

leaf_model = self._get_model(leafs, token)
if leaf_model:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
"name": "bgp peer 65100",
"ebgp_multihop_ttl": "3"
}
},
"BGP_MONITORS": {
"5.6.7.8": {
"admin_status": "up",
"asn": "65000",
"holdtime": "180",
"keepalive": "60",
"local_addr": "10.0.0.11",
"name": "BGPMonitor",
"nhopself": "0",
"rrclient": "0"
}
}
}
14 changes: 14 additions & 0 deletions tests/generic_config_updater/files/config_db_with_lldp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"LLDP": {
"GLOBAL": {
"mode": "TRANSMIT",
"enabled": "true",
"hello_time": "12",
"multiplier": "5",
"supp_mgmt_address_tlv": "true",
"supp_system_capabilities_tlv": "false",
"system_name": "sonic",
"system_description": "sonic-system"
}
}
}
12 changes: 12 additions & 0 deletions tests/generic_config_updater/gu_common_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,12 @@ def check(path, xpath, config=None):
check(path="/BGP_NEIGHBOR/default|1.2.3.4/asn",
xpath="/sonic-bgp-neighbor:sonic-bgp-neighbor/BGP_NEIGHBOR/BGP_NEIGHBOR_LIST[vrf_name='default'][neighbor='1.2.3.4']/asn",
config=Files.CONFIG_DB_WITH_BGP_NEIGHBOR)
check(path="/BGP_MONITORS/5.6.7.8/name",
xpath="/sonic-bgp-monitor:sonic-bgp-monitor/BGP_MONITORS/BGP_MONITORS_LIST[addr='5.6.7.8']/name",
config=Files.CONFIG_DB_WITH_BGP_NEIGHBOR)
check(path="/LLDP/GLOBAL/mode",
xpath="/sonic-lldp:sonic-lldp/LLDP/GLOBAL/mode",
config=Files.CONFIG_DB_WITH_LLDP)

def test_convert_xpath_to_path(self):
def check(xpath, path, config=None):
Expand Down Expand Up @@ -810,6 +816,12 @@ def check(xpath, path, config=None):
check(xpath="/sonic-bgp-neighbor:sonic-bgp-neighbor/BGP_NEIGHBOR/BGP_NEIGHBOR_LIST[vrf_name='default'][neighbor='1.2.3.4']/asn",
path="/BGP_NEIGHBOR/default|1.2.3.4/asn",
config=Files.CONFIG_DB_WITH_BGP_NEIGHBOR)
check(xpath="/sonic-bgp-monitor:sonic-bgp-monitor/BGP_MONITORS/BGP_MONITORS_LIST[addr='5.6.7.8']/name",
path="/BGP_MONITORS/5.6.7.8/name",
config=Files.CONFIG_DB_WITH_BGP_NEIGHBOR)
check(xpath="/sonic-lldp:sonic-lldp/LLDP/GLOBAL/mode",
path="/LLDP/GLOBAL/mode",
config=Files.CONFIG_DB_WITH_LLDP)

def test_has_path(self):
def check(config, path, expected):
Expand Down

0 comments on commit 21b748e

Please sign in to comment.