[Libguestfs] [PATCH] tests: Replace test-max-disks with several tests.

Richard W.M. Jones rjones at redhat.com
Thu Apr 20 12:55:44 UTC 2017


Replace the monolithic 'test-max-disks.pl' script with a test program
written in C.  The program is completely equivalent to the old script,
except for the enhancement that it is able to detect if disks are
added to the appliance in the wrong order.

The tests themselves are split out into some shell scripts:

 - test-27-disks: Fully tests 27 disks.
   This is the minimum supported configuration for all backends.

 - test-255-disks: Fully tests 255 disks.
   This is the most disks that libguestfs up to 1.36 supported.

 - test-add-lots-of-disks: Add ‘a lot’ of disks and exit (without
   any further testing).  This is meant to try to exercise > 255
   disk case but without being as slow as a test of the max number
   of disks (which takes ages, even for as few as 4000 disks).

 - test-max-disks: Test the maximum possible number of disks.
   The maximum depends on several factors, notably which backend
   is in use, and the limit on the number of open files.

   ‘test-max-disks’ is a slow test.
---
 .gitignore                            |   1 +
 tests/disks/Makefile.am               |  37 ++-
 tests/disks/test-255-disks.sh         |  25 ++
 tests/disks/test-27-disks.sh          |  26 ++
 tests/disks/test-add-disks.c          | 510 ++++++++++++++++++++++++++++++++++
 tests/disks/test-add-lots-of-disks.sh |  25 ++
 tests/disks/test-max-disks.pl         | 149 ----------
 tests/disks/test-max-disks.sh         |  24 ++
 8 files changed, 645 insertions(+), 152 deletions(-)
 create mode 100755 tests/disks/test-255-disks.sh
 create mode 100755 tests/disks/test-27-disks.sh
 create mode 100644 tests/disks/test-add-disks.c
 create mode 100755 tests/disks/test-add-lots-of-disks.sh
 delete mode 100755 tests/disks/test-max-disks.pl
 create mode 100755 tests/disks/test-max-disks.sh

diff --git a/.gitignore b/.gitignore
index 3b50afdcd..152a40051 100644
--- a/.gitignore
+++ b/.gitignore
@@ -560,6 +560,7 @@ Makefile.in
 /tests/c-api/test-user-cancel
 /tests/charsets/test-charset-fidelity
 /tests/daemon/captive-daemon.pm
+/tests/disks/test-add-disks
 /tests/disks/test-qemu-drive-libvirt.xml
 /tests/events/test-libvirt-auth-callbacks
 /tests/mount-local/test-parallel-mount-local
diff --git a/tests/disks/Makefile.am b/tests/disks/Makefile.am
index 8cc5bdcb0..779871aff 100644
--- a/tests/disks/Makefile.am
+++ b/tests/disks/Makefile.am
@@ -17,17 +17,48 @@
 
 include $(top_srcdir)/subdir-rules.mk
 
+TESTS_ENVIRONMENT = \
+	$(top_builddir)/run --test
+
 TESTS = \
-	test-max-disks.pl \
 	test-qemu-drive.sh
 
 if HAVE_LIBVIRT
 TESTS += \
 	test-qemu-drive-libvirt.sh
+
+if ENABLE_APPLIANCE
+TESTS += \
+	test-27-disks.sh \
+	test-255-disks.sh \
+	test-add-lots-of-disks.sh
+endif
 endif
 
-TESTS_ENVIRONMENT = \
-	$(top_builddir)/run --test
+SLOW_TESTS = \
+	test-max-disks.sh
+
+TESTS += \
+	$(SLOW_TESTS)
+
+check-slow:
+	$(MAKE) check TESTS="$(SLOW_TESTS)" SLOW=1
+
+check_PROGRAMS = test-add-disks
+
+test_add_disks_SOURCES = \
+	test-add-disks.c
+test_add_disks_CPPFLAGS = \
+	-I$(top_srcdir)/common/utils -I$(top_builddir)/common/utils \
+	-I$(top_srcdir)/lib -I$(top_builddir)/lib \
+	-I$(top_srcdir)/gnulib/lib -I$(top_builddir)/gnulib/lib
+test_add_disks_CFLAGS = \
+	$(WARN_CFLAGS) $(WERROR_CFLAGS)
+test_add_disks_LDADD = \
+	$(top_builddir)/common/utils/libutils.la \
+	$(top_builddir)/lib/libguestfs.la \
+	$(top_builddir)/gnulib/lib/libgnu.la \
+	$(LIBXML2_LIBS)
 
 EXTRA_DIST = \
 	debug-qemu.sh \
