From 0e3fddbc6e9e52e3835b1a4b905f077a63fdbd13 Mon Sep 17 00:00:00 2001 From: Wyatt Hoodes Date: Fri, 5 Jul 2019 12:29:17 -1000 Subject: [PATCH] test_fixtures: Add logic for removing stale test directories. Similarly to how stale database removal is handled, we add a check for stale test run directories at the end of the `test-backend` script. --- tools/test-backend | 7 ++++++- zerver/lib/test_fixtures.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/tools/test-backend b/tools/test-backend index d1558c6af7..d60592ca17 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -193,7 +193,8 @@ def main() -> None: os.environ["http_proxy"] = "" os.environ["https_proxy"] = "" - from zerver.lib.test_fixtures import update_test_databases_if_required + from zerver.lib.test_fixtures import update_test_databases_if_required, \ + remove_test_run_directories from tools.lib.test_script import ( assert_provisioning_status_ok, @@ -461,6 +462,10 @@ def main() -> None: # # destroy_leaked_test_databases() + removed = remove_test_run_directories() + if removed: + print("Removed %s stale test run directories!" % (removed,)) + # We'll have printed whether tests passed or failed above sys.exit(bool(failures)) diff --git a/zerver/lib/test_fixtures.py b/zerver/lib/test_fixtures.py index 95c3e6f72f..4b3b89b65f 100644 --- a/zerver/lib/test_fixtures.py +++ b/zerver/lib/test_fixtures.py @@ -10,6 +10,7 @@ from importlib import import_module from io import StringIO import glob import time +import shutil from django.db import connections, DEFAULT_DB_ALIAS, ProgrammingError, \ connection @@ -296,3 +297,15 @@ def destroy_leaked_test_databases(expiry_time: int = 60 * 60) -> int: if p.returncode != 0: raise RuntimeError("Error cleaning up test databases!") return len(databases_to_drop) + +def remove_test_run_directories(expiry_time: int = 60 * 60) -> int: + removed = 0 + directories = glob.glob(os.path.join(UUID_VAR_DIR, "test-backend", "run_*")) + for test_run in directories: + if round(time.time()) - os.path.getmtime(test_run) > expiry_time: + try: + shutil.rmtree(test_run) + removed += 1 + except FileNotFoundError: + pass + return removed