Skip to content
Merged
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
47 changes: 41 additions & 6 deletions tests/telemetry/test_telemetry.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import re
import pytest

from tests.common.helpers.assertions import pytest_assert
from tests.common.utilities import wait_until

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

logger = logging.getLogger(__name__)

# Helper functions
def get_dict_stdout(gnmi_out, certs_out):
""" Extracts dictionary from redis output.
Expand All @@ -24,21 +29,30 @@ def get_list_stdout(cmd_out):
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
"""
client_auth_out = duthost.shell('sonic-db-cli CONFIG_DB HGET "TELEMETRY|gnmi" "client_auth"', module_ignore_errors=False)['stdout_lines']
client_auth = str(client_auth_out[0])
if client_auth == "true":
duthost.shell('sonic-db-cli CONFIG_DB HSET "TELEMETRY|gnmi" "client_auth" "false"', module_ignore_errors=False)
duthost.service(name="telemetry", state="restarted")
else:
logger.info('client auth is false. No need to restart telemetry')

# Test functions
def test_config_db_parameters(duthost):
"""Verifies required telemetry parameters from config_db.
"""
gnmi = duthost.shell('/usr/bin/redis-cli -n 4 hgetall "TELEMETRY|gnmi"', module_ignore_errors=False)['stdout_lines']
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")

certs = duthost.shell('/usr/bin/redis-cli -n 4 hgetall "TELEMETRY|certs"', module_ignore_errors=False)['stdout_lines']
certs = duthost.shell('sonic-db-cli CONFIG_DB HGETALL "TELEMETRY|certs"', module_ignore_errors=False)['stdout_lines']
pytest_assert(certs is not None, "TELEMETRY|certs does not exist in config_db")

d = get_dict_stdout(gnmi, certs)
for key, value in d.items():
if str(key) == "client_auth":
client_auth_expected = "true"
pytest_assert(str(value) == client_auth_expected, "'client_auth' value is not '{}'".format(client_auth_expected))
if str(key) == "port":
port_expected = "50051"
pytest_assert(str(value) == port_expected, "'port' value is not '{}'".format(port_expected))
Expand All @@ -55,7 +69,7 @@ def test_config_db_parameters(duthost):
def test_telemetry_enabledbydefault(duthost):
"""Verify telemetry should be enabled by default
"""
status = duthost.shell('/usr/bin/redis-cli -n 4 hgetall "FEATURE|telemetry"', module_ignore_errors=False)['stdout_lines']
status = duthost.shell('sonic-db-cli CONFIG_DB HGETALL "FEATURE|telemetry"', module_ignore_errors=False)['stdout_lines']
status_list = get_list_stdout(status)
# Elements in list alternate between key and value. Separate them and combine into a dict.
status_key_list = status_list[0::2]
Expand All @@ -65,3 +79,24 @@ def test_telemetry_enabledbydefault(duthost):
if str(k) == "status":
status_expected = "enabled";
pytest_assert(str(v) == status_expected, "Telemetry feature is not enabled")

def test_telemetry_ouput(duthost, ptfhost):
"""Run pyclient from ptfdocker and show gnmi server outputself.
"""
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')
dut_ip = duthost.setup()['ansible_facts']['ansible_eth0']['ipv4']['address']
# 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 50051 -m get -x COUNTERS/Ethernet0 -xt COUNTERS_DB -o "ndastreamingservertest"'.format(dut_ip)
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")