Basic: don't call lstat when check_mode (#64279)

While mode is specified in check_mode, don't call lstat.
Since file may not present.

Fixes: #61185
This commit is contained in:
Logistic Bot 2021-04-28 10:17:03 +02:00 committed by GitHub
parent b043afa025
commit 7099657dd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 1 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- AnsibleModule.set_mode_if_different - don't check file existence when check_mode is activated (https://github.com/ansible/ansible/issues/61185).

View file

@ -890,11 +890,12 @@ class AnsibleModule(object):
b_path = to_bytes(path, errors='surrogate_or_strict') b_path = to_bytes(path, errors='surrogate_or_strict')
if expand: if expand:
b_path = os.path.expanduser(os.path.expandvars(b_path)) b_path = os.path.expanduser(os.path.expandvars(b_path))
path_stat = os.lstat(b_path)
if self.check_file_absent_if_check_mode(b_path): if self.check_file_absent_if_check_mode(b_path):
return True return True
path_stat = os.lstat(b_path)
if not isinstance(mode, int): if not isinstance(mode, int):
try: try:
mode = int(mode, 8) mode = int(mode, 8)

View file

@ -100,6 +100,13 @@ def test_mode_unchanged_when_already_0660(am, mock_stats, mocker, mode, check_mo
assert not m_lchmod.called assert not m_lchmod.called
@pytest.mark.parametrize('mode, stdin', product(SYNONYMS_0660, ({},)), indirect=['stdin'])
def test_mode_changed_to_0660_check_mode_no_file(am, mocker, mode):
am.check_mode = True
mocker.patch('os.path.exists', return_value=False)
assert am.set_mode_if_different('/path/to/file', mode, False)
@pytest.mark.parametrize('check_mode, stdin', @pytest.mark.parametrize('check_mode, stdin',
product((True, False), ({},)), product((True, False), ({},)),
indirect=['stdin']) indirect=['stdin'])