From 36d595dd6b9752b8ee99443c237d040d353b8336 Mon Sep 17 00:00:00 2001 From: Andreas Hasenack Date: Tue, 26 Mar 2024 11:06:12 -0300 Subject: [PATCH] environment.py: get apparmor logs from host in a function --- features/environment.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/features/environment.py b/features/environment.py index 248b37bdf1..049230b55d 100644 --- a/features/environment.py +++ b/features/environment.py @@ -10,6 +10,7 @@ import pycloudlib # type: ignore # noqa: F401 from behave.model import Feature, Scenario +from behave.model_core import Status from behave.runner import Context import features.cloud as cloud @@ -464,9 +465,23 @@ def after_scenario(context, scenario): logging.error(str(e)) logging.warning("Failed to collect coverage") +def _get_apparmor_logs_from_host(): + # get apparmor DENIED messages from the host + with open("/var/log/syslog", "r") as syslog_fd: + syslog_messages = syslog_fd.readlines() + apparmor_denied = [ + msg.strip() + for msg in syslog_messages + if ("DENIED" in msg and "ubuntu_pro_" in msg) + ] + return apparmor_denied def after_step(context, step): """Collect test artifacts in the event of failure.""" + apparmor_logs = _get_apparmor_logs_from_host() + if apparmor_logs: + # naughty + step.status = Status.failed if step.status == "failed": logging.warning("STEP FAILED. Collecting logs.") inner_dir = os.path.join( @@ -499,18 +514,10 @@ def after_step(context, step): ) ) - # get apparmor DENIED messages from the host - # XXX - with open("/var/log/syslog", "r") as syslog_fd: - syslog_messages = syslog_fd.readlines() - apparmor_denied = [ - msg.strip() - for msg in syslog_messages - if ("DENIED" in msg and "ubuntu_pro_" in msg) - ] - logging.warning("XXX apparmor DENIED from host begin") - logging.warning("\n".join(apparmor_denied)) - logging.warning("XXX apparmor DENIED from host end") + if apparmor_logs: + logging.warning("XXX apparmor DENIED from host begin") + logging.warning("\n".join(apparmor_logs)) + logging.warning("XXX apparmor DENIED from host end") if hasattr(context, "machines") and SUT in context.machines: try: