Skip to content

Commit

Permalink
Merge pull request avocado-framework#3573 from chloerh/disk
Browse files Browse the repository at this point in the history
Remove duplicate definition of driver tag in Disk xml
  • Loading branch information
dzhengfy authored Dec 20, 2022
2 parents 2387dd4 + 156ba0e commit a8c6b9e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 99 deletions.
106 changes: 24 additions & 82 deletions virttest/libvirt_xml/devices/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Disk(base.TypedDeviceBase):
'address', 'boot', 'loadparm', 'readonly', 'transient', 'share', 'model',
'mirror', 'ready', 'iotune', 'source', 'blockio', 'geometry',
'wwn', 'serial', 'vendor', 'product', 'encryption', 'auth',
'reservations', 'backingstore', 'drivermetadata', 'sharebacking')
'reservations', 'backingstore', 'driver_metadatacache', 'sharebacking')

def __init__(self, type_name='file', virsh_instance=base.base.virsh):
accessors.XMLAttribute('device', self, parent_xpath='/',
Expand All @@ -102,6 +102,12 @@ def __init__(self, type_name='file', virsh_instance=base.base.virsh):
tag_name='product')
accessors.XMLElementDict('driver', self, parent_xpath='/',
tag_name='driver')
accessors.XMLElementNest('driver_metadatacache', self,
parent_xpath='/driver',
tag_name='metadata_cache',
subclass=self.MetadataCache,
subclass_dargs={
'virsh_instance': virsh_instance})
accessors.XMLElementDict('target', self, parent_xpath='/',
tag_name='target')
accessors.XMLElementDict('alias', self, parent_xpath='/',
Expand Down Expand Up @@ -155,11 +161,6 @@ def __init__(self, type_name='file', virsh_instance=base.base.virsh):
subclass=self.BackingStore,
subclass_dargs={
'virsh_instance': virsh_instance})
accessors.XMLElementNest('drivermetadata', self, parent_xpath='/',
tag_name='driver',
subclass=self.DriverMetadata,
subclass_dargs={
'virsh_instance': virsh_instance})
super(Disk, self).__init__(device_tag='disk', type_name=type_name,
virsh_instance=virsh_instance)

Expand Down Expand Up @@ -249,15 +250,6 @@ def get_backingstore_list(self):
backingstore_list.append(elem)
return backingstore_list

def new_drivermetadata(self, **dargs):
"""
Return a new DriverMetadata instance and set properties from dargs
"""
new_one = self.DriverMetadata(virsh_instance=self.virsh)
for key, value in list(dargs.items()):
setattr(new_one, key, value)
return new_one

def get_all_backingstore(self):
"""
Get all backingstore of a Disk object
Expand Down Expand Up @@ -636,77 +628,27 @@ def __init__(self, virsh_instance=base.base.virsh):
super(self.__class__, self).__init__(virsh_instance=virsh_instance)
self.xml = '<source/>'

class DriverMetadata(base.base.LibvirtXMLBase):

class MetadataCache(base.base.LibvirtXMLBase):
"""
DriverMetaData XML class
<driver name='qemu' type='qcow2'>
<metadata_cache>
<max_size unit='bytes'>10</max_size>
</metadata_cache>
</driver>
Properties:
attrs: Dictionary of attributes, qualifying the driver name and type,etc.
metadata_cache: embedded class to describe metadata attributes
usages:
custom_disk = Disk(type_name='disk')
driver_dict = {"name": "qemu","type": "qcow2"}
new_one = custom_disk.new_drivermetadata(**{"attrs": driver_dict})
Source of MetadataCache xml class
metadata_cache_dict = {"max_size": "10", "max_size_unit": "bytes"}
new_one.metadata_cache = custom_disk.DriverMetadata().new_metadatacache(**metadata_cache_dict)
custom_disk.drivermetadata = new_one
max_size:
Int, attribute of MetadataCache max size
max_size_unit:
string, attribute of MetadataCache max size unit
"""

__slots__ = ('attrs', 'metadata_cache')
__slots__ = ('max_size', 'max_size_unit')

def __init__(self, virsh_instance=base.base.virsh):
accessors.XMLElementDict('attrs', self, parent_xpath='/',
tag_name='driver')
accessors.XMLElementNest('metadata_cache', self, parent_xpath='/',
tag_name='metadata_cache',
subclass=self.MetadataCache,
subclass_dargs={
'virsh_instance': virsh_instance})
accessors.XMLElementInt(property_name='max_size',
libvirtxml=self,
parent_xpath='/',
tag_name='max_size')
accessors.XMLAttribute(property_name="max_size_unit",
libvirtxml=self,
parent_xpath='/',
tag_name='max_size',
attribute='unit')
super(self.__class__, self).__init__(virsh_instance=virsh_instance)
self.xml = '<driver/>'

def new_metadatacache(self, **dargs):
"""
Create new MetadataCache for DriverMetadata
"""
new_one = self.MetadataCache(virsh_instance=self.virsh)
for key, value in list(dargs.items()):
setattr(new_one, key, value)
return new_one

class MetadataCache(base.base.LibvirtXMLBase):
"""
Source of MetadataCache xml class
max_size:
string, attribute of MetadataCache max size
max_size_unit:
string, attribute of MetadataCache max size unit
"""

__slots__ = ('max_size', 'max_size_unit')

def __init__(self, virsh_instance=base.base.virsh):
accessors.XMLElementText(property_name='max_size',
libvirtxml=self,
parent_xpath='/',
tag_name='max_size')
accessors.XMLAttribute(property_name="max_size_unit",
libvirtxml=self,
parent_xpath='/',
tag_name='max_size',
attribute='unit')
super(self.__class__, self).__init__(virsh_instance=virsh_instance)
self.xml = '<metadata_cache/>'
self.xml = '<metadata_cache/>'
28 changes: 11 additions & 17 deletions virttest/utils_libvirt/libvirt_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,17 @@ def create_custom_metadata_disk(disk_path, disk_format,
custom_disk = disk_inst
else:
custom_disk = Disk(type_name='file')
if disk_device:
custom_disk.device = disk_device
source_dict = {}
if disk_path:
source_dict.update({'file': disk_path})
custom_disk.source = custom_disk.new_disk_source(
**{"attrs": source_dict})
if device_target:
target_dict = {"dev": device_target, "bus": device_bus}
custom_disk.target = target_dict
driver_dict = {"name": "qemu", 'type': disk_format}
# Create drivermetadata object
new_one_drivermetadata = custom_disk.new_drivermetadata(**{"attrs": driver_dict})
metadata_cache_dict = {"max_size": max_size, "max_size_unit": "bytes"}
# Attach metadatacache into drivermetadata object
new_one_drivermetadata.metadata_cache = custom_disk.DriverMetadata().new_metadatacache(**metadata_cache_dict)
custom_disk.drivermetadata = new_one_drivermetadata

disk_attrs = {
'device': disk_device,
'source': {'attrs': {'file': disk_path}},
'target': {'dev': device_target, 'bus': device_bus},
'driver': {'name': 'qemu', 'type': disk_format},
'driver_metadatacache': {'max_size': int(max_size),
'max_size_unit': 'bytes'},
}

custom_disk.setup_attrs(**disk_attrs)
LOG.debug("disk xml in create_custom_metadata_disk: %s\n", custom_disk)
return custom_disk

Expand Down

0 comments on commit a8c6b9e

Please sign in to comment.