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
55 changes: 43 additions & 12 deletions tests/pfcwd/test_pfcwd_timer_accuracy.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
import pytest
import time
import re

from tests.common.errors import RunAnsibleModuleFail
from tests.common.fixtures.conn_graph_facts import enum_fanout_graph_facts # noqa F401
from tests.common.helpers.assertions import pytest_assert
from tests.common.helpers.pfc_storm import PFCStorm
Expand Down Expand Up @@ -202,18 +202,39 @@ def run_test(self, setup_info):
logger.info("Skip time detect for VS")
return

skip_this_loop = False
if self.dut.topo_type == 't2' and self.storm_handle.peer_device.os == 'sonic':
storm_detect_ms = self.retrieve_timestamp("[d]etected PFC storm")
else:
storm_start_ms = self.retrieve_timestamp("[P]FC_STORM_START")
storm_detect_ms = self.retrieve_timestamp("[d]etected PFC storm")

logger.info("Wait for PFC storm end marker to appear in logs")
time.sleep(16)

if self.dut.topo_type == 't2' and self.storm_handle.peer_device.os == 'sonic':
storm_restore_ms = self.retrieve_timestamp("[s]torm restored")
else:
storm_end_ms = self.retrieve_timestamp("[P]FC_STORM_END")
storm_restore_ms = self.retrieve_timestamp("[s]torm restored")

if self.dut.topo_type == 't2' and self.storm_handle.peer_device.os == 'sonic':
if storm_detect_ms == 0 or storm_restore_ms == 0:
logging.warning("storm_detect_ms {} or storm_restore_ms {} is 0".format(
storm_detect_ms, storm_restore_ms))
skip_this_loop = True
else:
if storm_start_ms == 0 or storm_detect_ms == 0 or storm_end_ms == 0 or storm_restore_ms == 0:
logging.warning("storm_start_ms {} or storm_detect_ms {} or "
"storm_end_ms {} or storm_restore_ms {} is 0".format(
storm_start_ms, storm_detect_ms, storm_end_ms, storm_restore_ms))
skip_this_loop = True

if skip_this_loop:
logger.warning("Skip this loop due to missing timestamps")
return

if not (self.dut.topo_type == 't2' and self.storm_handle.peer_device.os == 'sonic'):
real_detect_time = storm_detect_ms - storm_start_ms
real_restore_time = storm_restore_ms - storm_end_ms
self.all_detect_time.append(real_detect_time)
Expand All @@ -239,8 +260,8 @@ def verify_pfcwd_timers(self):
self.all_detect_time.sort()
self.all_restore_time.sort()
logger.info("Verify that real detection time is not greater than configured")
logger.info("all detect time {}".format(self.all_detect_time))
logger.info("all restore time {}".format(self.all_restore_time))
logger.info("sorted all detect time {}".format(self.all_detect_time))
logger.info("sorted all restore time {}".format(self.all_restore_time))

check_point = ITERATION_NUM // 2 - 1
config_detect_time = self.timers['pfc_wd_detect_time'] + self.timers['pfc_wd_poll_time']
Expand Down Expand Up @@ -324,17 +345,27 @@ def retrieve_timestamp(self, pattern):
Returns:
timestamp_ms (int): syslog timestamp in ms for the line matching the pattern
"""
cmd = "grep \"{}\" /var/log/syslog".format(pattern)
syslog_msg_list = self.dut.shell(cmd)['stdout'].split()
try:
timestamp_ms = float(self.dut.shell("date -d \"{}\" +%s%3N".format(syslog_msg_list[3]))['stdout'])
except RunAnsibleModuleFail:
timestamp_ms = float(self.dut.shell("date -d \"{}\" +%s%3N".format(syslog_msg_list[2]))['stdout'])
except Exception as e:
logging.error("Error when parsing syslog message timestamp: {}".format(repr(e)))
pytest.fail("Failed to parse syslog message timestamp")
cmd = "grep \"{}\" /var/log/syslog".format(pattern)
syslog_msg = self.dut.shell(cmd)['stdout']

# Regular expressions for the two timestamp formats
regex1 = re.compile(r'^[A-Za-z]{3} \d{2} \d{2}:\d{2}:\d{2}\.\d{6}')
regex2 = re.compile(r'^\d{4} [A-Za-z]{3} \d{2} \d{2}:\d{2}:\d{2}\.\d{6}')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lipxu is there any reasons why we're switching to regex here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, @auspham , just saw the comments. sorry for the late response.
I used regex here because I initially aimed to standardize the handling function by searching for the timestamp and processing it with the same function, however, during UT test, I found the date command does not support the format with the year. therefore, I reverted to the previous handling process but retained the regex change here. thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can only search the timestamp without year, let me try


return int(timestamp_ms)
if regex1.match(syslog_msg):
timestamp = syslog_msg.replace(' ', ' ').split(' ')[2]
elif regex2.match(syslog_msg):
timestamp = syslog_msg.replace(' ', ' ').split(' ')[3]
else:
logger.warning("Get {} timestamp: Unexpected syslog message format".format(syslog_msg))
return int(0)

timestamp_ms = self.dut.shell("date -d {} +%s%3N".format(timestamp))['stdout']
return int(timestamp_ms)
except Exception as e:
logger.warning("Get {} timestamp: An unexpected error occurred: {}".format(pattern, str(e)))
return int(0)

def test_pfcwd_timer_accuracy(self, duthosts, ptfhost, enum_rand_one_per_hwsku_frontend_hostname,
pfcwd_timer_setup_restore, fanouthosts):
Expand Down