Skip to content

Commit 52d3771

Browse files
ZhaohuiSmssonicbld
authored andcommitted
Restore configuration after vxlan module (#18714)
What is the motivation for this PR? Move the configuration restoration from each test case level to the common conftest.py which is module level. It also saves time to do config reload one by one, each config reload may take 5 mins. Module level configuration restore is more reasonable. How did you do it? Call the existing function backup_and_restore_config_db_on_duts in conftest.py and do config reload after config_db.json restoration. Make sure if vxlan module run, the configuration will be restored after the test. How did you verify/test it? Run test_vxlan_crm on dut and check the log. Backup config file before running tests 29/05/2025 08:48:01 conftest.restore_config_by_config_reload L0356 INFO | Prepare backup config before running tests 29/05/2025 08:48:01 duthost_utils._backup_and_restore_config L0043 INFO | Backup /etc/sonic/config_db.json to /host/config_db.json.before_test_module on <bound method DutHosts._Nodes._run_on_nodes of [<MultiAsicSonicHost bjw2-can-8101-2>]> 29/05/2025 08:48:01 base._run L0071 DEBUG | /data/sonic-mgmt-int/tests/common/devices/multi_asic.py::_run_on_asics#136: [bjw2-can-8101-2] AnsibleModule::shell, args=["cp /etc/sonic/config_db.json /host/config_db.json.before_test_module"], kwargs={} 29/05/2025 08:48:02 base._run L0108 DEBUG | /data/sonic-mgmt-int/tests/common/devices/multi_asic.py::_run_on_asics#136: [bjw2-can-8101-2] AnsibleModule::shell Result => {"changed": true, "stdout": "", "stderr": "", "rc": 0, "cmd": "cp /etc/sonic/config_db.json /host/config_db.json.before_test_module", "start": "2025-05-29 08:48:02.236422", "end": "2025-05-29 08:48:02.242861", "delta": "0:00:00.006439", "msg": "", "invocation": {"module_args": {"_raw_params": "cp /etc/sonic/config_db.json /host/config_db.json.before_test_module", "_uses_shell": true, "warn": false, "stdin_add_newline": true, "strip_empty_ends": true, "argv": null, "chdir": null, "executable": null, "creates": null, "removes": null, "stdin": null}}, "stdout_lines": [], "stderr_lines": [], "_ansible_no_log": null, "failed": false} Restore configuration file, then do config reload after running tests 29/05/2025 09:24:15 conftest.restore_config_by_config_reload L0359 INFO | Restore config after running tests 29 9/05/2025 09:24:15 __init__._fixture_generator_decorator L0102 INFO | -------------------- fixture fixture_setUp teardown ends -------------------- 29/05/2025 09:24:15 duthost_utils._backup_and_restore_config L0049 INFO | Restore /etc/sonic/config_db.json with /host/config_db.json.before_test_module on <bound method DutHosts._Nodes._run_on_nodes of [<MultiAsicSonicHost bjw2-can-8101-2>]> 29/05/2025 09:24:15 base._run L0071 DEBUG | /data/sonic-mgmt-int/tests/common/devices/multi_asic.py::_run_on_asics#136: [bjw2-can-8101-2] AnsibleModule::shell, args=["mv /host/config_db.json.before_test_module /etc/sonic/config_db.json"], kwargs={} 29/05/2025 09:24:15 base._run L0108 DEBUG | /data/sonic-mgmt-int/tests/common/devices/multi_asic.py::_run_on_asics#136: [bjw2-can-8101-2] AnsibleModule::shell Result => {"changed": true, "stdout": "", "stderr": "", "rc": 0, "cmd": "mv /host/config_db.json.before_test_module /etc/sonic/config_db.json", "start": "2025-05-29 09:24:15.369677", "end": "2025-05-29 09:24:15.374489", "delta": "0:00:00.004812", "msg": "", "invocation": {"module_args": {"_raw_params": "mv /host/config_db.json.before_test_module /etc/sonic/config_db.json", "_uses_shell": true, "warn": false, "stdin_add_newline": true, "strip_empty_ends": true, "argv": null, "chdir": null, "executable": null, "creates": null, "removes": null, "stdin": null}}, "stdout_lines": [], "stderr_lines": [], "_ansible_no_log": null, "failed": false} 29/05/2025 09:24:15 conftest.restore_config_by_config_reload L0359 INFO | Restore config after running tests 29/05/2025 09:24:15 config_reload.config_reload L0145 INFO | reloading config_db 29/05/2025 09:24:15 base._run L0071 DEBUG | /data/sonic-mgmt-int/tests/common/devices/multi_asic.py::_run_on_asics#136: [bjw2-can-8101-2] AnsibleModule::shell, args=["config reload -h"], kwargs={"executable": "/bin/bash"} 29/05/2025 09:24:17 base._run L0108 DEBUG | /data/sonic-mgmt-int/tests/common/devices/multi_asic.py::_run_on_asics#136: [bjw2-can-8101-2] AnsibleModule::shell Result => {"changed": true, "stdout": "Usage: config reload [OPTIONS] [FILENAME]\n\n Clear current configuration and import a previous saved config DB dump\n file. <filename> : Names of configuration file(s) to load, separated by\n comma with no spaces in between\n\nOptions:\n -y, --yes\n -l, --load-sysinfo load system default information (mac,\n portmap etc) first.\n -n, --no_service_restart Do not restart docker services\n -f, --force Force config reload without system checks\n -t, --file_format [config_yang|config_db]\n specify the file format [default:\n config_db]\n -b, --bypass-lock Do reload without acquiring lock\n -h, -?, --help Show this message and exit.", "stderr": "", "rc": 0, "cmd": "config reload -h", "start": "2025-05-29 09:24:15.830852", "end": "2025-05-29 09:24:17.219753", "delta": "0:00:01.388901", "msg": "", "invocation": {"module_args": {"executable": "/bin/bash", "_raw_params": "config reload -h", "_uses_shell": true, "warn": false, "stdin_add_newline": true, "strip_empty_ends": true, "argv": null, "chdir": null, "creates": null, "removes": null, "stdin": null}}, "stdout_lines": ["Usage: config reload [OPTIONS] [FILENAME]", "", " Clear current configuration and import a previous saved config DB dump", " file. <filename> : Names of configuration file(s) to load, separated by", " comma with no spaces in between", "", "Options:", " -y, --yes", " -l, --load-sysinfo load system default information (mac,", " portmap etc) first.", " -n, --no_service_restart Do not restart docker services", " -f, --force Force config reload without system checks", " -t, --file_format [config_yang|config_db]", " specify the file format [default:", " config_db]", " -b, --bypass-lock Do reload without acquiring lock", " -h, -?, --help Show this message and exit."], "stderr_lines": [], "_ansible_no_log": null, "failed": false} 29/05/2025 09:24:47 base._run L0071 DEBUG | /data/sonic-mgmt-int/tests/common/devices/multi_asic.py::_run_on_asics#136: [bjw2-can-8101-2] AnsibleModule::shell, args=["config reload -y -f &>/dev/null"], kwargs={"executable": "/bin/bash"} 29/05/2025 09:25:28 base._run L0
1 parent 3d0922f commit 52d3771

File tree

3 files changed

+14
-25
lines changed

3 files changed

+14
-25
lines changed

tests/vxlan/conftest.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
NUM_INTF_PER_VNET_KEY,
2121
TEMPLATE_DIR
2222
)
23+
from tests.common.fixtures.duthost_utils import backup_and_restore_config_db_on_duts # noqa F401
24+
from tests.common.config_reload import config_reload
2325

