tools: Add basic tests for the virt-* tools.
authorRichard Jones <rjones@redhat.com>
Tue, 30 Mar 2010 13:18:44 +0000 (14:18 +0100)
committerRichard Jones <rjones@redhat.com>
Tue, 30 Mar 2010 13:20:23 +0000 (14:20 +0100)
.gitignore
tools/Makefile.am
tools/make-test-img.sh [new file with mode: 0755]
tools/test-virt-cat.sh [new file with mode: 0755]
tools/test-virt-df.sh [new file with mode: 0755]
tools/test-virt-list-filesystems.sh [new file with mode: 0755]
tools/test-virt-ls.sh [new file with mode: 0755]
tools/test-virt-tar.sh [new file with mode: 0755]

index 6a47234..dada5ad 100644 (file)
@@ -225,6 +225,7 @@ stamp-h1
 test-tool/libguestfs-test-tool.1
 test-tool/libguestfs-test-tool
 test-tool/libguestfs-test-tool-helper
+tools/test.img
 tools/virt-*.1
 /GNUmakefile
 /maint.mk
index d212d38..08f126d 100644 (file)
@@ -30,9 +30,12 @@ tools = \
        win-reg
 
 EXTRA_DIST = \
+       make-test-img.sh
        run-locally \
        $(tools:%=virt-%)
 
+CLEANFILES = test.img
+
 if HAVE_TOOLS
 
 bin_SCRIPTS = $(tools:%=virt-%)
@@ -61,4 +64,25 @@ $(top_builddir)/html/virt-%.1.html: virt-%
          --outfile html/$<.1.html \
          tools/$<
 
+# Tests.
+
+random_val := $(shell awk 'BEGIN{srand(); print 1+int(255*rand())}' < /dev/null)
+
+TESTS_ENVIRONMENT = \
+       MALLOC_PERTURB_=$(random_val) \
+       LD_LIBRARY_PATH=$(top_builddir)/src/.libs \
+       LIBGUESTFS_PATH=$(top_builddir)/appliance \
+       PERL5LIB=$(top_builddir)/perl/blib/lib:$(top_builddir)/perl/blib/arch
+
+# Build a standard test image to be used by all these tests.
+check_DATA = test.img
+test.img: make-test-img.sh
+       $(TESTS_ENVIRONMENT) $(srcdir)/make-test-img.sh
+
+TESTS = test-virt-cat.sh \
+       test-virt-df.sh \
+       test-virt-list-filesystems.sh \
+       test-virt-ls.sh \
+       test-virt-tar.sh
+
 endif
