diff mbox series

[41/41] oeqa/selftest: Use the core executor's run()

Message ID 20261115084845.701136-41-richard.purdie@linuxfoundation.org
State New
Headers show
Series [01/41] oeqa/loader: Drop code for pre python 3.5 | expand

Commit Message

Richard Purdie Nov. 15, 2026, 8:48 a.m. UTC
OESelftestTestContextExecutor carried its own copy of run(), split into
run() and _internal_run(), which differed from the core version only in
finding the test module paths from BBPATH, always writing the json
results and creating the results log link even when the run raised an
exception. It also had its own versions of the configuration, result id
and json directory hooks under different names, which the core code
never called.

Split the core run() so the results log link is always created, pass
args to _get_configuration() as the selftest version needs it, rename
the selftest hooks to match the core ones and set the module paths in
_process_args(). The selftest copy of run() can then be dropped.

This also fixes the core run() failing with FileExistsError when the old
results log link is dangling, as os.path.exists() is false for a broken
link. Use os.path.lexists() as the selftest code did.

AI-Generated: Uses Claude Opus 5.5
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 meta/lib/oeqa/core/context.py     | 52 +++++++++++++++++--------------
 meta/lib/oeqa/selftest/context.py | 49 ++++-------------------------
 2 files changed, 34 insertions(+), 67 deletions(-)
diff mbox series

Patch

diff --git a/meta/lib/oeqa/core/context.py b/meta/lib/oeqa/core/context.py
index 7d7d9f3c5c8..fe071ddb409 100644
--- a/meta/lib/oeqa/core/context.py
+++ b/meta/lib/oeqa/core/context.py
@@ -194,7 +194,7 @@  class OETestContextExecutor:
     def _get_json_result_dir(self, args):
         return args.json_result_dir
 