diff --git a/tests/disks/test-255-disks.sh b/tests/disks/test-255-disks.sh
new file mode 100755
index 000000000..3c75dab19
--- /dev/null
+++ b/tests/disks/test-255-disks.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+# Copyright (C) 2012-2017 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.
+
+set -e
+
+$TEST_FUNCTIONS
+skip_if_skipped
+
+skip_unless_backend libvirt
+
+./test-add-disks -n 255
diff --git a/tests/disks/test-27-disks.sh b/tests/disks/test-27-disks.sh
new file mode 100755
index 000000000..c2cf7b223
--- /dev/null
+++ b/tests/disks/test-27-disks.sh
@@ -0,0 +1,26 @@
+#!/bin/bash
+# Copyright (C) 2012-2017 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.
+
+# The significance of 27 is that it's the minimum number of disks
+# supported by every known backend in every supported configuration.
+
+set -e
+
+$TEST_FUNCTIONS
+skip_if_skipped
+
+./test-add-disks -n 27
diff --git a/tests/disks/test-add-disks.c b/tests/disks/test-add-disks.c
new file mode 100644
index 000000000..b968bd352
--- /dev/null
+++ b/tests/disks/test-add-disks.c
@@ -0,0 +1,510 @@
+/* Test libguestfs with large/maximum number of disks.
+ * Copyright (C) 2012-2017 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 <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <unistd.h>
+#include <limits.h>
+#include <getopt.h>
+#include <errno.h>
+#include <error.h>
+#include <pwd.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <assert.h>
+
+#include <guestfs.h>
+#include "guestfs-internal-frontend.h"
+
+#include "getprogname.h"
+
+static ssize_t get_max_disks (guestfs_h *g);
+static void do_test (guestfs_h *g, size_t ndisks, bool just_add);
+static void make_disks (const char *tmpdir);
+static void rm_disks (void);
+
+static size_t ndisks;
+static char **disks;
+
+static void __attribute__((noreturn))
+usage (int status)
+{
+  if (status != EXIT_SUCCESS)
+    fprintf (stderr, "Try ‘%s --help’ for more information.\n",
+             getprogname ());
+  else {
+    printf ("Test libguestfs with large/maximum number of disks.\n"
+            "\n"
+            "Usage:\n"
+            "  %s -n NR_DISKS\n"
+            "          Do a full test with NR_DISKS.\n"
+            "  %s --max\n"
+            "          Do a full test with the max number of disks *.\n"
+            "  %s --just-add [-n N | --max]\n"
+            "          Don't do a full test, only add the disks and exit.\n"
+            "\n"
+            "Options:\n"
+            "  --help             Display this help and exit.\n"
+            "  --just-add         Only add the disks and exit if successful.\n"
+            "  --max              Test max disks possible *.\n"
+            "  -n NR_DISKS        Test NR_DISKS.\n"
+            "  -v | --verbose     Enable libguestfs debugging.\n"
+            "  -x | --trace       Enable libguestfs tracing.\n"
+            "\n"
+            "* Note that the max number of disks depends on the backend and\n"
+            "  limit on the number of open file descriptors (ulimit -n).\n",
+            getprogname (), getprogname (), getprogname ());
+  }
+  exit (status);
+}
+
+int
+main (int argc, char *argv[])
+{
+  enum { HELP_OPTION = CHAR_MAX + 1 };
+  static const char options[] = "amn:vVx";
+  static const struct option long_options[] = {
+    { "help", 0, 0, HELP_OPTION },
+    { "just-add", 0, 0, 0 },
+    { "max", 0, 0, 'm' },
+    { "trace", 0, 0, 'x' },
+    { "verbose", 0, 0, 'v' },
+    { "version", 0, 0, 'V' },
+    { 0, 0, 0, 0 }
+  };
+  int c;
+  int option_index;
+  bool just_add = false;
+  guestfs_h *g;
+  char *tmpdir;
+  ssize_t n = -1; /* -1: not set  0: max  > 0: specific value */
+
+  g = guestfs_create ();
+  if (g == NULL)
+    error (EXIT_FAILURE, errno, "guestfs_create");
+
+  for (;;) {
+    c = getopt_long (argc, argv, options, long_options, &option_index);
+    if (c == -1) break;
+
+    switch (c) {
+    case 0:			/* options which are long only */
+      if (STREQ (long_options[option_index].name, "just-add"))
+        just_add = true;
+      else
+        error (EXIT_FAILURE, 0,
+               "unknown long option: %s (%d)",
+               long_options[option_index].name, option_index);
+      break;
+
+    case 'm':
+      n = 0;
+      break;
+
+    case 'n':
+      if (sscanf (optarg, "%zd", &n) != 1 || n <= 0)
+        error (EXIT_FAILURE, 0, "cannot parse -n option");
+      break;
+
+    case 'x':
+      guestfs_set_trace (g, 1);
+      break;
+
+    case 'v':
+      guestfs_set_verbose (g, 1);
+      break;
+
+    case 'V':
+      printf ("%s %s\n",
+              getprogname (),
+              PACKAGE_VERSION_FULL);
+      exit (EXIT_SUCCESS);
+
+    case HELP_OPTION:
+      usage (EXIT_SUCCESS);
+
+    default:
+      usage (EXIT_FAILURE);
+    }
+  }
+
+  if (n == -1)
+    error (EXIT_FAILURE, 0, "either -n NR_DISKS or --max must be specified");
+
+  if (n == 0) {
+    n = get_max_disks (g);
+    if (n == -1)
+      error (EXIT_FAILURE, 0, "cannot calculate --max disks");
+  }
+  ndisks = n;
+
+  tmpdir = guestfs_get_cachedir (g);
+  if (tmpdir == NULL)
+    exit (EXIT_FAILURE);
+  make_disks (tmpdir);
+  free (tmpdir);
+  atexit (rm_disks);
+
+  do_test (g, ndisks, just_add);
+
+  if (guestfs_shutdown (g) == -1)
+    exit (EXIT_FAILURE);
+  guestfs_close (g);
+
+  exit (EXIT_SUCCESS);
+}
+
+/**
+ * Work out the maximum number of disks that could be added to the
+ * libguestfs appliance, based on these factors:
+ *
+ * =over 4
+ *
+ * =item the current backend
+ *
+ * =item the max number of open file descriptors (RLIMIT_NOFILE)
+ *
+ * =back
+ */
+static ssize_t
+get_max_disks (guestfs_h *g)
+{
+  ssize_t ret;
+  struct rlimit rlim;
+  /* We reserve a little bit of "headroom" because qemu uses more
+   * file descriptors than just the disk files.
+   */
+  const unsigned fd_headroom = 32;
+
+  ret = guestfs_max_disks (g);
+  if (ret == -1)
+    return -1;
+
+  if (getrlimit (RLIMIT_NOFILE, &rlim) == -1) {
+    perror ("getrlimit: RLIMIT_NOFILE");
+    return -1;
+  }
+  if (rlim.rlim_cur > fd_headroom) {
+    if ((size_t) ret > rlim.rlim_cur - fd_headroom) {
+      if (rlim.rlim_max > rlim.rlim_cur)
+        fprintf (stderr,
+                 "%s: warning: to get more complete testing, increase\n"
+                 "file limit up to hard limit:\n"
+                 "\n"
+                 "$ ulimit -Hn %lu\n"
+                 "\n",
+                 getprogname (), (unsigned long) rlim.rlim_max);
+      else {
+        struct passwd *pw;
+        unsigned long suggested_limit = ret + fd_headroom;
+
+        pw = getpwuid (geteuid ());
+        fprintf (stderr,
+                 "%s: warning: to get more complete testing, increase\n"
+                 "file descriptor limit to >= %lu.\n"
+                 "\n"
+                 "To do this, add this line to /etc/security/limits.conf:\n"
+                 "\n"
+                 "%s  hard  nofile  %lu\n"
+                 "\n",
+                 getprogname (), suggested_limit,
+                 pw ? pw->pw_name : "your_username",
+                 suggested_limit);
+      }
+
+      ret = rlim.rlim_cur - fd_headroom;
+    }
+  }
+
+  printf ("max_disks = %zd\n", ret);
+  return ret;
+}
+
+static void
+do_test (guestfs_h *g, size_t ndisks, bool just_add)
+{
+  size_t i, j, k, n;
+  unsigned errors;
+  CLEANUP_FREE_STRING_LIST char **devices = NULL;
+  CLEANUP_FREE_STRING_LIST char **partitions = NULL;
+
+  for (i = 0; i < ndisks; ++i) {
+    if (guestfs_add_drive_opts (g, disks[i],
+                                GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
+                                GUESTFS_ADD_DRIVE_OPTS_CACHEMODE, "unsafe",
+                                -1) == -1)
+      exit (EXIT_FAILURE);
+  }
+
+  if (guestfs_launch (g) == -1)
+    exit (EXIT_FAILURE);
+
+  /* Check the disks were added. */
+  devices = guestfs_list_devices (g);
+  n = guestfs_int_count_strings (devices);
+  if (n != ndisks) {
+    fprintf (stderr, "%s: incorrect number of devices returned by guestfs_list_devices:\n",
+             getprogname ());
+    fprintf (stderr, "counted %zu, expecting %zu\n", n, ndisks);
+    fprintf (stderr, "devices:\n");
+    for (i = 0; i < n; ++i)
+      fprintf (stderr, "\t%s\n", devices[i]);
+    exit (EXIT_FAILURE);
+  }
+
+  /* If the --just-add option was given, we're done. */
+  if (just_add)
+    return;
+
+  /* Check each device has the expected drive name, eg. /dev/sda,
+   * /dev/sdb, ..., /dev/sdaa, ...
+   */
+  for (i = 0; i < ndisks; ++i) {
+    char expected[64];
+
+    guestfs_int_drive_name (i, expected);
+    if (!STRSUFFIX (devices[i], expected)) {
+      fprintf (stderr,
+               "%s: incorrect device name at index %zu: "
+               "%s (expected suffix %s)\n",
+               getprogname (), i, devices[i], expected);
+      exit (EXIT_FAILURE);
+    }
+  }
+
+  /* Check drive index. */
+  for (i = 0; i < ndisks; ++i) {
+    int idx;
+
+    idx = guestfs_device_index (g, devices[i]);
+    if (idx == -1)
+      exit (EXIT_FAILURE);
+    if ((int) i != idx) {
+      fprintf (stderr,
+               "%s: incorrect device index for %s: "
+               "expected %zu by got %d\n",
+               getprogname (), devices[i], i, idx);
+      exit (EXIT_FAILURE);
+    }
+  }
+
+  /* Check the disk index written at the start of each disk.  This
+   * ensures that disks are added to the appliance in the same order
+   * that we called guestfs_add_drive.
+   */
+  errors = 0;
+  for (i = 0; i < ndisks; ++i) {
+    CLEANUP_FREE char *buf = NULL;
+    size_t j, r;
+
+    buf = guestfs_pread_device (g, devices[i], sizeof j, 0, &r);
+    if (buf == NULL)
+      exit (EXIT_FAILURE);
+    if (r != sizeof j)
+      error (EXIT_FAILURE, 0, "pread_device read incorrect number of bytes");
+    memcpy (&j, buf, r);
+    if (i != j) {
+      if (errors == 0)
+        fprintf (stderr, "%s: incorrect device enumeration\n",
+                 getprogname ());
+      errors++;
+      fprintf (stderr, "%s at device index %zu was added with index %zu\n",
+               devices[i], i, j);
+    }
+  }
+  if (errors > 0)
+    exit (EXIT_FAILURE);
+
+  /* Put some data on each disk to check they are writable and
+   * mountable.
+   */
+  for (i = 0; i < ndisks; ++i) {
+    CLEANUP_FREE char *mp = NULL;
+    CLEANUP_FREE char *part = NULL;
+    CLEANUP_FREE char *file = NULL;
+    CLEANUP_FREE char *data = NULL;
+
+    if (asprintf (&mp, "/mp%zu", i) == -1)
+      error (EXIT_FAILURE, errno, "asprintf");
+
+    if (guestfs_mkmountpoint (g, mp) == -1)
+      exit (EXIT_FAILURE);
+
+    /* To save time in the test, add 15 partitions to the first disk
+     * and last disks only, and 1 partition to every other disk.  Note
+     * that 15 partitions is the max allowed by virtio-blk.
+     */
+    if (i == 0 || i == ndisks-1) {
+      if (guestfs_part_init (g, devices[i], "gpt") == -1)
+        exit (EXIT_FAILURE);
+      for (j = 1; j <= 14; ++j) {
+        if (guestfs_part_add (g, devices[i], "p", 64*j, 64*j+63) == -1)
+          exit (EXIT_FAILURE);
+      }
+      if (guestfs_part_add (g, devices[i], "p", 64*15, -64) == -1)
+        exit (EXIT_FAILURE);
+      if (asprintf (&part, "%s15", devices[i]) == -1)
+        error (EXIT_FAILURE, errno, "asprintf");
+    }
+    else {
+      if (guestfs_part_disk (g, devices[i], "mbr") == -1)
+        exit (EXIT_FAILURE);
+      if (asprintf (&part, "%s1", devices[i]) == -1)
+        error (EXIT_FAILURE, errno, "asprintf");
+    }
+
+    if (guestfs_mkfs (g, "ext2", part) == -1)
+      exit (EXIT_FAILURE);
+    if (guestfs_mount (g, part, mp) == -1)
+      exit (EXIT_FAILURE);
+
+    if (asprintf (&file, "%s/disk%zu", mp, i) == -1)
+      error (EXIT_FAILURE, errno, "asprintf");
+    if (asprintf (&data, "This is disk %zu.", i) == -1)
+      error (EXIT_FAILURE, errno, "asprintf");
+
+    if (guestfs_write (g, file, data, strlen (data)) == -1)
+      exit (EXIT_FAILURE);
+  }
+
+  for (i = 0; i < ndisks; ++i) {
+    CLEANUP_FREE char *file = NULL, *expected = NULL, *actual = NULL;
+
+    if (asprintf (&file, "/mp%zu/disk%zu", i, i) == -1)
+      error (EXIT_FAILURE, errno, "asprintf");
+    if (asprintf (&expected, "This is disk %zu.", i) == -1)
+      error (EXIT_FAILURE, errno, "asprintf");
+
+    actual = guestfs_cat (g, file);
+    if (actual == NULL)
+      exit (EXIT_FAILURE);
+
+    if (STRNEQ (expected, actual)) {
+      fprintf (stderr,
+               "%s: unexpected content in file %s: "
+               "expected \"%s\", actual \"%s\"\n",
+               getprogname (), file,
+               expected, actual);
+      exit (EXIT_FAILURE);
+    }
+  }
+
+  /* Finally check the partition list. */
+  partitions = guestfs_list_partitions (g);
+  if (partitions == NULL)
+    exit (EXIT_FAILURE);
+
+  k = 0;
+  for (i = 0; i < ndisks; ++i) {
+    char dev[64];
+
+    guestfs_int_drive_name (i, dev);
+
+    if (i == 0 || i == ndisks-1) {
+      for (j = 1; j <= 15; ++j) {
+        CLEANUP_FREE char *expected = NULL;
+        const char *p;
+
+        if (asprintf (&expected, "%s%zu", dev, j) == -1)
+          error (EXIT_FAILURE, errno, "asprintf");
+        p = partitions[k++];
+        if (!STRSUFFIX (p, expected)) {
+          fprintf (stderr,
+                   "%s: incorrect partition name at index %zu, %zu: "
+                   "%s (expected suffix %s)\n",
+                   getprogname (), i, j, p, expected);
+          exit (EXIT_FAILURE);
+        }
+      }
+    }
+    else {
+      CLEANUP_FREE char *expected = NULL;
+      const char *p;
+
+      if (asprintf (&expected, "%s1", dev) == -1)
+        error (EXIT_FAILURE, errno, "asprintf");
+      p = partitions[k++];
+      if (!STRSUFFIX (p, expected)) {
+        fprintf (stderr,
+                 "%s: incorrect partition name at index %zu: "
+                 "%s (expected suffix %s)\n",
+                 getprogname (), i, p, expected);
+        exit (EXIT_FAILURE);
+      }
+    }
+  }
+}
+
+static void
+make_disks (const char *tmpdir)
+{
+  size_t i;
+  int fd;
+
+  assert (ndisks > 0);
+
+  disks = calloc (ndisks, sizeof (char *));
+  if (disks == NULL)
+    error (EXIT_FAILURE, errno, "calloc");
+
+  for (i = 0; i < ndisks; ++i) {
+    if (asprintf (&disks[i], "%s/testdiskXXXXXX", tmpdir) == -1)
+      error (EXIT_FAILURE, errno, "asprintf");
+    fd = mkstemp (disks[i]);
+    if (fd == -1)
+      error (EXIT_FAILURE, errno, "mkstemp: %s", disks[i]);
+
+    /* Create a raw format 1MB disk, and write the disk number at the
+     * start of the disk, so that we can later check that disks are
+     * added in the right order to the appliance.
+     *
+     * Note that we write the disk number in whatever is the current
+     * endian/integer size, which is fine because we'll only check it
+     * from the same program.
+     */
+    if (ftruncate (fd, 1024*1024) == -1)
+      error (EXIT_FAILURE, errno, "ftruncate: %s", disks[i]);
+    if (write (fd, &i, sizeof i) != sizeof i)
+      error (EXIT_FAILURE, errno, "write: %s", disks[i]);
+    if (close (fd) == -1)
+      error (EXIT_FAILURE, errno, "close: %s", disks[i]);
+  }
+}
+
+/* Called by an atexit handler. */
+static void
+rm_disks (void)
+{
+  size_t i;
+
+  if (disks == NULL)
+    return;
+
+  for (i = 0; i < ndisks; ++i) {
+    unlink (disks[i]);
+    free (disks[i]);
+  }
+
+  free (disks);
+  disks = NULL;
+}
diff --git a/tests/disks/test-add-lots-of-disks.sh b/tests/disks/test-add-lots-of-disks.sh
new file mode 100755
index 000000000..d59753338
--- /dev/null
+++ b/tests/disks/test-add-lots-of-disks.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+# Copyright (C) 2012-2017 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.
+
+set -e
+
+N=255
+
+$TEST_FUNCTIONS
+skip_if_skipped
+
+./test-add-disks --just-add -n $N
diff --git a/tests/disks/test-max-disks.pl b/tests/disks/test-max-disks.pl
deleted file mode 100755
index 11d465189..000000000
--- a/tests/disks/test-max-disks.pl
+++ /dev/null
@@ -1,149 +0,0 @@
-#!/usr/bin/env perl
-# 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.
-
-# Test adding maximum number of disks to the guest.
-
-use strict;
-use warnings;
-
-use Sys::Guestfs;
-
-my $errors = 0;
-
-my $g = Sys::Guestfs->new ();
-
-my $max_disks = $g->max_disks ();
-printf "max_disks is %d\n", $max_disks;
-
-# Create large number of disks.
-my ($name, $i, $j);
-for ($i = 0; $i < $max_disks; ++$i) {
-    $g->add_drive_scratch (1024*1024);
-}
-
-$g->launch ();
-
-# Check the disks were added.
-my @devices = $g->list_devices ();
-if (@devices != $max_disks) {
-    print STDERR "$0: incorrect number of devices returned by \$g->list_devices:\n";
-    print STDERR "$0: \@devices = ", join (" ", @devices), "\n";
-    $errors++;
-}
-
-for ($i = 0; $i < $max_disks; ++$i) {
-    my $expected = drive_name ($i);
-    unless ($devices[$i] =~ m{/dev/[abce-ln-z]+d$expected$}) {
-        print STDERR "$0: incorrect device name at index $i: ",
-            "expected /dev/sd$expected, but got $devices[$i]\n";
-        $errors++;
-    }
-}
-
-# Check device_index.
-for ($i = 0; $i < $max_disks; ++$i) {
-    if ($i != $g->device_index ($devices[$i])) {
-        print STDERR "$0: incorrect device index for $devices[$i]\n";
-        $errors++;
-    }
-}
-
-# Put some data on each disk to check they are mountable, writable etc.
-for ($i = 0; $i < $max_disks; ++$i) {
-    my $dev = $devices[$i];
-    $g->mkmountpoint ("/mp$i");
-
-    # To save time in the test, add 15 partitions to the first disk
-    # and last disks only, and 1 partition to every other disk.  Note
-    # that 15 partitions is the max allowed by virtio-blk.
-    my $part;
-    if ($i == 0 || $i == $max_disks-1) {
-        $g->part_init ($dev, "gpt");
-        for ($j = 1; $j <= 14; ++$j) {
-            $g->part_add ($dev, "p", 64*$j, 64*$j+63);
-        }
-        $g->part_add ($dev, "p", 64*15, -64);
-        $part = $dev . "15";
-    }
-    else {
-        $g->part_disk ($dev, "mbr");
-        $part = $dev . "1";
-    }
-    $g->mkfs ("ext2", "$part");
-    $g->mount ("$part", "/mp$i");
-    $g->write ("/mp$i/disk$i", "This is disk #$i.\n");
-}
-
-for ($i = 0; $i < $max_disks; ++$i) {
-    if ($g->cat ("/mp$i/disk$i") ne "This is disk #$i.\n") {
-        print STDERR "$0: unexpected content in file /mp$i/disk$i\n";
-        $errors++;
-    }
-}
-
-# Enumerate and check partition names.
-my @partitions = $g->list_partitions ();
-if (@partitions != $max_disks + 14*2) {
-    print STDERR "$0: incorrect number of partitions returned by \$g->list_partitions:\n";
-    print STDERR "$0: \@partitions = ", join (" ", @partitions), "\n";
-    $errors++;
-}
-
-for ($i = 0, $j = 0; $i < $max_disks; ++$i) {
-    my $expected = drive_name ($i);
-    if ($i == 0 || $i == $max_disks-1) {
-        unless ($partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}1$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}2$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}3$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}4$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}5$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}6$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}7$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}8$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}9$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}10$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}11$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}12$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}13$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}14$} &&
-                $partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}15$}) {
-            print STDERR "$0: incorrect partition name at index $i\n";
-            $errors++;
-        }
-    } else {
-        unless ($partitions[$j++] =~ m{/dev/[abce-ln-z]+d${expected}1$}) {
-            print STDERR "$0: incorrect partition name at index $i\n";
-            $errors++;
-        }
-    }
-}
-
-$g->shutdown ();
-$g->close ();
-
-exit ($errors == 0 ? 0 : 1);
-
-sub drive_name
-{
-    my $index = shift;
-    my $prefix = "";
-    if ($index >= 26) {
-        $prefix = drive_name ($index/26 - 1);
-    }
-    $index %= 26;
-    return $prefix . chr (97 + $index);
-}
diff --git a/tests/disks/test-max-disks.sh b/tests/disks/test-max-disks.sh
new file mode 100755
index 000000000..61ea95644
--- /dev/null
+++ b/tests/disks/test-max-disks.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+# Copyright (C) 2012-2017 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.
+
+set -e
+
+$TEST_FUNCTIONS
+skip_if_skipped
+slow_test
+
+./test-add-disks --max
-- 
2.12.0




More information about the Libguestfs mailing list