Skip to content
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
1 change: 1 addition & 0 deletions tests/kvmtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ test_t0() {
generic_config_updater/test_portchannel_interface.py \
generic_config_updater/test_syslog.py \
generic_config_updater/test_vlan_interface.py \
override_config_table/test_override_config_table.py \
process_monitoring/test_critical_process_monitoring.py \
show_techsupport/test_techsupport_no_secret.py \
system_health/test_system_status.py"
Expand Down
155 changes: 155 additions & 0 deletions tests/override_config_table/test_override_config_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import json
import logging
import pytest

from tests.common.helpers.assertions import pytest_assert
from tests.common.config_reload import config_reload

GOLDEN_CONFIG = "/etc/sonic/golden_config_db.json"
GOLDEN_CONFIG_BACKUP = "/etc/sonic/golden_config_db.json_before_override"
CONFIG_DB = "/etc/sonic/config_db.json"
CONFIG_DB_BACKUP = "/etc/sonic/config_db.json_before_override"

logger = logging.getLogger(__name__)


def file_exists_on_dut(duthost, filename):
return duthost.stat(path=filename).get('stat', {}).get('exists', False)


@pytest.fixture(scope="module")
def golden_config_exists_on_dut(duthost):
return file_exists_on_dut(duthost, GOLDEN_CONFIG)


def backup_config(duthost, config, config_backup):
logger.info("Backup {} to {} on {}".format(
config, config_backup, duthost.hostname))
duthost.shell("cp {} {}".format(config, config_backup))


def restore_config(duthost, config, config_backup):
logger.info("Restore {} with {} on {}".format(
config, config_backup, duthost.hostname))
duthost.shell("mv {} {}".format(config_backup, config))


def get_running_config(duthost):
return json.loads(duthost.shell("sonic-cfggen -d --print-data")['stdout'])


def reload_minigraph_with_golden_config(duthost, json_data):
duthost.copy(content=json.dumps(json_data, indent=4), dest=GOLDEN_CONFIG)
config_reload(duthost, config_source="minigraph", safe_reload=True)


@pytest.fixture(scope="module")
def setup_env(duthosts, rand_one_dut_hostname, golden_config_exists_on_dut):
"""
Setup/teardown
Args:
duthosts: list of DUTs.
rand_selected_dut: The fixture returns a randomly selected DuT.
"""
duthost = duthosts[rand_one_dut_hostname]

# Backup configDB
backup_config(duthost, CONFIG_DB, CONFIG_DB_BACKUP)
# Backup Golden Config if exists.
if golden_config_exists_on_dut:
backup_config(duthost, GOLDEN_CONFIG, GOLDEN_CONFIG_BACKUP)

# Reload test env with minigraph
config_reload(duthost, config_source="minigraph", safe_reload=True)
running_config = get_running_config(duthost)

yield running_config

# Restore configDB after test.
restore_config(duthost, CONFIG_DB, CONFIG_DB_BACKUP)
# Restore Golden Config after test, else cleanup test file.
if golden_config_exists_on_dut:
restore_config(duthost, GOLDEN_CONFIG, GOLDEN_CONFIG_BACKUP)
else:
duthost.file(path=GOLDEN_CONFIG, state='absent')

# Restore config before test
Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before

You mean "after this test"? #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mean restore to the config when the test is not started.

config_reload(duthost)


def load_minigraph_with_golden_empty_input(duthost):
"""Test Golden Config with empty input
"""
initial_config = get_running_config(duthost)

empty_input = {}
reload_minigraph_with_golden_config(duthost, empty_input)

current_config = get_running_config(duthost)
pytest_assert(initial_config == current_config,
"Running config differs.")


def load_minigraph_with_golden_partial_config(duthost):
"""Test Golden Config with partial config.

Here we assume all config contain SYSLOG_SERVER table
"""
partial_config = {
"SYSLOG_SERVER": {
"10.0.0.100": {},
"10.0.0.200": {}
}
}
reload_minigraph_with_golden_config(duthost, partial_config)

current_config = get_running_config(duthost)
pytest_assert(
current_config['SYSLOG_SERVER'] == partial_config['SYSLOG_SERVER'],
"Partial config override fail: {}".format(current_config['SYSLOG_SERVER'])
)


def load_minigraph_with_golden_new_feature(duthost):
"""Test Golden Config with new feature
"""
new_feature_config = {
"NEW_FEATURE_TABLE": {
"entry": {
"field": "value",
"state": "disabled"
}
}
}
reload_minigraph_with_golden_config(duthost, new_feature_config)

current_config = get_running_config(duthost)
pytest_assert(
'NEW_FEATURE_TABLE' in current_config and
current_config['NEW_FEATURE_TABLE'] == new_feature_config['NEW_FEATURE_TABLE'],
"new feature config update fail: {}".format(current_config['NEW_FEATURE_TABLE'])
)


def load_minigraph_with_golden_full_config(duthost, full_config):
"""Test Golden Config fully override minigraph config
"""
# Test if the config has been override by full_config
reload_minigraph_with_golden_config(duthost, full_config)

current_config = get_running_config(duthost)
for table in full_config:
pytest_assert(
full_config[table] == current_config[table],
"full config override fail! {}".format(table)
)


def test_load_minigraph_with_golden_config(duthost, setup_env):
"""Test Golden Config override during load minigraph
"""
load_minigraph_with_golden_empty_input(duthost)
load_minigraph_with_golden_partial_config(duthost)
load_minigraph_with_golden_new_feature(duthost)
full_config = setup_env
load_minigraph_with_golden_full_config(duthost, full_config)