Skip to content

Commit 85eedf7

Browse files
authoredMar 11, 2025
Add a testcase for GNOI OS.Verify. (sonic-net#16770)
Description of PR Summary: Add a testcase for GNOI OS.Verify. Approach What is the motivation for this PR? Add a testcase for GNOI OS.Verify. How did you do it? Add a testcase for GNOI OS.Verify. How did you verify/test it? On physical switch: with an up to date image
1 parent cc235c0 commit 85eedf7

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
 

‎tests/gnmi/helper.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22
import logging
33
import pytest
4+
import json
45
from tests.common.utilities import wait_until
56
from tests.common.helpers.gnmi_utils import GNMIEnvironment
67

@@ -452,3 +453,25 @@ def gnoi_request(duthost, localhost, module, rpc, request_json_data):
452453
return -1, output['stderr']
453454
else:
454455
return 0, output['stdout']
456+
457+
458+
def extract_gnoi_response(output):
459+
"""
460+
Extract the JSON response from the gNOI client output
461+
462+
Args:
463+
output: gNOI client output, the output is in the form of
464+
"Module RPC: <JSON response>", e.g. "System Time\n {"time":1735921221909617549}"
465+
466+
Returns:
467+
json response: JSON response extracted from the output
468+
"""
469+
try:
470+
if '\n' not in output:
471+
logging.error("Invalid output format: {}, expecting 'Module RPC: <JSON response>'.".format(output))
472+
return None
473+
response_line = output.split('\n')[1]
474+
return json.loads(response_line)
475+
except json.JSONDecodeError:
476+
logging.error("Failed to parse JSON: {}".format(response_line))
477+
return None

‎tests/gnmi/test_gnoi_os.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
import logging
3+
4+
from .helper import gnoi_request, extract_gnoi_response
5+
from tests.common.helpers.assertions import pytest_assert
6+
7+
pytestmark = [
8+
pytest.mark.topology('any')
9+
]
10+
11+
"""
12+
This module contains tests for the gNOI OS API.
13+
"""
14+
15+
16+
@pytest.mark.disable_loganalyzer
17+
def test_gnoi_os_verify(duthosts, rand_one_dut_hostname, localhost):
18+
"""
19+
Verify the gNOI OS Verify API returns the current OS version.
20+
"""
21+
duthost = duthosts[rand_one_dut_hostname]
22+
23+
# Get current OS version
24+
ret, msg = gnoi_request(duthost, localhost, "OS", "Verify", "")
25+
pytest_assert(ret == 0, "OS.Verify API reported failure (rc = {}) with message: {}".format(ret, msg))
26+
logging.info("OS.Verify API returned msg: {}".format(msg))
27+
# Message should contain a json substring like this {"version":"SONiC-OS-20240510.24"}
28+
# Extract JSON part from the message
29+
msg_json = extract_gnoi_response(msg)
30+
if not msg_json:
31+
pytest.fail("Failed to extract JSON from OS.Verify API response")
32+
logging.info("Extracted JSON: {}".format(msg_json))
33+
pytest_assert("version" in msg_json, "OS.Verify API did not return os_version")
34+
35+
os_version_ansible = duthost.image_facts()["ansible_facts"]["ansible_image_facts"]["current"]
36+
pytest_assert(msg_json["version"] == os_version_ansible, "OS.Verify API returned incorrect OS version")

0 commit comments

Comments
 (0)