Repository: avocado-framework/avocado-vt

Issue:
About how does the usb_by_params match the configuration
When working on some usb-related bug I had to check configuration files regarding usb devices, and there was something that it was quite confusing to me.

The base.cfg file sets up the following configuration:
https://github.com/avocado-framework/avocado-vt/blob/d10af686d73b08a9561b353d04eb89dbde0c4c2c/virttest/shared/cfg/base.cfg#L137-L144
Okay, so a usb-tablet device should be attached to the vm, whose id should be usb-tablet1. As I understood the first time I was reading the cfg, its bus should be set to `usb1.0` as the `usb_bus` variable sets.

However, while the qemu command is built, the `usb_by_params` will set the device's bus as the one given in the variable `bus_<suffix>` i.e. `bus_tablet1`, and will just ignore the value hold by the `usb_bus` variable:
https://github.com/avocado-framework/avocado-vt/blob/d10af686d73b08a9561b353d04eb89dbde0c4c2c/virttest/qemu_devices/qcontainer.py#L1656-L1662

I checked the code and it seems that no variable is reading from the `usb_bus` nor the `bus` config parameter when regarding to usb device configuration in avocado-vt. There are some cases in tp-qemu in which the `usb_bus` is set in the configuration and then read from the test case itself. There are also some cases in which the `usb_port` is set, but I'm not sure whether it is being correctly read when configuring the target device or not.

Due to the way in which ports and buses for other kind (e.g. `drive_bus_...`, ...) are set, I was thinking that changing the lines from qcontainer.py that I already referenced would help to read from the usb configuration that is being set in different files.  However, this could affect other test providers.

I'm opening the issue so it can be discussed.


Discussion:
> Due to the way in which ports and buses for other kind (e.g. `drive_bus_...`, ...) are set, I was thinking that changing the lines from qcontainer.py that I already referenced would help to read from the usb configuration that is being set in different files. 

This sounds fairly reasonable to me. After digging into the commit history (https://github.com/avocado-framework/avocado-vt/commit/c847833a1f15085eb6a2bf0a52ed71d58748962f, https://github.com/avocado-framework/avocado-vt/commit/4c5a4f74ce82afd733171907cb2e043ff4be6fc2) I intended to understand the mismatch of the parameter name between the configuration and the script as a mistake, and to me `usb_bus` looks much better or else I would suggest using another name `usbdev_bus` for easily distinguishing the params of usb controllers and usb devices.

> However, this could affect other test providers.

Indeed, but fortunately we only need to consider on those ones based on `vt_type=qemu`, and thus from my point of view it would probably be okay to have this change.

Thanks!

Resolving patch:
diff --git a/virttest/qemu_devices/qcontainer.py b/virttest/qemu_devices/qcontainer.py
--- a/virttest/qemu_devices/qcontainer.py
+++ b/virttest/qemu_devices/qcontainer.py
@@ -1597,34 +1597,35 @@ def usbc_by_params(self, usb_name, params, pci_bus={"aobject": "pci.0"}):
                                       params.get('pci_addr'))
 
     # USB Device related methods
-    def usb_by_variables(self, usb_name, usb_type, controller_type, bus=None,
-                         port=None, serial=None, dev_options=None):
+    def usb_by_variables(self, usb_name, usbdev_type, controller_type,
+                         usbdev_bus=None, usbdev_port=None, usbdev_serial=None,
+                         dev_options=None):
         """
         Creates usb-devices by variables.
         :param usb_name: usb name
-        :param usb_type: usb type (usb-tablet, usb-serial, ...)
+        :param usbdev_type: usb type (usb-tablet, usb-serial, ...)
         :param controller_type: type of the controller (uhci, ehci, xhci, ...)
-        :param bus: the bus name (my_bus.0, ...)
-        :param port: port specification (4, 4.1.2, ...)
-        :param serial: serial specification (1234, d1, ...)
-        :param dev_options: device options dict of usb_type
+        :param usbdev_bus: the bus name (my_bus.0, ...)
+        :param usbdev_port: port specification (4, 4.1.2, ...)
+        :param usbdev_serial: serial specification (1234, d1, ...)
+        :param dev_options: device options dict of usbdev_type
         :return: QDev device
         """
-        if not self.has_device(usb_type):
+        if not self.has_device(usbdev_type):
             raise exceptions.TestSkipError("usb device %s not available"
-                                           % usb_type)
+                                           % usbdev_type)
         if self.has_option('device'):
-            device = qdevices.QDevice(usb_type, aobject=usb_name)
+            device = qdevices.QDevice(usbdev_type, aobject=usb_name)
             device.set_param('id', 'usb-%s' % usb_name)
-            device.set_param('bus', bus)
-            device.set_param('port', port)
-            device.set_param('serial', serial)
+            device.set_param('bus', usbdev_bus)
+            device.set_param('port', usbdev_port)
+            device.set_param('serial', usbdev_serial)
             if dev_options:
                 for opt_name, opt_value in dev_options.items():
                     device.set_param(opt_name, opt_value)
             device.parent_bus += ({'type': controller_type},)
         else:
-            if "tablet" in usb_type:
+            if "tablet" in usbdev_type:
                 device = qdevices.QStringDevice('usb-%s' % usb_name,
                                                 cmdline='-usbdevice %s' % usb_name)
             else:
@@ -1641,8 +1642,8 @@ def usb_by_params(self, usb_name, params):
         :return: QDev device
         """
         dev_options = {}
-        usb_type = params.get("usb_type")
-        if usb_type == "usb-host":
+        usbdev_type = params.get("usbdev_type")
+        if usbdev_type == "usb-host":
             dev_options["hostbus"] = params.get("usbdev_option_hostbus")
             dev_options["hostaddr"] = params.get("usbdev_option_hostaddr")
             dev_options["hostport"] = params.get("usbdev_option_hostport")
@@ -1654,10 +1655,10 @@ def usb_by_params(self, usb_name, params):
                 dev_options["productid"] = "0x%s" % productid
 
         return self.usb_by_variables(usb_name,
-                                     usb_type,
+                                     usbdev_type,
                                      params.get("usb_controller"),
-                                     params.get("bus"),
-                                     params.get("port"),
+                                     params.get("usbdev_bus"),
+                                     params.get("usbdev_port"),
                                      params.get("usbdev_serial"),
                                      dev_options)
 


Test patch: