Skip to content

Commit

Permalink
Create test cases for testing L2 configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
hdwhdw committed Sep 26, 2024
1 parent 61a95ac commit 07bf697
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/common/helpers/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
91 changes: 91 additions & 0 deletions tests/l2/test_l2_configure.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 07bf697

Please sign in to comment.