2426
logger = logging.getLogger(__name__)
2527

@@ -307,7 +309,7 @@ def vnet_test_params(duthost, request):
307309

308310

309311
@pytest.fixture(scope="module")
310-
def minigraph_facts(duthosts, rand_one_dut_hostname, tbinfo):
312+
def minigraph_facts(duthosts, rand_one_dut_hostname, tbinfo, backup_and_restore_config_db_on_duts): # noqa F811
311313
"""
312314
Fixture to get minigraph facts
313315
Args:
@@ -347,3 +349,11 @@ def vnet_config(minigraph_facts, vnet_test_params, scaled_vnet_params):
347349
return yaml.safe_load(
348350
safe_open_template(
349351
join(TEMPLATE_DIR, "vnet_config.j2")).render(combined_args))
352+
353+
354+
@pytest.fixture(scope="module", autouse=True)
355+
def restore_config_by_config_reload(duthosts, rand_one_dut_hostname):
356+
yield
357+
duthost = duthosts[rand_one_dut_hostname]
358+
logger.info("Restore config after running tests")
359+
config_reload(duthost, safe_reload=True)

tests/vxlan/test_vxlan_bfd_tsa.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from tests.common.fixtures.ptfhost_utils import copy_ptftests_directory # noqa F401
1717
from tests.ptf_runner import ptf_runner
1818
from tests.common.vxlan_ecmp_utils import Ecmp_Utils
19-
from tests.common.fixtures.duthost_utils import backup_and_restore_config_db_on_duts # noqa F401
2019
from tests.common.config_reload import config_reload
2120
Logger = logging.getLogger(__name__)
2221
ecmp_utils = Ecmp_Utils()
@@ -70,8 +69,7 @@ def fixture_setUp(duthosts,
7069
rand_one_dut_hostname,
7170
minigraph_facts,
7271
tbinfo,
73-
encap_type,
74-
backup_and_restore_config_db_on_duts): # noqa F811
72+
encap_type):
7573
'''
7674
Setup for the entire script.
7775
The basic steps in VxLAN configs are:
@@ -233,22 +231,14 @@ def fixture_setUp(duthosts,
233231
ecmp_utils.stop_bfd_responder(data['ptfhost'])
234232

235233

236-
@pytest.fixture(scope="module", autouse=True)
237-
def restore_config_by_config_reload(duthosts, rand_one_dut_hostname, localhost):
238-
yield
239-
duthost = duthosts[rand_one_dut_hostname]
240-
241-
config_reload(duthost, safe_reload=True)
242-
243-
244234
def is_vnet_route_configured_on_asic(duthost, dest):
245235
'''
246236
Function to check if a VNET route to dest is configured on ASIC DB.
247237
A VNET route to dest must be configured on ASIC DB before running
248238
PTF tests.
249239
'''
250240
result = duthost.shell(f"sonic-db-cli ASIC_DB KEYS \
251-
'ASIC_STATE:SAI_OBJECT_TYPE_ROUTE_ENTRY*{dest}*'")["stdout_lines"]
241+
'ASIC_STATE:SAI_OBJECT_TYPE_ROUTE_ENTRY*{dest}*'")["stdout_lines"] # noqa: E231
252242
return bool(result)
253243

254244

tests/vxlan/test_vxlan_ecmp.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060

6161
from tests.common.helpers.assertions import pytest_assert
6262
from tests.common.fixtures.ptfhost_utils import copy_ptftests_directory # noqa F401
63-
from tests.common.fixtures.duthost_utils import backup_and_restore_config_db_on_duts # noqa F401
64-
from tests.common.config_reload import config_reload
6563
from tests.common.utilities import wait_until
6664
from tests.ptf_runner import ptf_runner
6765
from tests.common.vxlan_ecmp_utils import Ecmp_Utils
@@ -142,8 +140,7 @@ def fixture_setUp(duthosts,
142140
rand_one_dut_hostname,
143141
minigraph_facts,
144142
tbinfo,
145-
encap_type,
146-
backup_and_restore_config_db_on_duts): # noqa F811
143+
encap_type): # noqa F811
147144
'''
148145
Setup for the entire script.
149146
The basic steps in VxLAN configs are:
@@ -342,14 +339,6 @@ def fixture_setUp(duthosts,
342339
setup_crm_interval(data['duthost'], int(data['original_crm_interval']))
343340

344341

345-
@pytest.fixture(scope="module", autouse=True)
346-
def restore_config_by_config_reload(duthosts, rand_one_dut_hostname, localhost):
347-
yield
348-
duthost = duthosts[rand_one_dut_hostname]
349-
350-
config_reload(duthost, safe_reload=True)
351-
352-
353342
@pytest.fixture(scope="module")
354343
def default_routes(setUp, encap_type):
355344
vnet = list(setUp[encap_type]['vnet_vni_map'].keys())[0]

0 commit comments

Comments
 (0)