Merge pull request #56759 from s0undt3ch/port-to-master/49146

Port #49146 to master
This commit is contained in:
Daniel Wozniak 2020-04-21 17:25:38 -07:00 committed by GitHub
commit 14be8e5c2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 12 deletions

View file

@ -22,15 +22,17 @@ log = logging.getLogger(__name__)
def __virtual__():
"""
Only works on OpenBSD for now; other systems with pf (macOS, FreeBSD, etc)
need to be tested before enabling them.
Only works on OpenBSD and FreeBSD for now; other systems with pf (macOS,
FreeBSD, etc) need to be tested before enabling them.
"""
if __grains__["os"] == "OpenBSD" and salt.utils.path.which("pfctl"):
tested_oses = ["FreeBSD", "OpenBSD"]
if __grains__["os"] in tested_oses and salt.utils.path.which("pfctl"):
return True
return (
False,
"The pf execution module cannot be loaded: either the system is not OpenBSD or the pfctl binary was not found",
"The pf execution module cannot be loaded: either the OS ({}) is not "
"tested or the pfctl binary was not found".format(__grains__["os"]),
)
@ -102,7 +104,7 @@ def loglevel(level):
level:
Log level. Should be one of the following: emerg, alert, crit, err, warning, notice,
info or debug.
info or debug (OpenBSD); or none, urgent, misc, loud (FreeBSD).
CLI example:
@ -114,7 +116,20 @@ def loglevel(level):
# always made a change.
ret = {"changes": True}
all_levels = ["emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"]
myos = __grains__["os"]
if myos == "FreeBSD":
all_levels = ["none", "urgent", "misc", "loud"]
else:
all_levels = [
"emerg",
"alert",
"crit",
"err",
"warning",
"notice",
"info",
"debug",
]
if level not in all_levels:
raise SaltInvocationError("Unknown loglevel: {0}".format(level))

View file

@ -1,12 +1,8 @@
# -*- coding: utf-8 -*-
# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
# Import Salt Libs
import salt.modules.pf as pf
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
@ -64,14 +60,32 @@ class PfTestCase(TestCase, LoaderModuleMockMixin):
with patch.dict(pf.__salt__, {"cmd.run_all": mock_cmd}):
self.assertFalse(pf.disable()["changes"])
def test_loglevel(self):
def test_loglevel_freebsd(self):
"""
Tests setting a loglevel.
"""
ret = {}
ret["retcode"] = 0
mock_cmd = MagicMock(return_value=ret)
with patch.dict(pf.__salt__, {"cmd.run_all": mock_cmd}):
with patch.dict(pf.__salt__, {"cmd.run_all": mock_cmd}), patch.dict(
pf.__grains__, {"os": "FreeBSD"}
):
res = pf.loglevel("urgent")
mock_cmd.assert_called_once_with(
"pfctl -x urgent", output_loglevel="trace", python_shell=False
)
self.assertTrue(res["changes"])
def test_loglevel_openbsd(self):
"""
Tests setting a loglevel.
"""
ret = {}
ret["retcode"] = 0
mock_cmd = MagicMock(return_value=ret)
with patch.dict(pf.__salt__, {"cmd.run_all": mock_cmd}), patch.dict(
pf.__grains__, {"os": "OpenBSD"}
):
res = pf.loglevel("crit")
mock_cmd.assert_called_once_with(
"pfctl -x crit", output_loglevel="trace", python_shell=False