From d5a12cb33f416311f6a95aed98cda03b614230ec Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 16 Nov 2019 09:26:21 +0000 Subject: [PATCH] tests: Remove test-error.c and replace with a script that uses nbdsh. test-error.c was a strange test. It tested that ext2 filesystems could recover from block device errors after we trigger an error and then remove the trigger, which is kind of interesting but also irrelevant to NBD. It also failed to test that the EIO error is properly propagated back through libguestfs. test-error100.sh already tested error propagation correctly. This adds a new test that tests error triggering using nbdsh, and removes test-error.c. --- .gitignore | 1 - tests/Makefile.am | 8 +- tests/test-error-triggered.sh | 77 +++++++++++++++++++ tests/test-error.c | 171 ------------------------------------------ 4 files changed, 79 insertions(+), 178 deletions(-) create mode 100755 tests/test-error-triggered.sh delete mode 100644 tests/test-error.c diff --git a/.gitignore b/.gitignore index b6cfd735..c75beead 100644 --- a/.gitignore +++ b/.gitignore @@ -96,7 +96,6 @@ Makefile.in /tests/test-curl /tests/test-data /tests/test-delay -/tests/test-error /tests/test-exit-with-parent /tests/test-ext2 /tests/test-file diff --git a/tests/Makefile.am b/tests/Makefile.am index 8267664f..bc6fcaef 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -65,6 +65,7 @@ EXTRA_DIST = \ test-error0.sh \ test-error10.sh \ test-error100.sh \ + test-error-triggered.sh \ test-export-name.sh \ test-file-extents.sh \ test-floppy.sh \ @@ -990,16 +991,11 @@ test_delay_CFLAGS = $(WARNINGS_CFLAGS) $(LIBNBD_CFLAGS) test_delay_LDADD = $(LIBNBD_LIBS) # error filter test. -LIBGUESTFS_TESTS += test-error - -test_error_SOURCES = test-error.c test.h -test_error_CFLAGS = $(WARNINGS_CFLAGS) $(LIBGUESTFS_CFLAGS) -test_error_LDADD = libtest.la $(LIBGUESTFS_LIBS) - TESTS += \ test-error0.sh \ test-error10.sh \ test-error100.sh \ + test-error-triggered.sh \ $(NULL) # fua filter test. diff --git a/tests/test-error-triggered.sh b/tests/test-error-triggered.sh new file mode 100755 index 00000000..13cbeba6 --- /dev/null +++ b/tests/test-error-triggered.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +# nbdkit +# Copyright (C) 2018-2019 Red Hat Inc. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# * Neither the name of Red Hat nor the names of its contributors may be +# used to endorse or promote products derived from this software without +# specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. + +source ./functions.sh +set -e +set -x + +requires nbdsh --version + +sock=`mktemp -u` +files="$sock error-triggered.pid error-trigger" +rm -f $files +cleanup_fn rm -f $files + +# Run nbdkit with the error filter. +start_nbdkit -P error-triggered.pid -U $sock \ + --filter=error \ + pattern 1G \ + error-rate=100% error-file=error-trigger error=ENOSPC + +nbdsh --connect "nbd+unix://?socket=$sock" \ + -c ' +import os + +# The first operations should be fine. +h.pread (512, 0) +h.pread (512, 512) +h.pread (512, 1024) + +# Then we create the error-trigger file which should cause +# subsequent operations to fail with ENOSPC. +open ("error-trigger", "a").close () + +try: + h.pread (512, 0) + # This should not happen. + exit (1) +except nbd.Error as ex: + # Check the errno is expected. + assert ex.errno == "ENOSPC" + +# Remove the error-trigger file, operations should all succeed again. +os.unlink ("error-trigger") + +h.pread (512, 0) +h.pread (512, 512) +h.pread (512, 1024) +' diff --git a/tests/test-error.c b/tests/test-error.c deleted file mode 100644 index 12976bf4..00000000 --- a/tests/test-error.c +++ /dev/null @@ -1,171 +0,0 @@ -/* nbdkit - * Copyright (C) 2018 Red Hat Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of Red Hat nor the names of its contributors may be - * used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "test.h" - -int -main (int argc, char *argv[]) -{ - guestfs_h *g; - int r; - FILE *fp; - char *data; -#if 0 - int error; -#endif - char tmpdir[] = "/tmp/errorXXXXXX"; - char error_file[] = "/tmp/errorXXXXXX/trigger"; - char error_file_param[] = "error-file=/tmp/errorXXXXXX/trigger"; - - /* Create temporary directory to store the trigger file. */ - if (mkdtemp (tmpdir) == NULL) { - perror ("mkdtemp"); - exit (EXIT_FAILURE); - } - - memcpy (error_file, tmpdir, strlen (tmpdir)); - memcpy (&error_file_param[11], tmpdir, strlen (tmpdir)); - - if (test_start_nbdkit ("--filter", "error", - "memory", "1M", - "error=EIO", - "error-rate=100%", - error_file_param, - NULL) == -1) - exit (EXIT_FAILURE); - - g = guestfs_create (); - if (g == NULL) { - perror ("guestfs_create"); - exit (EXIT_FAILURE); - } - - r = guestfs_add_drive_opts (g, "", - GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", - GUESTFS_ADD_DRIVE_OPTS_PROTOCOL, "nbd", - GUESTFS_ADD_DRIVE_OPTS_SERVER, server, - -1); - if (r == -1) - exit (EXIT_FAILURE); - - if (guestfs_launch (g) == -1) - exit (EXIT_FAILURE); - - /* Format the disk with a filesystem. No errors are being injected - * yet so we expect this to work. - */ - if (guestfs_part_disk (g, "/dev/sda", "mbr") == -1) - exit (EXIT_FAILURE); - - if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1) - exit (EXIT_FAILURE); - - if (guestfs_mount (g, "/dev/sda1", "/") == -1) - exit (EXIT_FAILURE); - -#define filename "/hello.txt" -#define content "hello, people of the world" - - if (guestfs_write (g, filename, content, strlen (content)) == -1) - exit (EXIT_FAILURE); - - /* Try as hard as we can to sync data and kill the libguestfs cache. */ - if (guestfs_sync (g) == -1) - exit (EXIT_FAILURE); - if (guestfs_drop_caches (g, 3) == -1) - exit (EXIT_FAILURE); - sleep (1); - - /* Now start injecting EIO errors. */ - fp = fopen (error_file, "w"); - if (fp == NULL) { - perror (error_file); - exit (EXIT_FAILURE); - } - fclose (fp); - - data = guestfs_cat (g, filename); - if (data != NULL) { - fprintf (stderr, - "%s: error: " - "expecting Input/output error, but read data!\n", - program_name); - exit (EXIT_FAILURE); - } - -#if 0 - /* Apparently libguestfs doesn't preserve the errno here yet XXX */ - error = guestfs_last_errno (g); - if (error != EIO) { - fprintf (stderr, "%s: error: expecting errno = EIO, but got %d\n", - program_name, error); - exit (EXIT_FAILURE); - } -#endif - - /* Stop injecting errors, hope that the filesystem recovers. */ - unlink (error_file); - - /* But we'll probably have to remount the filesystem because ext2 - * will get itself into a "state". - */ - if (guestfs_umount (g, "/") == -1) - exit (EXIT_FAILURE); - if (guestfs_mount (g, "/dev/sda1", "/") == -1) - exit (EXIT_FAILURE); - - data = guestfs_cat (g, filename); - if (data == NULL) - exit (EXIT_FAILURE); - if (strcmp (data, content) != 0) { - fprintf (stderr, "%s: error: read unexpected data\n", program_name); - exit (EXIT_FAILURE); - } - - guestfs_close (g); - - rmdir (tmpdir); - - exit (EXIT_SUCCESS); -} -- 2.11.4.GIT