Running pre-commit. Updating tests to reflect changes.

This commit is contained in:
Gareth J. Greenaway 2022-09-22 10:18:11 -07:00
parent ce1de2d7ad
commit 66c348da2c
2 changed files with 43 additions and 22 deletions

View file

@ -63,9 +63,10 @@ class _Puppet:
self.kwargs = {"color": "false"} # e.g. --tags=apache::server
self.args = [] # e.g. --noop
conf = salt.utils.yaml.safe_load(
__salt__["cmd.run"]("puppet config print --render-as yaml vardir rundir confdir")
puppet_config = __salt__["cmd.run"](
"puppet config print --render-as yaml vardir rundir confdir"
)
conf = salt.utils.yaml.safe_load(puppet_config)
self.vardir = conf["vardir"]
self.rundir = conf["rundir"]
self.confdir = conf["confdir"]

View file

@ -19,16 +19,33 @@ def configure_loader_modules():
return {puppet: {}}
def test_run():
@pytest.fixture
def puppet_config():
_puppet_config = """
---
confdir: "/etc/puppet"
rundir: "/var/run/puppetlabs"
vardir: "/var/lib/puppet"
"""
yield _puppet_config
def test_run(puppet_config):
"""
Test to execute a puppet run
"""
mock_empty_lst = []
mock = MagicMock(return_value={"A": "B"})
with patch.object(salt.utils.args, "clean_kwargs", mock):
mock = MagicMock(return_value={"retcode": 0})
mock_lst = MagicMock(return_value=[])
with patch.dict(puppet.__salt__, {"cmd.run_all": mock, "cmd.run": mock_lst}):
assert puppet.run()
cmd_run_all_mock = MagicMock(return_value={"retcode": 0})
cmd_run_mock = MagicMock(side_effect=[puppet_config, mock_empty_lst])
with patch.dict(
puppet.__salt__, {"cmd.run_all": cmd_run_all_mock, "cmd.run": cmd_run_mock}
):
ret = puppet.run()
assert ret
def test_noop():
@ -40,12 +57,15 @@ def test_noop():
assert puppet.noop() == {"stderr": "A", "stdout": "B"}
def test_enable():
def test_enable(puppet_config):
"""
Test to enable the puppet agent
"""
mock_lst = MagicMock(return_value=[])
with patch.dict(puppet.__salt__, {"cmd.run": mock_lst}):
mock_empty_lst = []
cmd_run_mock = MagicMock(
side_effect=[puppet_config, puppet_config, puppet_config, mock_empty_lst]
)
with patch.dict(puppet.__salt__, {"cmd.run": cmd_run_mock}):
mock = MagicMock(return_value=True)
with patch.object(os.path, "isfile", mock):
mock = MagicMock(return_value=True)
@ -57,12 +77,12 @@ def test_enable():
assert not puppet.enable()
def test_disable():
def test_disable(puppet_config):
"""
Test to disable the puppet agent
"""
mock_lst = MagicMock(return_value=[])
with patch.dict(puppet.__salt__, {"cmd.run": mock_lst}):
cmd_run_mock = MagicMock(return_value=puppet_config)
with patch.dict(puppet.__salt__, {"cmd.run": cmd_run_mock}):
mock = MagicMock(side_effect=[True, False])
with patch.object(os.path, "isfile", mock):
assert not puppet.disable()
@ -78,12 +98,12 @@ def test_disable():
pass
def test_status():
def test_status(puppet_config):
"""
Test to display puppet agent status
"""
mock_lst = MagicMock(return_value=[])
with patch.dict(puppet.__salt__, {"cmd.run": mock_lst}):
cmd_run_mock = MagicMock(return_value=puppet_config)
with patch.dict(puppet.__salt__, {"cmd.run": cmd_run_mock}):
mock = MagicMock(side_effect=[True])
with patch.object(os.path, "isfile", mock):
assert puppet.status() == "Administratively disabled"
@ -121,12 +141,12 @@ def test_status():
assert puppet.status() == "Stopped"
def test_summary():
def test_summary(puppet_config):
"""
Test to show a summary of the last puppet agent run
"""
mock_lst = MagicMock(return_value=[])
with patch.dict(puppet.__salt__, {"cmd.run": mock_lst}):
cmd_run_mock = MagicMock(return_value=puppet_config)
with patch.dict(puppet.__salt__, {"cmd.run": cmd_run_mock}):
with patch("salt.utils.files.fopen", mock_open(read_data="resources: 1")):
assert puppet.summary() == {"resources": 1}
@ -137,12 +157,12 @@ def test_summary():
pytest.raises(CommandExecutionError, puppet.summary)
def test_plugin_sync():
def test_plugin_sync(puppet_config):
"""
Test to runs a plugin synch between the puppet master and agent
"""
mock_lst = MagicMock(return_value=[])
with patch.dict(puppet.__salt__, {"cmd.run": mock_lst}):
cmd_run_mock = MagicMock(return_value=puppet_config)
with patch.dict(puppet.__salt__, {"cmd.run": cmd_run_mock}):
mock_lst = MagicMock(side_effect=[False, True])
with patch.dict(puppet.__salt__, {"cmd.run": mock_lst}):
assert puppet.plugin_sync() == ""