diff --git a/tests/common/helpers/assertions.py b/tests/common/helpers/assertions.py index 93a3dfc79a7..3da6d4cbfde 100644 --- a/tests/common/helpers/assertions.py +++ b/tests/common/helpers/assertions.py @@ -9,6 +9,16 @@ def pytest_assert(condition, message=None): pytest.fail(message) +def pytest_expect(condition, message=None): + """ + Same as pytest_assert, but does not halt execution. + """ + if not condition: + if not isinstance(message, str): + message = str(message) + pytest.xfail(message) + + def pytest_require(condition, skip_message="", allow_module_level=True): if not condition: # We can't use pytest.skip here because pytest after 3.0 diff --git a/tests/l2/test_l2_configure.py b/tests/l2/test_l2_configure.py new file mode 100644 index 00000000000..3a88d66d89f --- /dev/null +++ b/tests/l2/test_l2_configure.py @@ -0,0 +1,91 @@ +""" +Tests related to L2 configuration +""" +import logging +import pytest +import time + +from tests.common import reboot +from tests.common.helpers.assertions import pytest_expect + +logger = logging.getLogger(__name__) + +pytestmark = [ + pytest.mark.topology('any') +] + + +def test_l2_configure(duthosts, rand_one_dut_hostname): + """ + @summary: Test configuring dut as a L2 switch. + + Args: + duthosts: set of DUTs. + """ + # Setup. + duthost = duthosts[rand_one_dut_hostname] + hwsku = duthost.facts["hwsku"] + + # Store original config for comparison. + orig_vlan = duthost.shell("show vlan config")["stdout"] + orig_int = duthost.shell("show int status")["stdout"] + + # Perform L2 configuration + logger.info("Configuring DUT into L2 mode.") + l2_cfg = "sudo sonic-cfggen --preset l2 -p -H -k {}" \ + " | sudo config load /dev/stdin -y".format(hwsku) + duthost.shell(l2_cfg) + duthost.shell("sudo config qos reload --no-dynamic-buffer") + time.sleep(30) + + logger.info("Checking vlan and interface facts.") + new_vlan = duthost.shell("show vlan config")["stdout"] + new_int = duthost.shell("show int status")["stdout"] + pytest_expect(orig_vlan != new_vlan, "vlan config not updated.") + pytest_expect(orig_int != new_int, "interface status not updated.") + + # Restore from L2 + logger.info("Restoring DUT into L3 mode.") + duthost.shell("sudo config reload -y") + time.sleep(30) + + +def test_l2_configure_over_reboot(duthosts, rand_one_dut_hostname, localhost): + """ + @summary: Test configuring dut as a L2 switch and reboot. + + Args: + duthosts: set of DUTs. + """ + # Setup. + duthost = duthosts[rand_one_dut_hostname] + hwsku = duthost.facts["hwsku"] + + # Save original config + duthost.shell("sudo cp /etc/sonic/config_db.json /tmp/orig_config.json") + + # Store original config for comparison. + orig_vlan = duthost.shell("show vlan config")["stdout"] + orig_int = duthost.shell("show int status")["stdout"] + + # Perform L2 configuration + logger.info("Configuring DUT into L2 mode.") + l2_cfg = "sudo sonic-cfggen --preset l2 -p -H -k {}" \ + " | sudo config load /dev/stdin -y".format(hwsku) + duthost.shell(l2_cfg) + duthost.shell("sudo config qos reload --no-dynamic-buffer") + duthost.shell("sudo config save -y") + reboot(duthost, localhost) + time.sleep(30) + + logger.info("Checking vlan and interface facts.") + new_vlan = duthost.shell("show vlan config")["stdout"] + new_int = duthost.shell("show int status")["stdout"] + pytest_expect(orig_vlan != new_vlan, "vlan config not updated.") + pytest_expect(orig_int != new_int, "interface status not updated.") + + # Restore from L2 + logger.info("Restoring DUT into L3 mode.") + duthost.shell("sudo config reload /tmp/orig_config.json -y") + duthost.shell("sudo config save -y") + time.sleep(30)