Skip to content

Commit ad3935a

Browse files
committed
fix conditional issue and check all possible substring locations to detect serial console enabled
1 parent 2951df2 commit ad3935a

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

microsoft/testsuites/core/azure_image_standard.py

+46-36
Original file line numberDiff line numberDiff line change
@@ -798,68 +798,78 @@ def verify_repository_installed(self, node: Node) -> None: # noqa: C901
798798

799799
@TestCaseMetadata(
800800
description="""
801-
This test will check the serial console is enabled from kernel command line
802-
in dmesg.
801+
This test will check the serial console is enabled from kernel command line.
803802
804803
Steps:
805-
1. Get the kernel command line from /var/log/messages or
806-
/var/log/syslog output.
804+
1. Get the kernel command line from /var/log/messages, /var/log/syslog,
805+
dmesg, or journalctl output.
807806
2. Check expected setting from kernel command line.
808807
2.1. Expected to see 'console=ttyAMA0' for aarch64.
809808
2.2. Expected to see 'console=ttyS0' for x86_64.
809+
2.3. Expected to see 'uart0: console 115200,n,8,1 for FreeBSD.
810810
""",
811811
priority=1,
812812
requirement=simple_requirement(supported_platform_type=[AZURE, READY]),
813813
)
814814
def verify_serial_console_is_enabled(self, node: Node) -> None:
815-
console_device = {
816-
CpuArchitecture.X64: "ttyS0",
817-
CpuArchitecture.ARM64: "ttyAMA0",
818-
}
819815
if isinstance(node.os, CBLMariner):
820816
if node.os.information.version < "2.0.0":
821817
raise SkippedException(
822818
"CBLMariner 1.0 has a known 'wont fix' issue with this test"
823819
)
824-
if isinstance(node.os, CBLMariner) or (
825-
isinstance(node.os, Ubuntu) and node.os.information.version >= "22.10.0"
826-
):
827-
log_output = node.tools[Dmesg].get_output()
828-
log_file = "dmesg"
829-
else:
830-
cat = node.tools[Cat]
831-
if node.shell.exists(node.get_pure_path("/var/log/messages")):
832-
log_file = "/var/log/messages"
833-
log_output = cat.read(log_file, force_run=True, sudo=True)
834-
elif node.shell.exists(node.get_pure_path("/var/log/syslog")):
835-
log_file = "/var/log/syslog"
836-
log_output = cat.read(log_file, force_run=True, sudo=True)
837-
else:
838-
log_file = "journalctl"
839-
journalctl = node.tools[Journalctl]
840-
log_output = journalctl.first_n_logs_from_boot()
841-
if not log_output:
842-
raise LisaException(
843-
"Neither /var/log/messages nor /var/log/syslog found."
844-
"and journal ctl log is empty."
845-
)
846-
820+
console_device = {
821+
CpuArchitecture.X64: "ttyS0",
822+
CpuArchitecture.ARM64: "ttyAMA0",
823+
}
847824
lscpu = node.tools[Lscpu]
848825
arch = lscpu.get_architecture()
849826
current_console_device = console_device[arch]
850827
console_enabled_pattern = re.compile(
851828
rf"^(.*console \[{current_console_device}\] enabled.*)$", re.M
852829
)
853830
freebsd_pattern = re.compile(r"^(.*uart0: console \(115200,n,8,1\).*)$", re.M)
854-
result = find_patterns_in_lines(
855-
log_output, [console_enabled_pattern, freebsd_pattern]
856-
)
857-
if not (result[0] or result[1]):
831+
patterns = [console_enabled_pattern, freebsd_pattern]
832+
logs_checked = []
833+
pattern_found = False
834+
# Check dmesg output for the patterns if certain OS detected
835+
if isinstance(node.os, CBLMariner) or (
836+
isinstance(node.os, Ubuntu) and node.os.information.version >= "22.10.0"
837+
):
838+
dmesg_tool = node.tools[Dmesg]
839+
log_output = dmesg_tool.get_output()
840+
logs_checked.append(f"{dmesg_tool.command}")
841+
if any(find_patterns_in_lines(log_output, patterns)):
842+
pattern_found = True
843+
else:
844+
# Check each log source, if it is accessible, for the defined patterns
845+
# If log files can be read, add to list of logs checked
846+
log_sources = [
847+
("/var/log/syslog", node.tools[Cat]),
848+
("/var/log/messages", node.tools[Cat]),
849+
]
850+
for log_file, tool in log_sources:
851+
if node.shell.exists(node.get_pure_path(log_file)):
852+
current_log_output = tool.read(log_file, force_run=True, sudo=True)
853+
if current_log_output:
854+
logs_checked.append(log_file)
855+
if any(find_patterns_in_lines(current_log_output, patterns)):
856+
pattern_found = True
857+
break
858+
# Check journalctl logs if patterns were not found in other log sources
859+
journalctl_tool = node.tools[Journalctl]
860+
if not pattern_found and journalctl_tool.exists:
861+
current_log_output = journalctl_tool.first_n_logs_from_boot()
862+
if current_log_output:
863+
logs_checked.append(f"{journalctl_tool.command}")
864+
if any(find_patterns_in_lines(current_log_output, patterns)):
865+
pattern_found = True
866+
# Raise an exception if the patterns were not found in any of the checked logs
867+
if not pattern_found:
858868
raise LisaException(
859869
"Fail to find console enabled line "
860870
f"'console [{current_console_device}] enabled' "
861871
"or 'uart0: console (115200,n,8,1)' "
862-
f"from {log_file} output",
872+
f"from {', '.join(logs_checked)} output"
863873
)
864874

865875
@TestCaseMetadata(

0 commit comments

Comments
 (0)