Skip to content

Commit

Permalink
Remove wmic from iscsi grains
Browse files Browse the repository at this point in the history
  • Loading branch information
twangboy authored and dwoz committed Oct 17, 2024
1 parent 6caf16d commit 98f49eb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
4 changes: 2 additions & 2 deletions changelog/66959.fixed.md
Original file line number Diff line number Diff line change
@@ -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.
29 changes: 13 additions & 16 deletions salt/grains/iscsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 33 additions & 10 deletions tests/pytests/unit/grains/test_iscsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,39 @@
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()

assert _grains.get("iscsi_iqn") == ["iqn.1991-05.com.microsoft:simon-x1"]
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


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():
Expand Down

0 comments on commit 98f49eb

Please sign in to comment.