| 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 |
from unittest.mock import patch |
| 11 |
|
| 12 |
from ansible.module_utils import basic |
| 13 |
from ansible.module_utils.testing import patch_module_args |
| 14 |
|
| 15 |
|
| 16 |
class TestAnsibleModuleSetCwd: |
| 17 |
|
| 18 |
def test_set_cwd(self, monkeypatch): |
| 19 |
|
| 20 |
"""make sure /tmp is used""" |
| 21 |
|
| 22 |
def mock_getcwd(): |
| 23 |
return '/tmp' |
| 24 |
|
| 25 |
def mock_access(path, perm): |
| 26 |
return True |
| 27 |
|
| 28 |
def mock_chdir(path): |
| 29 |
pass |
| 30 |
|
| 31 |
monkeypatch.setattr(os, 'getcwd', mock_getcwd) |
| 32 |
monkeypatch.setattr(os, 'access', mock_access) |
| 33 |
|
| 34 |
with patch_module_args(), \ |
| 35 |
patch('time.time', return_value=42): |
| 36 |
am = basic.AnsibleModule(argument_spec={}) |
| 37 |
|
| 38 |
result = am._set_cwd() |
| 39 |
assert result == '/tmp' |
| 40 |
|
| 41 |
def test_set_cwd_unreadable_use_self_tmpdir(self, monkeypatch): |
| 42 |
|
| 43 |
"""pwd is not readable, use instance's tmpdir property""" |
| 44 |
|
| 45 |
def mock_getcwd(): |
| 46 |
return '/tmp' |
| 47 |
|
| 48 |
def mock_access(path, perm): |
| 49 |
if path == '/tmp' and perm == 4: |
| 50 |
return False |
| 51 |
return True |
| 52 |
|
| 53 |
def mock_expandvars(var): |
| 54 |
if var == '$HOME': |
| 55 |
return '/home/foobar' |
| 56 |
return var |
| 57 |
|
| 58 |
def mock_gettempdir(): |
| 59 |
return '/tmp/testdir' |
| 60 |
|
| 61 |
def mock_chdir(path): |
| 62 |
if path == '/tmp': |
| 63 |
raise Exception() |
| 64 |
return |
| 65 |
|
| 66 |
monkeypatch.setattr(os, 'getcwd', mock_getcwd) |
| 67 |
monkeypatch.setattr(os, 'chdir', mock_chdir) |
| 68 |
monkeypatch.setattr(os, 'access', mock_access) |
| 69 |
monkeypatch.setattr(os.path, 'expandvars', mock_expandvars) |
| 70 |
|
| 71 |
with patch_module_args(), \ |
| 72 |
patch('time.time', return_value=42): |
| 73 |
am = basic.AnsibleModule(argument_spec={}) |
| 74 |
|
| 75 |
am._tmpdir = '/tmp2' |
| 76 |
result = am._set_cwd() |
| 77 |
assert result == am._tmpdir |
| 78 |
|
| 79 |
def test_set_cwd_unreadable_use_home(self, monkeypatch): |
| 80 |
|
| 81 |
"""cwd and instance tmpdir are unreadable, use home""" |
| 82 |
|
| 83 |
def mock_getcwd(): |
| 84 |
return '/tmp' |
| 85 |
|
| 86 |
def mock_access(path, perm): |
| 87 |
if path in ['/tmp', '/tmp2'] and perm == 4: |
| 88 |
return False |
| 89 |
return True |
| 90 |
|
| 91 |
def mock_expandvars(var): |
| 92 |
if var == '$HOME': |
| 93 |
return '/home/foobar' |
| 94 |
return var |
| 95 |
|
| 96 |
def mock_gettempdir(): |
| 97 |
return '/tmp/testdir' |
| 98 |
|
| 99 |
def mock_chdir(path): |
| 100 |
if path == '/tmp': |
| 101 |
raise Exception() |
| 102 |
return |
| 103 |
|
| 104 |
monkeypatch.setattr(os, 'getcwd', mock_getcwd) |
| 105 |
monkeypatch.setattr(os, 'chdir', mock_chdir) |
| 106 |
monkeypatch.setattr(os, 'access', mock_access) |
| 107 |
monkeypatch.setattr(os.path, 'expandvars', mock_expandvars) |
| 108 |
|
| 109 |
with patch_module_args(), \ |
| 110 |
patch('time.time', return_value=42): |
| 111 |
am = basic.AnsibleModule(argument_spec={}) |
| 112 |
|
| 113 |
am._tmpdir = '/tmp2' |
| 114 |
result = am._set_cwd() |
| 115 |
assert result == '/home/foobar' |
| 116 |
|
| 117 |
def test_set_cwd_unreadable_use_gettempdir(self, monkeypatch): |
| 118 |
|
| 119 |
"""fallback to tempfile.gettempdir""" |
| 120 |
|
| 121 |
thisdir = None |
| 122 |
|
| 123 |
def mock_getcwd(): |
| 124 |
return '/tmp' |
| 125 |
|
| 126 |
def mock_access(path, perm): |
| 127 |
if path in ['/tmp', '/tmp2', '/home/foobar'] and perm == 4: |
| 128 |
return False |
| 129 |
return True |
| 130 |
|
| 131 |
def mock_expandvars(var): |
| 132 |
if var == '$HOME': |
| 133 |
return '/home/foobar' |
| 134 |
return var |
| 135 |
|
| 136 |
def mock_gettempdir(): |
| 137 |
return '/tmp3' |
| 138 |
|
| 139 |
def mock_chdir(path): |
| 140 |
if path == '/tmp': |
| 141 |
raise Exception() |
| 142 |
thisdir = path |
| 143 |
|
| 144 |
monkeypatch.setattr(os, 'getcwd', mock_getcwd) |
| 145 |
monkeypatch.setattr(os, 'chdir', mock_chdir) |
| 146 |
monkeypatch.setattr(os, 'access', mock_access) |
| 147 |
monkeypatch.setattr(os.path, 'expandvars', mock_expandvars) |
| 148 |
|
| 149 |
with patch_module_args(), \ |
| 150 |
patch('time.time', return_value=42): |
| 151 |
am = basic.AnsibleModule(argument_spec={}) |
| 152 |
|
| 153 |
am._tmpdir = '/tmp2' |
| 154 |
monkeypatch.setattr(tempfile, 'gettempdir', mock_gettempdir) |
| 155 |
result = am._set_cwd() |
| 156 |
assert result == '/tmp3' |
| 157 |
|
| 158 |
def test_set_cwd_unreadable_use_None(self, monkeypatch): |
| 159 |
|
| 160 |
"""all paths are unreable, should return None and not an exception""" |
| 161 |
|
| 162 |
def mock_getcwd(): |
| 163 |
return '/tmp' |
| 164 |
|
| 165 |
def mock_access(path, perm): |
| 166 |
if path in ['/tmp', '/tmp2', '/tmp3', '/home/foobar'] and perm == 4: |
| 167 |
return False |
| 168 |
return True |
| 169 |
|
| 170 |
def mock_expandvars(var): |
| 171 |
if var == '$HOME': |
| 172 |
return '/home/foobar' |
| 173 |
return var |
| 174 |
|
| 175 |
def mock_gettempdir(): |
| 176 |
return '/tmp3' |
| 177 |
|
| 178 |
def mock_chdir(path): |
| 179 |
if path == '/tmp': |
| 180 |
raise Exception() |
| 181 |
|
| 182 |
monkeypatch.setattr(os, 'getcwd', mock_getcwd) |
| 183 |
monkeypatch.setattr(os, 'chdir', mock_chdir) |
| 184 |
monkeypatch.setattr(os, 'access', mock_access) |
| 185 |
monkeypatch.setattr(os.path, 'expandvars', mock_expandvars) |
| 186 |
|
| 187 |
with patch_module_args(), \ |
| 188 |
patch('time.time', return_value=42): |
| 189 |
am = basic.AnsibleModule(argument_spec={}) |
| 190 |
|
| 191 |
am._tmpdir = '/tmp2' |
| 192 |
monkeypatch.setattr(tempfile, 'gettempdir', mock_gettempdir) |
| 193 |
result = am._set_cwd() |
| 194 |
assert result is None |