mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Only catch error when value not found
This commit is contained in:
parent
2368341eca
commit
da2bf5ccd4
2 changed files with 86 additions and 31 deletions
|
@ -113,7 +113,9 @@ def _get_account_policy_data_value(name, key):
|
|||
ret = salt.utils.mac_utils.execute_return_result(cmd)
|
||||
except CommandExecutionError as exc:
|
||||
if "eDSUnknownNodeName" in exc.strerror:
|
||||
raise CommandExecutionError("User not found: {}".format(name))
|
||||
raise CommandExecutionError(f"User not found: {name}")
|
||||
if "eDSUnknownMatchType" in exc.strerror:
|
||||
raise CommandExecutionError(f"Value not found: {key}")
|
||||
raise CommandExecutionError("Unknown error: {}".format(exc.strerror))
|
||||
|
||||
return ret
|
||||
|
@ -204,12 +206,14 @@ def get_account_created(name):
|
|||
"""
|
||||
try:
|
||||
ret = _get_account_policy_data_value(name, "creationTime")
|
||||
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
|
||||
date_text = _convert_to_datetime(unix_timestamp)
|
||||
except CommandExecutionError:
|
||||
date_text = "0"
|
||||
except CommandExecutionError as exc:
|
||||
if "Value not found" in exc.message:
|
||||
return 0
|
||||
else:
|
||||
raise
|
||||
|
||||
return date_text
|
||||
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
|
||||
return _convert_to_datetime(unix_timestamp)
|
||||
|
||||
|
||||
def get_last_change(name):
|
||||
|
@ -231,12 +235,14 @@ def get_last_change(name):
|
|||
"""
|
||||
try:
|
||||
ret = _get_account_policy_data_value(name, "passwordLastSetTime")
|
||||
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
|
||||
date_text = _convert_to_datetime(unix_timestamp)
|
||||
except CommandExecutionError:
|
||||
date_text = "0"
|
||||
except CommandExecutionError as exc:
|
||||
if "Value not found" in exc.message:
|
||||
return 0
|
||||
else:
|
||||
raise
|
||||
|
||||
return date_text
|
||||
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
|
||||
return _convert_to_datetime(unix_timestamp)
|
||||
|
||||
|
||||
def get_login_failed_count(name):
|
||||
|
@ -258,9 +264,12 @@ def get_login_failed_count(name):
|
|||
"""
|
||||
try:
|
||||
ret = _get_account_policy_data_value(name, "failedLoginCount")
|
||||
return salt.utils.mac_utils.parse_return(ret)
|
||||
except CommandExecutionError:
|
||||
return "0"
|
||||
except CommandExecutionError as exc:
|
||||
if "Value not found" in exc.message:
|
||||
return 0
|
||||
else:
|
||||
raise
|
||||
return salt.utils.mac_utils.parse_return(ret)
|
||||
|
||||
|
||||
def get_login_failed_last(name):
|
||||
|
@ -283,12 +292,14 @@ def get_login_failed_last(name):
|
|||
"""
|
||||
try:
|
||||
ret = _get_account_policy_data_value(name, "failedLoginTimestamp")
|
||||
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
|
||||
date_text = _convert_to_datetime(unix_timestamp)
|
||||
except CommandExecutionError:
|
||||
date_text = "0"
|
||||
except CommandExecutionError as exc:
|
||||
if "Value not found" in exc.message:
|
||||
return 0
|
||||
else:
|
||||
raise
|
||||
|
||||
return date_text
|
||||
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
|
||||
return _convert_to_datetime(unix_timestamp)
|
||||
|
||||
|
||||
def set_maxdays(name, days):
|
||||
|
|
|
@ -20,15 +20,26 @@ def test_get_account_created():
|
|||
assert result == expected
|
||||
|
||||
|
||||
def test_get_account_created_error():
|
||||
def test_get_account_created_no_value():
|
||||
with patch.object(
|
||||
mac_shadow, "_get_account_policy_data_value", side_effect=CommandExecutionError
|
||||
mac_shadow,
|
||||
"_get_account_policy_data_value",
|
||||
side_effect=CommandExecutionError("Value not found: creationTime"),
|
||||
):
|
||||
result = mac_shadow.get_account_created("junk")
|
||||
expected = "0"
|
||||
expected = 0
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_get_account_created_error():
|
||||
with patch.object(
|
||||
mac_shadow,
|
||||
"_get_account_policy_data_value",
|
||||
side_effect=CommandExecutionError("Unknown error: something happened"),
|
||||
), pytest.raises(CommandExecutionError):
|
||||
mac_shadow.get_account_created("junk")
|
||||
|
||||
|
||||
def test_get_last_change():
|
||||
with patch.object(mac_shadow, "_get_account_policy_data_value", return_value="0"):
|
||||
result = mac_shadow.get_last_change("junk")
|
||||
|
@ -36,15 +47,26 @@ def test_get_last_change():
|
|||
assert result == expected
|
||||
|
||||
|
||||
def test_get_last_change_error():
|
||||
def test_get_last_change_no_value():
|
||||
with patch.object(
|
||||
mac_shadow, "_get_account_policy_data_value", side_effect=CommandExecutionError
|
||||
mac_shadow,
|
||||
"_get_account_policy_data_value",
|
||||
side_effect=CommandExecutionError("Value not found: creationTime"),
|
||||
):
|
||||
result = mac_shadow.get_last_change("junk")
|
||||
expected = "0"
|
||||
expected = 0
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_get_last_change_error():
|
||||
with patch.object(
|
||||
mac_shadow,
|
||||
"_get_account_policy_data_value",
|
||||
side_effect=CommandExecutionError("Unknown error: something happened"),
|
||||
), pytest.raises(CommandExecutionError):
|
||||
mac_shadow.get_last_change("junk")
|
||||
|
||||
|
||||
def test_login_failed_count():
|
||||
with patch.object(mac_shadow, "_get_account_policy_data_value", return_value="0"):
|
||||
result = mac_shadow.get_login_failed_count("junk")
|
||||
|
@ -52,15 +74,26 @@ def test_login_failed_count():
|
|||
assert result == expected
|
||||
|
||||
|
||||
def test_get_login_failed_count_error():
|
||||
def test_get_login_failed_count_no_value():
|
||||
with patch.object(
|
||||
mac_shadow, "_get_account_policy_data_value", side_effect=CommandExecutionError
|
||||
mac_shadow,
|
||||
"_get_account_policy_data_value",
|
||||
side_effect=CommandExecutionError("Value not found: creationTime"),
|
||||
):
|
||||
result = mac_shadow.get_login_failed_count("junk")
|
||||
expected = "0"
|
||||
expected = 0
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_get_login_failed_count_error():
|
||||
with patch.object(
|
||||
mac_shadow,
|
||||
"_get_account_policy_data_value",
|
||||
side_effect=CommandExecutionError("Unknown error: something happened"),
|
||||
), pytest.raises(CommandExecutionError):
|
||||
mac_shadow.get_login_failed_count("junk")
|
||||
|
||||
|
||||
def test_login_failed_last():
|
||||
with patch.object(mac_shadow, "_get_account_policy_data_value", return_value="0"):
|
||||
result = mac_shadow.get_login_failed_last("junk")
|
||||
|
@ -68,10 +101,21 @@ def test_login_failed_last():
|
|||
assert result == expected
|
||||
|
||||
|
||||
def test_get_login_failed_last_error():
|
||||
def test_get_login_failed_last_no_value():
|
||||
with patch.object(
|
||||
mac_shadow, "_get_account_policy_data_value", side_effect=CommandExecutionError
|
||||
mac_shadow,
|
||||
"_get_account_policy_data_value",
|
||||
side_effect=CommandExecutionError("Value not found: creationTime"),
|
||||
):
|
||||
result = mac_shadow.get_login_failed_last("junk")
|
||||
expected = "0"
|
||||
expected = 0
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_get_login_failed_last_error():
|
||||
with patch.object(
|
||||
mac_shadow,
|
||||
"_get_account_policy_data_value",
|
||||
side_effect=CommandExecutionError("Unknown error: something happened"),
|
||||
), pytest.raises(CommandExecutionError):
|
||||
mac_shadow.get_login_failed_last("junk")
|
||||
|
|
Loading…
Add table
Reference in a new issue