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

Fix config_db.json backup in test_counterpoll_watermark.py #16002

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 39 additions & 6 deletions tests/common/fixtures/duthost_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,47 @@ 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]
else:
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 is 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 is 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")
Expand All @@ -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."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`

Expand Down
Loading