Skip to content

Commit

Permalink
Backport saltstack#52992
Browse files Browse the repository at this point in the history
Use __utils__ for all registry calls
Add tests for other places where __salt__ was replaced with __utils__
  • Loading branch information
twangboy committed Apr 20, 2020
1 parent 5ad847d commit d1ea54a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 7 deletions.
10 changes: 5 additions & 5 deletions salt/modules/win_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def get_pending_computer_name():
salt 'minion-id' system.get_pending_computer_name
"""
current = get_computer_name()
pending = __salt__["reg.read_value"](
pending = __utils__["reg.read_value"](
"HKLM", r"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "NV Hostname"
)["vdata"]
if pending:
Expand Down Expand Up @@ -1386,7 +1386,7 @@ def get_pending_file_rename():
# then a reboot is pending.

for vname in vnames:
reg_ret = __salt__["reg.read_value"]("HKLM", key, vname)
reg_ret = __utils__["reg.read_value"]("HKLM", key, vname)

if reg_ret["success"]:
log.debug("Found key: %s", key)
Expand Down Expand Up @@ -1422,7 +1422,7 @@ def get_pending_servermanager():
# the value data, and since an actual reboot won't be pending in that
# instance, just catch instances where we try unsuccessfully to cast as int.

reg_ret = __salt__["reg.read_value"]("HKLM", key, vname)
reg_ret = __utils__["reg.read_value"]("HKLM", key, vname)

if reg_ret["success"]:
log.debug("Found key: %s", key)
Expand Down Expand Up @@ -1497,7 +1497,7 @@ def set_reboot_required_witnessed():
salt '*' system.set_reboot_required_witnessed
"""
return __salt__["reg.set_value"](
return __utils__["reg.set_value"](
hive="HKLM",
key=MINION_VOLATILE_KEY,
volatile=True,
Expand Down Expand Up @@ -1529,7 +1529,7 @@ def get_reboot_required_witnessed():
salt '*' system.get_reboot_required_witnessed
"""
value_dict = __salt__["reg.read_value"](
value_dict = __utils__["reg.read_value"](
hive="HKLM", key=MINION_VOLATILE_KEY, vname=REBOOT_REQUIRED_NAME
)
return value_dict["vdata"] == 1
Expand Down
108 changes: 106 additions & 2 deletions tests/unit/modules/test_win_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,117 @@ def test_get_pending_computer_name(self):
win_system, "get_computer_name", MagicMock(return_value="salt")
):
reg_mock = MagicMock(return_value={"vdata": "salt"})
with patch.dict(win_system.__salt__, {"reg.read_value": reg_mock}):
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertFalse(win_system.get_pending_computer_name())

reg_mock = MagicMock(return_value={"vdata": "salt_pending"})
with patch.dict(win_system.__salt__, {"reg.read_value": reg_mock}):
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertEqual(win_system.get_pending_computer_name(), "salt_pending")

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_get_pending_file_rename_no_key(self):
"""
Test to get a pending file rename, keys don't exist. Should return False
"""
reg_mock = MagicMock(return_value={"success": False})
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertFalse(win_system.get_pending_file_rename())

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_get_pending_file_rename_key_no_value(self):
"""
Test to get a pending file rename, keys exist but not set. Should return
False
"""
reg_mock = MagicMock(return_value={"success": True, "vdata": "(value not set)"})
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertFalse(win_system.get_pending_file_rename())

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_get_pending_file_rename_key_present_value_set(self):
"""
Test to get a pending file rename, keys exist and value set. Should
return True
"""
reg_mock = MagicMock(return_value={"success": True, "vdata": "something"})
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertTrue(win_system.get_pending_file_rename())

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_get_pending_servermanager_key_not_present(self):
"""
Test to get pending servermanager, key not present. Should return False
"""
reg_mock = MagicMock(return_value={"success": False})
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertFalse(win_system.get_pending_servermanager())

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_get_pending_servermanager_key_present_not_int(self):
"""
Test to get pending servermanager, key present, value not int. Should
return False
"""
reg_mock = MagicMock(return_value={"success": True, "vdata": "(value not set)"})
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertFalse(win_system.get_pending_servermanager())

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_get_pending_servermanager_key_present_int_0(self):
"""
Test to get pending servermanager, key present, value is int 0. Should
return False
"""
reg_mock = MagicMock(return_value={"success": True, "vdata": 0})
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertFalse(win_system.get_pending_servermanager())

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_get_pending_servermanager_key_present_int_1(self):
"""
Test to get pending servermanager, key present, value is int 1. Should
return True
"""
reg_mock = MagicMock(return_value={"success": True, "vdata": 1})
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertTrue(win_system.get_pending_servermanager())

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_get_pending_servermanager_key_not_present(self):
"""
Test to get pending servermanager, key no present. Should return False
"""
reg_mock = MagicMock(return_value={"success": False})
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertFalse(win_system.get_pending_servermanager())

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_set_reboot_required_witnessed(self):
"""
Test set_reboot_required_witnessed, Should return True
"""
reg_mock = MagicMock(return_value=True)
with patch.dict(win_system.__utils__, {"reg.set_value": reg_mock}):
self.assertTrue(win_system.set_reboot_required_witnessed())

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_get_reboot_required_witnessed_false(self):
"""
Test get_reboot_required_witnessed, not witnessed. Should return False
"""
reg_mock = MagicMock(return_value={"success": False, "vdata": None})
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertFalse(win_system.get_reboot_required_witnessed())

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_get_reboot_required_witnessed_true(self):
"""
Test get_reboot_required_witnessed, witnessed. Should return True
"""
reg_mock = MagicMock(return_value={"success": True, "vdata": 1})
with patch.dict(win_system.__utils__, {"reg.read_value": reg_mock}):
self.assertTrue(win_system.get_reboot_required_witnessed())

@skipIf(not win_system.HAS_WIN32NET_MODS, "Missing win32 libraries")
def test_get_computer_name(self):
"""
Expand Down

0 comments on commit d1ea54a

Please sign in to comment.