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

port 63880 to 3006 #63881

Merged
merged 2 commits into from
Mar 15, 2023
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/63879.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
remove eval and update logging to be more informative on bad config
18 changes: 13 additions & 5 deletions salt/modules/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,19 @@ def custom():
ret = {}
conf = __salt__["config.dot_vals"]("status")
for key, val in conf.items():
func = "{}()".format(key.split(".")[1])
vals = eval(func) # pylint: disable=W0123

for item in val:
ret[item] = vals[item]
func = ".".join(key.split(".")[:-1])
vals = {}
if func != "status.custom":
try:
vals = __salt__[func]()
for item in val:
try:
ret[item] = vals[item]
except KeyError:
log.warning(f"val {item} not in return of {func}")
ret[item] = "UNKNOWN"
except KeyError:
log.warning(f"custom status {func} isn't loaded")

return ret

Expand Down
33 changes: 32 additions & 1 deletion tests/pytests/unit/modules/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import salt.modules.status as status
from salt.exceptions import CommandExecutionError
from tests.support.mock import Mock, patch
from tests.support.mock import MagicMock, Mock, patch


@pytest.fixture
Expand Down Expand Up @@ -42,3 +42,34 @@ def test_boot_time_aix():
with patch.dict(status.__salt__, {"cmd.run_all": mock}):
with pytest.raises(CommandExecutionError):
status._get_boot_time_aix()


def test_custom():
mock = MagicMock()
mock2 = MagicMock()

mock.return_value = {
"days": 2,
"seconds": 249719,
"since_iso": "2023-02-27T06:19:01.590002",
"since_t": 1677478741,
"time": "21:21",
"users": 2,
}
# test pass correct info with correct config
mock2.return_value = {"status.uptime.custom": ["days"]}
with patch.dict(status.__salt__, {"config.dot_vals": mock2}):
with patch.dict(status.__salt__, {"status.uptime": mock}):
assert status.custom() == {"days": 2}

# test pass correct info with incorrect config
mock2.return_value = {"status.fail.custom": ["days"]}
with patch.dict(status.__salt__, {"config.dot_vals": mock2}):
with patch.dict(status.__salt__, {"status.uptime": mock}):
assert status.custom() == {}

# test incorrect info with correct config
mock2.return_value = {"status.uptime.custom": ["day"]}
with patch.dict(status.__salt__, {"config.dot_vals": mock2}):
with patch.dict(status.__salt__, {"status.uptime": mock}):
assert status.custom() == {"day": "UNKNOWN"}