mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Split tests
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
This commit is contained in:
parent
ab124be9c6
commit
0d904c2d79
6 changed files with 275 additions and 212 deletions
0
tests/pytests/unit/states/saltmod/__init__.py
Normal file
0
tests/pytests/unit/states/saltmod/__init__.py
Normal file
86
tests/pytests/unit/states/saltmod/test_function.py
Normal file
86
tests/pytests/unit/states/saltmod/test_function.py
Normal file
|
@ -0,0 +1,86 @@
|
|||
import pytest
|
||||
|
||||
import salt.states.saltmod as saltmod
|
||||
import salt.utils.state
|
||||
from tests.support.mock import MagicMock, patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configure_loader_modules(minion_opts):
|
||||
return {
|
||||
saltmod: {
|
||||
"__env__": "base",
|
||||
"__opts__": minion_opts,
|
||||
"__utils__": {"state.check_result": salt.utils.state.check_result},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_function():
|
||||
"""
|
||||
Test to execute a single module function on a remote
|
||||
minion via salt or salt-ssh
|
||||
"""
|
||||
name = "state"
|
||||
tgt = "larry"
|
||||
|
||||
expected = {
|
||||
"name": name,
|
||||
"changes": {},
|
||||
"result": None,
|
||||
"comment": "Function state would be executed on target {}".format(tgt),
|
||||
}
|
||||
|
||||
with patch.dict(saltmod.__opts__, {"test": True}):
|
||||
ret = saltmod.function(name, tgt)
|
||||
assert ret == expected
|
||||
|
||||
expected.update(
|
||||
{
|
||||
"result": True,
|
||||
"changes": {"ret": {tgt: ""}},
|
||||
"comment": (
|
||||
"Function ran successfully. Function state ran on {}.".format(tgt)
|
||||
),
|
||||
}
|
||||
)
|
||||
with patch.dict(saltmod.__opts__, {"test": False}):
|
||||
mock_ret = {"larry": {"ret": "", "retcode": 0, "failed": False}}
|
||||
mock_cmd = MagicMock(return_value=mock_ret)
|
||||
with patch.dict(saltmod.__salt__, {"saltutil.cmd": mock_cmd}):
|
||||
ret = saltmod.function(name, tgt)
|
||||
assert ret == expected
|
||||
|
||||
|
||||
def test_function_when_no_minions_match():
|
||||
"""
|
||||
Test to execute a single module function on a remote
|
||||
minion via salt or salt-ssh
|
||||
"""
|
||||
name = "state"
|
||||
tgt = "larry"
|
||||
|
||||
expected = {
|
||||
"name": name,
|
||||
"changes": {},
|
||||
"result": False,
|
||||
"comment": "No minions responded",
|
||||
}
|
||||
|
||||
with patch.dict(saltmod.__opts__, {"test": False}):
|
||||
with patch.dict(saltmod.__salt__, {"saltutil.cmd": MagicMock(return_value={})}):
|
||||
ret = saltmod.function(name, tgt)
|
||||
assert ret == expected
|
||||
|
||||
|
||||
def test_function_ssh():
|
||||
"""
|
||||
Test saltmod function passes roster to saltutil.cmd
|
||||
"""
|
||||
cmd_mock = MagicMock()
|
||||
with patch.dict(saltmod.__opts__, {"test": False}), patch.dict(
|
||||
saltmod.__salt__, {"saltutil.cmd": cmd_mock}
|
||||
):
|
||||
saltmod.function("state", tgt="*", ssh=True, roster="my_roster")
|
||||
assert "roster" in cmd_mock.call_args.kwargs
|
||||
assert cmd_mock.call_args.kwargs["roster"] == "my_roster"
|
35
tests/pytests/unit/states/saltmod/test_runner.py
Normal file
35
tests/pytests/unit/states/saltmod/test_runner.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
import pytest
|
||||
|
||||
import salt.states.saltmod as saltmod
|
||||
import salt.utils.state
|
||||
from tests.support.mock import MagicMock, patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configure_loader_modules(minion_opts):
|
||||
return {
|
||||
saltmod: {
|
||||
"__env__": "base",
|
||||
"__opts__": minion_opts,
|
||||
"__utils__": {"state.check_result": salt.utils.state.check_result},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_runner():
|
||||
"""
|
||||
Test to execute a runner module on the master
|
||||
"""
|
||||
name = "state"
|
||||
|
||||
expected = {
|
||||
"changes": {"return": True},
|
||||
"name": "state",
|
||||
"result": True,
|
||||
"comment": "Runner function 'state' executed.",
|
||||
}
|
||||
with patch.dict(
|
||||
saltmod.__salt__, {"saltutil.runner": MagicMock(return_value={"return": True})}
|
||||
):
|
||||
ret = saltmod.runner(name)
|
||||
assert ret == expected
|
|
@ -1,5 +1,3 @@
|
|||
import copy
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.modules.saltutil
|
||||
|
@ -10,38 +8,11 @@ import salt.utils.state
|
|||
from tests.support.mock import MagicMock, create_autospec, patch
|
||||
|
||||
|
||||
class MockedEvent:
|
||||
"""
|
||||
Mocked event class
|
||||
"""
|
||||
|
||||
def __init__(self, data):
|
||||
self.full = None
|
||||
self.flag = None
|
||||
self._data = data
|
||||
|
||||
def get_event(self, full):
|
||||
"""
|
||||
Mock get_event method
|
||||
"""
|
||||
self.full = full
|
||||
if self.flag:
|
||||
return self._data
|
||||
return None
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configure_loader_modules():
|
||||
def configure_loader_modules(minion_opts):
|
||||
return {
|
||||
saltmod: {
|
||||
"__env__": "base",
|
||||
"__opts__": {"__role": "testsuite"},
|
||||
"__opts__": minion_opts,
|
||||
"__salt__": {"saltutil.cmd": MagicMock()},
|
||||
"__utils__": {"state.check_result": salt.utils.state.check_result},
|
||||
},
|
||||
|
@ -996,174 +967,7 @@ def test_state_allow_fail():
|
|||
assert ret == expected
|
||||
|
||||
|
||||
def test_function():
|
||||
"""
|
||||
Test to execute a single module function on a remote
|
||||
minion via salt or salt-ssh
|
||||
"""
|
||||
name = "state"
|
||||
tgt = "larry"
|
||||
|
||||
expected = {
|
||||
"name": name,
|
||||
"changes": {},
|
||||
"result": None,
|
||||
"comment": "Function state would be executed on target {}".format(tgt),
|
||||
}
|
||||
|
||||
with patch.dict(saltmod.__opts__, {"test": True}):
|
||||
ret = saltmod.function(name, tgt)
|
||||
assert ret == expected
|
||||
|
||||
expected.update(
|
||||
{
|
||||
"result": True,
|
||||
"changes": {"ret": {tgt: ""}},
|
||||
"comment": (
|
||||
"Function ran successfully. Function state ran on {}.".format(tgt)
|
||||
),
|
||||
}
|
||||
)
|
||||
with patch.dict(saltmod.__opts__, {"test": False}):
|
||||
mock_ret = {"larry": {"ret": "", "retcode": 0, "failed": False}}
|
||||
mock_cmd = MagicMock(return_value=mock_ret)
|
||||
with patch.dict(saltmod.__salt__, {"saltutil.cmd": mock_cmd}):
|
||||
ret = saltmod.function(name, tgt)
|
||||
assert ret == expected
|
||||
|
||||
|
||||
def test_function_when_no_minions_match():
|
||||
"""
|
||||
Test to execute a single module function on a remote
|
||||
minion via salt or salt-ssh
|
||||
"""
|
||||
name = "state"
|
||||
tgt = "larry"
|
||||
|
||||
expected = {
|
||||
"name": name,
|
||||
"changes": {},
|
||||
"result": False,
|
||||
"comment": "No minions responded",
|
||||
}
|
||||
|
||||
with patch.dict(saltmod.__opts__, {"test": False}):
|
||||
with patch.dict(saltmod.__salt__, {"saltutil.cmd": MagicMock(return_value={})}):
|
||||
ret = saltmod.function(name, tgt)
|
||||
assert ret == expected
|
||||
|
||||
|
||||
def test_wait_for_event():
|
||||
"""
|
||||
Test to watch Salt's event bus and block until a condition is met
|
||||
"""
|
||||
name = "state"
|
||||
tgt = "minion1"
|
||||
|
||||
ret = {
|
||||
"name": name,
|
||||
"changes": {},
|
||||
"result": False,
|
||||
"comment": "Timeout value reached.",
|
||||
}
|
||||
|
||||
mocked_event = MockedEvent({"tag": name, "data": {}})
|
||||
with patch.object(
|
||||
salt.utils.event, "get_event", MagicMock(return_value=mocked_event)
|
||||
):
|
||||
with patch.dict(saltmod.__opts__, {"sock_dir": True, "transport": True}):
|
||||
with patch("salt.states.saltmod.time.time", MagicMock(return_value=1.0)):
|
||||
assert saltmod.wait_for_event(name, "salt", timeout=-1.0) == ret
|
||||
|
||||
mocked_event.flag = True
|
||||
ret.update(
|
||||
{"comment": "All events seen in 0.0 seconds.", "result": True}
|
||||
)
|
||||
assert saltmod.wait_for_event(name, "") == ret
|
||||
|
||||
ret.update({"comment": "Timeout value reached.", "result": False})
|
||||
assert saltmod.wait_for_event(name, tgt, timeout=-1.0) == ret
|
||||
|
||||
|
||||
def test_wait_for_event_list_single_event():
|
||||
"""
|
||||
Test to watch Salt's event bus and block until a condition is met
|
||||
"""
|
||||
name = "presence"
|
||||
event_id = "lost"
|
||||
tgt = ["minion_1", "minion_2", "minion_3"]
|
||||
|
||||
expected = {
|
||||
"name": name,
|
||||
"changes": {},
|
||||
"result": False,
|
||||
"comment": "Timeout value reached.",
|
||||
}
|
||||
|
||||
mocked_event = MockedEvent({"tag": name, "data": {"lost": tgt}})
|
||||
with patch.object(
|
||||
salt.utils.event, "get_event", MagicMock(return_value=mocked_event)
|
||||
):
|
||||
with patch.dict(saltmod.__opts__, {"sock_dir": True, "transport": True}):
|
||||
with patch("salt.states.saltmod.time.time", MagicMock(return_value=1.0)):
|
||||
expected.update({"comment": "Timeout value reached.", "result": False})
|
||||
ret = saltmod.wait_for_event(name, tgt, event_id=event_id, timeout=-1.0)
|
||||
assert ret == expected
|
||||
|
||||
mocked_event.flag = True
|
||||
expected.update(
|
||||
{
|
||||
"name": name,
|
||||
"changes": {"minions_seen": tgt},
|
||||
"result": True,
|
||||
"comment": "All events seen in 0.0 seconds.",
|
||||
}
|
||||
)
|
||||
ret = saltmod.wait_for_event(
|
||||
name, copy.deepcopy(tgt), event_id="lost", timeout=1.0
|
||||
)
|
||||
assert ret == expected
|
||||
|
||||
|
||||
def test_runner():
|
||||
"""
|
||||
Test to execute a runner module on the master
|
||||
"""
|
||||
name = "state"
|
||||
|
||||
expected = {
|
||||
"changes": {"return": True},
|
||||
"name": "state",
|
||||
"result": True,
|
||||
"comment": "Runner function 'state' executed.",
|
||||
}
|
||||
with patch.dict(
|
||||
saltmod.__salt__, {"saltutil.runner": MagicMock(return_value={"return": True})}
|
||||
):
|
||||
ret = saltmod.runner(name)
|
||||
assert ret == expected
|
||||
|
||||
|
||||
def test_wheel():
|
||||
"""
|
||||
Test to execute a wheel module on the master
|
||||
"""
|
||||
name = "state"
|
||||
|
||||
expected = {
|
||||
"changes": {"return": True},
|
||||
"name": "state",
|
||||
"result": True,
|
||||
"comment": "Wheel function 'state' executed.",
|
||||
}
|
||||
with patch.dict(
|
||||
saltmod.__salt__, {"saltutil.wheel": MagicMock(return_value={"return": True})}
|
||||
):
|
||||
ret = saltmod.wheel(name)
|
||||
assert ret == expected
|
||||
|
||||
|
||||
def test_state_ssh():
|
||||
def test_roster():
|
||||
"""
|
||||
Test saltmod state passes roster to saltutil.cmd
|
||||
"""
|
||||
|
@ -1174,16 +978,3 @@ def test_state_ssh():
|
|||
)
|
||||
assert "roster" in cmd_mock.call_args.kwargs
|
||||
assert cmd_mock.call_args.kwargs["roster"] == "my_roster"
|
||||
|
||||
|
||||
def test_function_ssh():
|
||||
"""
|
||||
Test saltmod function passes roster to saltutil.cmd
|
||||
"""
|
||||
cmd_mock = MagicMock()
|
||||
with patch.dict(saltmod.__opts__, {"test": False}), patch.dict(
|
||||
saltmod.__salt__, {"saltutil.cmd": cmd_mock}
|
||||
):
|
||||
saltmod.function("state", tgt="*", ssh=True, roster="my_roster")
|
||||
assert "roster" in cmd_mock.call_args.kwargs
|
||||
assert cmd_mock.call_args.kwargs["roster"] == "my_roster"
|
116
tests/pytests/unit/states/saltmod/test_wait_for_event.py
Normal file
116
tests/pytests/unit/states/saltmod/test_wait_for_event.py
Normal file
|
@ -0,0 +1,116 @@
|
|||
import copy
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.states.saltmod as saltmod
|
||||
import salt.utils.state
|
||||
from tests.support.mock import MagicMock, patch
|
||||
|
||||
|
||||
class MockedEvent:
|
||||
"""
|
||||
Mocked event class
|
||||
"""
|
||||
|
||||
def __init__(self, data):
|
||||
self.full = None
|
||||
self.flag = None
|
||||
self._data = data
|
||||
|
||||
def get_event(self, full):
|
||||
"""
|
||||
Mock get_event method
|
||||
"""
|
||||
self.full = full
|
||||
if self.flag:
|
||||
return self._data
|
||||
return None
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configure_loader_modules(minion_opts):
|
||||
return {
|
||||
saltmod: {
|
||||
"__env__": "base",
|
||||
"__opts__": minion_opts,
|
||||
"__utils__": {"state.check_result": salt.utils.state.check_result},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_wait_for_event():
|
||||
"""
|
||||
Test to watch Salt's event bus and block until a condition is met
|
||||
"""
|
||||
name = "state"
|
||||
tgt = "minion1"
|
||||
|
||||
ret = {
|
||||
"name": name,
|
||||
"changes": {},
|
||||
"result": False,
|
||||
"comment": "Timeout value reached.",
|
||||
}
|
||||
|
||||
mocked_event = MockedEvent({"tag": name, "data": {}})
|
||||
with patch.object(
|
||||
salt.utils.event, "get_event", MagicMock(return_value=mocked_event)
|
||||
):
|
||||
with patch.dict(saltmod.__opts__, {"sock_dir": True, "transport": True}):
|
||||
with patch("salt.states.saltmod.time.time", MagicMock(return_value=1.0)):
|
||||
assert saltmod.wait_for_event(name, "salt", timeout=-1.0) == ret
|
||||
|
||||
mocked_event.flag = True
|
||||
ret.update(
|
||||
{"comment": "All events seen in 0.0 seconds.", "result": True}
|
||||
)
|
||||
assert saltmod.wait_for_event(name, "") == ret
|
||||
|
||||
ret.update({"comment": "Timeout value reached.", "result": False})
|
||||
assert saltmod.wait_for_event(name, tgt, timeout=-1.0) == ret
|
||||
|
||||
|
||||
def test_wait_for_event_list_single_event():
|
||||
"""
|
||||
Test to watch Salt's event bus and block until a condition is met
|
||||
"""
|
||||
name = "presence"
|
||||
event_id = "lost"
|
||||
tgt = ["minion_1", "minion_2", "minion_3"]
|
||||
|
||||
expected = {
|
||||
"name": name,
|
||||
"changes": {},
|
||||
"result": False,
|
||||
"comment": "Timeout value reached.",
|
||||
}
|
||||
|
||||
mocked_event = MockedEvent({"tag": name, "data": {"lost": tgt}})
|
||||
with patch.object(
|
||||
salt.utils.event, "get_event", MagicMock(return_value=mocked_event)
|
||||
):
|
||||
with patch.dict(saltmod.__opts__, {"sock_dir": True, "transport": True}):
|
||||
with patch("salt.states.saltmod.time.time", MagicMock(return_value=1.0)):
|
||||
expected.update({"comment": "Timeout value reached.", "result": False})
|
||||
ret = saltmod.wait_for_event(name, tgt, event_id=event_id, timeout=-1.0)
|
||||
assert ret == expected
|
||||
|
||||
mocked_event.flag = True
|
||||
expected.update(
|
||||
{
|
||||
"name": name,
|
||||
"changes": {"minions_seen": tgt},
|
||||
"result": True,
|
||||
"comment": "All events seen in 0.0 seconds.",
|
||||
}
|
||||
)
|
||||
ret = saltmod.wait_for_event(
|
||||
name, copy.deepcopy(tgt), event_id="lost", timeout=1.0
|
||||
)
|
||||
assert ret == expected
|
35
tests/pytests/unit/states/saltmod/test_wheel.py
Normal file
35
tests/pytests/unit/states/saltmod/test_wheel.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
import pytest
|
||||
|
||||
import salt.states.saltmod as saltmod
|
||||
import salt.utils.state
|
||||
from tests.support.mock import MagicMock, patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configure_loader_modules(minion_opts):
|
||||
return {
|
||||
saltmod: {
|
||||
"__env__": "base",
|
||||
"__opts__": minion_opts,
|
||||
"__utils__": {"state.check_result": salt.utils.state.check_result},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_wheel():
|
||||
"""
|
||||
Test to execute a wheel module on the master
|
||||
"""
|
||||
name = "state"
|
||||
|
||||
expected = {
|
||||
"changes": {"return": True},
|
||||
"name": "state",
|
||||
"result": True,
|
||||
"comment": "Wheel function 'state' executed.",
|
||||
}
|
||||
with patch.dict(
|
||||
saltmod.__salt__, {"saltutil.wheel": MagicMock(return_value={"return": True})}
|
||||
):
|
||||
ret = saltmod.wheel(name)
|
||||
assert ret == expected
|
Loading…
Add table
Reference in a new issue