Skip to content

Commit 7ee0aab

Browse files
authored
fix: ensure temp files are cleaned up after failed HCC (ansible#15996)
1 parent 3eb8096 commit 7ee0aab

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

awx/main/analytics/core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,10 @@ def gather(dest=None, module=None, subset=None, since=None, until=None, collecti
324324
settings.AUTOMATION_ANALYTICS_LAST_ENTRIES = json.dumps(last_entries, cls=DjangoJSONEncoder)
325325

326326
if collection_type != 'dry-run':
327-
if succeeded:
328-
for fpath in tarfiles:
329-
if os.path.exists(fpath):
330-
os.remove(fpath)
327+
for fpath in tarfiles:
328+
if os.path.exists(fpath):
329+
os.remove(fpath)
330+
331331
with disable_activity_stream():
332332
if not settings.AUTOMATION_ANALYTICS_LAST_GATHER or until > settings.AUTOMATION_ANALYTICS_LAST_GATHER:
333333
# `AUTOMATION_ANALYTICS_LAST_GATHER` is set whether collection succeeds or fails;

awx/main/tests/functional/analytics/test_core.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,24 @@ def test_ship_credential(setting_map, expected_result, expected_auth, temp_analy
150150
assert mock_analytic_post.call_args[1]['auth'] == expected_auth
151151
else:
152152
mock_analytic_post.assert_not_called()
153+
154+
155+
@pytest.mark.django_db
156+
def test_gather_cleanup_on_auth_failure(mock_valid_license, temp_analytic_tar):
157+
settings.INSIGHTS_TRACKING_STATE = True
158+
settings.AUTOMATION_ANALYTICS_URL = 'https://example.com/api'
159+
settings.REDHAT_USERNAME = 'test_user'
160+
settings.REDHAT_PASSWORD = 'test_password'
161+
162+
with tempfile.NamedTemporaryFile(delete=False, suffix='.tar.gz') as temp_file:
163+
temp_file_path = temp_file.name
164+
165+
try:
166+
with mock.patch('awx.main.analytics.core.ship', return_value=False):
167+
with mock.patch('awx.main.analytics.core.package', return_value=temp_file_path):
168+
gather(module=importlib.import_module(__name__), collection_type='scheduled')
169+
170+
assert not os.path.exists(temp_file_path), "Temp file was not cleaned up after ship failure"
171+
finally:
172+
if os.path.exists(temp_file_path):
173+
os.remove(temp_file_path)

0 commit comments

Comments
 (0)