From 239af90e1a0d38b4742c0dde3c677082bda7eac2 Mon Sep 17 00:00:00 2001 From: Nathan Wolfe Date: Wed, 11 Dec 2024 02:33:16 +0000 Subject: [PATCH 1/2] Fix config_db backup in test_counterpoll_watermark.py Also fix '_backup_and_restore_config_db' to work on multi-asic lcs --- tests/common/fixtures/duthost_utils.py | 45 ++++++++++++++++--- .../counterpoll/test_counterpoll_watermark.py | 4 +- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/tests/common/fixtures/duthost_utils.py b/tests/common/fixtures/duthost_utils.py index 66514600763..80b7f1e425a 100644 --- a/tests/common/fixtures/duthost_utils.py +++ b/tests/common/fixtures/duthost_utils.py @@ -31,8 +31,8 @@ def _backup_and_restore_config_db(duts, scope='function'): to be recovered aftet reboot. In such a case we need to backup config_db.json before the test starts and then restore it after the test ends. """ - CONFIG_DB = "/etc/sonic/config_db.json" - CONFIG_DB_BAK = "/host/config_db.json.before_test_{}".format(scope) + CONFIG_DB = "/etc/sonic/config_db{}.json" + CONFIG_DB_BAK = "/host/config_db{{}}.json.before_test_{}".format(scope) if type(duts) is not list: duthosts = [duts] @@ -40,14 +40,38 @@ def _backup_and_restore_config_db(duts, scope='function'): duthosts = duts for duthost in duthosts: - logger.info("Backup {} to {} on {}".format(CONFIG_DB, CONFIG_DB_BAK, duthost.hostname)) - duthost.shell("cp {} {}".format(CONFIG_DB, CONFIG_DB_BAK)) + # Backup the default config_db.json + cfg_str = CONFIG_DB.format('') + cfg_bak_str = CONFIG_DB_BAK.format('') + logger.info("Backup {} to {} on {}".format(cfg_str, cfg_bak_str, duthost.hostname)) + duthost.shell("cp {} {}".format(cfg_str, cfg_bak_str)) + + # Backup the per-asic config_db.json files + for asicId in duthost.get_asic_ids(): + if asicId == None: + break + cfg_str = CONFIG_DB.format(asicId) + cfg_bak_str = CONFIG_DB_BAK.format(asicId) + logger.info("Backup {} to {} on {}".format(cfg_str, cfg_bak_str, duthost.hostname)) + duthost.shell("cp {} {}".format(cfg_str, cfg_bak_str)) yield for duthost in duthosts: - logger.info("Restore {} with {} on {}".format(CONFIG_DB, CONFIG_DB_BAK, duthost.hostname)) - duthost.shell("mv {} {}".format(CONFIG_DB_BAK, CONFIG_DB)) + # Restore the default config_db.json + cfg_str = CONFIG_DB.format('') + cfg_bak_str = CONFIG_DB_BAK.format('') + logger.info("Restore {} with {} on {}".format(cfg_str, cfg_bak_str, duthost.hostname)) + duthost.shell("mv {} {}".format(cfg_bak_str, cfg_str)) + + # Restore the per-asic config_db.json files + for asicId in duthost.get_asic_ids(): + if asicId == None: + break + cfg_str = CONFIG_DB.format(asicId) + cfg_bak_str = CONFIG_DB_BAK.format(asicId) + logger.info("Restore {} with {} on {}".format(cfg_str, cfg_bak_str, duthost.hostname)) + duthost.shell("mv {} {}".format(cfg_bak_str, cfg_str)) @pytest.fixture(scope="module") @@ -68,6 +92,15 @@ def backup_and_restore_config_db(duthosts, rand_one_dut_hostname): yield func +@pytest.fixture +def backup_and_restore_config_db_frontend(duthosts, enum_rand_one_per_hwsku_frontend_hostname): + """Back up and restore config DB at the function level.""" + duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname] + # TODO: Use the neater "yield from _function" syntax when we move to python3 + for func in _backup_and_restore_config_db(duthost, "function"): + yield func + + @pytest.fixture(scope="module") def backup_and_restore_config_db_module(duthosts, rand_one_dut_hostname): """Back up and restore config DB at the module level.""" diff --git a/tests/platform_tests/counterpoll/test_counterpoll_watermark.py b/tests/platform_tests/counterpoll/test_counterpoll_watermark.py index 9eb591f95f2..532781a7816 100644 --- a/tests/platform_tests/counterpoll/test_counterpoll_watermark.py +++ b/tests/platform_tests/counterpoll/test_counterpoll_watermark.py @@ -9,7 +9,7 @@ import pytest from tests.common.config_reload import config_reload -from tests.common.fixtures.duthost_utils import backup_and_restore_config_db # noqa F401 +from tests.common.fixtures.duthost_utils import backup_and_restore_config_db_frontend # noqa F401 from tests.common.helpers.assertions import pytest_assert from tests.common.helpers.sonic_db import SonicDbCli, SonicDbKeyNotFound from tests.common.utilities import get_inventory_files, get_host_visible_vars @@ -75,7 +75,7 @@ def check_counters_populated(duthost, key): def test_counterpoll_queue_watermark_pg_drop(duthosts, localhost, enum_rand_one_per_hwsku_frontend_hostname, dut_vars, - backup_and_restore_config_db): # noqa F811 + backup_and_restore_config_db_frontend): # noqa F811 """ @summary: Verify FLEXCOUNTERS_DB and COUNTERS_DB content after `counterpoll queue/watermark/queue enable` From fb7228726695522e2587fab41771a2be79a0e539 Mon Sep 17 00:00:00 2001 From: Nathan Wolfe Date: Wed, 11 Dec 2024 02:41:16 +0000 Subject: [PATCH 2/2] Fix conditional failing linter --- tests/common/fixtures/duthost_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/common/fixtures/duthost_utils.py b/tests/common/fixtures/duthost_utils.py index 80b7f1e425a..8eea9cde3a1 100644 --- a/tests/common/fixtures/duthost_utils.py +++ b/tests/common/fixtures/duthost_utils.py @@ -48,7 +48,7 @@ def _backup_and_restore_config_db(duts, scope='function'): # Backup the per-asic config_db.json files for asicId in duthost.get_asic_ids(): - if asicId == None: + if asicId is None: break cfg_str = CONFIG_DB.format(asicId) cfg_bak_str = CONFIG_DB_BAK.format(asicId) @@ -66,7 +66,7 @@ def _backup_and_restore_config_db(duts, scope='function'): # Restore the per-asic config_db.json files for asicId in duthost.get_asic_ids(): - if asicId == None: + if asicId is None: break cfg_str = CONFIG_DB.format(asicId) cfg_bak_str = CONFIG_DB_BAK.format(asicId)