From 98f49eba4e1e9b4f7c0a6ff95c9f841b2b7f2e32 Mon Sep 17 00:00:00 2001 From: twangboy Date: Tue, 15 Oct 2024 11:10:51 -0600 Subject: [PATCH] Remove wmic from iscsi grains --- changelog/66959.fixed.md | 4 +-- salt/grains/iscsi.py | 29 ++++++++--------- tests/pytests/unit/grains/test_iscsi.py | 41 +++++++++++++++++++------ 3 files changed, 47 insertions(+), 27 deletions(-) diff --git a/changelog/66959.fixed.md b/changelog/66959.fixed.md index ad9878c2577..69e40d66479 100644 --- a/changelog/66959.fixed.md +++ b/changelog/66959.fixed.md @@ -1,2 +1,2 @@ -Removed the usage of wmic to get the disk grains for Windows. The wmic binary is -being deprecated. +Removed the usage of wmic to get the disk and iscsi grains for Windows. The wmic +binary is being deprecated. diff --git a/salt/grains/iscsi.py b/salt/grains/iscsi.py index 64199d6e99d..62c4eccc719 100644 --- a/salt/grains/iscsi.py +++ b/salt/grains/iscsi.py @@ -85,28 +85,25 @@ def _aix_iqn(): def _windows_iqn(): """ - Return iSCSI IQN from a Windows host. + Return iSCSI nodes from a Windows host. """ + cmd = "Get-InitiatorPort | Select NodeAddress" ret = [] - wmic = salt.utils.path.which("wmic") + nodes = salt.modules.cmdmod.powershell(cmd) - if not wmic: + if not nodes: + log.trace("No iSCSI nodes found") return ret - namespace = r"\\root\WMI" - path = "MSiSCSIInitiator_MethodClass" - get = "iSCSINodeName" + # A single node will return a dictionary with a single entry + # {"NodeAddress": "iqn.1991-05.com.microsoft:johnj99-pc2.contoso.com"} + # Multiple nodes will return a list of single entry dicts + # We need a list of dict + if isinstance(nodes, dict): + nodes = [nodes] - cmd_ret = salt.modules.cmdmod.run_all( - "{} /namespace:{} path {} get {} /format:table".format( - wmic, namespace, path, get - ) - ) - - for line in cmd_ret["stdout"].splitlines(): - if line.startswith("iqn."): - line = line.rstrip() - ret.append(line.rstrip()) + for node in nodes: + ret.append(node["NodeAddress"]) return ret diff --git a/tests/pytests/unit/grains/test_iscsi.py b/tests/pytests/unit/grains/test_iscsi.py index 006c1bb4e8b..615416c19d0 100644 --- a/tests/pytests/unit/grains/test_iscsi.py +++ b/tests/pytests/unit/grains/test_iscsi.py @@ -9,16 +9,39 @@ import salt.grains.iscsi as iscsi from tests.support.mock import MagicMock, mock_open, patch -def test_windows_iscsi_iqn_grains(): - cmd_run_mock = MagicMock( - return_value={"stdout": "iSCSINodeName\niqn.1991-05.com.microsoft:simon-x1\n"} - ) - _grains = {} - with patch("salt.utils.path.which", MagicMock(return_value=True)): - with patch("salt.modules.cmdmod.run_all", cmd_run_mock): - _grains["iscsi_iqn"] = iscsi._windows_iqn() +def test_windows_iscsi_iqn_grains_empty(): + nodes_dict = {} + cmd_powershell_mock = MagicMock(return_value=nodes_dict) + with patch("salt.modules.cmdmod.powershell", cmd_powershell_mock): + result = iscsi._windows_iqn() + expected = [] + assert result == expected - assert _grains.get("iscsi_iqn") == ["iqn.1991-05.com.microsoft:simon-x1"] + +def test_windows_iscsi_iqn_grains_single(): + nodes_dict = {"NodeAddress": "iqn.1991-05.com.microsoft:simon-x1"} + cmd_powershell_mock = MagicMock(return_value=nodes_dict) + with patch("salt.modules.cmdmod.powershell", cmd_powershell_mock): + result = iscsi._windows_iqn() + expected = ["iqn.1991-05.com.microsoft:simon-x1"] + assert result == expected + + +def test_windows_iscsi_iqn_grains_multiple(): + nodes_list = [ + {"NodeAddress": "iqn.1991-05.com.microsoft:simon-x1"}, + {"NodeAddress": "iqn.1991-05.com.microsoft:simon-x2"}, + {"NodeAddress": "iqn.1991-05.com.microsoft:simon-x3"}, + ] + cmd_powershell_mock = MagicMock(return_value=nodes_list) + with patch("salt.modules.cmdmod.powershell", cmd_powershell_mock): + result = iscsi._windows_iqn() + expected = [ + "iqn.1991-05.com.microsoft:simon-x1", + "iqn.1991-05.com.microsoft:simon-x2", + "iqn.1991-05.com.microsoft:simon-x3", + ] + assert result == expected def test_aix_iscsi_iqn_grains():