[libvirt] [PATCH] Add TCK script for host USB device hotplug/unplug

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This adds support for testing host USB device hotplug/unplug in
libvirt drivers. This currently fails in all known libvirt
drivers :-)  The QEMU driver only support the hotplug action,
not unplug, hence the failure.

Since the TCK can't assume there are any host USB devices that
can be safely messed around with, the person running the test
suite must first list one or more devices in the config file.
If no devices are listed, the test will automatically skip
all parts, rather than failing.

* conf/default.cfg: Config entry to specify USB devices that the
  TCK can mess around with
* lib/Sys/Virt/TCK.pm: API for getting a host USB device from
  the config
* scripts/domain/240-usb-host-hotplug.t: Test case for USB
  device hotplug/unplug.
---
 conf/default.cfg                      |   13 +++++
 lib/Sys/Virt/TCK.pm                   |   18 +++++++
 scripts/domain/240-usb-host-hotplug.t |   89 +++++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+), 0 deletions(-)
 create mode 100644 scripts/domain/240-usb-host-hotplug.t

diff --git a/conf/default.cfg b/conf/default.cfg
index 556225e..f9c2b5c 100644
--- a/conf/default.cfg
+++ b/conf/default.cfg
@@ -99,3 +99,16 @@ kernels = (
 #     ostype = exe
 #   }
 )
+
+# Host USB devices that the test suite can safely mess around with
+# without risk of breaking the host OS
+host_usb_devices = (
+# Must list either vendor+product, or bus+dev, or both
+#   {
+#      vendor = 0x0627
+#      product = 0x0001
+#      bus = 001
+#      device = 002
+#   }
+# Can list more than one USB device if many are available
+)
diff --git a/lib/Sys/Virt/TCK.pm b/lib/Sys/Virt/TCK.pm
index 9e6b554..ce03935 100644
--- a/lib/Sys/Virt/TCK.pm
+++ b/lib/Sys/Virt/TCK.pm
@@ -769,4 +769,22 @@ sub xpath {
     return $xp->find($path);
 }
 
+sub get_host_usb_device {
+    my $self = shift;
+    my $devindex = @_ ? shift : 0;
+
+    my $devs = $self->config("host_usb_devices", []);
+
+    if ($devindex > $#{$devs}) {
+	return ();
+    }
+
+    my $bus = $self->config("host_usb_devices/[$devindex]/bus", undef);
+    my $device = $self->config("host_usb_devices/[$devindex]/device", undef);
+    my $vendor = $self->config("host_usb_devices/[$devindex]/vendor", undef);
+    my $product = $self->config("host_usb_devices/[$devindex]/product", undef);
+
+    return ($bus, $device, $vendor, $product);
+}
+
 1;
diff --git a/scripts/domain/240-usb-host-hotplug.t b/scripts/domain/240-usb-host-hotplug.t
new file mode 100644
index 0000000..4729300
--- /dev/null
+++ b/scripts/domain/240-usb-host-hotplug.t
@@ -0,0 +1,89 @@
+# -*- perl -*-
+#
+# Copyright (C) 2009 Red Hat
+# Copyright (C) 2009 Daniel P. Berrange
+#
+# This program is free software; You can redistribute it and/or modify
+# it under the GNU General Public License as published by the Free
+# Software Foundation; either version 2, or (at your option) any
+# later version
+#
+# The file "LICENSE" distributed along with this file provides full
+# details of the terms and conditions
+#
+
+=pod
+
+=head1 NAME
+
+domain/240-usb-host-hotplug.t - verify hot plug & unplug of a host USB device
+
+=head1 DESCRIPTION
+
+The test case validates that it is possible to hotplug a usb
+host device to a running domain, and then unplug it again.
+This requires that the TCK configuration file have at least
+one host USB device listed.
+
+=cut
+
+use strict;
+use warnings;
+
+use Test::More tests => 5;
+
+use Sys::Virt::TCK;
+use Test::Exception;
+
+my $tck = Sys::Virt::TCK->new();
+my $conn = eval { $tck->setup(); };
+BAIL_OUT "failed to setup test harness: $@" if $@;
+END {
+    $tck->cleanup if $tck;
+}
+
+
+my $xml = $tck->generic_domain("tck")->as_xml;
+
+diag "Creating a new transient domain";
+my $dom;
+ok_domain(sub { $dom = $conn->create_domain($xml) }, "created transient domain object");
+
+
+my ($bus, $device, $vendor, $product) = $tck->get_host_usb_device();
+
+SKIP: {
+    # Must have one of the pairs at least
+    unless (($bus && $device) || ($vendor && $product)) {
+	skip "No host usb device available in configuration file", 4;
+    }
+
+    my $devxml = "<hostdev mode='subsystem' type='usb'>\n" .
+	"<source>\n";
+    if ($bus && $device) {
+        $devxml .= "<address bus='$bus' device='$device'/>\n"
+    }
+    if ($vendor && $product) {
+        $devxml .= "<vendor id='$vendor'/>\n";
+        $devxml .= "<product id='$product'/>\n";
+    }
+    $devxml .= "</source>\n" .
+	"</hostdev>\n";
+
+    my $initialxml = $dom->get_xml_description;
+
+    diag "Attaching the new dev $devxml";
+    lives_ok(sub { $dom->attach_device($devxml); }, "USB dev has been attached");
+
+    my $newxml = $dom->get_xml_description;
+    ok($newxml =~ m|<hostdev|, "new XML has extra USB dev present");
+
+    diag "Detaching the new dev $devxml";
+    lives_ok(sub { $dom->detach_device($devxml); }, "USB dev has been detached");
+
+
+    my $finalxml = $dom->get_xml_description;
+
+    is($initialxml, $finalxml, "final XML has removed the disk")
+}
+
-- 
1.6.5.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]