]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virshtest: Adapt 'virsh-read-bufsiz' and 'virsh-read-non-seekable'
authorPeter Krempa <pkrempa@redhat.com>
Thu, 21 Mar 2024 16:50:09 +0000 (17:50 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 2 Apr 2024 12:24:30 +0000 (14:24 +0200)
Test both situations (reading from non-regular file and reading a file
larger than (arbitrary) buffer size) via 'virshtest'.

To feed the pipe we need to create a thread that does it, but otherwise
it's fairly straightforward.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tests/meson.build
tests/virsh-read-bufsiz [deleted file]
tests/virsh-read-non-seekable [deleted file]
tests/virshtest.c
tests/virshtestdata/read-big-pipe.out [new file with mode: 0644]

index 46e1679ad75235284f90bbc2bca1e464820b30f7..c760fe68d9b590e227bf75350125371cc7e78c4d 100644 (file)
@@ -691,8 +691,6 @@ if conf.has('WITH_LIBVIRTD')
     'libvirtd-fail',
     'libvirtd-pool',
     'virsh-auth',
-    'virsh-read-bufsiz',
-    'virsh-read-non-seekable',
     'virsh-self-test',
     'virsh-uriprecedence',
     'virt-admin-self-test',
diff --git a/tests/virsh-read-bufsiz b/tests/virsh-read-bufsiz
deleted file mode 100755 (executable)
index a61816c..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-#!/bin/sh
-# ensure that reading a file larger than BUFSIZ works
-
-# Copyright (C) 2008, 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, see
-# <http://www.gnu.org/licenses/>.
-
-. "$(dirname $0)/test-lib.sh"
-
-if test "$VERBOSE" = yes; then
-  set -x
-  $abs_top_builddir/tools/virsh --version
-fi
-
-fail=0
-
-# Output a valid definition, to be used as input.
-$abs_top_builddir/tools/virsh -c test:///default dumpxml 1 > xml.t || fail=1
-
-# Change the VM name and UUID
-sed -e "s|<name>test</name>|<name>newtest</name>|g" \
-  -e "\|<uuid>.*</uuid>|d" \
-  xml.t > xml
-
-for i in before after; do
-  # The largest BUFSIZ I've seen is 128K.  This is slightly larger.
-  printf %132000s ' ' > sp || fail=1
-  in=in-$i
-  # Append or prepend enough spaces to push the size over the limit:
-  ( test $i = before && cat sp xml || cat xml sp ) > $in || fail=1
-
-  $abs_top_builddir/tools/virsh --connect test:///default define $in > out || fail=1
-  printf "Domain 'newtest' defined from $in\n\n" > exp || fail=1
-  compare exp out || fail=1
-done
-
-(exit $fail); exit $fail
diff --git a/tests/virsh-read-non-seekable b/tests/virsh-read-non-seekable
deleted file mode 100755 (executable)
index 0f7504c..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-#!/bin/sh
-# ensure that certain file-reading commands can handle non-seekable files
-
-# Copyright (C) 2008 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, see
-# <http://www.gnu.org/licenses/>.
-
-. "$(dirname $0)/test-lib.sh"
-
-if test "$VERBOSE" = yes; then
-  set -x
-  $abs_top_builddir/tools/virsh --version
-fi
-
-fail=0
-
-cat <<\EOF > dom
-<domain type='test' id='2'>
-  <name>t2</name>
-  <uuid>004b96e1-2d78-c30f-5aa5-000000000000</uuid>
-  <memory>8388608</memory>
-  <vcpu>2</vcpu>
-  <os>
-    <type>xen</type>
-  </os>
-  <on_reboot>restart</on_reboot>
-  <on_poweroff>destroy</on_poweroff>
-  <on_crash>restart</on_crash>
-</domain>
-EOF
-
-$abs_top_builddir/tools/virsh -c test:///default define dom > /dev/null || fail=1
-
-mkfifo_or_skip_ fifo
-cat dom > fifo &
-
-$abs_top_builddir/tools/virsh -c test:///default define fifo > /dev/null || fail=1
-
-(exit $fail); exit $fail
index 3c345d5ac112c16ab56087798d980430e7195ccb..49e7268595188187f8a3353fc08998cacc188ed1 100644 (file)
@@ -1,11 +1,14 @@
 #include <config.h>
 
 #include <unistd.h>
+#include <fcntl.h>
 
 #include "internal.h"
 #include "testutils.h"
 #include "vircommand.h"
 
+#include "util/virthread.h"
+
 #define VIR_FROM_THIS VIR_FROM_NONE
 
 #ifdef WIN32
@@ -103,6 +106,90 @@ static int testCompare(const void *data)
 }
 
 
