| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 3 |
# (c) 2016 Toshio Kuratomi <[email protected]> |
| 4 |
# (c) 2017 Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import os |
| 10 |
import errno |
| 11 |
from itertools import product |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from ansible.module_utils import basic |
| 16 |
from ansible.module_utils.common.file import S_IRWU_RG_RO |
| 17 |
|
| 18 |
|
| 19 |
@pytest.fixture |
| 20 |
def atomic_am(am, mocker): |
| 21 |
am.selinux_enabled = mocker.MagicMock() |
| 22 |
am.selinux_context = mocker.MagicMock() |
| 23 |
am.selinux_default_context = mocker.MagicMock() |
| 24 |
am.set_context_if_different = mocker.MagicMock() |
| 25 |
am._unsafe_writes = mocker.MagicMock() |
| 26 |
|
| 27 |
yield am |
| 28 |
|
| 29 |
|
| 30 |
@pytest.fixture |
| 31 |
def atomic_mocks(mocker, monkeypatch): |
| 32 |
environ = dict() |
| 33 |
mocks = { |
| 34 |
'copystat': mocker.patch('shutil.copystat'), |
| 35 |
'chmod': mocker.patch('os.chmod'), |
| 36 |
'chown': mocker.patch('os.chown'), |
| 37 |
'close': mocker.patch('os.close'), |
| 38 |
'environ': mocker.patch('os.environ', environ), |
| 39 |
'getlogin': mocker.patch('os.getlogin'), |
| 40 |
'getuid': mocker.patch('os.getuid'), |
| 41 |
'path_exists': mocker.patch('os.path.exists'), |
| 42 |
'rename': mocker.patch('os.rename'), |
| 43 |
'stat': mocker.patch('os.stat'), |
| 44 |
'umask': mocker.patch('os.umask'), |
| 45 |
'getpwuid': mocker.patch('pwd.getpwuid'), |
| 46 |
'copy2': mocker.patch('shutil.copy2'), |
| 47 |
'copyfileobj': mocker.patch('shutil.copyfileobj'), |
| 48 |
'move': mocker.patch('shutil.move'), |
| 49 |
'mkstemp': mocker.patch('tempfile.mkstemp'), |
| 50 |
'utime': mocker.patch('os.utime'), |
| 51 |
} |
| 52 |
|
| 53 |
mocks['getlogin'].return_value = 'root' |
| 54 |
mocks['getuid'].return_value = 0 |
| 55 |
mocks['getpwuid'].return_value = ('root', '', 0, 0, '', '', '') |
| 56 |
mocks['umask'].side_effect = [18, 0] |
| 57 |
mocks['rename'].return_value = None |
| 58 |
|
| 59 |
# normalize OS specific features |
| 60 |
monkeypatch.delattr(os, 'chflags', raising=False) |
| 61 |
|
| 62 |
yield mocks |
| 63 |
|
| 64 |
|
| 65 |
@pytest.fixture |
| 66 |
def fake_stat(mocker): |
| 67 |
stat1 = mocker.MagicMock() |
| 68 |
stat1.st_mode = S_IRWU_RG_RO |
| 69 |
stat1.st_uid = 0 |
| 70 |
stat1.st_gid = 0 |
| 71 |
stat1.st_flags = 0 |
| 72 |
yield stat1 |
| 73 |
|
| 74 |
|
| 75 |
@pytest.mark.parametrize('stdin, selinux', list(product([{}], (True, False))), indirect=['stdin']) |
| 76 |
def test_new_file(atomic_am, atomic_mocks, mocker, selinux): |
| 77 |
# test destination does not exist, login name = 'root', no environment, os.rename() succeeds |
| 78 |
mock_context = atomic_am.selinux_default_context.return_value |
| 79 |
atomic_mocks['path_exists'].return_value = False |
| 80 |
atomic_am.selinux_enabled.return_value = selinux |
| 81 |
|
| 82 |
atomic_am.atomic_move('/path/to/src', '/path/to/dest') |
| 83 |
|
| 84 |
atomic_mocks['rename'].assert_called_with(b'/path/to/src', b'/path/to/dest') |
| 85 |
assert atomic_mocks['chmod'].call_args_list == [mocker.call(b'/path/to/dest', basic.DEFAULT_PERM & ~18)] |
| 86 |
|
| 87 |
if selinux: |
| 88 |
assert atomic_am.selinux_default_context.call_args_list == [mocker.call('/path/to/dest')] |
| 89 |
assert atomic_am.set_context_if_different.call_args_list == [mocker.call('/path/to/dest', mock_context, False)] |
| 90 |
else: |
| 91 |
assert not atomic_am.selinux_default_context.called |
| 92 |
assert not atomic_am.set_context_if_different.called |
| 93 |
|
| 94 |
|
| 95 |
@pytest.mark.parametrize('stdin, selinux', list(product([{}], (True, False))), indirect=['stdin']) |
| 96 |
def test_existing_file(atomic_am, atomic_mocks, fake_stat, mocker, selinux): |
| 97 |
# Test destination already present |
| 98 |
mock_context = atomic_am.selinux_context.return_value |
| 99 |
atomic_mocks['stat'].return_value = fake_stat |
| 100 |
atomic_mocks['path_exists'].return_value = True |
| 101 |
atomic_am.selinux_enabled.return_value = selinux |
| 102 |
|
| 103 |
atomic_am.atomic_move('/path/to/src', '/path/to/dest') |
| 104 |
|
| 105 |
atomic_mocks['rename'].assert_called_with(b'/path/to/src', b'/path/to/dest') |
| 106 |
assert atomic_mocks['copystat'].call_args_list == [mocker.call(b'/path/to/dest', b'/path/to/src')] |
| 107 |
|
| 108 |
if selinux: |
| 109 |
assert atomic_am.set_context_if_different.call_args_list == [mocker.call('/path/to/dest', mock_context, False)] |
| 110 |
assert atomic_am.selinux_context.call_args_list == [mocker.call('/path/to/dest')] |
| 111 |
else: |
| 112 |
assert not atomic_am.selinux_default_context.called |
| 113 |
assert not atomic_am.set_context_if_different.called |
| 114 |
|
| 115 |
|
| 116 |
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin']) |
| 117 |
def test_no_tty_fallback(atomic_am, atomic_mocks, fake_stat, mocker): |
| 118 |
"""Raise OSError when using getlogin() to simulate no tty cornercase""" |
| 119 |
mock_context = atomic_am.selinux_context.return_value |
| 120 |
atomic_mocks['stat'].return_value = fake_stat |
| 121 |
atomic_mocks['path_exists'].return_value = True |
| 122 |
atomic_am.selinux_enabled.return_value = True |
| 123 |
atomic_mocks['getlogin'].side_effect = OSError() |
| 124 |
atomic_mocks['environ']['LOGNAME'] = 'root' |
| 125 |
|
| 126 |
atomic_am.atomic_move('/path/to/src', '/path/to/dest') |
| 127 |
|
| 128 |
atomic_mocks['rename'].assert_called_with(b'/path/to/src', b'/path/to/dest') |
| 129 |
assert atomic_mocks['copystat'].call_args_list == [mocker.call(b'/path/to/dest', b'/path/to/src')] |
| 130 |
assert atomic_mocks['chmod'].call_args_list == [] |
| 131 |
|
| 132 |
assert atomic_am.set_context_if_different.call_args_list == [mocker.call('/path/to/dest', mock_context, False)] |
| 133 |
assert atomic_am.selinux_context.call_args_list == [mocker.call('/path/to/dest')] |
| 134 |
|
| 135 |
|
| 136 |
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin']) |
| 137 |
def test_existing_file_stat_failure(atomic_am, atomic_mocks, mocker): |
| 138 |
"""Failure to stat an existing file in order to copy permissions propagates the error (unless EPERM)""" |
| 139 |
atomic_mocks['copystat'].side_effect = FileNotFoundError('testing os copystat with non EPERM error') |
| 140 |
atomic_mocks['path_exists'].return_value = True |
| 141 |
|
| 142 |
with pytest.raises(FileNotFoundError, match='testing os copystat with non EPERM error'): |
| 143 |
atomic_am.atomic_move('/path/to/src', '/path/to/dest') |
| 144 |
|
| 145 |
|
| 146 |
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin']) |
| 147 |
def test_existing_file_stat_perms_failure(atomic_am, atomic_mocks, mocker): |
| 148 |
"""Failure to stat an existing file to copy the permissions due to permissions passes fine""" |
| 149 |
# and now have os.copystat return EPERM, which should not fail |
| 150 |
mock_context = atomic_am.selinux_context.return_value |
| 151 |
atomic_mocks['copystat'].side_effect = OSError(errno.EPERM, 'testing os copystat with EPERM') |
| 152 |
atomic_mocks['path_exists'].return_value = True |
| 153 |
atomic_am.selinux_enabled.return_value = True |
| 154 |
|
| 155 |
atomic_am.atomic_move('/path/to/src', '/path/to/dest') |
| 156 |
|
| 157 |
atomic_mocks['rename'].assert_called_with(b'/path/to/src', b'/path/to/dest') |
| 158 |
# FIXME: Should atomic_move() set a default permission value when it cannot retrieve the |
| 159 |
# existing file's permissions? (Right now it's up to the calling code. |
| 160 |
assert atomic_mocks['copystat'].call_args_list == [mocker.call(b'/path/to/dest', b'/path/to/src')] |
| 161 |
assert atomic_am.set_context_if_different.call_args_list == [mocker.call('/path/to/dest', mock_context, False)] |
| 162 |
assert atomic_am.selinux_context.call_args_list == [mocker.call('/path/to/dest')] |
| 163 |
|
| 164 |
|
| 165 |
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin']) |
| 166 |
def test_rename_failure(atomic_am, atomic_mocks, mocker, capfd): |
| 167 |
"""Test os.rename fails with EIO, causing it to bail out""" |
| 168 |
atomic_mocks['path_exists'].side_effect = [False, False] |
| 169 |
atomic_mocks['rename'].side_effect = OSError(errno.EIO, 'failing with EIO') |
| 170 |
|
| 171 |
with pytest.raises(Exception, match='Could not replace') as err: |
| 172 |
atomic_am.atomic_move('/path/to/src', '/path/to/dest') |
| 173 |
|
| 174 |
assert isinstance(err.value.__cause__, OSError) |
| 175 |
assert err.value.__cause__.errno == errno.EIO |
| 176 |
assert err.value.__cause__.strerror == 'failing with EIO' |
| 177 |
|
| 178 |
|
| 179 |
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin']) |
| 180 |
def test_rename_perms_fail_temp_creation_fails(atomic_am, atomic_mocks, mocker, capfd): |
| 181 |
"""Test os.rename fails with EPERM working but failure in mkstemp""" |
| 182 |
atomic_mocks['path_exists'].return_value = False |
| 183 |
atomic_mocks['close'].return_value = None |
| 184 |
atomic_mocks['rename'].side_effect = [OSError(errno.EPERM, 'failing with EPERM'), None] |
| 185 |
atomic_mocks['mkstemp'].return_value = None |
| 186 |
atomic_mocks['mkstemp'].side_effect = OSError() |
| 187 |
atomic_am.selinux_enabled.return_value = False |
| 188 |
|
| 189 |
with pytest.raises(Exception, match='is not writable by the current user'): |
| 190 |
atomic_am.atomic_move('/path/to/src', '/path/to/dest') |
| 191 |
|
| 192 |
|
| 193 |
@pytest.mark.parametrize('stdin, selinux', list(product([{}], (True, False))), indirect=['stdin']) |
| 194 |
def test_rename_perms_fail_temp_succeeds(atomic_am, atomic_mocks, fake_stat, mocker, selinux): |
| 195 |
"""Test os.rename raising an error but fallback to using mkstemp works""" |
| 196 |
mock_context = atomic_am.selinux_default_context.return_value |
| 197 |
atomic_mocks['path_exists'].return_value = False |
| 198 |
atomic_mocks['rename'].side_effect = [OSError(errno.EPERM, 'failing with EPERM'), None] |
| 199 |
atomic_mocks['mkstemp'].return_value = (None, '/path/to/tempfile') |
| 200 |
atomic_mocks['mkstemp'].side_effect = None |
| 201 |
atomic_am.selinux_enabled.return_value = selinux |
| 202 |
|
| 203 |
atomic_am.atomic_move('/path/to/src', '/path/to/dest') |
| 204 |
assert atomic_mocks['rename'].call_args_list == [mocker.call(b'/path/to/src', b'/path/to/dest'), |
| 205 |
mocker.call(b'/path/to/tempfile', b'/path/to/dest')] |
| 206 |
assert atomic_mocks['chmod'].call_args_list == [mocker.call(b'/path/to/dest', basic.DEFAULT_PERM & ~18)] |
| 207 |
|
| 208 |
if selinux: |
| 209 |
assert atomic_am.selinux_default_context.call_args_list == [mocker.call('/path/to/dest')] |
| 210 |
assert atomic_am.set_context_if_different.call_args_list == [mocker.call(b'/path/to/tempfile', mock_context, False), |
| 211 |
mocker.call('/path/to/dest', mock_context, False)] |
| 212 |
else: |
| 213 |
assert not atomic_am.selinux_default_context.called |
| 214 |
assert not atomic_am.set_context_if_different.called |