Skip to content

Commit

Permalink
Running pre-commit. Updating tests to reflect changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgreenaway committed Sep 22, 2022
1 parent ce1de2d commit 66c348d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
5 changes: 3 additions & 2 deletions salt/modules/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ def __init__(self):
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"]
Expand Down
60 changes: 40 additions & 20 deletions tests/pytests/unit/modules/test_puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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"
Expand Down Expand Up @@ -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}

Expand All @@ -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() == ""
Expand Down

0 comments on commit 66c348d

Please sign in to comment.