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.
This commit is contained in:
Wyatt Hoodes 2019-07-05 12:29:17 -10:00 committed by Tim Abbott
parent f7f32df3ef
commit 0e3fddbc6e
2 changed files with 19 additions and 1 deletions

View File

@ -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))

View File

@ -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