Ignore acl_log lines printed before a test SG logging starts

Instead of taking the last N lines printed that include "acl_log", print
only those "acl_log" lines with ID greater than the value obtained before
the test started.

Change-Id: I31d74e72678c21dac0850daea761fca1f9f736ac
This commit is contained in:
Eduardo Olivares
2024-12-23 09:55:21 +01:00
parent a575b79451
commit 901b1cd6b6

View File

@@ -203,36 +203,37 @@ class BaseSecGroupLoggingTest(
def _get_logs_and_counts(self, hypervisor_ssh, start_track): def _get_logs_and_counts(self, hypervisor_ssh, start_track):
# create dictionary to track values of a hypervisor if it doesn't exist # create dictionary to track values of a hypervisor if it doesn't exist
self._hypervisors_counts.setdefault(hypervisor_ssh.host, dict()) self._hypervisors_counts.setdefault(hypervisor_ssh.host, dict())
# tracks A value, before test traffic sent to be logged
if start_track: if start_track:
# tracks A value, before test traffic sent to be logged
_track_value = int(hypervisor_ssh.exec_command( _track_value = int(hypervisor_ssh.exec_command(
"sudo grep acl_log {} | tail -n1 | cut -d '|' -f 2" "sudo grep acl_log {} | tail -n1 | cut -d '|' -f 2"
.format(self.SG_LOG_FILE), timeout=120)) .format(self.SG_LOG_FILE), timeout=120))
self._hypervisors_counts[hypervisor_ssh.host]['A'] = _track_value self._hypervisors_counts[hypervisor_ssh.host]['A'] = _track_value
LOG.debug("Start log count value A on '%s' is %d", LOG.debug("Start log count value A on '%s' is %d",
hypervisor_ssh.host, _track_value) hypervisor_ssh.host, _track_value)
# tracks B value, after test traffic sent to be logged
# (extracts logs from file right away, to avoid race conditions).
else: else:
cmds_output = hypervisor_ssh.exec_command( # tracks B value, after test traffic sent to be logged
("B=$(sudo grep acl_log {0} | tail -n1 | cut -d '|' -f 2 | " # (extracts logs from file right away, to avoid race conditions).
"sed 's/^0*//') && echo $B && " cmd_outputs = hypervisor_ssh.exec_command(
"sudo grep acl_log {0} | tail -n $(($B-{1}))").format( "sudo grep acl_log {0}".format(self.SG_LOG_FILE),
self.SG_LOG_FILE,
self._hypervisors_counts[hypervisor_ssh.host]['A']),
timeout=120).splitlines() timeout=120).splitlines()
# save B in instance, and log in tempest the B value b = self._hypervisors_counts[hypervisor_ssh.host]['B'] = int(
_track_value = int(cmds_output[0]) cmd_outputs[-1].split("|")[1])
self._hypervisors_counts[hypervisor_ssh.host]['B'] = _track_value
LOG.debug("End log count value B on '%s' is %d", LOG.debug("End log count value B on '%s' is %d",
hypervisor_ssh.host, _track_value) hypervisor_ssh.host, b)
# parse and save logs retrieved, per hypervisor tracked counts # extract logs with ID greater than A and lower or equal than B
# i.e. the logs that were printed during this test
a = self._hypervisors_counts[hypervisor_ssh.host]['A']
new_logs = [output for output in cmd_outputs
if a < int(output.split("|")[1]) <= b]
self._hypervisors_counts[hypervisor_ssh.host][ self._hypervisors_counts[hypervisor_ssh.host][
'tested_logs'] = "\n".join(cmds_output[1:]) 'tested_logs'] = "\n".join(new_logs)
# log in tempest the retrieved entries amount # log in tempest the retrieved entries amount
_test_logs_amount = \ _test_logs_amount = b - a
self._hypervisors_counts[hypervisor_ssh.host]['B'] - \ mess = "Unexpected num of lines with {} < acl_log_ID <= {}".format(
self._hypervisors_counts[hypervisor_ssh.host]['A'] a, b)
mess += "\n" + "\n".join(cmd_outputs)
self.assertEqual(_test_logs_amount, len(new_logs), mess)
self._hypervisors_counts[hypervisor_ssh.host][ self._hypervisors_counts[hypervisor_ssh.host][
'test_logs_amount'] = _test_logs_amount 'test_logs_amount'] = _test_logs_amount
LOG.debug( LOG.debug(