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

Add fixtures to call dpus on smartswtich directly #15695

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
66 changes: 62 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ def pytest_addoption(parser):
parser.addoption("--is_parallel_leader", action="store_true", default=False, help="Is the parallel leader")
parser.addoption("--parallel_followers", action="store", default=0, type=int, help="Number of parallel followers")

############################
# SmartSwitch options #
############################
parser.addoption("--dpu-pattern", action="store", default=None, help="dpu host name")


def pytest_configure(config):
if config.getoption("enable_macsec"):
Expand Down Expand Up @@ -365,18 +370,17 @@ def parallel_run_context(request):
)


def get_specified_duts(request):
def get_specified_device_info(request, device_pattern):
"""
Get a list of DUT hostnames specified with the --host-pattern CLI option
or -d if using `run_tests.sh`
Get a list of device hostnames specified with the --host-pattern or --dpu-pattern CLI option
"""
tbname, tbinfo = get_tbinfo(request)
testbed_duts = tbinfo['duts']

if is_parallel_run(request):
return [get_target_hostname(request)]

host_pattern = request.config.getoption("--host-pattern")
host_pattern = request.config.getoption(device_pattern)
if host_pattern == 'all':
return testbed_duts

Expand All @@ -396,6 +400,21 @@ def get_specified_duts(request):
return duts


def get_specified_duts(request):
"""
Get a list of DUT hostnames specified with the --host-pattern CLI option
or -d if using `run_tests.sh`
"""
return get_specified_device_info(request, "--host-pattern")


def get_specified_dpus(request):
"""
Get a list of DUT hostnames specified with the --dpu-pattern CLI option
"""
return get_specified_device_info(request, "--dpu-pattern")


def pytest_sessionstart(session):
# reset all the sonic_custom_msg keys from cache
# reset here because this fixture will always be very first fixture to be called
Expand Down Expand Up @@ -453,6 +472,45 @@ def duthost(duthosts, request):
return duthost


@pytest.fixture(name="dpuhosts", scope="session")
def fixture_dpuhosts(enhance_inventory, ansible_adhoc, tbinfo, request):
"""
@summary: fixture to get DPU hosts defined in testbed.
@param ansible_adhoc: Fixture provided by the pytest-ansible package.
Source of the various device objects. It is
mandatory argument for the class constructors.
@param tbinfo: fixture provides information about testbed.
"""
try:
host = DutHosts(ansible_adhoc, tbinfo, get_specified_dpus(request),
target_hostname=get_target_hostname(request), is_parallel_leader=is_parallel_leader(request))
return host
except BaseException as e:
logger.error("Failed to initialize dpuhosts.")
request.config.cache.set("dpuhosts_fixture_failed", True)
pt_assert(False, "!!!!!!!!!!!!!!!! dpuhosts fixture failed !!!!!!!!!!!!!!!!"
"Exception: {}".format(repr(e)))


@pytest.fixture(scope="session")
def dpuhost(dpuhosts, request):
'''
@summary: Shortcut fixture for getting DPU host. For a lengthy test case, test case module can
pass a request to disable sh time out mechanis on dut in order to avoid ssh timeout.
After test case completes, the fixture will restore ssh timeout.
@param duthosts: fixture to get DPU hosts
@param request: request parameters for duphost test fixture
'''
dpu_index = getattr(request.session, "dpu_index", 0)
assert dpu_index < len(dpuhosts), \
"DPU index '{0}' is out of bound '{1}'".format(dpu_index,
len(dpuhosts))

duthost = dpuhosts[dpu_index]

return duthost


@pytest.fixture(scope="session")
def mg_facts(duthost):
return duthost.minigraph_facts(host=duthost.hostname)['ansible_facts']
Expand Down
Loading