Skip to content

Commit

Permalink
[pytest] Test the system uptime in streaming telemetry (#2583)
Browse files Browse the repository at this point in the history
Signed-off-by: Yong Zhao [email protected]

Summary:

Fixes # (issue)

 Bug fix
 Testbed and Framework(new/improvement)
 Test case(new/improvement)
Approach
What is the motivation for this PR?
This PR aims to add a function in test_telemetry.py to test the new dataset system uptime in non-db client of streaming telemetry.

How did you do it?
The ptf docker container will send the request/query to telemetry server by running the command python /gnxi/gnmi_cli_py/py_gnmicli.py -g -t <DuT_IP> -p Telemetry_Port -m get -x platform/sysuptime -xt OTHERS -o "ndastreamingservertest" and then get the response which includes a field showing the string of system uptime.

This function will first verify whether the string was an integer and then decide whether the system uptime was updated correctly.

How did you verify/test it?
I ran this test against the lab device str-dx010-acs-1.

Any platform specific information?
N/A

Supported testbed topology if it's a new test case?
N/A
  • Loading branch information
yozhao101 authored Dec 9, 2020
1 parent 3f2ff04 commit 2c5ff63
Showing 1 changed file with 72 additions and 24 deletions.
96 changes: 72 additions & 24 deletions tests/telemetry/test_telemetry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

import logging
import re
import pytest
Expand Down Expand Up @@ -45,23 +47,43 @@ def setup_telemetry_forpyclient(duthost):
else:
logger.info('client auth is false. No need to restart telemetry')

def verify_telemetry_dockerimage(duthost):
@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
"""
docker_out_list = []
duthost = duthosts[rand_one_dut_hostname]
docker_out = duthost.shell('docker images docker-sonic-telemetry', module_ignore_errors=False)['stdout_lines']
docker_out_list = get_list_stdout(docker_out)
matching = [s for s in docker_out_list if "docker-sonic-telemetry" in s]
return (len(matching) > 0)
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):
"""
@summary: Post setting up the streaming telemetry before running the test.
"""
duthost = duthosts[rand_one_dut_hostname]
setup_telemetry_forpyclient(duthost)

# Wait until telemetry was restarted
pytest_assert(wait_until(100, 10, duthost.is_service_fully_started, "telemetry"), "TELEMETRY not started.")
logger.info("telemetry process restarted. Now run pyclient on ptfdocker")

# Wait until the TCP port was opened
dut_ip = duthost.mgmt_ip
wait_tcp_connection(localhost, dut_ip, TELEMETRY_PORT, timeout_s=60)

# pyclient should be available on ptfhost. If it was not available, then fail pytest.
file_exists = ptfhost.stat(path="/gnxi/gnmi_cli_py/py_gnmicli.py")
pytest_assert(file_exists["stat"]["exists"] is True)

# Test functions
def test_config_db_parameters(duthosts, rand_one_dut_hostname):
"""Verifies required telemetry parameters from config_db.
"""
duthost = duthosts[rand_one_dut_hostname]
docker_present = verify_telemetry_dockerimage(duthost)
if not docker_present:
pytest.skip("docker-sonic-telemetry is not part of the image")

gnmi = duthost.shell('sonic-db-cli CONFIG_DB HGETALL "TELEMETRY|gnmi"', module_ignore_errors=False)['stdout_lines']
pytest_assert(gnmi is not None, "TELEMETRY|gnmi does not exist in config_db")
Expand All @@ -88,9 +110,6 @@ def test_telemetry_enabledbydefault(duthosts, rand_one_dut_hostname):
"""Verify telemetry should be enabled by default
"""
duthost = duthosts[rand_one_dut_hostname]
docker_present = verify_telemetry_dockerimage(duthost)
if not docker_present:
pytest.skip("docker-sonic-telemetry is not part of the image")

status = duthost.shell('sonic-db-cli CONFIG_DB HGETALL "FEATURE|telemetry"', module_ignore_errors=False)['stdout_lines']
status_list = get_list_stdout(status)
Expand All @@ -103,28 +122,13 @@ def test_telemetry_enabledbydefault(duthosts, rand_one_dut_hostname):
status_expected = "enabled";
pytest_assert(str(v) == status_expected, "Telemetry feature is not enabled")

def test_telemetry_ouput(duthosts, rand_one_dut_hostname, ptfhost, localhost):
def test_telemetry_ouput(duthosts, rand_one_dut_hostname, ptfhost, setup_streaming_telemetry, localhost):
"""Run pyclient from ptfdocker and show gnmi server outputself.
"""
duthost = duthosts[rand_one_dut_hostname]
docker_present = verify_telemetry_dockerimage(duthost)
if not docker_present:
pytest.skip("docker-sonic-telemetry is not part of the image")

logger.info('start telemetry output testing')
setup_telemetry_forpyclient(duthost)

# wait till telemetry is restarted
pytest_assert(wait_until(100, 10, duthost.is_service_fully_started, "telemetry"), "TELEMETRY not started")
logger.info('telemetry process restarted. Now run pyclient on ptfdocker')

# Wait until the TCP port is open
dut_ip = duthost.mgmt_ip
wait_tcp_connection(localhost, dut_ip, TELEMETRY_PORT, timeout_s=60)

# pyclient should be available on ptfhost. If not fail pytest.
file_exists = ptfhost.stat(path="/gnxi/gnmi_cli_py/py_gnmicli.py")
pytest_assert(file_exists["stat"]["exists"] is True)
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']
Expand All @@ -133,3 +137,47 @@ def test_telemetry_ouput(duthosts, rand_one_dut_hostname, ptfhost, localhost):
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_sysuptime(duthosts, rand_one_dut_hostname, ptfhost, setup_streaming_telemetry, localhost):
"""
@summary: Run pyclient from ptfdocker and test the dataset 'system uptime' to check
whether the value of 'system uptime' was float number and whether the value was
updated correctly.
"""
logger.info("start test the dataset 'system uptime'")
duthost = duthosts[rand_one_dut_hostname]
dut_ip = duthost.mgmt_ip
cmd = 'python /gnxi/gnmi_cli_py/py_gnmicli.py -g -t {0} -p {1} -m get -x proc/uptime -xt OTHERS \
-o "ndastreamingservertest"'.format(dut_ip, TELEMETRY_PORT)
system_uptime_info = ptfhost.shell(cmd)["stdout_lines"]
system_uptime_1st = 0
found_system_uptime_field = False
for line_info in system_uptime_info:
if "total" in line_info:
try:
system_uptime_1st = float(line_info.split(":")[1].strip())
found_system_uptime_field = True
except ValueError as err:
pytest.fail("The value of system uptime was not a float. Error message was '{}'".format(err))

if not found_system_uptime_field:
pytest.fail("The field of system uptime was not found.")

# Wait 10 seconds such that the value of system uptime was added 10 seconds.
time.sleep(10)
system_uptime_info = ptfhost.shell(cmd)["stdout_lines"]
system_uptime_2nd = 0
found_system_uptime_field = False
for line_info in system_uptime_info:
if "total" in line_info:
try:
system_uptime_2nd = float(line_info.split(":")[1].strip())
found_system_uptime_field = True
except ValueError as err:
pytest.fail("The value of system uptime was not a float. Error message was '{}'".format(err))

if not found_system_uptime_field:
pytest.fail("The field of system uptime was not found.")

if system_uptime_2nd - system_uptime_1st < 10:
pytest.fail("The value of system uptime was not updated correctly.")

0 comments on commit 2c5ff63

Please sign in to comment.