Remove wmic from iscsi grains

This commit is contained in:
twangboy 2024-10-15 11:10:51 -06:00 committed by Daniel Wozniak
parent 6caf16d439
commit 98f49eba4e
3 changed files with 47 additions and 27 deletions

View file

@ -1,2 +1,2 @@
Removed the usage of wmic to get the disk grains for Windows. The wmic binary is Removed the usage of wmic to get the disk and iscsi grains for Windows. The wmic
being deprecated. binary is being deprecated.

View file

@ -85,28 +85,25 @@ def _aix_iqn():
def _windows_iqn(): def _windows_iqn():
""" """
Return iSCSI IQN from a Windows host. Return iSCSI nodes from a Windows host.
""" """
cmd = "Get-InitiatorPort | Select NodeAddress"
ret = [] 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 return ret
namespace = r"\\root\WMI" # A single node will return a dictionary with a single entry
path = "MSiSCSIInitiator_MethodClass" # {"NodeAddress": "iqn.1991-05.com.microsoft:johnj99-pc2.contoso.com"}
get = "iSCSINodeName" # 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( for node in nodes:
"{} /namespace:{} path {} get {} /format:table".format( ret.append(node["NodeAddress"])
wmic, namespace, path, get
)
)
for line in cmd_ret["stdout"].splitlines():
if line.startswith("iqn."):
line = line.rstrip()
ret.append(line.rstrip())
return ret return ret

View file

@ -9,16 +9,39 @@ import salt.grains.iscsi as iscsi
from tests.support.mock import MagicMock, mock_open, patch from tests.support.mock import MagicMock, mock_open, patch
def test_windows_iscsi_iqn_grains(): def test_windows_iscsi_iqn_grains_empty():
cmd_run_mock = MagicMock( nodes_dict = {}
return_value={"stdout": "iSCSINodeName\niqn.1991-05.com.microsoft:simon-x1\n"} cmd_powershell_mock = MagicMock(return_value=nodes_dict)
) with patch("salt.modules.cmdmod.powershell", cmd_powershell_mock):
_grains = {} result = iscsi._windows_iqn()
with patch("salt.utils.path.which", MagicMock(return_value=True)): expected = []
with patch("salt.modules.cmdmod.run_all", cmd_run_mock): assert result == expected
_grains["iscsi_iqn"] = iscsi._windows_iqn()
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(): def test_aix_iscsi_iqn_grains():