From 7f9de039dd1e2739b90d4c3c6c06e21981ece0d1 Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Tue, 14 Dec 2021 10:56:38 -0500 Subject: [PATCH] Write auto-created private data dirs to pytest dir instead of /tmp (#916) Write auto-created private data dirs to pytest dir instead of /tmp I briefly discussed this option with @samdoran in #901 After this patch, we create directories that look like: /tmp/pytest-of-alancoding/pytest-0/popen-gw2/test_no_dst_all_dirs_src__dir_0/.ansible-runner-2fpvb4uo This is as opposed to leaving around directories like /tmp/.ansible-runner-2fpvb4uo. There are a lot of these, like ~1,700 created with a single test run excluding the integration tests. There are still a few directories created in /tmp after this, but it's a handful, not a flood. Reviewed-by: Sam Doran Reviewed-by: None --- ansible_runner/config/_base.py | 2 +- ansible_runner/defaults.py | 5 +++++ test/conftest.py | 7 +++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ansible_runner/config/_base.py b/ansible_runner/config/_base.py index a1e996fbe..4129b9285 100644 --- a/ansible_runner/config/_base.py +++ b/ansible_runner/config/_base.py @@ -100,7 +100,7 @@ def __init__(self, # attempt to compromise the directories via a race. os.makedirs(self.private_data_dir, exist_ok=True, mode=0o700) else: - self.private_data_dir = tempfile.mkdtemp(prefix=".ansible-runner-") + self.private_data_dir = tempfile.mkdtemp(prefix=defaults.AUTO_CREATE_NAMING, dir=defaults.AUTO_CREATE_DIR) if artifact_dir is None: artifact_dir = os.path.join(self.private_data_dir, 'artifacts') diff --git a/ansible_runner/defaults.py b/ansible_runner/defaults.py index 6a2681ee1..81ac2e976 100644 --- a/ansible_runner/defaults.py +++ b/ansible_runner/defaults.py @@ -4,3 +4,8 @@ # for ansible-runner worker cleanup command GRACE_PERIOD_DEFAULT = 60 # minutes + +# values passed to tempfile.mkdtemp to generate a private data dir +# when user did not provide one +AUTO_CREATE_NAMING = '.ansible-runner-' +AUTO_CREATE_DIR = None diff --git a/test/conftest.py b/test/conftest.py index 6e361811b..d8785b4ea 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -2,6 +2,8 @@ from distutils.version import LooseVersion +from ansible_runner import defaults + import pkg_resources import pytest @@ -17,6 +19,11 @@ def mock_env_user(monkeypatch): monkeypatch.setenv("ANSIBLE_DEVEL_WARNING", "False") +@pytest.fixture(autouse=True) +def change_save_path(tmp_path, mocker): + mocker.patch.object(defaults, 'AUTO_CREATE_DIR', str(tmp_path)) + + @pytest.fixture(scope='session') def is_pre_ansible28(): try: