mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fix mac_shadow module
Fixes an issue with the mac_shadow module where it would fail to retrieve values that weren't set yet... last login for example. Also adds some tests and a changelog
This commit is contained in:
parent
4157fe7ed0
commit
2368341eca
3 changed files with 103 additions and 18 deletions
3
changelog/34658.fixed.md
Normal file
3
changelog/34658.fixed.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
Fix an issue with mac_shadow that was causing a command execution error when
|
||||
retrieving values that were not yet set. For example, retrieving last login
|
||||
before the user had logged in.
|
|
@ -202,11 +202,12 @@ def get_account_created(name):
|
|||
|
||||
salt '*' shadow.get_account_created admin
|
||||
"""
|
||||
ret = _get_account_policy_data_value(name, "creationTime")
|
||||
|
||||
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
|
||||
|
||||
date_text = _convert_to_datetime(unix_timestamp)
|
||||
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"
|
||||
|
||||
return date_text
|
||||
|
||||
|
@ -228,11 +229,12 @@ def get_last_change(name):
|
|||
|
||||
salt '*' shadow.get_last_change admin
|
||||
"""
|
||||
ret = _get_account_policy_data_value(name, "passwordLastSetTime")
|
||||
|
||||
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
|
||||
|
||||
date_text = _convert_to_datetime(unix_timestamp)
|
||||
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"
|
||||
|
||||
return date_text
|
||||
|
||||
|
@ -254,9 +256,11 @@ def get_login_failed_count(name):
|
|||
|
||||
salt '*' shadow.get_login_failed_count admin
|
||||
"""
|
||||
ret = _get_account_policy_data_value(name, "failedLoginCount")
|
||||
|
||||
return salt.utils.mac_utils.parse_return(ret)
|
||||
try:
|
||||
ret = _get_account_policy_data_value(name, "failedLoginCount")
|
||||
return salt.utils.mac_utils.parse_return(ret)
|
||||
except CommandExecutionError:
|
||||
return "0"
|
||||
|
||||
|
||||
def get_login_failed_last(name):
|
||||
|
@ -277,11 +281,12 @@ def get_login_failed_last(name):
|
|||
|
||||
salt '*' shadow.get_login_failed_last admin
|
||||
"""
|
||||
ret = _get_account_policy_data_value(name, "failedLoginTimestamp")
|
||||
|
||||
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
|
||||
|
||||
date_text = _convert_to_datetime(unix_timestamp)
|
||||
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"
|
||||
|
||||
return date_text
|
||||
|
||||
|
|
77
tests/pytests/unit/modules/test_mac_shadow.py
Normal file
77
tests/pytests/unit/modules/test_mac_shadow.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
"""
|
||||
Unit Tests for the mac_desktop execution module.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.modules.mac_shadow as mac_shadow
|
||||
from salt.exceptions import CommandExecutionError
|
||||
from tests.support.mock import patch
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.skip_unless_on_darwin,
|
||||
]
|
||||
|
||||
|
||||
def test_get_account_created():
|
||||
with patch.object(mac_shadow, "_get_account_policy_data_value", return_value="0"):
|
||||
result = mac_shadow.get_account_created("junk")
|
||||
expected = "1969-12-31 17:00:00"
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_get_account_created_error():
|
||||
with patch.object(
|
||||
mac_shadow, "_get_account_policy_data_value", side_effect=CommandExecutionError
|
||||
):
|
||||
result = mac_shadow.get_account_created("junk")
|
||||
expected = "0"
|
||||
assert result == expected
|
||||
|
||||
|
||||
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")
|
||||
expected = "1969-12-31 17:00:00"
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_get_last_change_error():
|
||||
with patch.object(
|
||||
mac_shadow, "_get_account_policy_data_value", side_effect=CommandExecutionError
|
||||
):
|
||||
result = mac_shadow.get_last_change("junk")
|
||||
expected = "0"
|
||||
assert result == expected
|
||||
|
||||
|
||||
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")
|
||||
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
|
||||
):
|
||||
result = mac_shadow.get_login_failed_count("junk")
|
||||
expected = "0"
|
||||
assert result == expected
|
||||
|
||||
|
||||
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")
|
||||
expected = "1969-12-31 17:00:00"
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_get_login_failed_last_error():
|
||||
with patch.object(
|
||||
mac_shadow, "_get_account_policy_data_value", side_effect=CommandExecutionError
|
||||
):
|
||||
result = mac_shadow.get_login_failed_last("junk")
|
||||
expected = "0"
|
||||
assert result == expected
|
Loading…
Add table
Reference in a new issue