summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorSam Doran <sdoran@redhat.com>2019-06-28 16:19:27 -0400
committerGitHub <noreply@github.com>2019-06-28 16:19:27 -0400
commit6cf6f5a34bebe01f96782a171db8d83d4e7828ca (patch)
tree90eb9cb093c52d067bab0846ab91a4735d5fe253 /test
parent875e7c3e5036b1410e4170c4ff2589c3f12e5ac7 (diff)
downloadansible-6cf6f5a34bebe01f96782a171db8d83d4e7828ca.tar.gz
Use atexit to cleanup tmp dirs (#56532)
* Wrap everything in try/except to avoid leaving files behind * Add unit tests, integration tests, and changelog * Do text the correct way
Diffstat (limited to 'test')
-rwxr-xr-xtest/integration/targets/ansible/runme.sh25
-rw-r--r--test/units/utils/test_cleanup_tmp_file.py48
2 files changed, 71 insertions, 2 deletions
diff --git a/test/integration/targets/ansible/runme.sh b/test/integration/targets/ansible/runme.sh
index b3ddb65fd7..e9554934ba 100755
--- a/test/integration/targets/ansible/runme.sh
+++ b/test/integration/targets/ansible/runme.sh
@@ -13,5 +13,26 @@ ansible-config dump -c ./ansible-testé.cfg | grep 'DEFAULT_REMOTE_USER([^)]*) =
ANSIBLE_REMOTE_USER=administrator ansible-config dump| grep 'DEFAULT_REMOTE_USER([^)]*) = administrator\>'
ansible-config list | grep 'DEFAULT_REMOTE_USER'
-# 'view' command must fail when config file is missing
-ansible-config view -c ./ansible-non-existent.cfg && exit 1 || echo 'Failure is expected'
+# 'view' command must fail when config file is missing or has an invalid file extension
+ansible-config view -c ./ansible-non-existent.cfg 2> err1.txt || grep -Eq '(FileNotFoundError|IOError): \[Errno [0-9]+\] No such file or directory' err1.txt || (cat err*.txt; rm -f err1.txt; exit 1)
+ansible-config view -c ./no-extension 2> err2.txt || grep -q 'Unsupported configuration file extension' err2.txt || (cat err2.txt; rm -f err*.txt; exit 1)
+rm -f err*.txt
+
+# Test that no tmp dirs are left behind when running ansible-config
+TMP_DIR=~/.ansible/tmptest
+if [[ -d "$TMP_DIR" ]]; then
+ rm -rf "$TMP_DIR"
+fi
+ANSIBLE_LOCAL_TEMP="$TMP_DIR" ansible-config list > /dev/null
+ANSIBLE_LOCAL_TEMP="$TMP_DIR" ansible-config dump > /dev/null
+ANSIBLE_LOCAL_TEMP="$TMP_DIR" ansible-config view > /dev/null
+
+# wc on macOS is dumb and returns leading spaces
+file_count=$(find "$TMP_DIR" -type d -maxdepth 1 | wc -l | sed 's/^ *//')
+if [[ $file_count -ne 1 ]]; then
+ echo "$file_count temporary files were left behind by ansible-config"
+ if [[ -d "$TMP_DIR" ]]; then
+ rm -rf "$TMP_DIR"
+ fi
+ exit 1
+fi
diff --git a/test/units/utils/test_cleanup_tmp_file.py b/test/units/utils/test_cleanup_tmp_file.py
new file mode 100644
index 0000000000..5bc3900a55
--- /dev/null
+++ b/test/units/utils/test_cleanup_tmp_file.py
@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+# Copyright: (c) 2019, Ansible Project
+# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
+
+from __future__ import (absolute_import, division)
+__metaclass__ = type
+
+import os
+import pytest
+import tempfile
+
+from ansible.utils.path import cleanup_tmp_file
+
+
+def raise_error():
+ raise OSError
+
+
+def test_cleanup_tmp_file_file():
+ tmp_fd, tmp = tempfile.mkstemp()
+ cleanup_tmp_file(tmp)
+
+ assert not os.path.exists(tmp)
+
+
+def test_cleanup_tmp_file_dir():
+ tmp = tempfile.mkdtemp()
+ cleanup_tmp_file(tmp)
+
+ assert not os.path.exists(tmp)
+
+
+def test_cleanup_tmp_file_nonexistant():
+ assert None is cleanup_tmp_file('nope')
+
+
+def test_cleanup_tmp_file_failure(mocker):
+ tmp = tempfile.mkdtemp()
+ with pytest.raises(Exception):
+ mocker.patch('shutil.rmtree', side_effect=raise_error())
+ cleanup_tmp_file(tmp)
+
+
+def test_cleanup_tmp_file_failure_warning(mocker, capsys):
+ tmp = tempfile.mkdtemp()
+ with pytest.raises(Exception):
+ mocker.patch('shutil.rmtree', side_effect=raise_error())
+ cleanup_tmp_file(tmp, warn=True)