]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
test/py: Support --build when running tests in parallel
authorSimon Glass <sjg@chromium.org>
Sat, 6 Aug 2022 23:51:57 +0000 (17:51 -0600)
committerTom Rini <trini@konsulko.com>
Mon, 12 Sep 2022 22:06:36 +0000 (18:06 -0400)
At present when -n is used, all workers try to build U-Boot at once.
Add a lock to ensure that only one of them builds, with the others using
the build that is produced.

The lock file is removed on startup.

Signed-off-by: Simon Glass <sjg@chromium.org>
test/py/conftest.py
test/py/requirements.txt

index 906387db1c84fb9289f09d539516ae2120cc8b7d..304e93164aae0218eae29363567c78646baa2286 100644 (file)
 import atexit
 import configparser
 import errno
+import filelock
 import io
 import os
 import os.path
+from pathlib import Path
 import pytest
 import re
 from _pytest.runner import runtestprotocol
@@ -27,6 +29,8 @@ import sys
 log = None
 console = None
 
+TEST_PY_DIR = os.path.dirname(os.path.abspath(__file__))
+
 def mkdir_p(path):
     """Create a directory path.
 
@@ -111,6 +115,18 @@ def run_build(config, source_dir, build_dir, board_type, log):
         runner.close()
         log.status_pass('OK')
 
+def pytest_xdist_setupnodes(config, specs):
+    """Clear out any 'done' file from a previous build"""
+    global build_done_file
+    build_dir = config.getoption('build_dir')
+    board_type = config.getoption('board_type')
+    source_dir = os.path.dirname(os.path.dirname(TEST_PY_DIR))
+    if not build_dir:
+        build_dir = source_dir + '/build-' + board_type
+    build_done_file = Path(build_dir) / 'build.done'
+    if build_done_file.exists():
+        os.remove(build_done_file)
+
 def pytest_configure(config):
     """pytest hook: Perform custom initialization at startup time.
 
@@ -145,8 +161,7 @@ def pytest_configure(config):
     global console
     global ubconfig
 
-    test_py_dir = os.path.dirname(os.path.abspath(__file__))
-    source_dir = os.path.dirname(os.path.dirname(test_py_dir))
+    source_dir = os.path.dirname(os.path.dirname(TEST_PY_DIR))
 
     board_type = config.getoption('board_type')
     board_type_filename = board_type.replace('-', '_')
@@ -177,7 +192,13 @@ def pytest_configure(config):
     log = multiplexed_log.Logfile(result_dir + '/test-log.html')
 
     if config.getoption('build'):
-        run_build(config, source_dir, build_dir, board_type, log)
+        worker_id = os.environ.get("PYTEST_XDIST_WORKER")
+        with filelock.FileLock(os.path.join(build_dir, 'build.lock')):
+            build_done_file = Path(build_dir) / 'build.done'
+            if (not worker_id or worker_id == 'master' or
+                not build_done_file.exists()):
+                run_build(config, source_dir, build_dir, board_type, log)
+                build_done_file.touch()
 
     class ArbitraryAttributeContainer(object):
         pass
@@ -209,7 +230,7 @@ def pytest_configure(config):
     else:
         parse_config('include/autoconf.mk')
 
-    ubconfig.test_py_dir = test_py_dir
+    ubconfig.test_py_dir = TEST_PY_DIR
     ubconfig.source_dir = source_dir
     ubconfig.build_dir = build_dir
     ubconfig.result_dir = result_dir
index ead92ed8b48c7750081ab0b14bc2e4956d3072be..1bf77b59d84976d36f068cc2409691f1e5201347 100644 (file)
@@ -2,6 +2,7 @@ atomicwrites==1.4.1
 attrs==19.3.0
 coverage==4.5.4
 extras==1.0.0
+filelock==3.0.12
 fixtures==3.0.0
 importlib-metadata==0.23
 linecache2==1.0.0
@@ -15,6 +16,7 @@ pyelftools==0.27
 pygit2==1.9.2
 pyparsing==2.4.2
 pytest==6.2.5
+pytest-xdist==2.5.0
 python-mimeparse==1.6.0
 python-subunit==1.3.0
 requests==2.25.1