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
8 changes: 3 additions & 5 deletions tests/acl/test_acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from tests.common import reboot, port_toggle
from tests.common.plugins.loganalyzer.loganalyzer import LogAnalyzer, LogAnalyzerError
from tests.common.fixtures.duthost_utils import backup_and_restore_config_db_module

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -189,8 +190,8 @@ def acl_table_config(duthost, setup, stage):
}


@pytest.fixture(scope='module')
def acl_table(duthost, acl_table_config):
@pytest.fixture(scope="module")
def acl_table(duthost, acl_table_config, backup_and_restore_config_db_module):
"""
fixture to apply ACL table configuration and remove after tests
:param duthost: DUT object
Expand Down Expand Up @@ -223,9 +224,6 @@ def acl_table(duthost, acl_table_config):
logger.info('removing ACL table {}'.format(name))
duthost.command('config acl remove table {}'.format(name))

# save cleaned configuration
duthost.command('config save -y')


class BaseAclTest(object):
"""
Expand Down
31 changes: 25 additions & 6 deletions tests/common/fixtures/duthost_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,36 @@

logger = logging.getLogger(__name__)

@pytest.fixture
def backup_and_restore_config_db(duthost):
"""
Some cases will shutdown interfaces or BGP in test, and the change is writen to
config db after warm-reboot. Therefore, we need to backup config_db.json before
test starts and then restore after test ends

def _backup_and_restore_config_db(duthost):
"""Back up the existing config_db.json file and restore it once the test ends.

Some cases will update the running config during the test and save the config
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 = "/etc/sonic/config_db.json.before_test"
logger.info("Backup {} to {}".format(CONFIG_DB, CONFIG_DB_BAK))
duthost.shell("cp {} {}".format(CONFIG_DB, CONFIG_DB_BAK))

yield

logger.info("Restore {} with {}".format(CONFIG_DB, CONFIG_DB_BAK))
duthost.shell("mv {} {}".format(CONFIG_DB_BAK, CONFIG_DB))


@pytest.fixture
def backup_and_restore_config_db(duthost):
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.

@bingwang-ms can you help me verify that this works OK for the advanced_reboot tests? It seemed fine to me but I think a second set of eyes would be helpful. :)

"""Back up and restore config DB at the function level."""
# TODO: Use the neater "yield from _function" syntax when we move to python3
for func in _backup_and_restore_config_db(duthost):
yield func


@pytest.fixture(scope="module")
def backup_and_restore_config_db_module(duthost):
"""Back up and restore config DB at the module level."""
# TODO: Use the neater "yield from _function" syntax when we move to python3
for func in _backup_and_restore_config_db(duthost):
yield func