test: Separate out the exception handling

The tests currently catch a very broad Exception in each case. This is
thrown even in the event of a coding error.

We want to handle exceptions differently depending on their severity,
so that we can avoid hour-long delays waiting for a board that is
clearly broken.

As a first step, create some new exception types, separating out those
which are simply an unexpected result from executed a command, from
those which indicate some kind of hardware failure.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2024-10-09 18:29:02 -06:00 committed by Tom Rini
parent e4ad90149c
commit 681b8f86e4
2 changed files with 25 additions and 12 deletions

View file

@ -14,6 +14,7 @@ import pytest
import re import re
import sys import sys
import u_boot_spawn import u_boot_spawn
from u_boot_spawn import BootFail, Timeout, Unexpected
# Regexes for text we expect U-Boot to send to the console. # Regexes for text we expect U-Boot to send to the console.
pattern_u_boot_spl_signon = re.compile('(U-Boot SPL \\d{4}\\.\\d{2}[^\r\n]*\\))') pattern_u_boot_spl_signon = re.compile('(U-Boot SPL \\d{4}\\.\\d{2}[^\r\n]*\\))')
@ -189,13 +190,13 @@ class ConsoleBase(object):
m = self.p.expect([pattern_u_boot_spl_signon] + m = self.p.expect([pattern_u_boot_spl_signon] +
self.bad_patterns) self.bad_patterns)
if m != 0: if m != 0:
raise Exception('Bad pattern found on SPL console: ' + raise BootFail('Bad pattern found on SPL console: ' +
self.bad_pattern_ids[m - 1]) self.bad_pattern_ids[m - 1])
env_spl_banner_times -= 1 env_spl_banner_times -= 1
m = self.p.expect([pattern_u_boot_main_signon] + self.bad_patterns) m = self.p.expect([pattern_u_boot_main_signon] + self.bad_patterns)
if m != 0: if m != 0:
raise Exception('Bad pattern found on console: ' + raise BootFail('Bad pattern found on console: ' +
self.bad_pattern_ids[m - 1]) self.bad_pattern_ids[m - 1])
self.u_boot_version_string = self.p.after self.u_boot_version_string = self.p.after
while True: while True:
@ -206,13 +207,9 @@ class ConsoleBase(object):
if m == 1: if m == 1:
self.p.send(' ') self.p.send(' ')
continue continue
raise Exception('Bad pattern found on console: ' + raise BootFail('Bad pattern found on console: ' +
self.bad_pattern_ids[m - 2]) self.bad_pattern_ids[m - 2])
except Exception as ex:
self.log.error(str(ex))
self.cleanup_spawn()
raise
finally: finally:
self.log.timestamp() self.log.timestamp()
@ -278,7 +275,7 @@ class ConsoleBase(object):
m = self.p.expect([chunk] + self.bad_patterns) m = self.p.expect([chunk] + self.bad_patterns)
if m != 0: if m != 0:
self.at_prompt = False self.at_prompt = False
raise Exception('Bad pattern found on console: ' + raise BootFail('Bad pattern found on console: ' +
self.bad_pattern_ids[m - 1]) self.bad_pattern_ids[m - 1])
if not wait_for_prompt: if not wait_for_prompt:
return return
@ -288,14 +285,18 @@ class ConsoleBase(object):
m = self.p.expect([self.prompt_compiled] + self.bad_patterns) m = self.p.expect([self.prompt_compiled] + self.bad_patterns)
if m != 0: if m != 0:
self.at_prompt = False self.at_prompt = False
raise Exception('Bad pattern found on console: ' + raise BootFail('Missing prompt on console: ' +
self.bad_pattern_ids[m - 1]) self.bad_pattern_ids[m - 1])
self.at_prompt = True self.at_prompt = True
self.at_prompt_logevt = self.logstream.logfile.cur_evt self.at_prompt_logevt = self.logstream.logfile.cur_evt
# Only strip \r\n; space/TAB might be significant if testing # Only strip \r\n; space/TAB might be significant if testing
# indentation. # indentation.
return self.p.before.strip('\r\n') return self.p.before.strip('\r\n')
except Exception as ex: except Timeout as exc:
self.log.error(str(exc))
self.cleanup_spawn()
raise
except BootFail as ex:
self.log.error(str(ex)) self.log.error(str(ex))
self.cleanup_spawn() self.cleanup_spawn()
raise raise
@ -354,8 +355,9 @@ class ConsoleBase(object):
text = re.escape(text) text = re.escape(text)
m = self.p.expect([text] + self.bad_patterns) m = self.p.expect([text] + self.bad_patterns)
if m != 0: if m != 0:
raise Exception('Bad pattern found on console: ' + raise Unexpected(
self.bad_pattern_ids[m - 1]) "Unexpected pattern found on console (exp '{text}': " +
self.bad_pattern_ids[m - 1])
def drain_console(self): def drain_console(self):
"""Read from and log the U-Boot console for a short time. """Read from and log the U-Boot console for a short time.

View file

@ -16,6 +16,17 @@ import traceback
class Timeout(Exception): class Timeout(Exception):
"""An exception sub-class that indicates that a timeout occurred.""" """An exception sub-class that indicates that a timeout occurred."""
class BootFail(Exception):
"""An exception sub-class that indicates that a boot failure occurred.
This is used when a bad pattern is seen when waiting for the boot prompt.
It is regarded as fatal, to avoid trying to boot the again and again to no
avail.
"""
class Unexpected(Exception):
"""An exception sub-class that indicates that unexpected test was seen."""
class Spawn: class Spawn:
"""Represents the stdio of a freshly created sub-process. Commands may be """Represents the stdio of a freshly created sub-process. Commands may be
sent to the process, and responses waited for. sent to the process, and responses waited for.