diff options
-rw-r--r-- | tests/extra/Makefile.am | 17 | ||||
-rwxr-xr-x | tests/extra/pick-guests.pl | 59 |
2 files changed, 65 insertions, 11 deletions
diff --git a/tests/extra/Makefile.am b/tests/extra/Makefile.am index 22315e0d..dfdffd18 100644 --- a/tests/extra/Makefile.am +++ b/tests/extra/Makefile.am @@ -51,7 +51,7 @@ # - hard to test because guestmount forks into the background, and # if valgrind reports errors it doesn't stop the test shell script -EXTRA_DIST = suppressions +EXTRA_DIST = pick-guests.pl suppressions VG = valgrind \ --log-file=$(abs_builddir)/valgrind.log \ @@ -63,14 +63,9 @@ RUN_VG = $(abs_top_builddir)/run $(VG) export LIBVIRT_DEFAULT_URI = \ qemu+unix:///system?socket=/var/run/libvirt/libvirt-sock-ro -# Choose 5 guests at random on each run. -RANDOM_GUESTS = $(shell virsh -c $(LIBVIRT_DEFAULT_URI) list --all | \ - head -n -1 | \ - tail -n +3 | \ - awk '{print $$2}' | \ - sort -R | \ - head -5 | \ - sort) +# Choose guests at random on each run. +random_guests := \ + $(shell $(abs_top_builddir)/run $(srcdir)/pick-guests.pl 5) extra-tests: \ test-guests \ @@ -136,7 +131,7 @@ test-df-real: $(RUN_VG) ../../df/virt-df -h >/dev/null test-filesystems-real: - @for g in $(RANDOM_GUESTS); do \ + @for g in $(random_guests); do \ echo $(RUN_VG) ../../cat/virt-filesystems -d $$g --all --long -h --uuid; \ $(RUN_VG) ../../cat/virt-filesystems -d $$g --all --long -h --uuid > /dev/null; \ r=$$?; \ @@ -144,7 +139,7 @@ test-filesystems-real: done test-inspector-real: - @for g in $(RANDOM_GUESTS); do \ + @for g in $(random_guests); do \ echo $(RUN_VG) ../../inspector/virt-inspector -d $$g; \ $(RUN_VG) ../../inspector/virt-inspector -d $$g > /dev/null; \ r=$$?; \ diff --git a/tests/extra/pick-guests.pl b/tests/extra/pick-guests.pl new file mode 100755 index 00000000..d2e4a225 --- /dev/null +++ b/tests/extra/pick-guests.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl -w +# libguestfs +# Copyright (C) 2009-2012 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Pick guests at random on the local machine which are accessible. +# Note that the Makefile sets $LIBVIRT_DEFAULT_URI. + +use strict; + +use Sys::Guestfs; +use Sys::Virt; +use List::Util qw(shuffle); + +die "$0 nr-guests\n" unless @ARGV == 1; +my $n = $ARGV[0]; + +my $vmm = Sys::Virt->new; +my @domains = ($vmm->list_domains, $vmm->list_defined_domains); + +# Only guests which are accessible by the current (non-root) user. On +# the machine where I run these tests, I have added my user account to +# the 'disk' group, so that most guests are accessible. However +# because libvirt changes the permissions on guest disks, a guest +# which has been run on the machine becomes inaccessible, hence the +# need for this code - RWMJ. +my @accessible; +foreach my $dom (@domains) { + my $name = $dom->get_name; + my $g = Sys::Guestfs->new; + eval { + $g->add_domain ($name, readonly => 1); + # $g->launch (); - don't actually need to do this + }; + push @accessible, $name unless $@; +} + +# Randomize the list of guests. +@accessible = shuffle (@accessible); + +$n = @accessible if @accessible < $n; + +# Return the first n guests from the list. +for (my $i = 0; $i < $n; ++$i) { + print $accessible[$i], "\n"; +} |