Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace WMIC with powershell cmdlet #4242

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions provider/block_devices_plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ def wrapper(self, *args, **kwargs):
step=1.5,
):
disks_info_win = (
"wmic logicaldisk get drivetype,name,description "
"& wmic diskdrive list brief /format:list"
'powershell -command "Get-CimInstance Win32_LogicalDisk | '
"Select-Object DriveType, Name, Description;"
'Get-CimInstance Win32_DiskDrive | Select-Object *"'
)
disks_info_linux = "lsblk -a"
_session = self.vm.wait_for_login(timeout=360)
Expand Down
6 changes: 5 additions & 1 deletion provider/throttle_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ def _get_drive_path(session, params, image):
extra_params = image_params["blk_extra_params"]
serial = re.search(r"(serial|wwn)=(\w+)", extra_params, re.M).group(2)
if os_type == "windows":
cmd = "wmic diskdrive where SerialNumber='%s' get Index,Name"
cmd = (
'powshell "Get-CimInstance Win32_DiskDrive'
" | Where-Object {$_.SerialNumber -eq %s}"
' | Select-Object Index, Name"'
)
disks = session.cmd_output(cmd % serial)
info = disks.splitlines()
if len(info) > 1:
Expand Down
2 changes: 1 addition & 1 deletion provider/win_driver_installer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def install_gagent(session, test, qemu_ga_pkg, gagent_install_cmd, gagent_pkg_in
:param gagent_pkg_info_cmd: guest agent pkg info check command.
"""
LOG_JOB.info("Install 'qemu-guest-agent' package in guest.")
vol_virtio_key = "VolumeName like '%virtio-win%'"
vol_virtio_key = "VolumeName='virtio-win*'"
vol_virtio = utils_misc.get_win_disk_vol(session, vol_virtio_key)
qemu_ga_pkg_path = r"%s:\%s\%s" % (vol_virtio, "guest-agent", qemu_ga_pkg)
gagent_install_cmd = gagent_install_cmd % qemu_ga_pkg_path
Expand Down
36 changes: 27 additions & 9 deletions provider/win_driver_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import aexpect
from virttest import data_dir, error_context, utils_misc, utils_test
from virttest.utils_version import VersionInterval
from virttest.utils_windows import system, virtio_win, wmic
from virttest.utils_windows import system, virtio_win

LOG_JOB = logging.getLogger("avocado.test")

Expand Down Expand Up @@ -60,14 +60,32 @@


def _pnpdrv_info(session, name_pattern, props=None):
"""Get the driver props eg: InfName"""
cmd = wmic.make_query(
"path win32_pnpsigneddriver",
"DeviceName like '%s'" % name_pattern,
props=props,
get_swch=wmic.FMT_TYPE_LIST,
"""
Get the driver props eg: InfName

:param session: The guest session object.
:param name_pattern: String with the device name
:param props: Dict witj the properties list
"""
cmd = (
'powershell "Get-CimInstance Win32_PnPSignedDriver'
" | Where-Object { $_.DeviceName -like '%s' }"
' | Select-Object %s | Format-List *"'
)
return wmic.parse_list(session.cmd(cmd, timeout=QUERY_TIMEOUT))
cmd = cmd % (name_pattern, ",".join(props))
output = session.cmd_output(cmd, timeout=QUERY_TIMEOUT)
out = []
for para in re.split("(?:\r?\n){2,}", output.strip()):
keys, vals = [], []
for line in para.splitlines():
key, val = line.split(":", 1)
keys.append(key.strip())
vals.append(val.strip())
if len(keys) == 1:
out.append(vals[0])
else:
out.append(dict(zip(keys, vals)))
return out


def uninstall_driver(session, test, devcon_path, driver_name, device_name, device_hwid):
Expand Down Expand Up @@ -294,7 +312,7 @@ def copy_file_to_samepath(session, test, params):
"Copy autoit scripts and virtio-win-guest-tools.exe " "to the same path."
)
dst_path = r"C:\\"
vol_virtio_key = "VolumeName like '%virtio-win%'"
vol_virtio_key = "VolumeName='virtio-win*'"
vol_virtio = utils_misc.get_win_disk_vol(session, vol_virtio_key)

installer_path = r"%s:\%s" % (vol_virtio, "virtio-win-guest-tools.exe")
Expand Down
5 changes: 3 additions & 2 deletions qemu/deps/win_driver_install/win_driver_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ def get_current_driver_ver(device_name):
"""
key = r"\d*\.\d*\.\d*\.\d*"
get_driver_ver_cmd = (
"wmic path win32_pnpsigneddriver where"
" Devicename='%s' get driverversion" % device_name
'powershell "Get-CimInstance Win32_PnPSignedDriver'
" | Where-Object {$_.DeviceName -eq %s}"
' | Select-Object DriverVersion"'
)
driver_version = os.popen(get_driver_ver_cmd).read()
if not driver_version.strip():
Expand Down
2 changes: 1 addition & 1 deletion qemu/tests/balloon_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def get_disk_vol(self, session):

:param session: VM session.
"""
key = "VolumeName like 'virtio-win%'"
key = "VolumeName='virtio-win*'"
try:
return utils_misc.get_win_disk_vol(session, condition=key)
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion qemu/tests/qemu_guest_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4383,7 +4383,7 @@ def get_qga_pkg_path(self, qemu_ga_pkg, test, session, params, vm):
vm.copy_files_to(gagent_host_path, self.gagent_guest_dir)
qemu_ga_pkg_path = r"%s\%s" % (self.gagent_guest_dir, qemu_ga_pkg)
elif self.gagent_src_type == "virtio-win":
vol_virtio_key = "VolumeName like '%virtio-win%'"
vol_virtio_key = "VolumeName='%virtio-win*'"
vol_virtio = utils_misc.get_win_disk_vol(session, vol_virtio_key)
qemu_ga_pkg_path = r"%s:\%s\%s" % (vol_virtio, "guest-agent", qemu_ga_pkg)
else:
Expand Down
20 changes: 7 additions & 13 deletions qemu/tests/single_driver_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from aexpect import ShellTimeoutError
from virttest import error_context, utils_misc, utils_net
from virttest.utils_test.qemu import windrv_verify_running
from virttest.utils_windows import virtio_win, wmic
from virttest.utils_windows import virtio_win

from provider import win_driver_utils

LOG_JOB = logging.getLogger("avocado.test")

Expand All @@ -31,16 +33,6 @@ def _add_cert(session, cert_path, store):
session.cmd(add_cmd, timeout=OPERATION_TIMEOUT)


def _pnpdrv_info(session, name_pattern, props=None):
cmd = wmic.make_query(
"path win32_pnpsigneddriver",
"DeviceName like '%s'" % name_pattern,
props=props,
get_swch=wmic.FMT_TYPE_LIST,
)
return wmic.parse_list(session.cmd(cmd, timeout=QUERY_TIMEOUT))


def send_key(vm, key):
# Send key to guest
for i in key:
Expand Down Expand Up @@ -122,7 +114,9 @@ def run(test, params, env):

if params.get("need_uninstall", "no") == "yes":
error_context.context("Uninstalling previous installed driver", test.log.info)
for inf_name in _pnpdrv_info(session, device_name, ["InfName"]):
for inf_name in win_driver_utils._pnpdrv_info(
session, device_name, ["InfName"]
):
pnp_cmd = "pnputil /delete-driver %s /uninstall /force"
uninst_store_cmd = params.get("uninst_store_cmd", pnp_cmd) % inf_name
status, output = session.cmd_status_output(uninst_store_cmd, inst_timeout)
Expand Down Expand Up @@ -197,7 +191,7 @@ def run(test, params, env):
session = vm.reboot(session)
windrv_verify_running(session, test, driver_verifier)

ver_list = _pnpdrv_info(session, device_name, ["DriverVersion"])
ver_list = win_driver_utils._pnpdrv_info(session, device_name, ["DriverVersion"])
if expected_ver not in ver_list:
test.fail(
"The expected driver version is '%s', but "
Expand Down
2 changes: 1 addition & 1 deletion qemu/tests/virtio_win_installer_version_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def run(test, params, env):
# virtio-win-1.9.xx
iso_label_ver = re.findall(ver_pattern, iso_label_name, re.I)[0]

vol_virtio_key = "VolumeName like '%virtio-win%'"
vol_virtio_key = "VolumeName='virtio-win*'"
vol_virtio = utils_misc.get_win_disk_vol(session, vol_virtio_key)
installer_ver = session.cmd_output(params["installer_chk_cmd"] % vol_virtio).strip()
if not pkg_ver == iso_ver == iso_label_ver == installer_ver:
Expand Down
31 changes: 24 additions & 7 deletions qemu/tests/windows_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,40 @@ def run(test, params, env):
error_context.context("Get OS version and name.", test.log.info)
output = session.cmd("ver")
test.log.info("Windows version: %s", output.strip())
output = session.cmd("wmic os get Name")
output = output.strip().split()[-1]
get_os_name_cmd = (
'powershell -command "Get-CimInstance -ClassName Win32_OperatingSystem'
' | Select-Object -ExpandProperty Caption"'
)
output = session.cmd(get_os_name_cmd)
output = output.strip()
test.log.info("Windows name: %s", output)

error_context.context("Get driver version information in guest.", test.log.info)
system_drivers = session.cmd("wmic sysdriver get DisplayName,PathName")
get_driver_info_cmd = (
'powershell -command "Get-CimInstance -ClassName Win32_SystemDriver'
' | Select-Object DisplayName, PathName"'
)
system_drivers = session.cmd(get_driver_info_cmd)
test.log.debug("Drivers exist in the system:\n %s", system_drivers)
for i in system_drivers.splitlines():
if re.findall(drivers_pattern, i, re.I):
driver_info = i.strip().split()
driver_name = " ".join(driver_info[:-1])
path = driver_info[-1]
get_driver_path_cmd = (
'powershell -command "Get-CimInstance -ClassName Win32_SystemDriver'
" | Where-Object {$_.DisplayName -eq '%s'}"
" | Select-Object PathName"
' | Format-List"'
) % driver_name
driver_path = session.cmd(get_driver_path_cmd)
path = driver_path.strip().split(" : ")[-1]
path = re.sub(r"\\", "\\\\\\\\", path)
driver_ver_cmd = "wmic datafile where name="
driver_ver_cmd += "'%s' get version" % path
driver_ver_cmd = (
'powershell -command "(Get-Item -Path "%s").VersionInfo.FileVersion"'
% path
)
output = session.cmd(driver_ver_cmd)
msg = "Driver %s" % driver_name
msg += " version is %s" % output.strip().split()[-1]
msg += " version is %s" % output.strip()
test.log.info(msg)
session.close()
Loading