summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author David Srbecky <dsrbecky@google.com> 2024-11-11 19:36:29 +0000
committer Treehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com> 2024-11-11 23:40:13 +0000
commit298dc96f06058b86944023be94a7f8ab1e6ff3c5 (patch)
tree151ca9d2e3ba6a6f44f52f72a48c5b9a8fe2e6a2
parent2aaea4a0da6943f69cd16b8a9970f90413c40376 (diff)
run-test: Make temporary directory name deterministic
Use hash of the arguments instead of pid and append unique consecutive integer only if needed due to parallel runs. Test: "test.py -r --host" twice in parallel Change-Id: I48d70afa43dbec22a098c82e6d3522531c79517c
-rwxr-xr-xtest/run-test30
-rwxr-xr-xtest/run_test_build.py1
2 files changed, 24 insertions, 7 deletions
diff --git a/test/run-test b/test/run-test
index c9609ca4e7..5af84c89ca 100755
--- a/test/run-test
+++ b/test/run-test
@@ -21,14 +21,16 @@ import default_run as default_run_module
from argparse import ArgumentParser, BooleanOptionalAction
from default_run import get_target_arch
+from fcntl import lockf, LOCK_EX, LOCK_NB
+from hashlib import sha1
from importlib.machinery import SourceFileLoader
from inspect import currentframe, getframeinfo, FrameInfo
from pathlib import Path
+from pprint import pprint
from shutil import copyfile, copytree
from testrunner import env
from typing import Optional, Dict, List
from zipfile import ZipFile
-from hashlib import sha1
COLOR = (os.environ.get("LUCI_CONTEXT") == None) # Disable colors on LUCI.
COLOR_BLUE = '\033[94m' if COLOR else ''
@@ -100,19 +102,32 @@ class RunTestContext:
def default_run(self, args, **kwargs):
default_run_module.default_run(self, args, **kwargs)
+# Make unique temporary directory guarded by lock file.
+# The name is deterministic (appending suffix as needed).
+def make_tmp_dir():
+ parent = Path(os.environ.get("TMPDIR", "/tmp/art/rtest"))
+ parent.mkdir(parents=True, exist_ok=True)
+ args_hash = sha1((" ".join(sys.argv[1:])).encode()).digest().hex()
+ for i in range(100):
+ tmp_dir = parent / (f"{args_hash[:8]}" + (f"-{i}" if i > 0 else ""))
+ lock = tmp_dir.with_suffix(".lock") # NB: Next to the directory, not inside.
+ lock_handle = open(lock, "w")
+ try:
+ lockf(lock_handle, LOCK_EX | LOCK_NB)
+ tmp_dir.mkdir(exist_ok=True)
+ return str(tmp_dir), lock, lock_handle
+ except BlockingIOError:
+ continue
+ assert False, "Failed to create test directory"
# TODO: Replace with 'def main():' (which might change variables from globals to locals)
if True:
progdir = os.path.dirname(__file__)
oldwd = os.getcwd()
os.chdir(progdir)
- test_dir = "test-{}".format(os.getpid())
- TMPDIR = os.environ.get("TMPDIR")
PYTHON3 = os.environ.get("PYTHON3")
- if not TMPDIR:
- tmp_dir = f"/tmp/art/{test_dir}"
- else:
- tmp_dir = f"{TMPDIR}/{test_dir}"
+ tmp_dir, tmp_dir_lock, tmp_dir_lock_handle = make_tmp_dir()
+ test_dir = Path(tmp_dir).name
checker = f"{progdir}/../tools/checker/checker.py"
ON_VM = env.ART_TEST_ON_VM
@@ -794,6 +809,7 @@ if True:
if always_clean or (passed and not never_clean):
os.chdir(oldwd)
shutil.rmtree(tmp_dir)
+ os.remove(tmp_dir_lock)
if target_mode:
if ON_VM:
run(f"{SSH_CMD} \"rm -rf {chroot_dex_location}\"")
diff --git a/test/run_test_build.py b/test/run_test_build.py
index c795b7df93..ecd2518c62 100755
--- a/test/run_test_build.py
+++ b/test/run_test_build.py
@@ -525,6 +525,7 @@ def create_ci_runner_scripts(mode, test_names):
# The only dependency is setting of "-Djava.library.path".
"TARGET_ARCH": "arm64",
"TARGET_2ND_ARCH": "arm",
+ "TMPDIR": Path(getcwd()) / "tmp",
}
args = [
f"--run-test-option=--create-runner={tmpdir}",