selftest/qemurunner: Work around possible control character contamination

Using a binary string as the login banner search expression is fraught with
risks. We've seen cases on the autobuilder where "login:" is clearly shown
but the code hasn't triggered. The most likely cause is hidden control characters
in the output causing the search to fail.

Take the opportunity to remove the horrible binary string search, at the expense of
decoding the bootlog multiple times.

Tweak the logging so we can know which log was printed (self.msg or bootlog)
just in case this isn't the issue and we need more information in future.

(From OE-Core rev: 91b9e30e08695e715ef14c3df7471e8c99f9deb5)

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Richard Purdie
2022-09-13 10:29:01 +01:00
parent 8d5cb0399f
commit 2d96d3f5ac
2 changed files with 14 additions and 13 deletions

View File

@@ -189,11 +189,7 @@ def get_testimage_boot_patterns(d):
search_login_succeeded,search_cmd_finished\n Make sure your TESTIMAGE_BOOT_PATTERNS=%s \
contains an accepted flag.' % d.getVar('TESTIMAGE_BOOT_PATTERNS'))
return
# We know boot prompt is searched through in binary format, others might be expressions
if flag == 'search_reached_prompt':
boot_patterns[flag] = flagval.encode()
else:
boot_patterns[flag] = flagval.encode().decode('unicode-escape')
boot_patterns[flag] = flagval.encode().decode('unicode-escape')
return boot_patterns

View File

@@ -85,7 +85,7 @@ class QemuRunner:
accepted_patterns = ['search_reached_prompt', 'send_login_user', 'search_login_succeeded', 'search_cmd_finished']
default_boot_patterns = defaultdict(str)
# Default to the usual paterns used to communicate with the target
default_boot_patterns['search_reached_prompt'] = b' login:'
default_boot_patterns['search_reached_prompt'] = ' login:'
default_boot_patterns['send_login_user'] = 'root\n'
default_boot_patterns['search_login_succeeded'] = r"root@[a-zA-Z0-9\-]+:~#"
default_boot_patterns['search_cmd_finished'] = r"[a-zA-Z0-9]+@[a-zA-Z0-9\-]+:~#"
@@ -109,12 +109,15 @@ class QemuRunner:
sock.close()
raise
def decode_qemulog(self, todecode):
# Sanitize the data received from qemu as it may contain control characters
msg = todecode.decode("utf-8", errors='ignore')
msg = re_control_char.sub('', msg)
return msg
def log(self, msg):
if self.logfile:
# It is needed to sanitize the data received from qemu
# because is possible to have control characters
msg = msg.decode("utf-8", errors='ignore')
msg = re_control_char.sub('', msg)
msg = self.decode_qemulog(msg)
self.msg += msg
with codecs.open(self.logfile, "a", encoding="utf-8") as f:
f.write("%s" % msg)
@@ -468,7 +471,9 @@ class QemuRunner:
self.log(data)
data = b''
if self.boot_patterns['search_reached_prompt'] in bootlog:
decodedlog = self.decode_qemulog(bootlog)
if self.boot_patterns['search_reached_prompt'] in decodedlog:
self.server_socket = qemusock
stopread = True
reachedlogin = True
@@ -488,10 +493,10 @@ class QemuRunner:
self.logger.warning("Target didn't reach login banner in %d seconds (%s)" %
(self.boottime, time.strftime("%D %H:%M:%S")))
tail = lambda l: "\n".join(l.splitlines()[-25:])
bootlog = bootlog.decode("utf-8")
bootlog = self.decode_qemulog(bootlog)
# in case bootlog is empty, use tail qemu log store at self.msg
lines = tail(bootlog if bootlog else self.msg)
self.logger.warning("Last 25 lines of text:\n%s" % lines)
self.logger.warning("Last 25 lines of text (%d):\n%s" % (len(bootlog), lines))
self.logger.warning("Check full boot log: %s" % self.logfile)
self._dump_host()
self.stop()