| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2017, 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 os.path |
| 9 |
import stat |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from ansible.config.manager import find_ini_config_file |
| 14 |
from ansible.module_utils.common.text.converters import to_text |
| 15 |
|
| 16 |
real_exists = os.path.exists |
| 17 |
real_isdir = os.path.isdir |
| 18 |
|
| 19 |
working_dir = os.path.dirname(__file__) |
| 20 |
cfg_in_cwd = os.path.join(working_dir, 'ansible.cfg') |
| 21 |
|
| 22 |
cfg_dir = os.path.join(working_dir, 'data') |
| 23 |
cfg_file = os.path.join(cfg_dir, 'ansible.cfg') |
| 24 |
alt_cfg_file = os.path.join(cfg_dir, 'test.cfg') |
| 25 |
cfg_in_homedir = os.path.expanduser('~/.ansible.cfg') |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture |
| 29 |
def setup_env(request, monkeypatch): |
| 30 |
cur_config = os.environ.get('ANSIBLE_CONFIG', None) |
| 31 |
cfg_path = request.param[0] |
| 32 |
|
| 33 |
if cfg_path is None and cur_config: |
| 34 |
monkeypatch.delenv('ANSIBLE_CONFIG') |
| 35 |
else: |
| 36 |
monkeypatch.setenv('ANSIBLE_CONFIG', request.param[0]) |
| 37 |
|
| 38 |
yield |
| 39 |
|
| 40 |
|
| 41 |
@pytest.fixture |
| 42 |
def setup_existing_files(request, monkeypatch): |
| 43 |
def _os_path_exists(path): |
| 44 |
if to_text(path) in (request.param[0]): |
| 45 |
return True |
| 46 |
else: |
| 47 |
return False |
| 48 |
|
| 49 |
def _os_access(path, access): |
| 50 |
assert to_text(path) in (request.param[0]) |
| 51 |
return True |
| 52 |
|
| 53 |
# Enable user and system dirs so that we know cwd takes precedence |
| 54 |
monkeypatch.setattr("os.path.exists", _os_path_exists) |
| 55 |
monkeypatch.setattr("os.access", _os_access) |
| 56 |
monkeypatch.setattr("os.getcwd", lambda: os.path.dirname(cfg_dir)) |
| 57 |
monkeypatch.setattr("os.path.isdir", lambda path: True if to_text(path) == cfg_dir else real_isdir(path)) |
| 58 |
|
| 59 |
|
| 60 |
class TestFindIniFile: |
| 61 |
# This tells us to run twice, once with a file specified and once with a directory |
| 62 |
@pytest.mark.parametrize('setup_env, expected', (([alt_cfg_file], alt_cfg_file), ([cfg_dir], cfg_file)), indirect=['setup_env']) |
| 63 |
# This just passes the list of files that exist to the fixture |
| 64 |
@pytest.mark.parametrize('setup_existing_files', |
| 65 |
[[('/etc/ansible/ansible.cfg', cfg_in_homedir, cfg_in_cwd, alt_cfg_file, cfg_file)]], |
| 66 |
indirect=['setup_existing_files']) |
| 67 |
def test_env_has_cfg_file(self, setup_env, setup_existing_files, expected): |
| 68 |
"""ANSIBLE_CONFIG is specified, use it""" |
| 69 |
warnings = set() |
| 70 |
assert find_ini_config_file(warnings) == expected |
| 71 |
assert warnings == set() |
| 72 |
|
| 73 |
@pytest.mark.parametrize('setup_env', ([alt_cfg_file], [cfg_dir]), indirect=['setup_env']) |
| 74 |
@pytest.mark.parametrize('setup_existing_files', |
| 75 |
[[('/etc/ansible/ansible.cfg', cfg_in_homedir, cfg_in_cwd)]], |
| 76 |
indirect=['setup_existing_files']) |
| 77 |
def test_env_has_no_cfg_file(self, setup_env, setup_existing_files): |
| 78 |
"""ANSIBLE_CONFIG is specified but the file does not exist""" |
| 79 |
|
| 80 |
warnings = set() |
| 81 |
# since the cfg file specified by ANSIBLE_CONFIG doesn't exist, the one at cwd that does |
| 82 |
# exist should be returned |
| 83 |
assert find_ini_config_file(warnings) == cfg_in_cwd |
| 84 |
assert warnings == set() |
| 85 |
|
| 86 |
# ANSIBLE_CONFIG not specified |
| 87 |
@pytest.mark.parametrize('setup_env', [[None]], indirect=['setup_env']) |
| 88 |
# All config files are present |
| 89 |
@pytest.mark.parametrize('setup_existing_files', |
| 90 |
[[('/etc/ansible/ansible.cfg', cfg_in_homedir, cfg_in_cwd, cfg_file, alt_cfg_file)]], |
| 91 |
indirect=['setup_existing_files']) |
| 92 |
def test_ini_in_cwd(self, setup_env, setup_existing_files): |
| 93 |
"""ANSIBLE_CONFIG not specified. Use the cwd cfg""" |
| 94 |
warnings = set() |
| 95 |
assert find_ini_config_file(warnings) == cfg_in_cwd |
| 96 |
assert warnings == set() |
| 97 |
|
| 98 |
# ANSIBLE_CONFIG not specified |
| 99 |
@pytest.mark.parametrize('setup_env', [[None]], indirect=['setup_env']) |
| 100 |
# No config in cwd |
| 101 |
@pytest.mark.parametrize('setup_existing_files', |
| 102 |
[[('/etc/ansible/ansible.cfg', cfg_in_homedir, cfg_file, alt_cfg_file)]], |
| 103 |
indirect=['setup_existing_files']) |
| 104 |
def test_ini_in_homedir(self, setup_env, setup_existing_files): |
| 105 |
"""First config found is in the homedir""" |
| 106 |
warnings = set() |
| 107 |
assert find_ini_config_file(warnings) == cfg_in_homedir |
| 108 |
assert warnings == set() |
| 109 |
|
| 110 |
# ANSIBLE_CONFIG not specified |
| 111 |
@pytest.mark.parametrize('setup_env', [[None]], indirect=['setup_env']) |
| 112 |
# No config in cwd |
| 113 |
@pytest.mark.parametrize('setup_existing_files', [[('/etc/ansible/ansible.cfg', cfg_file, alt_cfg_file)]], indirect=['setup_existing_files']) |
| 114 |
def test_ini_in_systemdir(self, setup_env, setup_existing_files): |
| 115 |
"""First config found is the system config""" |
| 116 |
warnings = set() |
| 117 |
assert find_ini_config_file(warnings) == '/etc/ansible/ansible.cfg' |
| 118 |
assert warnings == set() |
| 119 |
|
| 120 |
# ANSIBLE_CONFIG not specified |
| 121 |
@pytest.mark.parametrize('setup_env', [[None]], indirect=['setup_env']) |
| 122 |
# No config in cwd |
| 123 |
@pytest.mark.parametrize('setup_existing_files', |
| 124 |
[[('/etc/ansible/ansible.cfg', cfg_in_homedir, cfg_file, alt_cfg_file)]], |
| 125 |
indirect=['setup_existing_files']) |
| 126 |
def test_cwd_does_not_exist(self, setup_env, setup_existing_files, monkeypatch): |
| 127 |
"""Smoketest current working directory doesn't exist""" |
| 128 |
def _os_stat(path): |
| 129 |
raise OSError('%s does not exist' % path) |
| 130 |
monkeypatch.setattr('os.stat', _os_stat) |
| 131 |
|
| 132 |
warnings = set() |
| 133 |
assert find_ini_config_file(warnings) == cfg_in_homedir |
| 134 |
assert warnings == set() |
| 135 |
|
| 136 |
@pytest.mark.parametrize('setup_env', [[None]], indirect=['setup_env']) |
| 137 |
# No config in cwd |
| 138 |
@pytest.mark.parametrize('setup_existing_files', [[list()]], indirect=['setup_existing_files']) |
| 139 |
def test_no_config(self, setup_env, setup_existing_files): |
| 140 |
"""No config present, no config found""" |
| 141 |
warnings = set() |
| 142 |
assert find_ini_config_file(warnings) is None |
| 143 |
assert warnings == set() |
| 144 |
|
| 145 |
# ANSIBLE_CONFIG not specified |
| 146 |
@pytest.mark.parametrize('setup_env', [[None]], indirect=['setup_env']) |
| 147 |
# All config files are present except in cwd |
| 148 |
@pytest.mark.parametrize('setup_existing_files', |
| 149 |
[[('/etc/ansible/ansible.cfg', cfg_in_homedir, cfg_file, alt_cfg_file)]], |
| 150 |
indirect=['setup_existing_files']) |
| 151 |
def test_no_cwd_cfg_no_warning_on_writable(self, setup_env, setup_existing_files, monkeypatch): |
| 152 |
"""If the cwd is writable but there is no config file there, move on with no warning""" |
| 153 |
real_stat = os.stat |
| 154 |
|
| 155 |
def _os_stat(path): |
| 156 |
assert path == working_dir |
| 157 |
from posix import stat_result |
| 158 |
stat_info = list(real_stat(path)) |
| 159 |
stat_info[stat.ST_MODE] |= stat.S_IWOTH |
| 160 |
return stat_result(stat_info) |
| 161 |
|
| 162 |
monkeypatch.setattr('os.stat', _os_stat) |
| 163 |
|
| 164 |
warnings = set() |
| 165 |
assert find_ini_config_file(warnings) == cfg_in_homedir |
| 166 |
assert len(warnings) == 0 |
| 167 |
|
| 168 |
# ANSIBLE_CONFIG not specified |
| 169 |
@pytest.mark.parametrize('setup_env', [[None]], indirect=['setup_env']) |
| 170 |
# All config files are present |
| 171 |
@pytest.mark.parametrize('setup_existing_files', |
| 172 |
[[('/etc/ansible/ansible.cfg', cfg_in_homedir, cfg_in_cwd, cfg_file, alt_cfg_file)]], |
| 173 |
indirect=['setup_existing_files']) |
| 174 |
def test_cwd_warning_on_writable(self, setup_env, setup_existing_files, monkeypatch): |
| 175 |
"""If the cwd is writable, warn and skip it """ |
| 176 |
real_stat = os.stat |
| 177 |
|
| 178 |
def _os_stat(path): |
| 179 |
assert path == working_dir |
| 180 |
from posix import stat_result |
| 181 |
stat_info = list(real_stat(path)) |
| 182 |
stat_info[stat.ST_MODE] |= stat.S_IWOTH |
| 183 |
return stat_result(stat_info) |
| 184 |
|
| 185 |
monkeypatch.setattr('os.stat', _os_stat) |
| 186 |
|
| 187 |
warnings = set() |
| 188 |
assert find_ini_config_file(warnings) == cfg_in_homedir |
| 189 |
assert len(warnings) == 1 |
| 190 |
warning = warnings.pop() |
| 191 |
assert u'Ansible is being run in a world writable directory' in warning |
| 192 |
assert u'ignoring it as an ansible.cfg source' in warning |
| 193 |
|
| 194 |
# ANSIBLE_CONFIG is specified |
| 195 |
@pytest.mark.parametrize('setup_env, expected', (([alt_cfg_file], alt_cfg_file), ([cfg_in_cwd], cfg_in_cwd)), indirect=['setup_env']) |
| 196 |
# All config files are present |
| 197 |
@pytest.mark.parametrize('setup_existing_files', |
| 198 |
[[('/etc/ansible/ansible.cfg', cfg_in_homedir, cfg_in_cwd, cfg_file, alt_cfg_file)]], |
| 199 |
indirect=['setup_existing_files']) |
| 200 |
def test_no_warning_on_writable_if_env_used(self, setup_env, setup_existing_files, monkeypatch, expected): |
| 201 |
"""If the cwd is writable but ANSIBLE_CONFIG was used, no warning should be issued""" |
| 202 |
real_stat = os.stat |
| 203 |
|
| 204 |
def _os_stat(path): |
| 205 |
if path != working_dir: |
| 206 |
return real_stat(path) |
| 207 |
|
| 208 |
from posix import stat_result |
| 209 |
stat_info = list(real_stat(path)) |
| 210 |
stat_info[stat.ST_MODE] |= stat.S_IWOTH |
| 211 |
return stat_result(stat_info) |
| 212 |
|
| 213 |
monkeypatch.setattr('os.stat', _os_stat) |
| 214 |
|
| 215 |
warnings = set() |
| 216 |
assert find_ini_config_file(warnings) == expected |
| 217 |
assert warnings == set() |
| 218 |
|
| 219 |
# ANSIBLE_CONFIG not specified |
| 220 |
@pytest.mark.parametrize('setup_env', [[None]], indirect=['setup_env']) |
| 221 |
# All config files are present |
| 222 |
@pytest.mark.parametrize('setup_existing_files', |
| 223 |
[[('/etc/ansible/ansible.cfg', cfg_in_homedir, cfg_in_cwd, cfg_file, alt_cfg_file)]], |
| 224 |
indirect=['setup_existing_files']) |
| 225 |
def test_cwd_warning_on_writable_no_warning_set(self, setup_env, setup_existing_files, monkeypatch): |
| 226 |
"""Smoketest that the function succeeds even though no warning set was passed in""" |
| 227 |
real_stat = os.stat |
| 228 |
|
| 229 |
def _os_stat(path): |
| 230 |
assert path == working_dir |
| 231 |
from posix import stat_result |
| 232 |
stat_info = list(real_stat(path)) |
| 233 |
stat_info[stat.ST_MODE] |= stat.S_IWOTH |
| 234 |
return stat_result(stat_info) |
| 235 |
|
| 236 |
monkeypatch.setattr('os.stat', _os_stat) |
| 237 |
|
| 238 |
assert find_ini_config_file() == cfg_in_homedir |