From 00202b9c2941c2b4ff514c5ea32818807f42acf4 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 3 May 2012 12:15:27 +0100 Subject: tests: Regression test for RHBZ#701814. This commit adds a tests/xml directory, and an LD_PRELOAD module which can fake arbitrary libvirt XML from an external file (and is therefore a much more flexible test than using the libvirt test:// driver alone). Also added is one regression test for: https://bugzilla.redhat.com/show_bug.cgi?id=701814 Loading the given libvirt XML using Sys::Guestfs::Lib::open_guest used to fail with the error: format parameter is empty or contains disallowed characters at /home/rjones/d/libguestfs/perl/blib/lib/Sys/Guestfs/Lib.pm line 256. Thanks to Tom Horsley for supplying the test data. --- Makefile.am | 1 + configure.ac | 1 + tests/xml/Makefile.am | 49 ++++++++++++++++++++++++ tests/xml/fake_libvirt_xml.c | 87 ++++++++++++++++++++++++++++++++++++++++++ tests/xml/rhbz701814-faked.xml | 70 +++++++++++++++++++++++++++++++++ tests/xml/rhbz701814-node.xml | 19 +++++++++ tests/xml/rhbz701814.pl | 30 +++++++++++++++ 7 files changed, 257 insertions(+) create mode 100644 tests/xml/Makefile.am create mode 100644 tests/xml/fake_libvirt_xml.c create mode 100644 tests/xml/rhbz701814-faked.xml create mode 100644 tests/xml/rhbz701814-node.xml create mode 100755 tests/xml/rhbz701814.pl diff --git a/Makefile.am b/Makefile.am index 3ff66085..a4839184 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,6 +43,7 @@ SUBDIRS += tests/luks SUBDIRS += tests/md SUBDIRS += tests/ntfsclone SUBDIRS += tests/btrfs +SUBDIRS += tests/xml SUBDIRS += tests/regressions endif diff --git a/configure.ac b/configure.ac index dc1f27a8..ca1c146a 100644 --- a/configure.ac +++ b/configure.ac @@ -1344,6 +1344,7 @@ AC_CONFIG_FILES([Makefile tests/protocol/Makefile tests/qemu/Makefile tests/regressions/Makefile + tests/xml/Makefile tools/Makefile]) AC_OUTPUT diff --git a/tests/xml/Makefile.am b/tests/xml/Makefile.am new file mode 100644 index 00000000..d9abd5cd --- /dev/null +++ b/tests/xml/Makefile.am @@ -0,0 +1,49 @@ +# libguestfs +# Copyright (C) 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. + +include $(top_srcdir)/subdir-rules.mk + +if HAVE_LIBVIRT + +# This LD_PRELOAD library can be used to precisely control the XML +# returned by libvirt. +check_LTLIBRARIES = libfakevirtxml.la + +libfakevirtxml_la_SOURCES = fake_libvirt_xml.c +libfakevirtxml_la_CFLAGS = $(LIBVIRT_CFLAGS) +# -version-info and -rpath force libtool to build a shared library. +libfakevirtxml_la_LDFLAGS = -version-info 0:0:0 -rpath /nowhere + +random_val := $(shell awk 'BEGIN{srand(); print 1+int(255*rand())}' < /dev/null) + +TESTS = \ + rhbz701814.pl + +TESTS_ENVIRONMENT = \ + MALLOC_PERTURB_=$(random_val) \ + abs_srcdir=$(abs_srcdir) \ + LD_PRELOAD=.libs/libfakevirtxml.so \ + $(top_builddir)/run + +endif + +EXTRA_DIST = \ + rhbz701814.pl \ + rhbz701814-faked.xml \ + rhbz701814-node.xml + +CLEANFILES = *~ diff --git a/tests/xml/fake_libvirt_xml.c b/tests/xml/fake_libvirt_xml.c new file mode 100644 index 00000000..61192413 --- /dev/null +++ b/tests/xml/fake_libvirt_xml.c @@ -0,0 +1,87 @@ +/* libguestfs + * Copyright (C) 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. + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include + +char * +virDomainGetXMLDesc (virDomainPtr dom, unsigned int flags) +{ + const char *path; + int fd; + struct stat statbuf; + char *buf, *p; + size_t n; + ssize_t r; + + path = getenv ("FAKE_LIBVIRT_XML"); + + if (!path) { + fprintf (stderr, "environment variable FAKE_LIBVIRT_XML is not set\n"); + _exit (1); + } + + fprintf (stderr, + "fake_libvirt_xml: returning fake libvirt XML from %s\n", path); + + fd = open (path, O_RDONLY | O_CLOEXEC); + if (fd == -1) { + perror (path); + _exit (1); + } + + if (fstat (fd, &statbuf) == -1) { + perror ("fstat"); + _exit (1); + } + + buf = malloc (statbuf.st_size + 1); + if (buf == NULL) { + perror ("malloc"); + _exit (1); + } + + for (n = 0, p = buf; n < statbuf.st_size; ++n) { + r = read (fd, p, statbuf.st_size - n); + if (r == -1) { + perror ("read"); + _exit (1); + } + if (r == 0) + break; + n += r; + p += r; + } + + *p = '\0'; + + if (close (fd) == -1) { + perror ("close"); + _exit (1); + } + + return buf; /* caller frees */ +} diff --git a/tests/xml/rhbz701814-faked.xml b/tests/xml/rhbz701814-faked.xml new file mode 100644 index 00000000..ae31915c --- /dev/null +++ b/tests/xml/rhbz701814-faked.xml @@ -0,0 +1,70 @@ + + + winxppro + 6d626c31-643c-ca9a-d36d-3c1612e39bbd + 1048576 + 1048576 + 2 + + hvm + + + + + + + + + destroy + restart + restart + + /usr/bin/qemu-spice-wrapper + + + + +
+ + + + + +
+ + + + + +
+ + +
+ + + + + +
+ + + + + + + + + + +
+ +