Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix puppet.py for non-aio installs #62323

Merged
merged 5 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/62323.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes the Puppet module for non-aio Puppet packages for example running the Puppet module on FreeBSD.
28 changes: 7 additions & 21 deletions salt/modules/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import datetime
import logging
import os
from distutils import version # pylint: disable=no-name-in-module

import salt.utils.args
import salt.utils.files
Expand Down Expand Up @@ -64,26 +63,13 @@ def __init__(self):
self.kwargs = {"color": "false"} # e.g. --tags=apache::server
self.args = [] # e.g. --noop

if salt.utils.platform.is_windows():
self.vardir = "C:\\ProgramData\\PuppetLabs\\puppet\\var"
self.rundir = "C:\\ProgramData\\PuppetLabs\\puppet\\run"
self.confdir = "C:\\ProgramData\\PuppetLabs\\puppet\\etc"
else:
self.puppet_version = __salt__["cmd.run"]("puppet --version")
if "Enterprise" in self.puppet_version:
self.vardir = "/var/opt/lib/pe-puppet"
self.rundir = "/var/opt/run/pe-puppet"
self.confdir = "/etc/puppetlabs/puppet"
elif self.puppet_version != [] and version.StrictVersion(
self.puppet_version
) >= version.StrictVersion("4.0.0"):
self.vardir = "/opt/puppetlabs/puppet/cache"
self.rundir = "/var/run/puppetlabs"
self.confdir = "/etc/puppetlabs/puppet"
else:
self.vardir = "/var/lib/puppet"
self.rundir = "/var/run/puppet"
self.confdir = "/etc/puppet"
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"]

self.disabled_lockfile = self.vardir + "/state/agent_disabled.lock"
self.run_lockfile = self.vardir + "/state/agent_catalog_run.lock"
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