Skip to content

Commit f374616

Browse files
authored
[GCU] Fix JsonPointerFilter bug (sonic-net#2477)
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.
1 parent 58dbb3e commit f374616

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

generic_config_updater/patch_sorter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,10 @@ def _get_paths_recursive(self, config, pattern_tokens, matching_tokens, idx, com
399399
if token == "*":
400400
matching_keys = config.keys()
401401
elif token.startswith("*|"):
402-
suffix = token[2:]
402+
suffix = token[1:]
403403
matching_keys = [key for key in config.keys() if key.endswith(suffix)]
404404
elif token.endswith("|*"):
405-
prefix = token[:-2]
405+
prefix = token[:-1]
406406
matching_keys = [key for key in config.keys() if key.startswith(prefix)]
407407
elif token in config:
408408
matching_keys = [token]

tests/generic_config_updater/patch_sorter_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,41 @@ def test_simulate__applies_move(self):
753753
# Assert
754754
self.assertIs(self.any_diff, actual)
755755

756+
class TestJsonPointerFilter(unittest.TestCase):
757+
def test_get_paths__common_prefix__exact_match_returned(self):
758+
config = {
759+
"BUFFER_PG": {
760+
"Ethernet1|0": {},
761+
"Ethernet12|0": {},
762+
"Ethernet120|0": {}, # 'Ethernet12' is a common prefix with the previous line
763+
},
764+
}
765+
766+
filter = ps.JsonPointerFilter([["BUFFER_PG", "Ethernet12|*"]], PathAddressing())
767+
768+
expected_paths = ["/BUFFER_PG/Ethernet12|0"]
769+
770+
actual_paths = list(filter.get_paths(config))
771+
772+
self.assertCountEqual(expected_paths, actual_paths)
773+
774+
def test_get_paths__common_suffix__exact_match_returned(self):
775+
config = {
776+
"QUEUE": {
777+
"Ethernet1|0": {},
778+
"Ethernet1|10": {},
779+
"Ethernet1|110": {}, # 10 is a common suffix with the previous line
780+
},
781+
}
782+
783+
filter = ps.JsonPointerFilter([["QUEUE", "*|10"]], PathAddressing())
784+
785+
expected_paths = ["/QUEUE/Ethernet1|10"]
786+
787+
actual_paths = list(filter.get_paths(config))
788+
789+
self.assertCountEqual(expected_paths, actual_paths)
790+
756791
class TestRequiredValueIdentifier(unittest.TestCase):
757792
def test_hard_coded_required_value_data(self):
758793
identifier = ps.RequiredValueIdentifier(PathAddressing())

0 commit comments

Comments
 (0)