Skip to content

Commit

Permalink
Merge pull request #2729 from jgao12345/cdrom_slices
Browse files Browse the repository at this point in the history
Add tests of update device for cdrom slice test
  • Loading branch information
kylazhang authored Aug 12, 2020
2 parents 18f4153 + 2b30f2f commit 1190a69
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 18 deletions.
13 changes: 13 additions & 0 deletions libvirt/tests/cfg/virsh_cmd/domain/virsh_update_device.cfg
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
updatedevice_target_bus = "scsi"
updatedevice_target_dev = "sdc"
disk_type = "cdrom"
variants:
- slice_test_qcow2:
disk_slice = "yes"
driver_type = "qcow2"
device_type = "cdrom"
target_bus = "scsi"
- slice_test_raw:
disk_slice = "yes"
driver_type = "raw"
device_type = "cdrom"
target_bus = "scsi"
- not_slice_test:
disk_slice = "False"
- floppy_option:
updatedevice_target_bus = "fdc"
updatedevice_target_dev = "fda"
Expand Down
74 changes: 56 additions & 18 deletions libvirt/tests/src/virsh_cmd/domain/virsh_update_device.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import shutil
import re
import logging

from avocado.utils import process
from virttest import virsh
from virttest import data_dir
from virttest import utils_misc
Expand All @@ -10,35 +12,53 @@
from virttest.utils_test import libvirt


def create_disk(test, vm_name, orig_iso, disk_type, target_dev, mode=""):
def create_disk(params, test, vm_name, orig_iso, disk_type, target_dev, disk_format="", mode=""):
"""
Prepare image for attach and attach it to domain
:param parameters from cfg file
:param vm_name : vm_name
:param source_iso : disk's source backing file
:param disk_type: disk's device type: cdrom or floppy
:param target_dev: disk's target device name
:param disk_format: disk's target format
:param mode: readonly or shareable
"""
slice_test = "yes" == params.get("disk_slice", "yes")
try:
with open(orig_iso, 'wb') as _file:
_file.seek((1024 * 1024) - 1)
_file.write(str(0).encode())
# create slice cdrom image for slice test
if slice_test:
libvirt.create_local_disk("file", orig_iso, size="10", disk_format=disk_format, extra="-o preallocation=full")
params["input_source_file"] = orig_iso
params["disk_slice"] = {"slice": "yes"}
params["target_dev"] = "sdc"
disk_xml = libvirt.create_disk_xml(params)
else:
with open(orig_iso, 'wb') as _file:
_file.seek((1024 * 1024) - 1)
_file.write(str(0).encode())
except IOError:
test.fail("Create orig_iso failed!")
options = "--type %s --sourcetype=file --config" % disk_type
if mode:
options += " --mode %s" % mode
try:
virsh.attach_disk(vm_name, orig_iso, target_dev, options)
if mode:
options += " --mode %s" % mode
if slice_test:
# Use attach_device to add cdrom file with slice to guest
virsh.attach_device(vm_name, disk_xml, flagstr="--config")
else:
virsh.attach_disk(vm_name, orig_iso, target_dev, options)
except Exception:
os.remove(orig_iso)
test.fail("Failed to attach")


def create_attach_xml(test, update_xmlfile, source_iso, disk_type, target_bus,
def create_attach_xml(params, test, update_xmlfile, source_iso, disk_type, target_bus,
target_dev, disk_mode="", disk_alias=None):
"""
Create a xml file to update a device.
:param parameters from cfg file
:param update_xmlfile : path/file to save device XML
:param source_iso : disk's source backing file.
:param disk_type: disk's device type: cdrom or floppy
Expand All @@ -47,18 +67,30 @@ def create_attach_xml(test, update_xmlfile, source_iso, disk_type, target_bus,
:param disk_mode: readonly or shareable
:param disk_alias: disk's alias name
"""
slice_test = "yes" == params.get("disk_slice", "no")
try:
with open(source_iso, 'wb') as _file:
_file.seek((1024 * 1024) - 1)
_file.write(str(0).encode())
if slice_test:
libvirt.create_local_disk("file", source_iso, size="1", disk_format="qcow2", extra="-o preallocation=full")
else:
with open(source_iso, 'wb') as _file:
_file.seek((1024 * 1024) - 1)
_file.write(str(0).encode())
except IOError:
test.fail("Create source_iso failed!")
disk_class = VMXML.get_device_class('disk')
disk = disk_class(type_name='file')
# Static definition for comparison in check_attach()
# Static definition for comparison in check_attach()`
disk.device = disk_type
disk.driver = dict(name='qemu')
disk.source = disk.new_disk_source(attrs={'file': source_iso})
disk_source = disk.new_disk_source(**{"attrs": {'file': source_iso}})
# Add slice field involved in source
if slice_test:
slice_size_param = process.run("du -b %s" % source_iso).stdout_text.strip()
slice_size = re.findall(r'[0-9]+', slice_size_param)
slice_size = ''.join(slice_size)
disk_source.slices = disk.new_slices(
**{"slice_type": "storage", "slice_offset": "0", "slice_size": slice_size})
disk.source = disk_source
disk.target = dict(bus=target_bus, dev=target_dev)
if disk_mode == "readonly":
disk.readonly = True
Expand Down Expand Up @@ -165,17 +197,23 @@ def run(test, params, env):
if vm.is_alive():
vm.destroy(gracefully=False)
# Vm should be in 'shut off' status
slice_test = params.get("disk_slice", "False")
disk_format = params.get("driver_type")
utils_misc.wait_for(lambda: vm.state() == "shut off", 30)
create_disk(test, vm_name, orig_iso, disk_type, target_dev, disk_mode)
create_disk(params, test, vm_name, orig_iso, disk_type, target_dev, disk_format, disk_mode)
inactive_vmxml = VMXML.new_from_dumpxml(vm_name,
options="--inactive")
if start_vm:
vm.start()
vm.wait_for_login().close()
domid = vm.get_id()
else:
domid = "domid invalid; domain is shut-off"

disk_alias = libvirt.get_disk_alias(vm, orig_iso)
create_attach_xml(test, update_xmlfile, test_iso, disk_type, target_bus,
if slice_test:
disk_alias = ""
else:
disk_alias = libvirt.get_disk_alias(vm, orig_iso)
create_attach_xml(params, test, update_xmlfile, test_iso, disk_type, target_bus,
target_dev, disk_mode, disk_alias)

# Get remaining parameters for configuration.
Expand All @@ -201,7 +239,7 @@ def run(test, params, env):
if diff_iso:
# Swap filename of device backing file in update.xml
os.remove(update_xmlfile)
create_attach_xml(test, update_xmlfile, test_diff_iso, disk_type,
create_attach_xml(params, test, update_xmlfile, test_diff_iso, disk_type,
target_bus, target_dev, disk_mode, disk_alias)
elif vm_ref == "uuid":
vm_ref = vmxml.uuid
Expand Down

0 comments on commit 1190a69

Please sign in to comment.