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

Create a test case for testing L2 configuration. #14714

Merged
merged 12 commits into from
Nov 5, 2024
11 changes: 11 additions & 0 deletions tests/l2/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# conftest.py for L2 configuration.


def pytest_addoption(parser):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nightly test is used to verify specified sonic version, if we upgrade sonic image in this test, we can't add test to nightly test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what the issue is asking so if we can't upgrade, we need to find another way to trigger db_migrator?

Btw the test will restore the original image. Is that good enough?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please confirm with Vaibhav, I guess we need to upgrade from 20191130.xx to 20240531.xx and verify L2 configuration.
If we do this in nightly test for 20240531.xx, how do you choose new image to upgrade?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check with him. Maybe we should do a --source_image and --target_image. It will make more sense to upgrade from an older image to 20240531 in the nightly test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked with Vaibhab, this test should be in nightly. I will rewrite part of the test according to test_upgrade_path.py to better accommodate that.

# Target image to upgrade to.
parser.addoption(
"--target_image",
action="store",
default="",
help="Specify a target image for upgrade",
)
87 changes: 87 additions & 0 deletions tests/l2/test_l2_configure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""
Tests related to L2 configuration
"""
import logging
import pytest

from tests.common import reboot
from tests.common.config_reload import config_reload
from tests.common.helpers.assertions import pytest_assert

CONFIG_DB = '/etc/sonic/config_db.json'
CONFIG_DB_BAK = '/etc/sonic/config_db.json.bak'
TARGET_IMG_LOCALHOST = '/var/tmp/target_sonic_localhost.bin'
TARGET_IMG_DUTHOST = '/var/tmp/target_sonic_duthost.bin'

logger = logging.getLogger(__name__)

pytestmark = [
pytest.mark.topology('any')
]


def test_l2_config_and_upgrade(request, duthosts, rand_one_dut_hostname, localhost):
"""
@summary: A testcase that verifies DB migrator does not add bad data.
1. Cold reboot.
2. Configure switch into L2 mode.
3. Install a new image.
4. Reboot into the new image. DB migrator does its work.
5. Verify.

Args:
request: From pytest.
duthosts: set of DUTs.
rand_one_duthostname: sample a random DUT.
localhost: localhost object.
tbinfo: testbed info
"""
# Setup.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duthost = duthosts[rand_one_dut_hostname]
hwsku = duthost.facts["hwsku"]

# Get target image path:
target_image = request.config.getoption('target_image', default=None)
if target_image is None:
pytest.skip("Skipping test due to missing --target_image.")
init_img = duthost.shell('sudo sonic-installer list | grep Current | cut -f2 -d " "')['stdout']

# Step 1: Reboot.
reboot(duthost, localhost, reboot_type="cold")

# Step 2: Configure DUT into L2 mode.
# Save original config
duthost.shell("sudo cp {} {}".format(CONFIG_DB, CONFIG_DB_BAK))
# Perform L2 configuration
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")

# Step 3: Install new image and reboot.
init_img = duthost.shell('sudo sonic-installer list | grep Current | cut -f2 -d " "')['stdout']
logger.info("Init image: {}".format(init_img))
localhost.get_url(url=target_image, dest=TARGET_IMG_LOCALHOST)
duthost.copy(src=TARGET_IMG_LOCALHOST, dest=TARGET_IMG_DUTHOST)
duthost.shell("sudo sonic-installer install -y {}".format(TARGET_IMG_DUTHOST))
reboot(duthost, localhost, reboot_type="cold")
new_img = duthost.shell('sudo sonic-installer list | grep Current | cut -f2 -d " "')['stdout']
logger.info("New image: {}".format(new_img))

# Step 4: Verifies no config fro minigraph is written into ConfigDB.
try:
for table in ["TELEMETRY", "RESTAPI", "DEVICE_METADATA"]:
count = int(duthost.shell('redis-cli --scan --pattern "{}*" | wc -l'.format(table))['stdout'])
Copy link
Contributor

@qiluo-msft qiluo-msft Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redis-cli

are you checking ConfigDB? by default it is checkin ApplDB.

suggest to use sonic-db-cli instead of redis-cli. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this reflect a real issue as I can see those unwanted table all the time, i.e. before l2, after l2, and after installing the image.

admin@vlab-01:~$ sonic-db-cli CONFIG_DB KEYS "TELEMETRY|*"
TELEMETRY|gnmi
TELEMETRY|certs

pytest_assert(count == 0, "{} table is not empty!".format(table))
except Exception:
raise
finally:
# Restore from L2 and new images, restore image first.
duthost.shell("sudo sonic-installer set-next-boot {}".format(init_img))
reboot(duthost, localhost, reboot_type="cold")
cur_img = duthost.shell('sudo sonic-installer list | grep Current | cut -f2 -d " "')['stdout']
logger.info("Current image: {}".format(cur_img))
duthost.shell("sudo cp {} {}".format(CONFIG_DB_BAK, CONFIG_DB))
config_reload(duthost)
duthost.shell("sudo rm {}".format(CONFIG_DB_BAK))
Loading