Skip to content

Commit b3c860f

Browse files
committed
utils_net/utils_misc: add PowerShell support for windows nic attribute
Signed-off-by: Wenli Quan <wquan@redhat.com>
1 parent c08ecce commit b3c860f

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

virttest/utils_misc.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2343,6 +2343,20 @@ def __get_unit_index(M):
23432343
return ("%.20f" % data_size).rstrip("0")
23442344

23452345

2346+
def is_wmic_available(session):
2347+
"""
2348+
Check if 'wmic' is available on the Windows guest.
2349+
:param session: Windows guest session
2350+
:return: True if wmic can be executed, False otherwise
2351+
"""
2352+
try:
2353+
status, _ = session.cmd_status_output("wmic /?", timeout=10)
2354+
except (aexpect.ShellError, aexpect.ShellTimeoutError):
2355+
return False
2356+
else:
2357+
return status == 0
2358+
2359+
23462360
def get_free_disk(session, mount):
23472361
"""
23482362
Get FreeSpace for given mount point.
@@ -2481,7 +2495,14 @@ def get_win_disk_vol(session, condition="VolumeName='WIN_UTILS'"):
24812495
24822496
:return: volume ID.
24832497
"""
2484-
cmd = "wmic logicaldisk where (%s) get DeviceID" % condition
2498+
if is_wmic_available(session):
2499+
cmd = "wmic logicaldisk where (%s) get DeviceID" % condition
2500+
else:
2501+
cmd = (
2502+
"PowerShell -NoProfile -Command "
2503+
'"(Get-CimInstance Win32_LogicalDisk | Where-Object { $_.%s }).DeviceID"'
2504+
% condition.replace("=", " -eq ")
2505+
)
24852506
output = session.cmd(cmd, timeout=120)
24862507
device = re.search(r"(\w):", output, re.M)
24872508
if not device:

virttest/utils_net.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,7 +3981,25 @@ def get_windows_nic_attribute(
39813981
:param target: which nic attribute you want to get.
39823982
39833983
"""
3984-
cmd = 'wmic %s where %s="%s" get %s' % (global_switch, key, value, target)
3984+
if utils_misc.is_wmic_available(session):
3985+
cmd = 'wmic %s where %s="%s" get %s' % (global_switch, key, value, target)
3986+
else:
3987+
value_ps = value
3988+
if value and key and key.lower() in {"macaddress", "mac"}:
3989+
value_ps = value.replace(":", "-").upper()
3990+
if global_switch == "nic":
3991+
wmi_class = "Win32_NetworkAdapter"
3992+
elif global_switch == "nicconfig":
3993+
wmi_class = "Win32_NetworkAdapterConfiguration"
3994+
else:
3995+
raise exceptions.TestError(
3996+
"Unsupported global_switch '%s' for PowerShell fallback"% global_switch
3997+
)
3998+
cmd = (
3999+
"PowerShell -NoProfile -Command "
4000+
'"(Get-CimInstance {wmi_class} | '
4001+
"Where-Object {{$_.{key} -eq '{value}'}}).{target}\""
4002+
).format(wmi_class=wmi_class, key=key, value=value_ps, target=target)
39854003
status, out = session.cmd_status_output(cmd, timeout=timeout)
39864004
if status != 0:
39874005
err_msg = "Execute guest shell command('%s') " "failed with error: '%s'" % (
@@ -3990,8 +4008,9 @@ def get_windows_nic_attribute(
39904008
)
39914009
raise exceptions.TestError(err_msg)
39924010
lines = [l.strip() for l in out.splitlines() if l.strip()]
3993-
# First line is header, return second line
3994-
return lines[1]
4011+
# WMIC output has a header (value is in lines[1])
4012+
# PowerShell output has no header, so return out.strip()
4013+
return lines[1] if len(lines) > 1 else out.strip()
39954014

39964015

39974016
def set_win_guest_nic_status(session, connection_id, status, timeout=240):

0 commit comments

Comments
 (0)