Skip to content

Commit

Permalink
Write auto-created private data dirs to pytest dir instead of /tmp (#916
Browse files Browse the repository at this point in the history
)

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 <github@samdoran.com>
Reviewed-by: None <None>
  • Loading branch information
AlanCoding committed Dec 14, 2021
1 parent 02b15da commit 7f9de03
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ansible_runner/config/_base.py
Expand Up @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions ansible_runner/defaults.py
Expand Up @@ -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
7 changes: 7 additions & 0 deletions test/conftest.py
Expand Up @@ -2,6 +2,8 @@

from distutils.version import LooseVersion

from ansible_runner import defaults

import pkg_resources
import pytest

Expand All @@ -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:
Expand Down

0 comments on commit 7f9de03

Please sign in to comment.