From d1ea54ace81d658e6e6014cdaf9a2e6b7e8cfaab Mon Sep 17 00:00:00 2001 From: twangboy Date: Mon, 20 Apr 2020 10:59:11 -0600 Subject: [PATCH] Backport #52992 Use __utils__ for all registry calls Add tests for other places where __salt__ was replaced with __utils__ --- salt/modules/win_system.py | 10 +-- tests/unit/modules/test_win_system.py | 108 +++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 7 deletions(-) diff --git a/salt/modules/win_system.py b/salt/modules/win_system.py index a3c0218ec975..6f1d6248fb9b 100644 --- a/salt/modules/win_system.py +++ b/salt/modules/win_system.py @@ -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: @@ -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) @@ -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) @@ -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, @@ -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 diff --git a/tests/unit/modules/test_win_system.py b/tests/unit/modules/test_win_system.py index d1f19350fb2c..72c35dbe21e7 100644 --- a/tests/unit/modules/test_win_system.py +++ b/tests/unit/modules/test_win_system.py @@ -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): """