mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
port 63880 to 3006
This commit is contained in:
parent
8901ff57d9
commit
ecc6230bf6
3 changed files with 46 additions and 6 deletions
1
changelog/63879.fixed.md
Normal file
1
changelog/63879.fixed.md
Normal file
|
@ -0,0 +1 @@
|
|||
remove eval and update logging to be more informative on bad config
|
|
@ -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
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ import pytest
|
|||
|
||||
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
|
||||
|
@ -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,
|
||||
}
|
||||
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}
|
||||
|
||||
mock.return_value = {
|
||||
"days": 2,
|
||||
"seconds": 249719,
|
||||
"since_iso": "2023-02-27T06:19:01.590002",
|
||||
"since_t": 1677478741,
|
||||
"time": "21:21",
|
||||
"users": 2,
|
||||
}
|
||||
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() == {"days": "UNKNOWN"}
|
||||
|
|
Loading…
Add table
Reference in a new issue