Skip to content

Commit

Permalink
tests fixes for zero ports setups
Browse files Browse the repository at this point in the history
Signed-off-by: Sharon Lutati <slutati@nvidia.com>
  • Loading branch information
slutati1536 committed Jun 24, 2021
1 parent 447d6fc commit 136d9b9
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions tests/telemetry/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
SUBMODE_ON_CHANGE = 1
SUBMODE_SAMPLE = 2


# Helper functions
def get_dict_stdout(gnmi_out, certs_out):
""" Extracts dictionary from redis output.
Expand All @@ -39,13 +40,15 @@ def get_dict_stdout(gnmi_out, certs_out):
params_dict = dict(zip(key_list, value_list))
return params_dict


def get_list_stdout(cmd_out):
out_list = []
for x in cmd_out:
result = x.encode('UTF-8')
out_list.append(result)
return out_list


def setup_telemetry_forpyclient(duthost):
""" Set client_auth=false. This is needed for pyclient to sucessfully set up channel with gnmi server.
Restart telemetry process
Expand All @@ -58,6 +61,7 @@ def setup_telemetry_forpyclient(duthost):
else:
logger.info('client auth is false. No need to restart telemetry')


def generate_client_cli(duthost, method=METHOD_GET, xpath="COUNTERS/Ethernet0", target="COUNTERS_DB", subscribe_mode=SUBSCRIBE_MODE_STREAM, submode=SUBMODE_SAMPLE, intervalms=0, update_count=3):
"""Generate the py_gnmicli command line based on the given params.
"""
Expand All @@ -68,11 +72,13 @@ def generate_client_cli(duthost, method=METHOD_GET, xpath="COUNTERS/Ethernet0",
cmd += " --subscribe_mode {0} --submode {1} --interval {2} --update_count {3}".format(subscribe_mode, submode, intervalms, update_count)
return cmd


def assert_equal(actual, expected, message):
"""Helper method to compare an expected value vs the actual value.
"""
pytest_assert(actual == expected, "{0}. Expected {1} vs actual {2}".format(message, expected, actual))


@pytest.fixture(scope="module", autouse=True)
def verify_telemetry_dockerimage(duthosts, rand_one_dut_hostname):
"""If telemetry docker is available in image then return true
Expand All @@ -85,6 +91,7 @@ def verify_telemetry_dockerimage(duthosts, rand_one_dut_hostname):
if not (len(matching) > 0):
pytest.skip("docker-sonic-telemetry is not part of the image")


@pytest.fixture
def setup_streaming_telemetry(duthosts, rand_one_dut_hostname, localhost, ptfhost):
"""
Expand All @@ -105,12 +112,26 @@ def setup_streaming_telemetry(duthosts, rand_one_dut_hostname, localhost, ptfho
file_exists = ptfhost.stat(path="/gnxi/gnmi_cli_py/py_gnmicli.py")
pytest_assert(file_exists["stat"]["exists"] is True)


def skip_201911_and_older(duthost):
""" Skip the current test if the DUT version is 201911 or older.
"""
if parse_version(duthost.kernel_version) <= parse_version('4.9.0'):
pytest.skip("Test not supported for 201911 images. Skipping the test")


@pytest.fixture(scope='function')
def skip_if_no_data_port(duthosts, rand_one_dut_hostname):
"""
Fixture that skips test execution in case dut doesn't have data port Ethernet0
"""
duthost = duthosts[rand_one_dut_hostname]
for asic_index in duthost.get_frontend_asic_ids():
interface_list = get_port_map(duthost, asic_index)
if "Ethernet0" not in interface_list:
pytest.skip("This test is not supported as there is no data port Ethernet0 in dut")


# Test functions
def test_config_db_parameters(duthosts, rand_one_dut_hostname):
"""Verifies required telemetry parameters from config_db.
Expand Down Expand Up @@ -138,6 +159,7 @@ def test_config_db_parameters(duthosts, rand_one_dut_hostname):
server_crt_expected = "/etc/sonic/telemetry/streamingtelemetryserver.cer"
pytest_assert(str(value) == server_crt_expected, "'server_crt' value is not '{}'".format(server_crt_expected))


def test_telemetry_enabledbydefault(duthosts, rand_one_dut_hostname):
"""Verify telemetry should be enabled by default
"""
Expand All @@ -155,23 +177,22 @@ def test_telemetry_enabledbydefault(duthosts, rand_one_dut_hostname):
pytest_assert(str(v) == status_expected, "Telemetry feature is not enabled")


def test_telemetry_ouput(duthosts, rand_one_dut_hostname, ptfhost, setup_streaming_telemetry, localhost):
def test_telemetry_ouput(duthosts, rand_one_dut_hostname, ptfhost, skip_if_no_data_port,
setup_streaming_telemetry, localhost):
"""Run pyclient from ptfdocker and show gnmi server outputself.
"""
duthost = duthosts[rand_one_dut_hostname]
for asic_index in duthost.get_frontend_asic_ids():
interface_list = get_port_map(duthost, asic_index)
if "Ethernet0" in interface_list:
logger.info('start telemetry output testing')
dut_ip = duthost.mgmt_ip
cmd = 'python /gnxi/gnmi_cli_py/py_gnmicli.py -g -t {0} -p {1} -m get -x COUNTERS/Ethernet0 -xt COUNTERS_DB \
-o "ndastreamingservertest"'.format(dut_ip, TELEMETRY_PORT)
show_gnmi_out = ptfhost.shell(cmd)['stdout']
logger.info("GNMI Server output")
logger.info(show_gnmi_out)
result = str(show_gnmi_out)
inerrors_match = re.search("SAI_PORT_STAT_IF_IN_ERRORS", result)
pytest_assert(inerrors_match is not None, "SAI_PORT_STAT_IF_IN_ERRORS not found in gnmi_output")

logger.info('start telemetry output testing')
dut_ip = duthost.mgmt_ip
cmd = 'python /gnxi/gnmi_cli_py/py_gnmicli.py -g -t {0} -p {1} -m get -x COUNTERS/Ethernet0 -xt COUNTERS_DB \
-o "ndastreamingservertest"'.format(dut_ip, TELEMETRY_PORT)
show_gnmi_out = ptfhost.shell(cmd)['stdout']
logger.info("GNMI Server output")
logger.info(show_gnmi_out)
result = str(show_gnmi_out)
inerrors_match = re.search("SAI_PORT_STAT_IF_IN_ERRORS", result)
pytest_assert(inerrors_match is not None, "SAI_PORT_STAT_IF_IN_ERRORS not found in gnmi_output")


def test_osbuild_version(duthosts, rand_one_dut_hostname, ptfhost, localhost):
Expand Down Expand Up @@ -233,20 +254,17 @@ def test_sysuptime(duthosts, rand_one_dut_hostname, ptfhost, setup_streaming_tel
pytest.fail("The value of system uptime was not updated correctly.")


def test_virtualdb_table_streaming(duthosts, rand_one_dut_hostname, ptfhost, localhost):
def test_virtualdb_table_streaming(duthosts, rand_one_dut_hostname, skip_if_no_data_port, ptfhost, localhost):
"""Run pyclient from ptfdocker to stream a virtual-db query multiple times.
"""
logger.info('start virtual db sample streaming testing')

duthost = duthosts[rand_one_dut_hostname]
skip_201911_and_older(duthost)
cmd = generate_client_cli(duthost=duthost, method=METHOD_SUBSCRIBE, update_count = 3)
for asic_index in duthost.get_frontend_asic_ids():
interface_list = get_port_map(duthost, asic_index)
if "Ethernet0" in interface_list:
show_gnmi_out = ptfhost.shell(cmd)['stdout']
result = str(show_gnmi_out)
show_gnmi_out = ptfhost.shell(cmd)['stdout']
result = str(show_gnmi_out)

assert_equal(len(re.findall('Max update count reached 3', result)), 1, "Streaming update count in:\n{0}".format(result))
assert_equal(len(re.findall('name: "Ethernet0"\n', result)), 4, "Streaming updates for Ethernet0 in:\n{0}".format(result)) # 1 for request, 3 for response
assert_equal(len(re.findall('timestamp: \d+', result)), 3, "Timestamp markers for each update message in:\n{0}".format(result))
assert_equal(len(re.findall('Max update count reached 3', result)), 1, "Streaming update count in:\n{0}".format(result))
assert_equal(len(re.findall('name: "Ethernet0"\n', result)), 4, "Streaming updates for Ethernet0 in:\n{0}".format(result)) # 1 for request, 3 for response
assert_equal(len(re.findall('timestamp: \d+', result)), 3, "Timestamp markers for each update message in:\n{0}".format(result))

0 comments on commit 136d9b9

Please sign in to comment.