Replies: 8 comments 2 replies
-
@chunfuwen @Yingshun @chloerh @kylazhang @iccaszhulili @yafu-1 @yanqzhan @meinaLi @yalzhang @smitterl @cliping @nanli1 @vasekhodina |
Beta Was this translation helpful? Give feedback.
-
@dzhengfy @chloerh I like it very much, it's very convenient to set xml in the code and we don't need to maintain several create_xml functions as you mentioned. |
Beta Was this translation helpful? Give feedback.
-
@dzhengfy I think this method will be very effective and I also really like it. Here I have some questions for it:
Thank you very much for your contribution on this. |
Beta Was this translation helpful? Give feedback.
-
I totally support this, I found setup_attrs() is very convenient in my practice. |
Beta Was this translation helpful? Give feedback.
-
It's very concise. I totally support it, too. |
Beta Was this translation helpful? Give feedback.
-
I will use it next time when creating a device xml. Thanks very much for providing such a great solution. |
Beta Was this translation helpful? Give feedback.
-
I love it, I used this for many times. It's very convenient to edit or post one object by dict . |
Beta Was this translation helpful? Give feedback.
-
It is very convenient. I like it, too. |
Beta Was this translation helpful? Give feedback.
-
Problem
As we know, libvirt automation test scripts usually prepare a special guest vm with different devices for testing. To make that, we created utility functions in avocado-vt/tp-libvirt, like
In
avocado-vt/virrtest/utils_test/libvirt.py
, we have below functions._create_channel_xml(), create_controller_xml(), create_disk_xml(), create_hostdev_xml(), create_net_xml(), create_rng_xml(), create_tpm_dev(), create_vsock_xml(), …_
We almost define every function for creating every different device. But most of their codes are similar like below:
And when updating a device xml, we have similar problem like below sample function :
modify_vm_iface()
, whose logic is below:When we need to update devices in other types, we have to define different functions for them.
So is it necessary to define so many functions for different devices with the same code logic? I do not think so. I think the old way has too many duplicate efforts.
Suggestion
I recommend we use the
setup_attrs()
method to solve the above problem at one stop.About setup_attrs()
setup_attrs() function was implemented in PR Add function to setup attrs of an xml object #3168
and its paired function fetch_attrs() in Add function to fetch attrs of an xml object #3212
by haizhao (github: chloerh).
Usage for setup_attrs()
Take memory device as an example for the usage of setup_attrs() as below:
We define the below dict in the cfg file of tp-libvirt. The fields should align with
__slots__
and__init__()
in the device class’s definition, see avocado-vt-/virttest/libvirt_xml/devices/memory.py.Then we use below codes in python file which can easily create a memory device and get the memory device’s full attributes :
There are also some unit tests for devices of other types under avocado-vt/virttest/unittests/libvirt_xml/ which were provided by chloerh.
Benefits
With setup_attrs(), we do not need to define those create_xxx() functions for different devices, while tp-libvirt users could easily use above 4 lines to meet their needs or they can use
create_vm_device_by_type()
in virttest/utils_libvirt/libvirt_vmxml.py to meet their needs. And for updating a device, they can usemodify_vm_device()
in virttest/utils_libvirt/libvirt_vmxml.py. Both above two functions are based on setup_attrs().In a word, we use two functions to provide the same functionalities which more than ten functions do.
Requirement
If you like this way, now there is a requirement that when we define a new device class in avocado-vt, let’s make it support setup_attrs() usage and provide your test result using setup_attrs() in your PR. This is helpful for other users to use this way in their tp-libvirt cases. There is an example to support it tpm.py: optimize pcrbank part to support setup_attr #3404
Hi guys, what do you think about this? And if you would like to use it, what's your opinion for providing the test result using it when defining a new device xml class?
Beta Was this translation helpful? Give feedback.
All reactions