| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: |
| 3 |
# (c) 2018 Ansible Project |
| 4 |
# License: GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import unittest.mock |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from ansible.modules.copy import AnsibleModuleError, split_pre_existing_dir |
| 13 |
|
| 14 |
from ansible.module_utils.basic import AnsibleModule |
| 15 |
|
| 16 |
THREE_DIRS_DATA: tuple[tuple[str, tuple[str, list[str]] | None, tuple[str, list[str]], tuple[str, list[str]], tuple[str, list[str]]], ...] = ( |
| 17 |
('/dir1/dir2', |
| 18 |
# 0 existing dirs: error (because / should always exist) |
| 19 |
None, |
| 20 |
# 1 existing dir: |
| 21 |
('/', ['dir1', 'dir2']), |
| 22 |
# 2 existing dirs: |
| 23 |
('/dir1', ['dir2']), |
| 24 |
# 3 existing dirs: |
| 25 |
('/dir1/dir2', []) |
| 26 |
), |
| 27 |
('/dir1/dir2/', |
| 28 |
# 0 existing dirs: error (because / should always exist) |
| 29 |
None, |
| 30 |
# 1 existing dir: |
| 31 |
('/', ['dir1', 'dir2']), |
| 32 |
# 2 existing dirs: |
| 33 |
('/dir1', ['dir2']), |
| 34 |
# 3 existing dirs: |
| 35 |
('/dir1/dir2', []) |
| 36 |
), |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
TWO_DIRS_DATA: tuple[tuple[str, tuple[str, list[str]] | None, tuple[str, list[str]], tuple[str, list[str]]], ...] = ( |
| 41 |
('dir1/dir2', |
| 42 |
# 0 existing dirs: |
| 43 |
('.', ['dir1', 'dir2']), |
| 44 |
# 1 existing dir: |
| 45 |
('dir1', ['dir2']), |
| 46 |
# 2 existing dirs: |
| 47 |
('dir1/dir2', []), |
| 48 |
# 3 existing dirs: Same as 2 because we never get to the third |
| 49 |
), |
| 50 |
('dir1/dir2/', |
| 51 |
# 0 existing dirs: |
| 52 |
('.', ['dir1', 'dir2']), |
| 53 |
# 1 existing dir: |
| 54 |
('dir1', ['dir2']), |
| 55 |
# 2 existing dirs: |
| 56 |
('dir1/dir2', []), |
| 57 |
# 3 existing dirs: Same as 2 because we never get to the third |
| 58 |
), |
| 59 |
('/dir1', |
| 60 |
# 0 existing dirs: error (because / should always exist) |
| 61 |
None, |
| 62 |
# 1 existing dir: |
| 63 |
('/', ['dir1']), |
| 64 |
# 2 existing dirs: |
| 65 |
('/dir1', []), |
| 66 |
# 3 existing dirs: Same as 2 because we never get to the third |
| 67 |
), |
| 68 |
('/dir1/', |
| 69 |
# 0 existing dirs: error (because / should always exist) |
| 70 |
None, |
| 71 |
# 1 existing dir: |
| 72 |
('/', ['dir1']), |
| 73 |
# 2 existing dirs: |
| 74 |
('/dir1', []), |
| 75 |
# 3 existing dirs: Same as 2 because we never get to the third |
| 76 |
), |
| 77 |
) |
| 78 |
TWO_DIRS_DATA += tuple(item[:4] for item in THREE_DIRS_DATA) |
| 79 |
|
| 80 |
|
| 81 |
ONE_DIR_DATA: tuple[tuple[str, tuple[str, list[str]] | None, tuple[str, list[str]]], ...] = ( |
| 82 |
('dir1', |
| 83 |
# 0 existing dirs: |
| 84 |
('.', ['dir1']), |
| 85 |
# 1 existing dir: |
| 86 |
('dir1', []), |
| 87 |
# 2 existing dirs: Same as 1 because we never get to the third |
| 88 |
), |
| 89 |
('dir1/', |
| 90 |
# 0 existing dirs: |
| 91 |
('.', ['dir1']), |
| 92 |
# 1 existing dir: |
| 93 |
('dir1', []), |
| 94 |
# 2 existing dirs: Same as 1 because we never get to the third |
| 95 |
), |
| 96 |
) |
| 97 |
ONE_DIR_DATA += tuple(item[:3] for item in TWO_DIRS_DATA) |
| 98 |
|
| 99 |
|
| 100 |
@pytest.mark.parametrize('directory, expected', [(d[0], d[4]) for d in THREE_DIRS_DATA]) |
| 101 |
@pytest.mark.xfail(reason='broken test and/or code, original test missing assert', strict=False) |
| 102 |
def test_split_pre_existing_dir_three_levels_exist(directory, expected): |
| 103 |
with unittest.mock.patch('os.path.exists', side_effect=[True, True, True]): |
| 104 |
assert split_pre_existing_dir(directory) == expected |
| 105 |
|
| 106 |
|
| 107 |
@pytest.mark.parametrize('directory, expected', [(d[0], d[3]) for d in TWO_DIRS_DATA]) |
| 108 |
@pytest.mark.xfail(reason='broken test and/or code, original test missing assert', strict=False) |
| 109 |
def test_split_pre_existing_dir_two_levels_exist(directory, expected): |
| 110 |
with unittest.mock.patch('os.path.exists', side_effect=[True, True, False]): |
| 111 |
assert split_pre_existing_dir(directory) == expected |
| 112 |
|
| 113 |
|
| 114 |
@pytest.mark.parametrize('directory, expected', [(d[0], d[2]) for d in ONE_DIR_DATA]) |
| 115 |
@pytest.mark.xfail(reason='broken test and/or code, original test missing assert', strict=False) |
| 116 |
def test_split_pre_existing_dir_one_level_exists(directory, expected): |
| 117 |
with unittest.mock.patch('os.path.exists', side_effect=[True, False, False]): |
| 118 |
assert split_pre_existing_dir(directory) == expected |
| 119 |
|
| 120 |
|
| 121 |
@pytest.mark.parametrize('directory', [d[0] for d in ONE_DIR_DATA if d[1] is None]) |
| 122 |
def test_split_pre_existing_dir_root_does_not_exist(directory): |
| 123 |
with unittest.mock.patch('os.path.exists', return_value=False): |
| 124 |
with pytest.raises(AnsibleModuleError) as excinfo: |
| 125 |
split_pre_existing_dir(directory) |
| 126 |
assert excinfo.value.results['msg'].startswith("The '/' directory doesn't exist on this machine.") |
| 127 |
|
| 128 |
|
| 129 |
@pytest.mark.parametrize('directory, expected', [(d[0], d[1]) for d in ONE_DIR_DATA if not d[0].startswith('/')]) |
| 130 |
@pytest.mark.xfail(reason='broken test and/or code, original test missing assert', strict=False) |
| 131 |
def test_split_pre_existing_dir_working_dir_exists(directory, expected): |
| 132 |
with unittest.mock.patch('os.path.exists', return_value=False): |
| 133 |
assert split_pre_existing_dir(directory) == expected |
| 134 |
|
| 135 |
|
| 136 |
# |
| 137 |
# Info helpful for making new test cases: |
| 138 |
# |
| 139 |
# base_mode = { |
| 140 |
# 'dir no perms': 0o040000, |
| 141 |
# 'file no perms': 0o100000, |
| 142 |
# 'dir all perms': 0o040000 | 0o777, |
| 143 |
# 'file all perms': 0o100000 | 0o777} |
| 144 |
# |
| 145 |
# perm_bits = { |
| 146 |
# 'x': 0b001, |
| 147 |
# 'w': 0b010, |
| 148 |
# 'r': 0b100} |
| 149 |
# |
| 150 |
# role_shift = { |
| 151 |
# 'u': 6, |
| 152 |
# 'g': 3, |
| 153 |
# 'o': 0} |
| 154 |
|
| 155 |
DATA = ( # Going from no permissions to setting all for user, group, and/or other |
| 156 |
(0o040000, u'a+rwx', 0o0777), |
| 157 |
(0o040000, u'u+rwx,g+rwx,o+rwx', 0o0777), |
| 158 |
(0o040000, u'o+rwx', 0o0007), |
| 159 |
(0o040000, u'g+rwx', 0o0070), |
| 160 |
(0o040000, u'u+rwx', 0o0700), |
| 161 |
|
| 162 |
# Going from all permissions to none for user, group, and/or other |
| 163 |
(0o040777, u'a-rwx', 0o0000), |
| 164 |
(0o040777, u'u-rwx,g-rwx,o-rwx', 0o0000), |
| 165 |
(0o040777, u'o-rwx', 0o0770), |
| 166 |
(0o040777, u'g-rwx', 0o0707), |
| 167 |
(0o040777, u'u-rwx', 0o0077), |
| 168 |
|
| 169 |
# now using absolute assignment from None to a set of perms |
| 170 |
(0o040000, u'a=rwx', 0o0777), |
| 171 |
(0o040000, u'u=rwx,g=rwx,o=rwx', 0o0777), |
| 172 |
(0o040000, u'o=rwx', 0o0007), |
| 173 |
(0o040000, u'g=rwx', 0o0070), |
| 174 |
(0o040000, u'u=rwx', 0o0700), |
| 175 |
|
| 176 |
# X effect on files and dirs |
| 177 |
(0o040000, u'a+X', 0o0111), |
| 178 |
(0o100000, u'a+X', 0), |
| 179 |
(0o040000, u'a=X', 0o0111), |
| 180 |
(0o100000, u'a=X', 0), |
| 181 |
(0o040777, u'a-X', 0o0666), |
| 182 |
# Same as chmod but is it a bug? |
| 183 |
# chmod a-X statfile <== removes execute from statfile |
| 184 |
(0o100777, u'a-X', 0o0666), |
| 185 |
|
| 186 |
# Verify X uses computed not original mode |
| 187 |
(0o100777, u'a=,u=rX', 0o0400), |
| 188 |
(0o040777, u'a=,u=rX', 0o0500), |
| 189 |
|
| 190 |
# Multiple permissions |
| 191 |
(0o040000, u'u=rw-x+X,g=r-x+X,o=r-x+X', 0o0755), |
| 192 |
(0o100000, u'u=rw-x+X,g=r-x+X,o=r-x+X', 0o0644), |
| 193 |
) |
| 194 |
|
| 195 |
UMASK_DATA = ( |
| 196 |
(0o100000, '+rwx', 0o770), |
| 197 |
(0o100777, '-rwx', 0o007), |
| 198 |
) |
| 199 |
|
| 200 |
INVALID_DATA = ( |
| 201 |
(0o040000, u'a=foo', "bad symbolic permission for mode: a=foo"), |
| 202 |
(0o040000, u'f=rwx', "bad symbolic permission for mode: f=rwx"), |
| 203 |
(0o100777, u'of=r', "bad symbolic permission for mode: of=r"), |
| 204 |
|
| 205 |
(0o100777, u'ao=r', "bad symbolic permission for mode: ao=r"), |
| 206 |
(0o100777, u'oa=r', "bad symbolic permission for mode: oa=r"), |
| 207 |
) |
| 208 |
|
| 209 |
|
| 210 |
@pytest.mark.parametrize('stat_info, mode_string, expected', DATA) |
| 211 |
def test_good_symbolic_modes(stat_info, mode_string, expected): |
| 212 |
mock_stat = unittest.mock.MagicMock() |
| 213 |
mock_stat.st_mode = stat_info |
| 214 |
assert AnsibleModule._symbolic_mode_to_octal(mock_stat, mode_string) == expected |
| 215 |
|
| 216 |
|
| 217 |
@pytest.mark.parametrize('stat_info, mode_string, expected', UMASK_DATA) |
| 218 |
def test_umask_with_symbolic_modes(stat_info, mode_string, expected): |
| 219 |
with unittest.mock.patch('os.umask') as mock_umask: |
| 220 |
mock_umask.return_value = 0o7 |
| 221 |
|
| 222 |
mock_stat = unittest.mock.MagicMock() |
| 223 |
mock_stat.st_mode = stat_info |
| 224 |
|
| 225 |
assert AnsibleModule._symbolic_mode_to_octal(mock_stat, mode_string) == expected |
| 226 |
|
| 227 |
|
| 228 |
@pytest.mark.parametrize('stat_info, mode_string, expected', INVALID_DATA) |
| 229 |
def test_invalid_symbolic_modes(stat_info, mode_string, expected): |
| 230 |
mock_stat = unittest.mock.MagicMock() |
| 231 |
mock_stat.st_mode = stat_info |
| 232 |
with pytest.raises(ValueError) as exc: |
| 233 |
assert AnsibleModule._symbolic_mode_to_octal(mock_stat, mode_string) == 'blah' |
| 234 |
assert exc.match(expected) |