diff options
author | Jordan Borean <jborean93@gmail.com> | 2018-05-17 01:52:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-17 01:52:46 +0200 |
commit | 5c39c3b2d16acc0415de146c31c4b8ecaf01fb7d (patch) | |
tree | 67c314e3d8e108cec1561673f57283fbe69ee9e1 /test | |
parent | Fix case (#40272) (diff) | |
download | ansible-5c39c3b2d16acc0415de146c31c4b8ecaf01fb7d.tar.xz ansible-5c39c3b2d16acc0415de146c31c4b8ecaf01fb7d.zip |
Module basic.py to create parent dirs of tmpdir if needed (#40201)
* Module basic.py to create parent dirs of tmpdir if needed
* Added warning to dir creation
* Assert if make_dirs was called or not in unit tests
Diffstat (limited to 'test')
-rw-r--r-- | test/units/module_utils/basic/test_tmpdir.py | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/test/units/module_utils/basic/test_tmpdir.py b/test/units/module_utils/basic/test_tmpdir.py index 0d7996719f..95c864c2cd 100644 --- a/test/units/module_utils/basic/test_tmpdir.py +++ b/test/units/module_utils/basic/test_tmpdir.py @@ -24,6 +24,7 @@ class TestAnsibleModuleTmpDir: "_ansible_remote_tmp": "/path/tmpdir", "_ansible_keep_remote_files": False, }, + True, "/path/to/dir" ), ( @@ -32,6 +33,16 @@ class TestAnsibleModuleTmpDir: "_ansible_remote_tmp": "/path/tmpdir", "_ansible_keep_remote_files": False }, + False, + "/path/tmpdir/ansible-moduletmp-42-" + ), + ( + { + "_ansible_tmpdir": None, + "_ansible_remote_tmp": "/path/tmpdir", + "_ansible_keep_remote_files": False + }, + True, "/path/tmpdir/ansible-moduletmp-42-" ), ( @@ -40,18 +51,31 @@ class TestAnsibleModuleTmpDir: "_ansible_remote_tmp": "$HOME/.test", "_ansible_keep_remote_files": False }, + False, os.path.join(os.environ['HOME'], ".test/ansible-moduletmp-42-") ), ) # pylint bug: https://github.com/PyCQA/pylint/issues/511 # pylint: disable=undefined-variable - @pytest.mark.parametrize('stdin, expected', ((s, e) for s, e in DATA), + @pytest.mark.parametrize('stdin, expected, stat_exists', ((s, e, t) for s, t, e in DATA), indirect=['stdin']) - def test_tmpdir_property(self, am, monkeypatch, expected): + def test_tmpdir_property(self, am, monkeypatch, expected, stat_exists): + makedirs = {'called': False} + def mock_mkdtemp(prefix, dir): return os.path.join(dir, prefix) + + def mock_makedirs(path, mode): + makedirs['called'] = True + expected = os.path.expanduser(os.path.expandvars(am._remote_tmp)) + assert path == expected + assert mode == 0o700 + return + monkeypatch.setattr(tempfile, 'mkdtemp', mock_mkdtemp) + monkeypatch.setattr(os.path, 'exists', lambda x: stat_exists) + monkeypatch.setattr(os, 'makedirs', mock_makedirs) monkeypatch.setattr(shutil, 'rmtree', lambda x: None) with patch('time.time', return_value=42): @@ -60,3 +84,6 @@ class TestAnsibleModuleTmpDir: # verify subsequent calls always produces the same tmpdir assert am.tmpdir == actual_tmpdir + + if not stat_exists: + assert makedirs['called'] |