Skip to content

Commit 6722117

Browse files
authored
[parametize by ports] add infrastructure to allow test parametized by ports (#2411)
* [parametize by ports] add infrastructure to allow test parametized by ports Assumptions: - Run test_pretest before actual test. - test_update_testbed_metadata will populate testbed information and save to tests/metadata/<testbed_name>.json. Available parameterizes: - all_ports : all ports available on target testbed. - oper_up_ports : all ports in oper_up state. - admin_up_ports : all ports in admin_up state. - all_pc : all port channel available on target testbed. - oper_up_pc : all port channel in oper_up state. - admin_up_pc : all port channel in admin_up state. Parameter format: { 'dut' : <dut name, str>, 'interface' : <port or port channel name, str> } Expectations: - when { 'dut' : 'unknown', 'interface' : 'found-no-<port|pc>' } is passed in as parameter, test should warn and loop through all ports (port channels). Signed-off-by: Ying Xie <ying.xie@microsoft.com> * add port channel enumerators * format json file to be more human readable * encode/decode dut/port names so the paramters are more informative * unify encoding for no found cases
1 parent 928ebb3 commit 6722117

File tree

5 files changed

+518
-25
lines changed

5 files changed

+518
-25
lines changed

tests/common/helpers/dut_ports.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def encode_dut_port_name(dutname, portname):
2+
return dutname + '|' + portname
3+
4+
5+
def decode_dut_port_name(dut_portname):
6+
tokens = dut_portname.split('|')
7+
if len(tokens) >= 2:
8+
dutname = tokens[0]
9+
portname = tokens[1]
10+
elif len(tokens) == 1:
11+
dutname = None
12+
portname = dut_portname
13+
else:
14+
dutname = None
15+
portname = None
16+
return dutname, portname
17+

tests/conftest.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from tests.common.devices import SonicHost, Localhost
2222
from tests.common.devices import PTFHost, EosHost, FanoutHost
2323
from tests.common.helpers.constants import ASIC_PARAM_TYPE_ALL, ASIC_PARAM_TYPE_FRONTEND, DEFAULT_ASIC_ID
24+
from tests.common.helpers.dut_ports import encode_dut_port_name
2425

2526
logger = logging.getLogger(__name__)
2627

@@ -482,6 +483,53 @@ def generate_params_dut_index(request):
482483
logging.info("Num of duts in testbed topology {}".format(num_duts))
483484
return range(num_duts)
484485

486+
487+
def generate_port_lists(request, port_scope):
488+
empty = [ encode_dut_port_name('unknown', 'unknown') ]
489+
if 'ports' in port_scope:
490+
scope = 'Ethernet'
491+
elif 'pcs' in port_scope:
492+
scope = 'PortChannel'
493+
else:
494+
return empty
495+
496+
if 'all' in port_scope:
497+
state = None
498+
elif 'oper_up' in port_scope:
499+
state = 'oper_state'
500+
elif 'admin_up' in port_scope:
501+
state = 'admin_state'
502+
else:
503+
return empty
504+
505+
tbname = request.config.getoption("--testbed")
506+
if not tbname:
507+
return empty
508+
509+
folder = 'metadata'
510+
filepath = os.path.join(folder, tbname + '.json')
511+
512+
try:
513+
with open(filepath, 'r') as yf:
514+
ports = json.load(yf)
515+
except IOError as e:
516+
return empty
517+
518+
if tbname not in ports:
519+
return empty
520+
521+
dut_ports = ports[tbname]
522+
ret = []
523+
for dut, val in dut_ports.items():
524+
if 'intf_status' not in val:
525+
continue
526+
for intf, status in val['intf_status'].items():
527+
if scope in intf and (not state or status[state] == 'up'):
528+
ret.append(encode_dut_port_name(dut, intf))
529+
530+
return ret if ret else empty
531+
532+
485533
def pytest_generate_tests(metafunc):
486534
# The topology always has atleast 1 dut
487535
dut_indices = [0]
@@ -493,3 +541,15 @@ def pytest_generate_tests(metafunc):
493541
if "frontend_asic_index" in metafunc.fixturenames:
494542
metafunc.parametrize("frontend_asic_index",generate_param_asic_index(metafunc, dut_indices, ASIC_PARAM_TYPE_FRONTEND))
495543

544+
if "all_ports" in metafunc.fixturenames:
545+
metafunc.parametrize("all_ports", generate_port_lists(metafunc, "all_ports"))
546+
if "oper_up_ports" in metafunc.fixturenames:
547+
metafunc.parametrize("oper_up_ports", generate_port_lists(metafunc, "oper_up_ports"))
548+
if "admin_up_ports" in metafunc.fixturenames:
549+
metafunc.parametrize("admin_up_ports", generate_port_lists(metafunc, "admin_up_ports"))
550+
if "all_pcs" in metafunc.fixturenames:
551+
metafunc.parametrize("all_pcs", generate_port_lists(metafunc, "all_pcs"))
552+
if "oper_up_pcs" in metafunc.fixturenames:
553+
metafunc.parametrize("oper_up_pcs", generate_port_lists(metafunc, "oper_up_pcs"))
554+
if "admin_up_pcs" in metafunc.fixturenames:
555+
metafunc.parametrize("admin_up_pcs", generate_port_lists(metafunc, "admin_up_pcs"))

0 commit comments

Comments
 (0)