diff mbox series

test/py: Add support to enable check for bad pattern

Message ID 60e9180b7eba8f97410a320d9ac69101479991d3.1716383488.git.love.kumar@amd.com
State Accepted
Commit a57973ab47615f9d471b0c1d51554803c533df3a
Delegated to: Tom Rini
Headers show
Series test/py: Add support to enable check for bad pattern | expand

Commit Message

Love Kumar May 22, 2024, 1:15 p.m. UTC
Executing a u-boot command may raise an error or extra bad pattern,
beyond the default bad patterns. Providing a way to enable the console
output error check in test.

For example, description for OS boot test:
import re
check_type = 'kernel_boot_error'
check_pattern = re.compile('ERROR -2: can't get kernel image!')
with u_boot_console.enable_check(check_type, check_pattern):
    u_boot_console.run_command('<boot command>')

Signed-off-by: Love Kumar <love.kumar@amd.com>
---
 test/py/u_boot_console_base.py | 44 ++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

Comments

Tom Rini May 22, 2024, 2:40 p.m. UTC | #1
On Wed, May 22, 2024 at 06:45:13PM +0530, Love Kumar wrote:

> Executing a u-boot command may raise an error or extra bad pattern,
> beyond the default bad patterns. Providing a way to enable the console
> output error check in test.
> 
> For example, description for OS boot test:
> import re
> check_type = 'kernel_boot_error'
> check_pattern = re.compile('ERROR -2: can't get kernel image!')
> with u_boot_console.enable_check(check_type, check_pattern):
>     u_boot_console.run_command('<boot command>')
> 
> Signed-off-by: Love Kumar <love.kumar@amd.com>
> ---
>  test/py/u_boot_console_base.py | 44 ++++++++++++++++++++++++++++++++++
>  1 file changed, 44 insertions(+)

Looks reasonable, please put this as a pre-req for the updated kernel
test, thanks!
Tom Rini June 14, 2024, 2:54 p.m. UTC | #2
On Wed, 22 May 2024 18:45:13 +0530, Love Kumar wrote:

> Executing a u-boot command may raise an error or extra bad pattern,
> beyond the default bad patterns. Providing a way to enable the console
> output error check in test.
> 
> For example, description for OS boot test:
> import re
> check_type = 'kernel_boot_error'
> check_pattern = re.compile('ERROR -2: can't get kernel image!')
> with u_boot_console.enable_check(check_type, check_pattern):
>     u_boot_console.run_command('<boot command>')
> 
> [...]

Applied to u-boot/master, thanks!
diff mbox series

Patch

diff --git a/test/py/u_boot_console_base.py b/test/py/u_boot_console_base.py
index 26b6de07f88f..2f882fb53fc5 100644
--- a/test/py/u_boot_console_base.py
+++ b/test/py/u_boot_console_base.py
@@ -57,6 +57,32 @@  class ConsoleDisableCheck(object):
         self.console.disable_check_count[self.check_type] -= 1
         self.console.eval_bad_patterns()
 
+class ConsoleEnableCheck(object):
+    """Context manager (for Python's with statement) that temporarily enables
+    the specified console output error check. This is useful when executing a
+    command that might raise an extra bad pattern, beyond the default bad
+    patterns, in order to validate that the extra bad pattern is actually
+    detected. This class is used internally by ConsoleBase::enable_check(); it
+    is not intended for direct usage."""
+
+    def __init__(self, console, check_type, check_pattern):
+        self.console = console
+        self.check_type = check_type
+        self.check_pattern = check_pattern
+
+    def __enter__(self):
+        global bad_pattern_defs
+        self.default_bad_patterns = bad_pattern_defs
+        bad_pattern_defs += ((self.check_type, self.check_pattern),)
+        self.console.disable_check_count = {pat[PAT_ID]: 0 for pat in bad_pattern_defs}
+        self.console.eval_bad_patterns()
+
+    def __exit__(self, extype, value, traceback):
+        global bad_pattern_defs
+        bad_pattern_defs = self.default_bad_patterns
+        self.console.disable_check_count = {pat[PAT_ID]: 0 for pat in bad_pattern_defs}
+        self.console.eval_bad_patterns()
+
 class ConsoleSetupTimeout(object):
     """Context manager (for Python's with statement) that temporarily sets up
     timeout for specific command. This is useful when execution time is greater
@@ -499,6 +525,24 @@  class ConsoleBase(object):
 
         return ConsoleDisableCheck(self, check_type)
 
+    def enable_check(self, check_type, check_pattern):
+        """Temporarily enable an error check of U-Boot's output.
+
+        Create a new context manager (for use with the "with" statement) which
+        temporarily enables a particular console output error check. The
+        arguments form a new element of bad_pattern_defs defined above.
+
+        Args:
+            check_type: The type of error-check or bad pattern to enable.
+            check_pattern: The regexes for text error pattern or bad pattern
+                to be checked.
+
+        Returns:
+            A context manager object.
+        """
+
+        return ConsoleEnableCheck(self, check_type, check_pattern)
+
     def temporary_timeout(self, timeout):
         """Temporarily set up different timeout for commands.