+static void
+testPipeFeeder(void *opaque)
+{
+    /* feed more than observed buffer size which was historically 128k in the
+     * test this was adapted from */
+    size_t emptyspace = 140 * 1024;
+    const char *pipepath = opaque;
+    const char *xml =
+        "<domain type='test' id='2'>\n"
+        "  <name>t2</name>\n"
+        "  <uuid>004b96e1-2d78-c30f-5aa5-000000000000</uuid>\n"
+        "  <memory>8388608</memory>\n"
+        "  <vcpu>2</vcpu>\n"
+        "  <os>\n"
+        "    <type>xen</type>\n"
+        "  </os>\n"
+        "</domain>\n";
+    size_t xmlsize = strlen(xml);
+    g_autofree char *doc = g_new0(char, emptyspace + xmlsize + 1);
+    VIR_AUTOCLOSE fd = -1;
+
+    if ((fd = open(pipepath, O_RDWR)) < 0) {
+        fprintf(stderr, "\nfailed to open pipe '%s': %s\n", pipepath, g_strerror(errno));
+        return;
+    }
+
+    memset(doc, ' ', emptyspace);
+    virStrcpy(doc + emptyspace, xml, xmlsize);
+
+    if (safewrite(fd, doc, emptyspace + xmlsize + 1) < 0) {
+        fprintf(stderr, "\nfailed to write to pipe '%s': %s\n", pipepath, g_strerror(errno));
+        return;
+    }
+}
+
+
+static int
+testVirshPipe(const void *data G_GNUC_UNUSED)
+{
+    char tmpdir[] = "/tmp/libvirt_virshtest_XXXXXXX";
+    g_autofree char *pipepath = NULL;
+    virThread feeder;
+    bool join = false;
+    g_autofree char *cmdstr = NULL;
+    const char *argv[] = { VIRSH_DEFAULT, NULL, NULL };
+    int ret = -1;
+
+    if (!g_mkdtemp(tmpdir)) {
+        fprintf(stderr, "\nfailed to create temporary directory\n");
+        return -1;
+    }
+
+    pipepath = g_strdup_printf("%s/pipe", tmpdir);
+
+
+    cmdstr = g_strdup_printf("define %s ; list --all", pipepath);
+    argv[3] = cmdstr;
+
+    if (mkfifo(pipepath, 0600) < 0) {
+        fprintf(stderr, "\nfailed to create pipe '%s': %s\n", pipepath, g_strerror(errno));
+        goto cleanup;
+    }
+
+    if (virThreadCreate(&feeder, true, testPipeFeeder, pipepath) < 0)
+        goto cleanup;
+
+    join = true;
+
+    if (testCompareOutputLit(abs_srcdir "/virshtestdata/read-big-pipe.out",
+                             "/tmp/libvirt_virshtest", argv) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    if (join)
+        virThreadJoin(&feeder);
+    unlink(pipepath);
+    rmdir(tmpdir);
+
+    return ret;
+}
+
+
 static int
 mymain(void)
 {
@@ -238,6 +325,9 @@ mymain(void)
                  "checkpoint-create test --redefine checkpoint-c2.xml ;"
                  "checkpoint-info test c2");
 
+    if (virTestRun("read-big-pipe", testVirshPipe, NULL) < 0)
+        ret = -1;
+
     VIR_FREE(custom_uri);
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
diff --git a/tests/virshtestdata/read-big-pipe.out b/tests/virshtestdata/read-big-pipe.out
new file mode 100644 (file)
index 0000000..3404322
--- /dev/null
@@ -0,0 +1,7 @@
+Domain 't2' defined from 
+
+ Id   Name   State
+-----------------------
+ 1    test   running
+ -    t2     shut off
+