Skip to content

Instantly share code, notes, and snippets.

@tadeboro
Created May 8, 2020 22:00
Embed
What would you like to do?
Testing AnsibleModule._set_cwd method
# -*- coding: utf-8 -*-
# (c) 2020, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import pytest
@pytest.mark.parametrize('stdin', [{}], indirect=True)
def test_cwd_is_acceptable(am, mocker, tmpdir):
ok_dir = tmpdir.mkdir('ok_dir')
mocker.patch('os.getcwd').return_value = str(ok_dir)
assert str(ok_dir) == am._set_cwd()
@pytest.mark.parametrize('stdin', [{}], indirect=True)
def test_cwd_not_executable(am, mocker, tmpdir):
bad_dir = tmpdir.mkdir('bad_dir')
bad_dir.chmod(0o444) # Executable bit not set
mocker.patch('os.getcwd').return_value = str(bad_dir)
assert str(bad_dir) != am._set_cwd()
@pytest.mark.parametrize('stdin', [{}], indirect=True)
def test_cwd_tmpdir_not_executable(am, mocker, tmpdir):
bad_dir = tmpdir.mkdir('bad_dir')
bad_dir.chmod(0o444) # Executable bit not set
bad_dir_str = str(bad_dir)
mocker.patch('os.getcwd').return_value = bad_dir_str
am._tmpdir = bad_dir_str
assert str(bad_dir) != am._set_cwd()
@pytest.mark.parametrize('stdin', [{}], indirect=True)
def test_cwd_tmpdir_home_not_executable(am, mocker, tmpdir):
bad_dir = tmpdir.mkdir('bad_dir')
bad_dir.chmod(0o444) # Executable bit not set
bad_dir_str = str(bad_dir)
mocker.patch('os.getcwd').return_value = bad_dir_str
am._tmpdir = bad_dir_str
mocker.patch('os.path.expandvars').return_value = bad_dir_str
assert bad_dir_str != am._set_cwd()
@pytest.mark.parametrize('stdin', [{}], indirect=True)
def test_cwd_tmpdir_home_tempdir_not_executable(am, mocker, tmpdir):
bad_dir = tmpdir.mkdir('bad_dir')
bad_dir.chmod(0o444) # Executable bit not set
bad_dir_str = str(bad_dir)
mocker.patch('os.getcwd').return_value = bad_dir_str
am._tmpdir = bad_dir_str
mocker.patch('os.path.expandvars').return_value = bad_dir_str
mocker.patch('tempfile.gettempdir').return_value = bad_dir_str
assert am._set_cwd() is None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment