-
Notifications
You must be signed in to change notification settings - Fork 1k
[tests/override_config_table] load_minigraph with Golden Config test #5625
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
tests/override_config_table/test_override_config_table.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
wen587 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| restore_config(duthost, GOLDEN_CONFIG, GOLDEN_CONFIG_BACKUP) | ||
| else: | ||
| duthost.file(path=GOLDEN_CONFIG, state='absent') | ||
|
|
||
| # Restore config before test | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.