[PATCH v2 03/15] KVM: selftests: Add timeout option in selftests runner
Vipin Sharma
vipinsh at google.com
Fri Jun 6 16:56:07 PDT 2025
Add a command line argument in KVM selftest runner to limit amount of
time (seconds) given to a test for execution. Kill the test if it exceeds the
given value. Define a new SelftestStatus.TIMED_OUT to denote a selftest
final result. Add terminal color for status messages of timed out tests.
Set the default value of 120 seconds for all tests.
Signed-off-by: Vipin Sharma <vipinsh at google.com>
---
.../testing/selftests/kvm/runner/__main__.py | 10 +++++++-
tools/testing/selftests/kvm/runner/command.py | 4 +++-
.../testing/selftests/kvm/runner/selftest.py | 23 +++++++++++--------
.../selftests/kvm/runner/test_runner.py | 2 +-
4 files changed, 27 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/kvm/runner/__main__.py b/tools/testing/selftests/kvm/runner/__main__.py
index 599300831504..f7f679be0e03 100644
--- a/tools/testing/selftests/kvm/runner/__main__.py
+++ b/tools/testing/selftests/kvm/runner/__main__.py
@@ -35,6 +35,12 @@ def cli():
default=".",
help="Finds the test executables in the given directory. Default is the current directory.")
+ parser.add_argument("-t",
+ "--timeout",
+ default=120,
+ type=int,
+ help="Timeout, in seconds, before runner kills the running test. (Default: 120 seconds)")
+
return parser.parse_args()
@@ -42,6 +48,7 @@ def setup_logging(args):
class TerminalColorFormatter(logging.Formatter):
reset = "\033[0m"
red_bold = "\033[31;1m"
+ red = "\033[31;1m"
green = "\033[32m"
yellow = "\033[33m"
blue = "\033[34m"
@@ -50,7 +57,8 @@ def setup_logging(args):
SelftestStatus.PASSED: green,
SelftestStatus.NO_RUN: blue,
SelftestStatus.SKIPPED: yellow,
- SelftestStatus.FAILED: red_bold
+ SelftestStatus.FAILED: red_bold,
+ SelftestStatus.TIMED_OUT: red
}
def __init__(self, fmt=None, datefmt=None):
diff --git a/tools/testing/selftests/kvm/runner/command.py b/tools/testing/selftests/kvm/runner/command.py
index a63ff53a92b3..44c8e0875779 100644
--- a/tools/testing/selftests/kvm/runner/command.py
+++ b/tools/testing/selftests/kvm/runner/command.py
@@ -12,14 +12,16 @@ class Command:
Returns the exit code, std output and std error of the command.
"""
- def __init__(self, command):
+ def __init__(self, command, timeout):
self.command = command
+ self.timeout = timeout
def run(self):
run_args = {
"universal_newlines": True,
"shell": True,
"capture_output": True,
+ "timeout": self.timeout,
}
proc = subprocess.run(self.command, **run_args)
diff --git a/tools/testing/selftests/kvm/runner/selftest.py b/tools/testing/selftests/kvm/runner/selftest.py
index a0b06f150087..4c72108c47de 100644
--- a/tools/testing/selftests/kvm/runner/selftest.py
+++ b/tools/testing/selftests/kvm/runner/selftest.py
@@ -7,6 +7,7 @@ import command
import pathlib
import enum
import os
+import subprocess
class SelftestStatus(enum.IntEnum):
@@ -18,6 +19,7 @@ class SelftestStatus(enum.IntEnum):
NO_RUN = 22
SKIPPED = 23
FAILED = 24
+ TIMED_OUT = 25
def __str__(self):
return str.__str__(self.name)
@@ -30,7 +32,7 @@ class Selftest:
Extract the test execution command from test file and executes it.
"""
- def __init__(self, test_path, executable_dir):
+ def __init__(self, test_path, executable_dir, timeout):
test_command = pathlib.Path(test_path).read_text().strip()
if not test_command:
raise ValueError("Empty test command in " + test_path)
@@ -38,7 +40,7 @@ class Selftest:
test_command = os.path.join(executable_dir, test_command)
self.exists = os.path.isfile(test_command.split(maxsplit=1)[0])
self.test_path = test_path
- self.command = command.Command(test_command)
+ self.command = command.Command(test_command, timeout)
self.status = SelftestStatus.NO_RUN
self.stdout = ""
self.stderr = ""
@@ -48,10 +50,13 @@ class Selftest:
self.stderr = "File doesn't exists."
return
- ret, self.stdout, self.stderr = self.command.run()
- if ret == 0:
- self.status = SelftestStatus.PASSED
- elif ret == 4:
- self.status = SelftestStatus.SKIPPED
- else:
- self.status = SelftestStatus.FAILED
+ try:
+ ret, self.stdout, self.stderr = self.command.run()
+ if ret == 0:
+ self.status = SelftestStatus.PASSED
+ elif ret == 4:
+ self.status = SelftestStatus.SKIPPED
+ else:
+ self.status = SelftestStatus.FAILED
+ except subprocess.TimeoutExpired as e:
+ self.status = SelftestStatus.TIMED_OUT
diff --git a/tools/testing/selftests/kvm/runner/test_runner.py b/tools/testing/selftests/kvm/runner/test_runner.py
index 104f0b4c2e4e..1409e1cfe7d5 100644
--- a/tools/testing/selftests/kvm/runner/test_runner.py
+++ b/tools/testing/selftests/kvm/runner/test_runner.py
@@ -15,7 +15,7 @@ class TestRunner:
self.tests = []
for test_file in test_files:
- self.tests.append(Selftest(test_file, args.executable))
+ self.tests.append(Selftest(test_file, args.executable, args.timeout))
def _log_result(self, test_result):
logger.log(test_result.status,
--
2.50.0.rc0.604.gd4ff7b7c86-goog
More information about the linux-arm-kernel
mailing list