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
being deprecated.
Removed the usage of wmic to get the disk and iscsi grains for Windows. The wmic
binary is being deprecated.

View file

@ -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

View file

@ -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():