Skip to content

Commit 388a0be

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

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

virttest/utils_misc.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2336,6 +2336,19 @@ def __get_unit_index(M):
23362336
return ("%.20f" % data_size).rstrip("0")
23372337

23382338

2339+
def is_wmic_available(session):
2340+
"""
2341+
Check if 'wmic' is available on the Windows guest.
2342+
:param session: Windows guest session
2343+
:return: True if wmic can be executed, False otherwise
2344+
"""
2345+
try:
2346+
status, _ = session.cmd_status_output("wmic /?", timeout=10)
2347+
return status == 0
2348+
except Exception:
2349+
return False
2350+
2351+
23392352
def get_free_disk(session, mount):
23402353
"""
23412354
Get FreeSpace for given mount point.
@@ -2474,7 +2487,14 @@ def get_win_disk_vol(session, condition="VolumeName='WIN_UTILS'"):
24742487
24752488
:return: volume ID.
24762489
"""
2477-
cmd = "wmic logicaldisk where (%s) get DeviceID" % condition
2490+
if is_wmic_available(session):
2491+
cmd = "wmic logicaldisk where (%s) get DeviceID" % condition
2492+
else:
2493+
cmd = (
2494+
"PowerShell -NoProfile -Command "
2495+
'"(Get-CimInstance Win32_LogicalDisk | Where-Object { $_.%s }).DeviceID"'
2496+
% condition.replace("=", "-eq")
2497+
)
24782498
output = session.cmd(cmd, timeout=120)
24792499
device = re.search(r"(\w):", output, re.M)
24802500
if not device:

virttest/utils_net.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3952,7 +3952,14 @@ def get_windows_nic_attribute(
39523952
:param target: which nic attribute you want to get.
39533953
39543954
"""
3955-
cmd = 'wmic %s where %s="%s" get %s' % (global_switch, key, value, target)
3955+
if utils_misc.is_wmic_available(session):
3956+
cmd = 'wmic %s where %s="%s" get %s' % (global_switch, key, value, target)
3957+
else:
3958+
value_ps = value.replace(":", "-").upper()
3959+
cmd = (
3960+
"PowerShell -NoProfile -Command "
3961+
"\"(Get-NetAdapter | Where-Object {{$_.{key} -eq '{value}'}}).Name\""
3962+
).format(key=key, value=value_ps)
39563963
status, out = session.cmd_status_output(cmd, timeout=timeout)
39573964
if status != 0:
39583965
err_msg = "Execute guest shell command('%s') " "failed with error: '%s'" % (
@@ -3961,8 +3968,9 @@ def get_windows_nic_attribute(
39613968
)
39623969
raise exceptions.TestError(err_msg)
39633970
lines = [l.strip() for l in out.splitlines() if l.strip()]
3964-
# First line is header, return second line
3965-
return lines[1]
3971+
# WMIC output has a header (value is in lines[1])
3972+
# PowerShell output has no header, so return out.strip()
3973+
return lines[1] if len(lines) > 1 else out.strip()
39663974

39673975

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

0 commit comments

Comments
 (0)