Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic: don't call lstat when check_mode #64279

Merged
merged 1 commit into from Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/61185-basic.py-fix-check_mode.yaml
@@ -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).
3 changes: 2 additions & 1 deletion lib/ansible/module_utils/basic.py
Expand Up @@ -1041,11 +1041,12 @@ def set_mode_if_different(self, path, mode, changed, diff=None, expand=True):
b_path = to_bytes(path, errors='surrogate_or_strict')
if expand:
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):
return True

path_stat = os.lstat(b_path)

if not isinstance(mode, int):
try:
mode = int(mode, 8)
Expand Down
7 changes: 7 additions & 0 deletions test/units/module_utils/basic/test_set_mode_if_different.py
Expand Up @@ -93,6 +93,13 @@ def test_mode_unchanged_when_already_0660(am, mock_stats, mocker, mode, check_mo
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',
product((True, False), ({},)),
indirect=['stdin'])
Expand Down