diff --git a/tools/make-test-img.sh b/tools/make-test-img.sh
new file mode 100755 (executable)
index 0000000..c6481cd
--- /dev/null
@@ -0,0 +1,88 @@
+#!/bin/bash -
+# libguestfs virt-* tools
+# Copyright (C) 2010 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+# Make a standard test image which is used by all the tools/test-*.sh
+# test scripts.  This test image is supposed to look like a Fedora
+# installation, or at least enough of one to fool virt-inspector's
+# heuristics.
+
+export LANG=C
+set -e
+
+rm -f test.img
+
+cat > fstab <<EOF
+LABEL=BOOT /boot ext2 default 0 0
+LABEL=ROOT / ext2 default 0 0
+EOF
+
+# Create a disk image.
+../fish/guestfish <<'EOF'
+sparse test.img- 512M
+run
+
+# Format the disk.
+part-init /dev/sda mbr
+part-add /dev/sda p 64     524287
+part-add /dev/sda p 524288    -64
+
+pvcreate /dev/sda2
+vgcreate VG /dev/sda2
+lvcreate Root VG 32
+lvcreate LV1 VG 32
+lvcreate LV2 VG 32
+lvcreate LV3 VG 64
+
+# Phony /boot filesystem.
+mkfs ext2 /dev/sda1
+set-e2label /dev/sda1 BOOT
+
+# Phony root filesystem.
+mkfs ext2 /dev/VG/Root
+set-e2label /dev/VG/Root ROOT
+
+# Enough to fool virt-inspector.
+mount-options "" /dev/VG/Root /
+mkdir /boot
+mount-options "" /dev/sda1 /boot
+mkdir /bin
+mkdir /etc
+mkdir /usr
+upload fstab /etc/fstab
+mkdir /boot/grub
+touch /boot/grub/grub.conf
+
+# Test files.
+write-file /etc/test1 "abcdefg" 0
+write-file /etc/test2 "" 0
+write-file /bin/test1 "abcdefg" 0
+write-file /bin/test2 "zxcvbnm" 0
+write-file /bin/test3 "1234567" 0
+write-file /bin/test4 "" 0
+ln-s /bin/test1 /bin/test5
+mkfifo 0777 /bin/test6
+mknod 0777 10 10 /bin/test7
+
+# Other filesystems.
+mkfs ext2 /dev/VG/LV1
+mkfs ext2 /dev/VG/LV2
+mkfs ext2 /dev/VG/LV3
+EOF
+
+rm fstab
+mv test.img- test.img
diff --git a/tools/test-virt-cat.sh b/tools/test-virt-cat.sh
new file mode 100755 (executable)
index 0000000..d87c4b8
--- /dev/null
@@ -0,0 +1,14 @@
+#!/bin/bash -
+
+export LANG=C
+set -e
+
+# Read out the test files from the image using virt-cat.
+if [ "$(./virt-cat test.img /etc/test1)" != "abcdefg" ]; then
+    echo "$0: error: mismatch in file test1"
+    exit 1
+fi
+if [ "$(./virt-cat test.img /etc/test2)" != "" ]; then
+    echo "$0: error: mismatch in file test2"
+    exit 1
+fi
diff --git a/tools/test-virt-df.sh b/tools/test-virt-df.sh
new file mode 100755 (executable)
index 0000000..8e61063
--- /dev/null
@@ -0,0 +1,46 @@
+#!/bin/bash -
+
+export LANG=C
+set -e
+
+# Run virt-df.
+output=$(./virt-df test.img -h)
+
+# The output will be slightly different from one machine to another.
+# So just do some tests to make sure it looks reasonable.
+
+# Check title is the first line.
+if [[ ! $output =~ ^Filesystem[[:space:]]+Size[[:space:]]+Used[[:space:]]+Available[[:space:]]+Use% ]]; then
+    echo "$0: error: no title line"
+    exit 1
+fi
+
+# Check 6 lines (title line + 5 * filesystems).
+if [ $(echo "$output" | wc -l) -ne 6 ]; then
+    echo "$0: error: not all filesystems were found"
+    exit 1
+fi
+
+# Check /dev/VG/LV[1-3] and /dev/VG/Root were found.
+if [[ ! $output =~ test.img:/dev/VG/LV1 ]]; then
+    echo "$0: error: filesystem /dev/VG/LV1 was not found"
+    exit 1
+fi
+if [[ ! $output =~ test.img:/dev/VG/LV2 ]]; then
+    echo "$0: error: filesystem /dev/VG/LV2 was not found"
+    exit 1
+fi
+if [[ ! $output =~ test.img:/dev/VG/LV3 ]]; then
+    echo "$0: error: filesystem /dev/VG/LV3 was not found"
+    exit 1
+fi
+if [[ ! $output =~ test.img:/dev/VG/Root ]]; then
+    echo "$0: error: filesystem /dev/VG/Root was not found"
+    exit 1
+fi
+
+# Check /dev/sda1 was found.  Might be called /dev/vda1.
+if [[ ! $output =~ test.img:/dev/[hsv]da1 ]]; then
+    echo "$0: error: filesystem /dev/VG/sda1 was not found"
+    exit 1
+fi
diff --git a/tools/test-virt-list-filesystems.sh b/tools/test-virt-list-filesystems.sh
new file mode 100755 (executable)
index 0000000..5fd4b08
--- /dev/null
@@ -0,0 +1,17 @@
+#!/bin/bash -
+
+export LANG=C
+set -e
+
+# Run virt-list-filesystems.
+# Only columns 1 & 2 are guaranteed, we may add more in future.
+if [ "$(./virt-list-filesystems -l test.img | sort | awk '{print $1 $2}')" \
+    != \
+"/dev/VG/LV1ext2
+/dev/VG/LV2ext2
+/dev/VG/LV3ext2
+/dev/VG/Rootext2
+/dev/sda1ext2" ]; then
+    echo "$0: error: unexpected output from virt-list-filesystems"
+    exit 1
+fi
diff --git a/tools/test-virt-ls.sh b/tools/test-virt-ls.sh
new file mode 100755 (executable)
index 0000000..d55b764
--- /dev/null
@@ -0,0 +1,19 @@
+#!/bin/bash -
+
+export LANG=C
+set -e
+
+# Just a random UUID.
+uuid=868b1447-0ec5-41bf-a2e5-6a77a4c9b66f
+
+# Read out the test directory using virt-ls.
+if [ "$(./virt-ls test.img /bin)" != "test1
+test2
+test3
+test4
+test5
+test6
+test7" ]; then
+    echo "$0: error: unexpected output from virt-ls"
+    exit 1
+fi
diff --git a/tools/test-virt-tar.sh b/tools/test-virt-tar.sh
new file mode 100755 (executable)
index 0000000..4ee87cb
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/bash -
+
+export LANG=C
+set -e
+
+# Read out the test directory using virt-tar.
+./virt-tar -x test.img /bin test.tar
+
+if [ "$(tar tf test.tar)" != "./
+./test1
+./test2
+./test3
+./test4
+./test5
+./test6
+./test7" ]; then
+    echo "$0: error: unexpected output in tarball from virt-tar"
+    exit 1
+fi
+
+rm test.tar