-    def _get_configuration(self):
+    def _get_configuration(self, args):
         td = self.td
         configuration = {'TEST_TYPE': self.name,
                         'MACHINE': td.get("MACHINE"),
@@ -210,9 +210,7 @@  class OETestContextExecutor:
     def _pre_run(self):
         pass
 
-    def run(self, logger, args):
-        self._process_args(logger, args)
-
+    def _run(self, logger, args):
         self.tc = self._context_class(td=self.td, logger=self.logger, **self.context_args)
         try:
             self.tc.loadTests(self.module_paths, modules=self.modules,
@@ -222,28 +220,34 @@  class OETestContextExecutor:
             sys.exit(1)
 
         if args.list_tests:
-            rc = self.tc.listTests(args.list_tests)
+            return self.tc.listTests(args.list_tests)
+
+        self._pre_run()
+        rc = self.tc.runTests(processes=self.processes, skips=self.skips)
+
+        json_result_dir = self._get_json_result_dir(args)
+        if json_result_dir:
+            configuration = self._get_configuration(args)
+            rc.logDetails(json_result_dir,
+                          configuration,
+                          self._get_result_id(configuration))
         else:
-            self._pre_run()
-            rc = self.tc.runTests(processes=self.processes, skips=self.skips)
-
-            json_result_dir = self._get_json_result_dir(args)
-            if json_result_dir:
-                configuration = self._get_configuration()
-                rc.logDetails(json_result_dir,
-                              configuration,
-                              self._get_result_id(configuration))
-            else:
-                rc.logDetails()
-
-            rc.logSummary(self.name)
-
-        output_link = os.path.join(os.path.dirname(args.output_log),
-                "%s-results.log" % self.name)
-        if os.path.exists(output_link):
-            os.remove(output_link)
-        os.symlink(args.output_log, output_link)
+            rc.logDetails()
 
+        rc.logSummary(self.name)
         return rc
 
+    def run(self, logger, args):
+        self._process_args(logger, args)
+        try:
+            return self._run(logger, args)
+        finally:
+            # Link to the latest log, even if running the tests failed. The
+            # old link may be dangling if its log was removed.
+            output_link = os.path.join(os.path.dirname(args.output_log),
+                    "%s-results.log" % self.name)
+            if os.path.lexists(output_link):
+                os.unlink(output_link)
+            os.symlink(args.output_log, output_link)
+
 _executor_class = OETestContextExecutor
diff --git a/meta/lib/oeqa/selftest/context.py b/meta/lib/oeqa/selftest/context.py
index 49d9fc2cba1..1f03f511af2 100644
--- a/meta/lib/oeqa/selftest/context.py
+++ b/meta/lib/oeqa/selftest/context.py
@@ -17,7 +17,7 @@  import bb.utils
 import bb.tinfoil
 
 from oeqa.core.context import OETestContext, OETestContextExecutor
-from oeqa.core.exception import OEQAPreRun, OEQATestNotFound
+from oeqa.core.exception import OEQAPreRun
 
 from oeqa.utils.commands import runCmd, get_bb_vars, get_test_layer
 
@@ -289,6 +289,8 @@  class OESelftestTestContextExecutor(OETestContextExecutor):
         self.skips = args.skips
         self.processes = args.processes
 
+        self.module_paths = self._get_cases_paths(self.td['BBPATH'].split(':'))
+
     def _pre_run(self):
         def _check_required_env_variables(vars):
             for var in vars:
@@ -358,14 +360,14 @@  class OESelftestTestContextExecutor(OETestContextExecutor):
             vars['BBLAYERS'] = str(d.getVar('BBLAYERS'))
         self.tc.set_variables(vars)
 
-    def get_json_result_dir(self, args):
+    def _get_json_result_dir(self, args):
         json_result_dir = os.path.join(self.tc.td["LOG_DIR"], 'oeqa')
         if "OEQA_JSON_RESULT_DIR" in self.tc.td:
             json_result_dir = self.tc.td["OEQA_JSON_RESULT_DIR"]
 
         return json_result_dir
 
-    def get_configuration(self, args):
+    def _get_configuration(self, args):
         from oeqa.utils.metadata import metadata_from_bb
         metadata = metadata_from_bb()
         oeselftest_metadata = get_oeselftest_metadata(args)
@@ -378,46 +380,7 @@  class OESelftestTestContextExecutor(OETestContextExecutor):
                         'OESELFTEST_METADATA': oeselftest_metadata}
         return configuration
 
-    def get_result_id(self, configuration):
+    def _get_result_id(self, configuration):
         return '%s_%s_%s_%s' % (configuration['TEST_TYPE'], configuration['HOST_DISTRO'], configuration['MACHINE'], configuration['STARTTIME'])
 
-    def _internal_run(self, logger, args):
-        self.module_paths = self._get_cases_paths(self.td['BBPATH'].split(':'))
-
-        self.tc = self._context_class(td=self.td, logger=self.logger, **self.context_args)
-        try:
-            self.tc.loadTests(self.module_paths, modules=self.modules,
-                    modules_required=self.modules_required, tags_filter=self.tags_filter)
-        except OEQATestNotFound as ex:
-            logger.error(ex)
-            sys.exit(1)
-
-        if args.list_tests:
-            rc = self.tc.listTests(args.list_tests)
-        else:
-            self._pre_run()
-            rc = self.tc.runTests(processes=self.processes, skips=self.skips)
-            configuration = self.get_configuration(args)
-            rc.logDetails(self.get_json_result_dir(args),
-                          configuration,
-                          self.get_result_id(configuration))
-            rc.logSummary(self.name)
-
-        return rc
-
-    def run(self, logger, args):
-        self._process_args(logger, args)
-
-        rc = None
-        try:
-             rc = self._internal_run(logger, args)
-        finally:
-            output_link = os.path.join(os.path.dirname(args.output_log),
-                    "%s-results.log" % self.name)
-            if os.path.lexists(output_link):
-                os.unlink(output_link)
-            os.symlink(args.output_log, output_link)
-
-        return rc
-
 _executor_class = OESelftestTestContextExecutor