Skip to content

Commit

Permalink
[GCU] protect loopback0 from deletion (#2638)
Browse files Browse the repository at this point in the history
What I did
Refer to sonic-net/sonic-buildimage#11171, protect loopback0 from deletion

How I did it
Add patch checker to fail the validation when remove loopback0

How to verify it
Unit test
  • Loading branch information
wen587 authored Feb 10, 2023
1 parent 9126e7f commit 7e94c5f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
11 changes: 8 additions & 3 deletions generic_config_updater/gu_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,14 @@ def validate_field_operation(self, old_config, target_config):
patch = jsonpatch.JsonPatch.from_diff(old_config, target_config)

# illegal_operations_to_fields_map['remove'] yields a list of fields for which `remove` is an illegal operation
illegal_operations_to_fields_map = {'add':[],
'replace': [],
'remove': ['/PFC_WD/GLOBAL/POLL_INTERVAL', '/PFC_WD/GLOBAL']}
illegal_operations_to_fields_map = {
'add':[],
'replace': [],
'remove': [
'/PFC_WD/GLOBAL/POLL_INTERVAL',
'/PFC_WD/GLOBAL',
'/LOOPBACK_INTERFACE/Loopback0']
}
for operation, field_list in illegal_operations_to_fields_map.items():
for field in field_list:
if any(op['op'] == operation and field == op['path'] for op in patch):
Expand Down
42 changes: 39 additions & 3 deletions tests/generic_config_updater/gu_common_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,54 @@ def setUp(self):
self.config_wrapper_mock = gu_common.ConfigWrapper()
self.config_wrapper_mock.get_config_db_as_json=MagicMock(return_value=Files.CONFIG_DB_AS_JSON)

def test_validate_field_operation_legal(self):
def test_validate_field_operation_legal__pfcwd(self):
old_config = {"PFC_WD": {"GLOBAL": {"POLL_INTERVAL": "60"}}}
target_config = {"PFC_WD": {"GLOBAL": {"POLL_INTERVAL": "40"}}}
config_wrapper = gu_common.ConfigWrapper()
config_wrapper.validate_field_operation(old_config, target_config)

def test_validate_field_operation_illegal(self):

def test_validate_field_operation_legal__rm_loopback1(self):
old_config = {
"LOOPBACK_INTERFACE": {
"Loopback0": {},
"Loopback0|10.1.0.32/32": {},
"Loopback1": {},
"Loopback1|10.1.0.33/32": {}
}
}
target_config = {
"LOOPBACK_INTERFACE": {
"Loopback0": {},
"Loopback0|10.1.0.32/32": {}
}
}
config_wrapper = gu_common.ConfigWrapper()
config_wrapper.validate_field_operation(old_config, target_config)

def test_validate_field_operation_illegal__pfcwd(self):
old_config = {"PFC_WD": {"GLOBAL": {"POLL_INTERVAL": 60}}}
target_config = {"PFC_WD": {"GLOBAL": {}}}
config_wrapper = gu_common.ConfigWrapper()
self.assertRaises(gu_common.IllegalPatchOperationError, config_wrapper.validate_field_operation, old_config, target_config)

def test_validate_field_operation_illegal__rm_loopback0(self):
old_config = {
"LOOPBACK_INTERFACE": {
"Loopback0": {},
"Loopback0|10.1.0.32/32": {},
"Loopback1": {},
"Loopback1|10.1.0.33/32": {}
}
}
target_config = {
"LOOPBACK_INTERFACE": {
"Loopback1": {},
"Loopback1|10.1.0.33/32": {}
}
}
config_wrapper = gu_common.ConfigWrapper()
self.assertRaises(gu_common.IllegalPatchOperationError, config_wrapper.validate_field_operation, old_config, target_config)

def test_ctor__default_values_set(self):
config_wrapper = gu_common.ConfigWrapper()

Expand Down

0 comments on commit 7e94c5f

Please sign in to comment.