Skip to content

Commit

Permalink
[GCU] Fix JsonPointerFilter bug (sonic-net#2477)
Browse files Browse the repository at this point in the history
What I did
Fix a bug of JsonPointerFilter in GCU that will make the wrong selection of candidates.

How I did it
Add the patch addressing '|' in the prefix and suffix filter to avoid wrong selection.

How to verify it
Add UT.
  • Loading branch information
wen587 authored and Muhammad Danish committed Nov 23, 2022
1 parent 68d4f5c commit ee6a34b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions generic_config_updater/patch_sorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,10 @@ def _get_paths_recursive(self, config, pattern_tokens, matching_tokens, idx, com
if token == "*":
matching_keys = config.keys()
elif token.startswith("*|"):
suffix = token[2:]
suffix = token[1:]
matching_keys = [key for key in config.keys() if key.endswith(suffix)]
elif token.endswith("|*"):
prefix = token[:-2]
prefix = token[:-1]
matching_keys = [key for key in config.keys() if key.startswith(prefix)]
elif token in config:
matching_keys = [token]
Expand Down
35 changes: 35 additions & 0 deletions tests/generic_config_updater/patch_sorter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,41 @@ def test_simulate__applies_move(self):
# Assert
self.assertIs(self.any_diff, actual)

class TestJsonPointerFilter(unittest.TestCase):
def test_get_paths__common_prefix__exact_match_returned(self):
config = {
"BUFFER_PG": {
"Ethernet1|0": {},
"Ethernet12|0": {},
"Ethernet120|0": {}, # 'Ethernet12' is a common prefix with the previous line
},
}

filter = ps.JsonPointerFilter([["BUFFER_PG", "Ethernet12|*"]], PathAddressing())

expected_paths = ["/BUFFER_PG/Ethernet12|0"]

actual_paths = list(filter.get_paths(config))

self.assertCountEqual(expected_paths, actual_paths)

def test_get_paths__common_suffix__exact_match_returned(self):
config = {
"QUEUE": {
"Ethernet1|0": {},
"Ethernet1|10": {},
"Ethernet1|110": {}, # 10 is a common suffix with the previous line
},
}

filter = ps.JsonPointerFilter([["QUEUE", "*|10"]], PathAddressing())

expected_paths = ["/QUEUE/Ethernet1|10"]

actual_paths = list(filter.get_paths(config))

self.assertCountEqual(expected_paths, actual_paths)

class TestRequiredValueIdentifier(unittest.TestCase):
def test_hard_coded_required_value_data(self):
identifier = ps.RequiredValueIdentifier(PathAddressing())
Expand Down

0 comments on commit ee6a34b

Please sign in to comment.