| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2018 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import os |
| 8 |
import tempfile |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from unittest.mock import patch, MagicMock |
| 13 |
|
| 14 |
from ansible.module_utils import basic |
| 15 |
from ansible.module_utils.testing import patch_module_args |
| 16 |
|
| 17 |
|
| 18 |
class TestAnsibleModuleTmpDir: |
| 19 |
|
| 20 |
DATA = ( |
| 21 |
( |
| 22 |
{ |
| 23 |
"_ansible_tmpdir": "/path/to/dir", |
| 24 |
"_ansible_remote_tmp": "/path/tmpdir", |
| 25 |
"_ansible_keep_remote_files": False, |
| 26 |
}, |
| 27 |
True, |
| 28 |
"/path/to/dir" |
| 29 |
), |
| 30 |
( |
| 31 |
{ |
| 32 |
"_ansible_tmpdir": None, |
| 33 |
"_ansible_remote_tmp": "/path/tmpdir", |
| 34 |
"_ansible_keep_remote_files": False |
| 35 |
}, |
| 36 |
False, |
| 37 |
"/path/tmpdir/ansible-moduletmp-42-" |
| 38 |
), |
| 39 |
( |
| 40 |
{ |
| 41 |
"_ansible_tmpdir": None, |
| 42 |
"_ansible_remote_tmp": "/path/tmpdir", |
| 43 |
"_ansible_keep_remote_files": False |
| 44 |
}, |
| 45 |
True, |
| 46 |
"/path/tmpdir/ansible-moduletmp-42-" |
| 47 |
), |
| 48 |
( |
| 49 |
{ |
| 50 |
"_ansible_tmpdir": None, |
| 51 |
"_ansible_remote_tmp": "$HOME/.test", |
| 52 |
"_ansible_keep_remote_files": False |
| 53 |
}, |
| 54 |
False, |
| 55 |
os.path.join(os.environ['HOME'], ".test/ansible-moduletmp-42-") |
| 56 |
), |
| 57 |
) |
| 58 |
|
| 59 |
@pytest.mark.parametrize('args, expected, stat_exists', [(s, e, t) for s, t, e in DATA]) |
| 60 |
def test_tmpdir_property(self, monkeypatch, args, expected, stat_exists): |
| 61 |
makedirs_calls = [] |
| 62 |
|
| 63 |
def mock_mkdtemp(prefix, dir): |
| 64 |
return os.path.join(dir, prefix) |
| 65 |
|
| 66 |
def mock_makedirs(path, mode, exist_ok=False): |
| 67 |
makedirs_calls.append({'path': path, 'mode': mode}) |
| 68 |
return |
| 69 |
|
| 70 |
monkeypatch.setattr(tempfile, 'mkdtemp', mock_mkdtemp) |
| 71 |
monkeypatch.setattr(os.path, 'exists', lambda x: stat_exists) |
| 72 |
monkeypatch.setattr(os, 'makedirs', mock_makedirs) |
| 73 |
|
| 74 |
with patch_module_args(args), \ |
| 75 |
patch('time.time', return_value=42): |
| 76 |
am = basic.AnsibleModule(argument_spec={}) |
| 77 |
actual_tmpdir = am.tmpdir |
| 78 |
|
| 79 |
assert actual_tmpdir == expected |
| 80 |
|
| 81 |
# verify subsequent calls always produces the same tmpdir |
| 82 |
assert am.tmpdir == actual_tmpdir |
| 83 |
|
| 84 |
if not stat_exists: |
| 85 |
expected = os.path.expanduser(os.path.expandvars(am._remote_tmp)) |
| 86 |
tmpdir_calls = [c for c in makedirs_calls if c['path'] == expected] |
| 87 |
assert len(tmpdir_calls) == 1 |
| 88 |
assert tmpdir_calls[0]['mode'] == 0o700 |
| 89 |
|
| 90 |
@pytest.mark.parametrize('stdin', ({"_ansible_tmpdir": None, |
| 91 |
"_ansible_remote_tmp": "$HOME/.test", |
| 92 |
"_ansible_keep_remote_files": True},), |
| 93 |
indirect=['stdin']) |
| 94 |
def test_tmpdir_makedirs_failure(self, am, monkeypatch): |
| 95 |
|
| 96 |
mock_mkdtemp = MagicMock(return_value="/tmp/path") |
| 97 |
mock_makedirs = MagicMock(side_effect=OSError("Some OS Error here")) |
| 98 |
|
| 99 |
monkeypatch.setattr(tempfile, 'mkdtemp', mock_mkdtemp) |
| 100 |
monkeypatch.setattr(os.path, 'exists', lambda x: False) |
| 101 |
monkeypatch.setattr(os, 'makedirs', mock_makedirs) |
| 102 |
|
| 103 |
# Reset cached tmpdir so the property re-evaluates with mocked makedirs |
| 104 |
am._tmpdir = None |
| 105 |
|
| 106 |
actual = am.tmpdir |
| 107 |
assert actual == "/tmp/path" |
| 108 |
assert mock_makedirs.call_args[0] == (os.path.expanduser(os.path.expandvars("$HOME/.test")),) |
| 109 |
assert mock_makedirs.call_args[1] == {"mode": 0o700} |
| 110 |
|
| 111 |
# because makedirs failed the dir should be None so it uses the System tmp |
| 112 |
assert mock_mkdtemp.call_args[1]['dir'] is None |
| 113 |
assert mock_mkdtemp.call_args[1]['prefix'].startswith("ansible-moduletmp-") |