Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[action] [PR:3188] [config] Add YANG alerting for override (#3188) #3230

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,18 @@ def override_config_table(db, input_config_db, dry_run):
validate_config_by_cm(cm, ns_config_input, "config_input")
# Validate updated whole config
validate_config_by_cm(cm, updated_config, "updated_config")
else:
cm = None
try:
# YANG validate of config minigraph generated
cm = ConfigMgmt(configdb=config_db)
cm.validateConfigData()
except Exception as ex:
log.log_warning("Failed to validate running config. Alerting: {}".format(ex))

# YANG validate config of minigraph generated overriden by golden config
if cm:
validate_config_by_cm_alerting(cm, updated_config, "updated_config")

if dry_run:
print(json.dumps(updated_config, sort_keys=True,
Expand All @@ -1986,6 +1998,15 @@ def validate_config_by_cm(cm, config_json, jname):
sys.exit(1)


def validate_config_by_cm_alerting(cm, config_json, jname):
tmp_config_json = copy.deepcopy(config_json)
try:
cm.loadData(tmp_config_json)
cm.validateConfigData()
except Exception as ex:
log.log_warning("Failed to validate {}. Alerting: {}".format(jname, ex))


def override_config_db(config_db, config_input):
# Deserialized golden config to DB recognized format
sonic_cfggen.FormatConverter.to_deserialized(config_input)
Expand Down
11 changes: 0 additions & 11 deletions tests/config_override_input/multi_asic_dm_rm.json

This file was deleted.

11 changes: 11 additions & 0 deletions tests/config_override_input/multi_asic_feature_rm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"localhost": {
"FEATURE": {}
},
"asic0": {
"FEATURE": {}
},
"asic1": {
"FEATURE": {}
}
}
15 changes: 12 additions & 3 deletions tests/config_override_input/multi_asic_macsec_ov.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@
"localhost": {
"MACSEC_PROFILE": {
"profile": {
"key": "value"
"primary_cak": "1159485744465e5a537272050a1011073557475152020c0e040c57223a357d7d71",
"primary_ckn": "6162636465666768696A6B6C6D6E6F70",
"fallback_cak": "000000000000000000000000000000000000000000000000000000000000000000",
"fallback_ckn": "11111111111111111111111111111111"
}
}
},
"asic0": {
"MACSEC_PROFILE": {
"profile": {
"key": "value"
"primary_cak": "1159485744465e5a537272050a1011073557475152020c0e040c57223a357d7d71",
"primary_ckn": "6162636465666768696A6B6C6D6E6F70",
"fallback_cak": "000000000000000000000000000000000000000000000000000000000000000000",
"fallback_ckn": "11111111111111111111111111111111"
}
}
},
"asic1": {
"MACSEC_PROFILE": {
"profile": {
"key": "value"
"primary_cak": "1159485744465e5a537272050a1011073557475152020c0e040c57223a357d7d71",
"primary_ckn": "6162636465666768696A6B6C6D6E6F70",
"fallback_cak": "000000000000000000000000000000000000000000000000000000000000000000",
"fallback_ckn": "11111111111111111111111111111111"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/config_override_input/multi_asic_missing_asic.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"localhost": {
"DEVICE_METADATA": {}
"FEATURE": {}
},
"asic0": {
"DEVICE_METADATA": {}
"FEATURE": {}
}
}
28 changes: 19 additions & 9 deletions tests/config_override_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
GOLDEN_INPUT_YANG_FAILURE = os.path.join(DATA_DIR, "golden_input_yang_failure.json")
FINAL_CONFIG_YANG_FAILURE = os.path.join(DATA_DIR, "final_config_yang_failure.json")
MULTI_ASIC_MACSEC_OV = os.path.join(DATA_DIR, "multi_asic_macsec_ov.json")
MULTI_ASIC_DEVICE_METADATA_RM = os.path.join(DATA_DIR, "multi_asic_dm_rm.json")
MULTI_ASIC_FEATURE_RM = os.path.join(DATA_DIR, "multi_asic_feature_rm.json")
MULTI_ASIC_DEVICE_METADATA_GEN_SYSINFO = os.path.join(DATA_DIR, "multi_asic_dm_gen_sysinfo.json")
MULTI_ASIC_MISSING_LOCALHOST_OV = os.path.join(DATA_DIR, "multi_asic_missing_localhost.json")
MULTI_ASIC_MISSING_ASIC_OV = os.path.join(DATA_DIR, "multi_asic_missing_asic.json")
Expand Down Expand Up @@ -104,7 +104,9 @@ def read_json_file_side_effect(filename):
['golden_config_db.json', '--dry-run'])

assert result.exit_code == 0
assert json.loads(result.output) == current_config
start_pos = result.output.find('{')
json_text = result.output[start_pos:]
assert json.loads(json_text) == current_config

def test_golden_config_db_empty(self):
db = Db()
Expand Down Expand Up @@ -288,7 +290,15 @@ def read_json_file_side_effect(filename):

# The profile_content was copied from MULTI_ASIC_MACSEC_OV, where all
# ns sharing the same content: {"profile": {"key": "value"}}
profile_content = {"profile": {"key": "value"}}
profile_content = {
"profile": {
"primary_cak": "1159485744465e5a537272050a1011073557475152020c0e040c57223a357d7d71",
"primary_ckn": "6162636465666768696A6B6C6D6E6F70",
"fallback_cak": "000000000000000000000000000000000000000000000000000000000000000000",
"fallback_ckn": "11111111111111111111111111111111"

}
}

with mock.patch('config.main.read_json_file',
mock.MagicMock(side_effect=read_json_file_side_effect)):
Expand All @@ -300,16 +310,16 @@ def read_json_file_side_effect(filename):
for ns, config_db in cfgdb_clients.items():
assert config_db.get_config()['MACSEC_PROFILE'] == profile_content

def test_device_metadata_table_rm(self):
def test_feature_table_rm(self):
def read_json_file_side_effect(filename):
with open(MULTI_ASIC_DEVICE_METADATA_RM, "r") as f:
device_metadata = json.load(f)
return device_metadata
with open(MULTI_ASIC_FEATURE_RM, "r") as f:
feature = json.load(f)
return feature
db = Db()
cfgdb_clients = db.cfgdb_clients

for ns, config_db in cfgdb_clients.items():
assert 'DEVICE_METADATA' in config_db.get_config()
assert 'FEATURE' in config_db.get_config()

with mock.patch('config.main.read_json_file',
mock.MagicMock(side_effect=read_json_file_side_effect)):
Expand All @@ -319,7 +329,7 @@ def read_json_file_side_effect(filename):
assert result.exit_code == 0

for ns, config_db in cfgdb_clients.items():
assert 'DEVICE_METADATA' not in config_db.get_config()
assert 'FEATURE' not in config_db.get_config()

def test_device_metadata_keep_sysinfo(self):
def read_json_file_side_effect(filename):
Expand Down
Loading