Skip to content

Commit

Permalink
Add tests and a changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
MKLeb authored and Megan Wilhite committed Nov 18, 2022
1 parent 700274b commit ad2b54e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/62131.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ignore some command return codes in openbsdrcctl_service to prevent spurious errors
77 changes: 77 additions & 0 deletions tests/pytests/unit/modules/test_openbsdrcctl_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import pytest

import salt.modules.openbsdrcctl_service as openbsdrcctl
from tests.support.mock import MagicMock, patch


@pytest.fixture(autouse=True)
def rcctl():
cmd = "rcctl"
with patch.object(openbsdrcctl, "_cmd", return_value=cmd):
yield cmd


@pytest.fixture
def retcode_mock():
return MagicMock()


@pytest.fixture
def configure_loader_modules(retcode_mock):
return {
openbsdrcctl: {
"__salt__": {"cmd.retcode": retcode_mock},
},
}


def test_available(retcode_mock, rcctl):
retcode_mock.return_value = 0
assert openbsdrcctl.available("test") is True
retcode_mock.assert_called_with("{} get test".format(rcctl), ignore_retcode=True)
retcode_mock.return_value = 2
assert openbsdrcctl.available("test") is False
retcode_mock.assert_called_with("{} get test".format(rcctl), ignore_retcode=True)


def test_status(retcode_mock, rcctl):
retcode_mock.return_value = 0
assert openbsdrcctl.status("test") is True
retcode_mock.assert_called_with("{} check test".format(rcctl), ignore_retcode=True)
retcode_mock.return_value = 2
assert openbsdrcctl.status("test") is False
retcode_mock.assert_called_with("{} check test".format(rcctl), ignore_retcode=True)


def test_disabled(retcode_mock, rcctl):
retcode_mock.return_value = 0
assert openbsdrcctl.disabled("test") is False
retcode_mock.assert_called_with(
"{} get test status".format(rcctl), ignore_retcode=True
)
retcode_mock.return_value = 2
assert openbsdrcctl.disabled("test") is True
retcode_mock.assert_called_with(
"{} get test status".format(rcctl), ignore_retcode=True
)


def test_enabled(retcode_mock, rcctl):
retcode_mock.return_value = 0
flags_return = {"flag1": "value1"}
stdout_mock = MagicMock(return_value=flags_return)
salt_mock = {
"cmd.run_stdout": stdout_mock,
"config.option": MagicMock(),
}
with patch.dict(openbsdrcctl.__salt__, salt_mock):
assert openbsdrcctl.enabled("test", flags=flags_return) is True
retcode_mock.assert_called_with(
"{} get test status".format(rcctl), ignore_retcode=True
)
retcode_mock.return_value = 2
stdout_mock.reset_mock()
assert openbsdrcctl.enabled("test") is False
retcode_mock.assert_called_with(
"{} get test status".format(rcctl), ignore_retcode=True
)

0 comments on commit ad2b54e

Please sign in to comment.