mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
fix patch decorators
This commit is contained in:
parent
9e6e532e29
commit
4cf4c470af
1 changed files with 225 additions and 228 deletions
|
@ -14,9 +14,7 @@ from salt.exceptions import CommandExecutionError
|
|||
from tests.support.mock import MagicMock, patch
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.skipif(
|
||||
salt.utils.platform.is_windows(), reason="This test cannot work on Windows"
|
||||
)
|
||||
pytest.mark.skip_on_windows,
|
||||
]
|
||||
|
||||
|
||||
|
@ -43,286 +41,285 @@ def test__create_and_execute_salt_state():
|
|||
chroot._create_and_execute_salt_state("", {}, {}, False, "md5")
|
||||
|
||||
|
||||
@patch("os.path.isdir")
|
||||
def test_exist(isdir):
|
||||
def test_exist():
|
||||
"""
|
||||
Test if the chroot environment exist.
|
||||
"""
|
||||
isdir.side_effect = (True, True, True, True)
|
||||
assert chroot.exist("/chroot")
|
||||
with patch("os.path.isdir") as isdir:
|
||||
isdir.side_effect = (True, True, True, True)
|
||||
assert chroot.exist("/chroot")
|
||||
|
||||
isdir.side_effect = (True, True, True, False)
|
||||
assert not chroot.exist("/chroot")
|
||||
with patch("os.path.isdir") as isdir:
|
||||
isdir.side_effect = (True, True, True, False)
|
||||
assert not chroot.exist("/chroot")
|
||||
|
||||
|
||||
@patch("os.makedirs")
|
||||
@patch("salt.modules.chroot.exist")
|
||||
def test_create(exist, makedirs):
|
||||
def test_create():
|
||||
"""
|
||||
Test the creation of an empty chroot environment.
|
||||
"""
|
||||
exist.return_value = True
|
||||
assert chroot.create("/chroot")
|
||||
makedirs.assert_not_called()
|
||||
with patch("os.makedirs") as makedirs:
|
||||
with patch("salt.modules.chroot.exist") as exist:
|
||||
exist.return_value = True
|
||||
assert chroot.create("/chroot")
|
||||
makedirs.assert_not_called()
|
||||
|
||||
exist.return_value = False
|
||||
assert chroot.create("/chroot")
|
||||
makedirs.assert_called()
|
||||
with patch("os.makedirs") as makedirs:
|
||||
with patch("salt.modules.chroot.exist") as exist:
|
||||
exist.return_value = False
|
||||
assert chroot.create("/chroot")
|
||||
makedirs.assert_called()
|
||||
|
||||
|
||||
@patch("salt.utils.files.fopen")
|
||||
def test_in_chroot(fopen):
|
||||
def test_in_chroot():
|
||||
"""
|
||||
Test the detection of chroot environment.
|
||||
"""
|
||||
matrix = (("a", "b", True), ("a", "a", False))
|
||||
for root_mountinfo, self_mountinfo, result in matrix:
|
||||
fopen.return_value.__enter__.return_value = fopen
|
||||
fopen.read = MagicMock(side_effect=(root_mountinfo, self_mountinfo))
|
||||
assert chroot.in_chroot() == result
|
||||
with patch("salt.utils.files.fopen") as fopen:
|
||||
for root_mountinfo, self_mountinfo, result in matrix:
|
||||
fopen.return_value.__enter__.return_value = fopen
|
||||
fopen.read = MagicMock(side_effect=(root_mountinfo, self_mountinfo))
|
||||
assert chroot.in_chroot() == result
|
||||
|
||||
|
||||
@patch("salt.modules.chroot.exist")
|
||||
def test_call_fails_input_validation(exist):
|
||||
def test_call_fails_input_validation():
|
||||
"""
|
||||
Test execution of Salt functions in chroot.
|
||||
"""
|
||||
# Basic input validation
|
||||
exist.return_value = False
|
||||
pytest.raises(CommandExecutionError, chroot.call, "/chroot", "")
|
||||
pytest.raises(CommandExecutionError, chroot.call, "/chroot", "test.ping")
|
||||
with patch("salt.modules.chroot.exist") as exist:
|
||||
exist.return_value = False
|
||||
pytest.raises(CommandExecutionError, chroot.call, "/chroot", "")
|
||||
pytest.raises(CommandExecutionError, chroot.call, "/chroot", "test.ping")
|
||||
|
||||
|
||||
@patch("salt.modules.chroot.exist")
|
||||
@patch("tempfile.mkdtemp")
|
||||
def test_call_fails_untar(mkdtemp, exist):
|
||||
def test_call_fails_untar():
|
||||
"""
|
||||
Test execution of Salt functions in chroot.
|
||||
"""
|
||||
# Fail the tar command
|
||||
exist.return_value = True
|
||||
mkdtemp.return_value = "/chroot/tmp01"
|
||||
utils_mock = {
|
||||
"thin.gen_thin": MagicMock(return_value="/salt-thin.tgz"),
|
||||
"files.rm_rf": MagicMock(),
|
||||
}
|
||||
salt_mock = {
|
||||
"cmd.run": MagicMock(return_value="Error"),
|
||||
"config.option": MagicMock(),
|
||||
}
|
||||
with patch.dict(chroot.__utils__, utils_mock), patch.dict(
|
||||
chroot.__salt__, salt_mock
|
||||
):
|
||||
assert chroot.call("/chroot", "test.ping") == {
|
||||
"result": False,
|
||||
"comment": "Error",
|
||||
}
|
||||
utils_mock["thin.gen_thin"].assert_called_once()
|
||||
salt_mock["config.option"].assert_called()
|
||||
salt_mock["cmd.run"].assert_called_once()
|
||||
utils_mock["files.rm_rf"].assert_called_once()
|
||||
with patch("salt.modules.chroot.exist") as exist:
|
||||
with patch("tempfile.mkdtemp") as mkdtemp:
|
||||
exist.return_value = True
|
||||
mkdtemp.return_value = "/chroot/tmp01"
|
||||
utils_mock = {
|
||||
"thin.gen_thin": MagicMock(return_value="/salt-thin.tgz"),
|
||||
"files.rm_rf": MagicMock(),
|
||||
}
|
||||
salt_mock = {
|
||||
"cmd.run": MagicMock(return_value="Error"),
|
||||
"config.option": MagicMock(),
|
||||
}
|
||||
with patch.dict(chroot.__utils__, utils_mock), patch.dict(
|
||||
chroot.__salt__, salt_mock
|
||||
):
|
||||
assert chroot.call("/chroot", "test.ping") == {
|
||||
"result": False,
|
||||
"comment": "Error",
|
||||
}
|
||||
utils_mock["thin.gen_thin"].assert_called_once()
|
||||
salt_mock["config.option"].assert_called()
|
||||
salt_mock["cmd.run"].assert_called_once()
|
||||
utils_mock["files.rm_rf"].assert_called_once()
|
||||
|
||||
|
||||
@patch("salt.modules.chroot.exist")
|
||||
@patch("tempfile.mkdtemp")
|
||||
def test_call_fails_salt_thin(mkdtemp, exist):
|
||||
def test_call_fails_salt_thin():
|
||||
"""
|
||||
Test execution of Salt functions in chroot.
|
||||
"""
|
||||
# Fail the inner command
|
||||
exist.return_value = True
|
||||
mkdtemp.return_value = "/chroot/tmp01"
|
||||
utils_mock = {
|
||||
"thin.gen_thin": MagicMock(return_value="/salt-thin.tgz"),
|
||||
"files.rm_rf": MagicMock(),
|
||||
"json.find_json": MagicMock(side_effect=ValueError()),
|
||||
}
|
||||
salt_mock = {
|
||||
"cmd.run": MagicMock(return_value=""),
|
||||
"config.option": MagicMock(),
|
||||
"cmd.run_chroot": MagicMock(
|
||||
return_value={"retcode": 1, "stdout": "", "stderr": "Error"}
|
||||
),
|
||||
}
|
||||
with patch.dict(chroot.__utils__, utils_mock), patch.dict(
|
||||
chroot.__salt__, salt_mock
|
||||
):
|
||||
assert chroot.call("/chroot", "test.ping") == {
|
||||
"result": False,
|
||||
"retcode": 1,
|
||||
"comment": {"stdout": "", "stderr": "Error"},
|
||||
}
|
||||
utils_mock["thin.gen_thin"].assert_called_once()
|
||||
salt_mock["config.option"].assert_called()
|
||||
salt_mock["cmd.run"].assert_called_once()
|
||||
salt_mock["cmd.run_chroot"].assert_called_with(
|
||||
"/chroot",
|
||||
[
|
||||
"python{}".format(sys.version_info[0]),
|
||||
"/tmp01/salt-call",
|
||||
"--metadata",
|
||||
"--local",
|
||||
"--log-file",
|
||||
"/tmp01/log",
|
||||
"--cachedir",
|
||||
"/tmp01/cache",
|
||||
"--out",
|
||||
"json",
|
||||
"-l",
|
||||
"quiet",
|
||||
"--",
|
||||
"test.ping",
|
||||
],
|
||||
)
|
||||
utils_mock["files.rm_rf"].assert_called_once()
|
||||
with patch("salt.modules.chroot.exist") as exist:
|
||||
with patch("tempfile.mkdtemp") as mkdtemp:
|
||||
exist.return_value = True
|
||||
mkdtemp.return_value = "/chroot/tmp01"
|
||||
utils_mock = {
|
||||
"thin.gen_thin": MagicMock(return_value="/salt-thin.tgz"),
|
||||
"files.rm_rf": MagicMock(),
|
||||
"json.find_json": MagicMock(side_effect=ValueError()),
|
||||
}
|
||||
salt_mock = {
|
||||
"cmd.run": MagicMock(return_value=""),
|
||||
"config.option": MagicMock(),
|
||||
"cmd.run_chroot": MagicMock(
|
||||
return_value={"retcode": 1, "stdout": "", "stderr": "Error"}
|
||||
),
|
||||
}
|
||||
with patch.dict(chroot.__utils__, utils_mock), patch.dict(
|
||||
chroot.__salt__, salt_mock
|
||||
):
|
||||
assert chroot.call("/chroot", "test.ping") == {
|
||||
"result": False,
|
||||
"retcode": 1,
|
||||
"comment": {"stdout": "", "stderr": "Error"},
|
||||
}
|
||||
utils_mock["thin.gen_thin"].assert_called_once()
|
||||
salt_mock["config.option"].assert_called()
|
||||
salt_mock["cmd.run"].assert_called_once()
|
||||
salt_mock["cmd.run_chroot"].assert_called_with(
|
||||
"/chroot",
|
||||
[
|
||||
"python{}".format(sys.version_info[0]),
|
||||
"/tmp01/salt-call",
|
||||
"--metadata",
|
||||
"--local",
|
||||
"--log-file",
|
||||
"/tmp01/log",
|
||||
"--cachedir",
|
||||
"/tmp01/cache",
|
||||
"--out",
|
||||
"json",
|
||||
"-l",
|
||||
"quiet",
|
||||
"--",
|
||||
"test.ping",
|
||||
],
|
||||
)
|
||||
utils_mock["files.rm_rf"].assert_called_once()
|
||||
|
||||
|
||||
@patch("salt.modules.chroot.exist")
|
||||
@patch("tempfile.mkdtemp")
|
||||
def test_call_success(mkdtemp, exist):
|
||||
def test_call_success():
|
||||
"""
|
||||
Test execution of Salt functions in chroot.
|
||||
"""
|
||||
# Success test
|
||||
exist.return_value = True
|
||||
mkdtemp.return_value = "/chroot/tmp01"
|
||||
utils_mock = {
|
||||
"thin.gen_thin": MagicMock(return_value="/salt-thin.tgz"),
|
||||
"files.rm_rf": MagicMock(),
|
||||
"json.find_json": MagicMock(return_value={"return": "result"}),
|
||||
}
|
||||
salt_mock = {
|
||||
"cmd.run": MagicMock(return_value=""),
|
||||
"config.option": MagicMock(),
|
||||
"cmd.run_chroot": MagicMock(return_value={"retcode": 0, "stdout": ""}),
|
||||
}
|
||||
with patch.dict(chroot.__utils__, utils_mock), patch.dict(
|
||||
chroot.__salt__, salt_mock
|
||||
):
|
||||
assert chroot.call("/chroot", "test.ping") == "result"
|
||||
utils_mock["thin.gen_thin"].assert_called_once()
|
||||
salt_mock["config.option"].assert_called()
|
||||
salt_mock["cmd.run"].assert_called_once()
|
||||
salt_mock["cmd.run_chroot"].assert_called_with(
|
||||
"/chroot",
|
||||
[
|
||||
"python{}".format(sys.version_info[0]),
|
||||
"/tmp01/salt-call",
|
||||
"--metadata",
|
||||
"--local",
|
||||
"--log-file",
|
||||
"/tmp01/log",
|
||||
"--cachedir",
|
||||
"/tmp01/cache",
|
||||
"--out",
|
||||
"json",
|
||||
"-l",
|
||||
"quiet",
|
||||
"--",
|
||||
"test.ping",
|
||||
],
|
||||
)
|
||||
utils_mock["files.rm_rf"].assert_called_once()
|
||||
with patch("salt.modules.chroot.exist") as exist:
|
||||
with patch("tempfile.mkdtemp") as mkdtemp:
|
||||
exist.return_value = True
|
||||
mkdtemp.return_value = "/chroot/tmp01"
|
||||
utils_mock = {
|
||||
"thin.gen_thin": MagicMock(return_value="/salt-thin.tgz"),
|
||||
"files.rm_rf": MagicMock(),
|
||||
"json.find_json": MagicMock(return_value={"return": "result"}),
|
||||
}
|
||||
salt_mock = {
|
||||
"cmd.run": MagicMock(return_value=""),
|
||||
"config.option": MagicMock(),
|
||||
"cmd.run_chroot": MagicMock(return_value={"retcode": 0, "stdout": ""}),
|
||||
}
|
||||
with patch.dict(chroot.__utils__, utils_mock), patch.dict(
|
||||
chroot.__salt__, salt_mock
|
||||
):
|
||||
assert chroot.call("/chroot", "test.ping") == "result"
|
||||
utils_mock["thin.gen_thin"].assert_called_once()
|
||||
salt_mock["config.option"].assert_called()
|
||||
salt_mock["cmd.run"].assert_called_once()
|
||||
salt_mock["cmd.run_chroot"].assert_called_with(
|
||||
"/chroot",
|
||||
[
|
||||
"python{}".format(sys.version_info[0]),
|
||||
"/tmp01/salt-call",
|
||||
"--metadata",
|
||||
"--local",
|
||||
"--log-file",
|
||||
"/tmp01/log",
|
||||
"--cachedir",
|
||||
"/tmp01/cache",
|
||||
"--out",
|
||||
"json",
|
||||
"-l",
|
||||
"quiet",
|
||||
"--",
|
||||
"test.ping",
|
||||
],
|
||||
)
|
||||
utils_mock["files.rm_rf"].assert_called_once()
|
||||
|
||||
|
||||
@patch("salt.modules.chroot.exist")
|
||||
@patch("tempfile.mkdtemp")
|
||||
def test_call_success_parameters(mkdtemp, exist):
|
||||
def test_call_success_parameters():
|
||||
"""
|
||||
Test execution of Salt functions in chroot with parameters.
|
||||
"""
|
||||
# Success test
|
||||
exist.return_value = True
|
||||
mkdtemp.return_value = "/chroot/tmp01"
|
||||
utils_mock = {
|
||||
"thin.gen_thin": MagicMock(return_value="/salt-thin.tgz"),
|
||||
"files.rm_rf": MagicMock(),
|
||||
"json.find_json": MagicMock(return_value={"return": "result"}),
|
||||
}
|
||||
salt_mock = {
|
||||
"cmd.run": MagicMock(return_value=""),
|
||||
"config.option": MagicMock(),
|
||||
"cmd.run_chroot": MagicMock(return_value={"retcode": 0, "stdout": ""}),
|
||||
}
|
||||
with patch.dict(chroot.__utils__, utils_mock), patch.dict(
|
||||
chroot.__salt__, salt_mock
|
||||
):
|
||||
assert chroot.call("/chroot", "module.function", key="value") == "result"
|
||||
utils_mock["thin.gen_thin"].assert_called_once()
|
||||
salt_mock["config.option"].assert_called()
|
||||
salt_mock["cmd.run"].assert_called_once()
|
||||
salt_mock["cmd.run_chroot"].assert_called_with(
|
||||
"/chroot",
|
||||
[
|
||||
"python{}".format(sys.version_info[0]),
|
||||
"/tmp01/salt-call",
|
||||
"--metadata",
|
||||
"--local",
|
||||
"--log-file",
|
||||
"/tmp01/log",
|
||||
"--cachedir",
|
||||
"/tmp01/cache",
|
||||
"--out",
|
||||
"json",
|
||||
"-l",
|
||||
"quiet",
|
||||
"--",
|
||||
"module.function",
|
||||
"key=value",
|
||||
],
|
||||
)
|
||||
utils_mock["files.rm_rf"].assert_called_once()
|
||||
with patch("salt.modules.chroot.exist") as exist:
|
||||
with patch("tempfile.mkdtemp") as mkdtemp:
|
||||
exist.return_value = True
|
||||
mkdtemp.return_value = "/chroot/tmp01"
|
||||
utils_mock = {
|
||||
"thin.gen_thin": MagicMock(return_value="/salt-thin.tgz"),
|
||||
"files.rm_rf": MagicMock(),
|
||||
"json.find_json": MagicMock(return_value={"return": "result"}),
|
||||
}
|
||||
salt_mock = {
|
||||
"cmd.run": MagicMock(return_value=""),
|
||||
"config.option": MagicMock(),
|
||||
"cmd.run_chroot": MagicMock(return_value={"retcode": 0, "stdout": ""}),
|
||||
}
|
||||
with patch.dict(chroot.__utils__, utils_mock), patch.dict(
|
||||
chroot.__salt__, salt_mock
|
||||
):
|
||||
assert (
|
||||
chroot.call("/chroot", "module.function", key="value") == "result"
|
||||
)
|
||||
utils_mock["thin.gen_thin"].assert_called_once()
|
||||
salt_mock["config.option"].assert_called()
|
||||
salt_mock["cmd.run"].assert_called_once()
|
||||
salt_mock["cmd.run_chroot"].assert_called_with(
|
||||
"/chroot",
|
||||
[
|
||||
"python{}".format(sys.version_info[0]),
|
||||
"/tmp01/salt-call",
|
||||
"--metadata",
|
||||
"--local",
|
||||
"--log-file",
|
||||
"/tmp01/log",
|
||||
"--cachedir",
|
||||
"/tmp01/cache",
|
||||
"--out",
|
||||
"json",
|
||||
"-l",
|
||||
"quiet",
|
||||
"--",
|
||||
"module.function",
|
||||
"key=value",
|
||||
],
|
||||
)
|
||||
utils_mock["files.rm_rf"].assert_called_once()
|
||||
|
||||
|
||||
@patch("salt.modules.chroot._create_and_execute_salt_state")
|
||||
@patch("salt.client.ssh.state.SSHHighState")
|
||||
@patch("salt.fileclient.get_file_client")
|
||||
@patch("salt.utils.state.get_sls_opts")
|
||||
def test_sls(
|
||||
get_sls_opts,
|
||||
get_file_client,
|
||||
SSHHighState,
|
||||
_create_and_execute_salt_state,
|
||||
):
|
||||
def test_sls():
|
||||
"""
|
||||
Test execution of Salt states in chroot.
|
||||
"""
|
||||
SSHHighState.return_value = SSHHighState
|
||||
SSHHighState.render_highstate.return_value = (None, [])
|
||||
SSHHighState.state.reconcile_extend.return_value = (None, [])
|
||||
SSHHighState.state.requisite_in.return_value = (None, [])
|
||||
SSHHighState.state.verify_high.return_value = []
|
||||
with patch("salt.utils.state.get_sls_opts") as get_sls_opts:
|
||||
with patch("salt.fileclient.get_file_client") as get_file_client:
|
||||
with patch("salt.client.ssh.state.SSHHighState") as SSHHighState:
|
||||
with patch(
|
||||
"salt.modules.chroot._create_and_execute_salt_state"
|
||||
) as _create_and_execute_salt_state:
|
||||
SSHHighState.return_value = SSHHighState
|
||||
SSHHighState.render_highstate.return_value = (None, [])
|
||||
SSHHighState.state.reconcile_extend.return_value = (None, [])
|
||||
SSHHighState.state.requisite_in.return_value = (None, [])
|
||||
SSHHighState.state.verify_high.return_value = []
|
||||
|
||||
_create_and_execute_salt_state.return_value = "result"
|
||||
opts_mock = {
|
||||
"hash_type": "md5",
|
||||
}
|
||||
get_sls_opts.return_value = opts_mock
|
||||
with patch.dict(chroot.__opts__, opts_mock):
|
||||
assert chroot.sls("/chroot", "module") == "result"
|
||||
_create_and_execute_salt_state.assert_called_once()
|
||||
_create_and_execute_salt_state.return_value = "result"
|
||||
opts_mock = {
|
||||
"hash_type": "md5",
|
||||
}
|
||||
get_sls_opts.return_value = opts_mock
|
||||
with patch.dict(chroot.__opts__, opts_mock):
|
||||
assert chroot.sls("/chroot", "module") == "result"
|
||||
_create_and_execute_salt_state.assert_called_once()
|
||||
|
||||
|
||||
@patch("salt.modules.chroot._create_and_execute_salt_state")
|
||||
@patch("salt.client.ssh.state.SSHHighState")
|
||||
@patch("salt.fileclient.get_file_client")
|
||||
@patch("salt.utils.state.get_sls_opts")
|
||||
def test_highstate(
|
||||
get_sls_opts,
|
||||
get_file_client,
|
||||
SSHHighState,
|
||||
_create_and_execute_salt_state,
|
||||
):
|
||||
def test_highstate():
|
||||
"""
|
||||
Test execution of Salt states in chroot.
|
||||
"""
|
||||
SSHHighState.return_value = SSHHighState
|
||||
with patch("salt.utils.state.get_sls_opts") as get_sls_opts:
|
||||
with patch("salt.fileclient.get_file_client") as get_file_client:
|
||||
with patch("salt.client.ssh.state.SSHHighState") as SSHHighState:
|
||||
with patch(
|
||||
"salt.modules.chroot._create_and_execute_salt_state"
|
||||
) as _create_and_execute_salt_state:
|
||||
SSHHighState.return_value = SSHHighState
|
||||
|
||||
_create_and_execute_salt_state.return_value = "result"
|
||||
opts_mock = {
|
||||
"hash_type": "md5",
|
||||
}
|
||||
get_sls_opts.return_value = opts_mock
|
||||
with patch.dict(chroot.__opts__, opts_mock):
|
||||
assert chroot.highstate("/chroot") == "result"
|
||||
_create_and_execute_salt_state.assert_called_once()
|
||||
_create_and_execute_salt_state.return_value = "result"
|
||||
opts_mock = {
|
||||
"hash_type": "md5",
|
||||
}
|
||||
get_sls_opts.return_value = opts_mock
|
||||
with patch.dict(chroot.__opts__, opts_mock):
|
||||
assert chroot.highstate("/chroot") == "result"
|
||||
_create_and_execute_salt_state.assert_called_once()
|
||||
|
|
Loading…
Add table
Reference in a new issue