Blame SOURCES/0001-tests-Add-a-regression-test-for-all-disk_-info-comma.patch

022f11
From 11e147bea7392f3c59b76efb1c62c81250e9ff20 Mon Sep 17 00:00:00 2001
022f11
From: "Richard W.M. Jones" <rjones@redhat.com>
022f11
Date: Thu, 18 Jul 2013 13:59:09 +0100
022f11
Subject: [PATCH] tests: Add a regression test for all disk_* info commands
022f11
 (RHBZ#981663).
022f11
022f11
(cherry picked from commit 2311fb0632a64cfa2bab8bf65c82ffaa042d619e)
022f11
---
022f11
 Makefile.am                  |   1 +
022f11
 configure.ac                 |   1 +
022f11
 tests/disk-info/Makefile.am  |  31 +++++++++
022f11
 tests/disk-info/disk-info.pl | 158 +++++++++++++++++++++++++++++++++++++++++++
022f11
 4 files changed, 191 insertions(+)
022f11
 create mode 100644 tests/disk-info/Makefile.am
022f11
 create mode 100755 tests/disk-info/disk-info.pl
022f11
022f11
diff --git a/Makefile.am b/Makefile.am
022f11
index 071fc43..de747b0 100644
022f11
--- a/Makefile.am
022f11
+++ b/Makefile.am
022f11
@@ -60,6 +60,7 @@ SUBDIRS += tests/hotplug
022f11
 SUBDIRS += tests/nbd
022f11
 SUBDIRS += tests/http
022f11
 SUBDIRS += tests/syslinux
022f11
+SUBDIRS += tests/disk-info
022f11
 SUBDIRS += tests/regressions
022f11
 endif
022f11
 
022f11
diff --git a/configure.ac b/configure.ac
022f11
index f48f3a3..62d558f 100644
022f11
--- a/configure.ac
022f11
+++ b/configure.ac
022f11
@@ -1633,6 +1633,7 @@ AC_CONFIG_FILES([Makefile
022f11
                  tests/charsets/Makefile
022f11
                  tests/data/Makefile
022f11
                  tests/disks/Makefile
022f11
+                 tests/disk-info/Makefile
022f11
                  tests/disk-labels/Makefile
022f11
                  tests/guests/Makefile
022f11
                  tests/guests/guests.xml
022f11
diff --git a/tests/disk-info/Makefile.am b/tests/disk-info/Makefile.am
022f11
new file mode 100644
022f11
index 0000000..5a34c47
022f11
--- /dev/null
022f11
+++ b/tests/disk-info/Makefile.am
022f11
@@ -0,0 +1,31 @@
022f11
+# libguestfs
022f11
+# Copyright (C) 2013 Red Hat Inc.
022f11
+#
022f11
+# This program is free software; you can redistribute it and/or modify
022f11
+# it under the terms of the GNU General Public License as published by
022f11
+# the Free Software Foundation; either version 2 of the License, or
022f11
+# (at your option) any later version.
022f11
+#
022f11
+# This program is distributed in the hope that it will be useful,
022f11
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
022f11
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
022f11
+# GNU General Public License for more details.
022f11
+#
022f11
+# You should have received a copy of the GNU General Public License
022f11
+# along with this program; if not, write to the Free Software
022f11
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
022f11
+
022f11
+include $(top_srcdir)/subdir-rules.mk
022f11
+
022f11
+EXTRA_DIST = \
022f11
+	$(TESTS)
022f11
+
022f11
+TESTS = \
022f11
+	disk-info.pl
022f11
+
022f11
+TESTS_ENVIRONMENT = $(top_builddir)/run --test $(VG)
022f11
+
022f11
+check-valgrind:
022f11
+	$(MAKE) VG="@VG@" check
022f11
+
022f11
+CLEANFILES = *~
022f11
diff --git a/tests/disk-info/disk-info.pl b/tests/disk-info/disk-info.pl
022f11
new file mode 100755
022f11
index 0000000..2fecc20
022f11
--- /dev/null
022f11
+++ b/tests/disk-info/disk-info.pl
022f11
@@ -0,0 +1,158 @@
022f11
+#!/usr/bin/perl -w
022f11
+# Test guestfs_disk_* functions.
022f11
+# Copyright (C) 2013 Red Hat Inc.
022f11
+#
022f11
+# This program is free software; you can redistribute it and/or modify
022f11
+# it under the terms of the GNU General Public License as published by
022f11
+# the Free Software Foundation; either version 2 of the License, or
022f11
+# (at your option) any later version.
022f11
+#
022f11
+# This program is distributed in the hope that it will be useful,
022f11
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
022f11
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
022f11
+# GNU General Public License for more details.
022f11
+#
022f11
+# You should have received a copy of the GNU General Public License
022f11
+# along with this program; if not, write to the Free Software
022f11
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
022f11
+
022f11
+use strict;
022f11
+use warnings;
022f11
+
022f11
+use File::Temp qw(tempdir);
022f11
+
022f11
+use Sys::Guestfs;
022f11
+
022f11
+# Bail on 32 bit perl.
022f11
+if (~1 == 4294967294) {
022f11
+    print "$0: test skipped because your perl is not 64 bits\n";
022f11
+    exit 77;
022f11
+}
022f11
+
022f11
+$ENV{LC_ALL} = "C";
022f11
+
022f11
+my $tmpdir = tempdir (CLEANUP => 1);
022f11
+my $errors = 0;
022f11
+
022f11
+my $g = Sys::Guestfs->new ();
022f11
+
022f11
+my ($format, $size, $backing);
022f11
+foreach $format ("raw", "qcow2") {
022f11
+    foreach $size (1024, 1024*1024, 1024*1024*1024, 1024*1024*1024*1024) {
022f11
+        foreach $backing (0, 1) {
022f11
+            # Raw files can't have backing files.
022f11
+            next if $format eq "raw" && $backing;
022f11
+
022f11
+            my $filename = create_disk ($tmpdir, $format, $size, $backing);
022f11
+
022f11
+            my $detected_format = $g->disk_format ($filename);
022f11
+            if ($detected_format ne $format) {
022f11
+                print_disk ($filename, $format, $size, $backing, \*STDERR);
022f11
+                printf STDERR ("unexpected format: detected %s, expected %s\n",
022f11
+                               $detected_format, $format);
022f11
+                $errors++;
022f11
+            }
022f11
+
022f11
+            my $detected_size = $g->disk_virtual_size ($filename);
022f11
+            if ($detected_size != $size) {
022f11
+                print_disk ($filename, $format, $size, $backing, \*STDERR);
022f11
+                printf STDERR ("unexpected size: detected %d, expected %d\n",
022f11
+                               $detected_size, $size);
022f11
+                $errors++;
022f11
+            }
022f11
+
022f11
+            my $detected_backing = $g->disk_has_backing_file ($filename);
022f11
+            if ($detected_backing != $backing) {
022f11
+                print_disk ($filename, $format, $size, $backing, \*STDERR);
022f11
+                printf STDERR ("unexpected backing file: detected %d, expected %d\n",
022f11
+                               $detected_backing, $backing);
022f11
+                $errors++;
022f11
+            }
022f11
+        }
022f11
+    }
022f11
+}
022f11
+
022f11
+# Check the negative cases too: file not found, file is a directory.
022f11
+# Note that since anything can be a raw file, there's no way to test
022f11
+# that this would fail for a non-disk-image.
022f11
+eval { $g->disk_format ($tmpdir . "/nosuchfile") };
022f11
+if (!$@) {
022f11
+    print STDERR "expected non-existent file to fail, but it did not\n";
022f11
+    $errors++;
022f11
+}
022f11
+if ($@ !~ /No such file/) {
022f11
+    print STDERR "unexpected error from non-existent file: $@\n";
022f11
+    $errors++;
022f11
+}
022f11
+
022f11
+my $testdir = $tmpdir . "/dir";
022f11
+mkdir $testdir, 0755;
022f11
+eval { $g->disk_format ($testdir) };
022f11
+if (!$@) {
022f11
+    print STDERR "expected directory fail, but it did not\n";
022f11
+    $errors++;
022f11
+}
022f11
+if ($@ !~ /is a directory/) {
022f11
+    print STDERR "unexpected error from directory: $@\n";
022f11
+    $errors++;
022f11
+}
022f11
+
022f11
+#----------------------------------------------------------------------
022f11
+
022f11
+my $unique = 0;
022f11
+sub get_unique
022f11
+{
022f11
+    return $unique++;
022f11
+}
022f11
+
022f11
+sub create_disk
022f11
+{
022f11
+    my $tmpdir = shift;
022f11
+    my $format = shift;
022f11
+    my $size = shift;
022f11
+    my $backing = shift;
022f11
+
022f11
+    my $options;
022f11
+
022f11
+    if ($backing) {
022f11
+        my $backing_file = sprintf ("%s/b%d", $tmpdir, get_unique ());
022f11
+        qemu_img_create ("raw", "", $backing_file, 1024*1024);
022f11
+        $options = "backing_file=%s"
022f11
+    }
022f11
+
022f11
+    my $filename = sprintf ("%s/d%d", $tmpdir, get_unique ());
022f11
+    qemu_img_create ($format, $options, $filename, $size);
022f11
+    return $filename;
022f11
+}
022f11
+
022f11
+sub qemu_img_create
022f11
+{
022f11
+    my $format = shift;
022f11
+    my $options = shift;
022f11
+    my $filename = shift;
022f11
+    my $size = shift;
022f11
+
022f11
+    my @cmd = ("qemu-img", "create", "-f", $format);
022f11
+    if (defined $options) {
022f11
+        push @cmd, "-o", $options;
022f11
+    }
022f11
+    push @cmd, $filename;
022f11
+    if (defined $size) {
022f11
+        push @cmd, $size;
022f11
+    }
022f11
+    system (@cmd) == 0 or die "system ", join (" ", @cmd), " failed: $?"
022f11
+}
022f11
+
022f11
+sub print_disk
022f11
+{
022f11
+    my $filename = shift;
022f11
+    my $format = shift;
022f11
+    my $size = shift;
022f11
+    my $backing = shift;
022f11
+    my $io = shift;
022f11
+
022f11
+    printf $io ("created disk %s: format=%s, size=%d, backing=%d\n",
022f11
+                $filename, $format, $size, $backing);
022f11
+}
022f11
+
022f11
+exit ($errors == 0 ? 0 : 1);
022f11
-- 
022f11
1.8.3.1
022f11