From cc2fbf332c09918973984b801eb2fba1a1359bf3 Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Tue, 17 Mar 2026 17:02:47 -0400 Subject: [PATCH] Stop writing tmp test files that are not cleaned up (#16358) * Stop writing tmp test files that are not cleaned up --- awx/main/tests/unit/tasks/test_jobs.py | 19 +++++++++---------- awx/main/tests/unit/test_tasks.py | 14 +++++--------- .../unit/utils/test_execution_environments.py | 16 ++++------------ 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/awx/main/tests/unit/tasks/test_jobs.py b/awx/main/tests/unit/tasks/test_jobs.py index 288dac7206..bcc6f4d0fd 100644 --- a/awx/main/tests/unit/tasks/test_jobs.py +++ b/awx/main/tests/unit/tasks/test_jobs.py @@ -1,8 +1,4 @@ # -*- coding: utf-8 -*- -import os -import tempfile -import shutil - import pytest from unittest import mock @@ -32,13 +28,12 @@ from ansible_base.lib.workload_identity.controller import AutomationControllerJo @pytest.fixture -def private_data_dir(): - private_data = tempfile.mkdtemp(prefix='awx_') +def private_data_dir(tmp_path): + private_data = tmp_path / 'awx_pdd' + private_data.mkdir() for subfolder in ('inventory', 'env'): - runner_subfolder = os.path.join(private_data, subfolder) - os.makedirs(runner_subfolder, exist_ok=True) - yield private_data - shutil.rmtree(private_data, True) + (private_data / subfolder).mkdir() + return str(private_data) @pytest.fixture @@ -101,6 +96,8 @@ def test_pre_post_run_hook_facts(mock_create_partition, mock_facts_settings, pri proj = mock.MagicMock(spec=Project, pk=1, organization=org) job = mock.MagicMock( spec=Job, + pk=1, + id=1, use_fact_cache=True, project=proj, organization=org, @@ -170,6 +167,8 @@ def test_pre_post_run_hook_facts_deleted_sliced( # Mock job object job = mock.MagicMock(spec=Job) + job.pk = 2 + job.id = 2 job.use_fact_cache = True job.project = proj job.organization = org diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index e1ff5bc34b..41b6ff058b 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -1,8 +1,6 @@ # -*- coding: utf-8 -*- import json import os -import shutil -import tempfile from pathlib import Path import fcntl @@ -60,14 +58,12 @@ class TestJobExecution(object): @pytest.fixture -def private_data_dir(): - private_data = tempfile.mkdtemp(prefix='awx_') +def private_data_dir(tmp_path): + private_data = tmp_path / 'awx_pdd' + private_data.mkdir() for subfolder in ('inventory', 'env'): - runner_subfolder = os.path.join(private_data, subfolder) - if not os.path.exists(runner_subfolder): - os.mkdir(runner_subfolder) - yield private_data - shutil.rmtree(private_data, True) + (private_data / subfolder).mkdir() + return str(private_data) @pytest.fixture diff --git a/awx/main/tests/unit/utils/test_execution_environments.py b/awx/main/tests/unit/utils/test_execution_environments.py index 1e41890e5f..31ed3eaf7d 100644 --- a/awx/main/tests/unit/utils/test_execution_environments.py +++ b/awx/main/tests/unit/utils/test_execution_environments.py @@ -1,6 +1,4 @@ -import shutil import os -from uuid import uuid4 import pytest @@ -24,17 +22,11 @@ def test_switch_paths(container_path, host_path): assert get_incontainer_path(host_path, private_data_dir) == container_path -def test_symlink_isolation_dir(request): - rand_str = str(uuid4())[:8] - dst_path = f'/tmp/ee_{rand_str}_symlink_dst' - src_path = f'/tmp/ee_{rand_str}_symlink_src' +def test_symlink_isolation_dir(tmp_path): + src_path = tmp_path / 'symlink_src' + dst_path = tmp_path / 'symlink_dst' - def remove_folders(): - os.unlink(dst_path) - shutil.rmtree(src_path) - - request.addfinalizer(remove_folders) - os.mkdir(src_path) + src_path.mkdir() os.symlink(src_path, dst_path) pdd = f'{dst_path}/awx_xxx'