Skip to content

Commit

Permalink
Skip GNMI check during teardown and disable LogAnalyzer for reboot tests
Browse files Browse the repository at this point in the history
Skip GNMI check during teardown and disable LogAnalyzer for reboot tests
  • Loading branch information
hdwhdw authored Feb 3, 2025
2 parents 9fcacf4 + f452482 commit 84075f7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
8 changes: 7 additions & 1 deletion tests/gnmi/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,13 @@ def setup_gnmi_server(duthosts, rand_one_dut_hostname, localhost, ptfhost):

# Rollback configuration
rollback(duthost, SETUP_ENV_CP)
recover_cert_config(duthost)
# Get the skip_gnmi_check flag from duthost options
skip_gnmi_check = duthost.host.options.get('skip_gnmi_check', False)
# Skip GNMI restart if the reboot flag is set
if not skip_gnmi_check:
recover_cert_config(duthost)
else:
logging.info("Skipping GNMI restart due to skip_gnmi_check flag")


@pytest.fixture(scope="module", autouse=True)
Expand Down
51 changes: 37 additions & 14 deletions tests/gnmi/test_gnoi_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
pytest.mark.topology('any')
]

MAX_TIME_TO_REBOOT = 300

"""
This module contains tests for the gNOI System API.
Expand Down Expand Up @@ -42,45 +43,58 @@ def test_gnoi_system_reboot(duthosts, rand_one_dut_hostname, localhost):
"""
duthost = duthosts[rand_one_dut_hostname]

# Set flag to indicate that this test involves reboot
duthost.host.options['skip_gnmi_check'] = True

# Trigger reboot
ret, msg = gnoi_request(duthost, localhost, "Reboot", '{"method": 1}')
ret, msg = gnoi_request(duthost, localhost, "Reboot", '{"method": 1,"delay":0,"message":"Cold Reboot"}')
pytest_assert(ret == 0, "System.Reboot API reported failure (rc = {}) with message: {}".format(ret, msg))
logging.info("System.Reboot API returned msg: {}".format(msg))


@pytest.mark.disable_loganalyzer
def test_gnoi_system_reboot_fail_invalid_method(duthosts, rand_one_dut_hostname, localhost):
"""
Verify the gNOI System Reboot API fails with invalid method.
"""
duthost = duthosts[rand_one_dut_hostname]

# Set flag to indicate that this test involves reboot
duthost.host.options['skip_gnmi_check'] = True

# Trigger reboot with invalid method
ret, msg = gnoi_request(duthost, localhost, "Reboot", '{"method": 99}')
pytest_assert(ret != 0, "System.Reboot API did not report failure with invalid method")


@pytest.mark.disable_loganalyzer
def test_gnoi_system_reboot_when_reboot_active(duthosts, rand_one_dut_hostname, localhost):
"""
Verify the gNOI System Reboot API fails if a reboot is already active.
"""
duthost = duthosts[rand_one_dut_hostname]

# Set flag to indicate that this test involves reboot
duthost.host.options['skip_gnmi_check'] = True

# Trigger first reboot
ret, msg = gnoi_request(duthost, localhost, "Reboot", '{"method": 1}')
ret, msg = gnoi_request(duthost, localhost, "Reboot", '{"method": 1,"delay":0,"message":"Cold Reboot"}')
pytest_assert(ret == 0, "System.Reboot API reported failure (rc = {}) with message: {}".format(ret, msg))
logging.info("System.Reboot API returned msg: {}".format(msg))

# Trigger second reboot while the first one is still active
ret, msg = gnoi_request(duthost, localhost, "Reboot", '{"method": 1}')
ret, msg = gnoi_request(duthost, localhost, "Reboot", '{"method": 1,"delay":0,"message":"Cold Reboot"}')
pytest_assert(ret != 0, "System.Reboot API did not report failure when reboot is already active")


@pytest.mark.disable_loganalyzer
def test_gnoi_system_reboot_status_immediately(duthosts, rand_one_dut_hostname, localhost):
"""
Verify the gNOI System RebootStatus API returns the correct status immediately after reboot.
"""
duthost = duthosts[rand_one_dut_hostname]

# Set flag to indicate that this test involves reboot
duthost.host.options['skip_gnmi_check'] = True

# Trigger reboot
ret, msg = gnoi_request(duthost, localhost, "Reboot", '{"method": 1, "message": "test"}')
pytest_assert(ret == 0, "System.Reboot API reported failure (rc = {}) with message: {}".format(ret, msg))
Expand All @@ -107,6 +121,9 @@ def gnoi_system_reboot_status_after_startup(duthosts, rand_one_dut_hostname, loc
"""
duthost = duthosts[rand_one_dut_hostname]

# Set flag to indicate that this test involves reboot
duthost.host.options['skip_gnmi_check'] = True

# Trigger reboot
ret, msg = gnoi_request(duthost, localhost, "Reboot", '{"method": 1, "message": "test"}')
pytest_assert(ret == 0, "System.Reboot API reported failure (rc = {}) with message: {}".format(ret, msg))
Expand Down Expand Up @@ -138,12 +155,18 @@ def extract_first_json_substring(s):
:return: The first JSON substring if found, otherwise None.
"""

json_pattern = re.compile(r'\{.*?\}')
match = json_pattern.search(s)
if match:
try:
return json.loads(match.group())
except json.JSONDecodeError:
logging.error("Failed to parse JSON: {}".format(match.group()))
return None
return None
start_index = s.find('{') # Find the first '{' in the string
if start_index == -1:
logging.error("No JSON found in response: {}".format(s))
return None
json_str = s[start_index:] # Extract substring starting from '{'
try:
parsed_json = json.loads(json_str) # Attempt to parse the JSON
# Handle cases where "status": {} is empty
if "status" in parsed_json and parsed_json["status"] == {}:
logging.warning("Replacing empty 'status' field with a default value.")
parsed_json["status"] = {"unknown": "empty_status"}
return parsed_json
except json.JSONDecodeError as e:
logging.error("Failed to parse JSON: {} | Error: {}".format(json_str, e))
return None

0 comments on commit 84075f7

Please sign in to comment.