]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virshtest: Adapt virsh-uriprecedence test case
authorPeter Krempa <pkrempa@redhat.com>
Fri, 22 Mar 2024 15:23:57 +0000 (16:23 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 2 Apr 2024 12:24:30 +0000 (14:24 +0200)
Reimplement the virsh-uriprecedence test case in virshtest. To do this
we need to add infrastructure to pass extra environment variables to the
tested virsh.

The user config files are shipped in repo rather than created in the
script.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tests/meson.build
tests/virsh-uriprecedence [deleted file]
tests/virshtest.c
tests/virshtestdata/uriprecedence-LIBVIRT_DEFAULT_URI.out [new file with mode: 0644]
tests/virshtestdata/uriprecedence-VIRSH_DEFAULT_CONNECT_URI.out [new file with mode: 0644]
tests/virshtestdata/uriprecedence-param.out [new file with mode: 0644]
tests/virshtestdata/uriprecedence-xdg-config.out [new file with mode: 0644]
tests/virshtestdata/uriprecedence-xdg/bad/libvirt/libvirt.conf [new file with mode: 0644]
tests/virshtestdata/uriprecedence-xdg/good/libvirt/libvirt.conf [new file with mode: 0644]

index 4da1b099e483d1f140ed495f49f239ae72c2e013..3f8f3dee7cc4ca34a809e2203e68c16a526db902 100644 (file)
@@ -710,7 +710,6 @@ if conf.has('WITH_LIBVIRTD')
 
   test_scripts += [
     'virsh-auth',
-    'virsh-uriprecedence',
   ]
 
   if conf.has('WITH_SECDRIVER_APPARMOR')
diff --git a/tests/virsh-uriprecedence b/tests/virsh-uriprecedence
deleted file mode 100755 (executable)
index f141d08..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-#!/bin/sh
-
-. "$(dirname $0)/test-lib.sh"
-
-# This test checks if virsh obeys the proper precedence of different
-# URI settings
-test_intro "virsh-uriprecedence"
-
-virsh_bin="$abs_top_builddir/tools/virsh"
-virsh_cmd="$virsh_bin"
-counter=0
-ret=0
-
-mock_xdg_ || framework_failure
-
-is_uri_good()
-{
-    echo "$1" | grep -F "$good_uri" >/dev/null
-}
-
-test_uri_internal()
-{
-    test_name=$1
-    test_cmd="$virsh_cmd \"$2\""
-    result=0
-
-    debug "Running '$test_cmd'"
-    out="$($virsh_cmd "$2")"
-
-    if ! is_uri_good "$out"; then
-        debug "Invalid output: '$out'"
-        result=1
-        ret=1
-    fi
-
-    counter="$((counter+1))"
-    test_result "$counter" "$1" "$result"
-}
-
-test_uri_connect()
-{
-    test_uri_internal "$1" "connect; uri"
-}
-
-test_uri_noconnect()
-{
-    test_uri_internal "$1" "uri"
-}
-
-test_uri()
-{
-    test_uri_connect "$1"
-    test_uri_noconnect "$1"
-}
-
-# Precedence is the following (lowest priority first):
-#
-# 1) if run as root, 'uri_default' from /etc/libvirtd/libvirt.conf,
-#    otherwise qemu:///session.  There is no way to mock this file for
-#    virsh/libvirt.so and the user may have set anything in there that
-#    would spoil the test, so we don't test this
-#
-# 2) 'uri_default' from $XDG_CONFIG_HOME/libvirt/libvirt.conf
-#
-# 3) LIBVIRT_DEFAULT_URI
-#
-# 4) VIRSH_DEFAULT_CONNECT_URI
-#
-# 5) parameter -c (--connect)
-
-unset LIBVIRT_DEFAULT_URI
-unset VIRSH_DEFAULT_CONNECT_URI
-bad_uri="test:///default?bad_uri"
-good_uri="test:///default?good_uri"
-
-printf "uri_default=\"%s\"\n" "$good_uri" >"$XDG_CONFIG_HOME/libvirt/libvirt.conf"
-if uid_is_privileged_; then
-    counter="$((counter+1))"
-    test_skip_case "$counter" "User config file" "must not be run as root"
-else
-    test_uri "User config file"
-fi
-
-printf "uri_default=\"%s\"\n" "$bad_uri" >"$XDG_CONFIG_HOME/libvirt/libvirt.conf"
-export LIBVIRT_DEFAULT_URI="$good_uri"
-test_uri "LIBVIRT_DEFAULT_URI"
-
-export LIBVIRT_DEFAULT_URI="$bad_uri"
-export VIRSH_DEFAULT_CONNECT_URI="$good_uri"
-test_uri "VIRSH_DEFAULT_CONNECT_URI"
-
-export VIRSH_DEFAULT_CONNECT_URI="$bad_uri"
-virsh_cmd="$virsh_bin --connect $good_uri"
-test_uri "Parameter"
-
-test_final "$counter" "$ret"
-(exit "$ret"); exit "$ret"
index 72c6302a54e8a5a0319492fda7d61fd7a7641276..14a96f2d35cbf9c20eed92293b56d1c8c1d0d13c 100644 (file)
@@ -40,6 +40,7 @@ static void testFilterLine(char *buffer,
 static int
 testCompareOutputLit(const char *expectFile,
                      const char *filter,
+                     const char *const *env,
                      const char *const argv[])
 {
     g_autofree char *actual = NULL;
@@ -50,6 +51,12 @@ testCompareOutputLit(const char *expectFile,
     cmd = virCommandNewArgs(argv);
 
     virCommandAddEnvString(cmd, "LANG=C");
+
+    while (env && *env) {
+        virCommandAddEnvString(cmd, *env);
+        env++;
+    }
+
     virCommandSetInputBuffer(cmd, empty);
     virCommandSetOutputBuffer(cmd, &actual);
     virCommandSetErrorBuffer(cmd, &actual);
@@ -87,6 +94,8 @@ struct testInfo {
     const char *filter;
     const char *const *argv;
     bool expensive;
+    const char *const *env; /* extra environment variables to pass */
+    bool forbid_root;
 };
 
 static int testCompare(const void *data)
@@ -97,12 +106,15 @@ static int testCompare(const void *data)
     if (info->expensive && virTestGetExpensive() == 0)
         return EXIT_AM_SKIP;
 
+    if (info->forbid_root && geteuid() == 0)
+        return EXIT_AM_SKIP;
+
     if (info->testname) {
         outfile = g_strdup_printf("%s/virshtestdata/%s.out",
                                   abs_srcdir, info->testname);
     }
 
-    return testCompareOutputLit(outfile, info->filter, info->argv);
+    return testCompareOutputLit(outfile, info->filter, info->env, info->argv);
 }
 
 
@@ -175,7 +187,7 @@ testVirshPipe(const void *data G_GNUC_UNUSED)
     join = true;
 
     if (testCompareOutputLit(abs_srcdir "/virshtestdata/read-big-pipe.out",
-                             "/tmp/libvirt_virshtest", argv) < 0)
+                             "/tmp/libvirt_virshtest", NULL, argv) < 0)
         goto cleanup;
 
     ret = 0;
@@ -205,7 +217,7 @@ mymain(void)
                                                   abs_srcdir, testname); \
         const char *myargv[] = { __VA_ARGS__, NULL, NULL }; \
         const char **tmp = myargv; \
-        const struct testInfo info = { testname, testfilter, myargv, expensive }; \
+        const struct testInfo info = { testname, testfilter, myargv, expensive, NULL, false}; \
         g_autofree char *scriptarg = NULL; \
         if (virFileReadAll(infile, 256 * 1024, &scriptarg) < 0) { \
             fprintf(stderr, "\nfailed to load '%s'\n", infile); \
@@ -227,13 +239,15 @@ mymain(void)
     DO_TEST_SCRIPT("blkiotune", NULL, VIRSH_CUSTOM);
     DO_TEST_SCRIPT("iothreads", NULL, VIRSH_CUSTOM);
 
-# define DO_TEST_FULL(testname_, filter, ...) \
+# define DO_TEST_INFO(infostruct) \
+    if (virTestRun((infostruct)->testname, testCompare, (infostruct)) < 0) \
+        ret = -1;
+
+# define DO_TEST_FULL(testname, filter, ...) \
     do { \
-        const char *testname = testname_; \
         const char *myargv[] = { __VA_ARGS__, NULL }; \
-        const struct testInfo info = { testname, NULL, myargv, false }; \
-        if (virTestRun(testname, testCompare, &info) < 0) \
-            ret = -1; \
+        const struct testInfo info = { testname, NULL, myargv, false, NULL, false }; \
+        DO_TEST_INFO(&info); \
     } while (0)
 
     /* automatically numbered test invocation */
@@ -331,6 +345,102 @@ mymain(void)
     if (virTestRun("read-big-pipe", testVirshPipe, NULL) < 0)
         ret = -1;
 
+    /* Test precedence of URI lookup in virsh:
+     *
+     * Precedence is the following (lowest priority first):
+     *
+     * 1) if run as root, 'uri_default' from /etc/libvirtd/libvirt.conf,
+     * otherwise qemu:///session.  There is no way to mock this file for
+     * virsh/libvirt.so and the user may have set anything in there that
+     * would spoil the test, so we don't test this
+     *
+     * 2) 'uri_default' from $XDG_CONFIG_HOME/libvirt/libvirt.conf
+     *
+     * 3) LIBVIRT_DEFAULT_URI
+     *
+     * 4) VIRSH_DEFAULT_CONNECT_URI
+     *
+     * 5) parameter -c (--connect)
+     *
+     * There are two pre-prepared directories in tests/virshtestdata/ serving
+     * as mock XDG_CONFIG_HOME containing the test configs.
+     */
+    {
+        const char *uriTest = "uri; connect; uri";
+        const char *myargv_noconnect[] = { abs_top_builddir "/tools/virsh", uriTest, NULL };
+        const char *xdgDirBad = "XDG_CONFIG_HOME=" abs_srcdir "/virshtestdata/uriprecedence-xdg/bad/";
+        struct testInfo info = { NULL, NULL, myargv_noconnect, false, NULL, false };
+
+        /* test 1 - default from config */
+        {
+            const char *myenv[] = {
+                "XDG_CONFIG_HOME=" abs_srcdir "/virshtestdata/uriprecedence-xdg/good/",
+                NULL,
+            };
+
+            info.testname = "uriprecedence-xdg-config";
+            info.env = myenv;
+            info.forbid_root = true;
+
+            DO_TEST_INFO(&info);
+        }
+
+        /* all other tests don't care */
+        info.forbid_root = false;
+
+        /* test 2 - LIBVIRT_DEFAULT_URI env variable */
+        {
+            const char *myenv[] = {
+                xdgDirBad,
+                "LIBVIRT_DEFAULT_URI=test:///default?good_uri",
+                NULL,
+            };
+
+            info.testname = "uriprecedence-LIBVIRT_DEFAULT_URI";
+            info.env = myenv;
+
+            DO_TEST_INFO(&info);
+        }
+
+        /* test 3 - VIRSH_DEFAULT_CONNECT_URI env variable */
+        {
+            const char *myenv[] = {
+                xdgDirBad,
+                "LIBVIRT_DEFAULT_URI=test:///default?bad_uri",
+                "VIRSH_DEFAULT_CONNECT_URI=test:///default?good_uri",
+                NULL,
+            };
+
+            info.testname = "uriprecedence-VIRSH_DEFAULT_CONNECT_URI";
+            info.env = myenv;
+
+            DO_TEST_INFO(&info);
+        }
+
+        /* test 3 - --connect parameter */
+        {
+            const char *myenv[] = {
+                xdgDirBad,
+                "LIBVIRT_DEFAULT_URI=test:///default?bad_uri",
+                "VIRSH_DEFAULT_CONNECT_URI=test:///default?bad_uri",
+                NULL,
+            };
+
+            const char *myargv[] = {
+                 abs_top_builddir "/tools/virsh",
+                 "--connect", "test:///default?good_uri",
+                 uriTest,
+                 NULL,
+            };
+
+            info.testname = "uriprecedence-param";
+            info.env = myenv;
+            info.argv = myargv;
+
+            DO_TEST_INFO(&info);
+        }
+    }
+
     VIR_FREE(custom_uri);
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
diff --git a/tests/virshtestdata/uriprecedence-LIBVIRT_DEFAULT_URI.out b/tests/virshtestdata/uriprecedence-LIBVIRT_DEFAULT_URI.out
new file mode 100644 (file)
index 0000000..1d66548
--- /dev/null
@@ -0,0 +1,5 @@
+test:///default?good_uri
+
+
+test:///default?good_uri
+
diff --git a/tests/virshtestdata/uriprecedence-VIRSH_DEFAULT_CONNECT_URI.out b/tests/virshtestdata/uriprecedence-VIRSH_DEFAULT_CONNECT_URI.out
new file mode 100644 (file)
index 0000000..1d66548
--- /dev/null
@@ -0,0 +1,5 @@
+test:///default?good_uri
+
+
+test:///default?good_uri
+
diff --git a/tests/virshtestdata/uriprecedence-param.out b/tests/virshtestdata/uriprecedence-param.out
new file mode 100644 (file)
index 0000000..1d66548
--- /dev/null
@@ -0,0 +1,5 @@
+test:///default?good_uri
+
+
+test:///default?good_uri
+
diff --git a/tests/virshtestdata/uriprecedence-xdg-config.out b/tests/virshtestdata/uriprecedence-xdg-config.out
new file mode 100644 (file)
index 0000000..1d66548
--- /dev/null
@@ -0,0 +1,5 @@
+test:///default?good_uri
+
+
+test:///default?good_uri
+
diff --git a/tests/virshtestdata/uriprecedence-xdg/bad/libvirt/libvirt.conf b/tests/virshtestdata/uriprecedence-xdg/bad/libvirt/libvirt.conf
new file mode 100644 (file)
index 0000000..bcc732e
--- /dev/null
@@ -0,0 +1 @@
+uri_default="test:///default?bad_uri"
diff --git a/tests/virshtestdata/uriprecedence-xdg/good/libvirt/libvirt.conf b/tests/virshtestdata/uriprecedence-xdg/good/libvirt/libvirt.conf
new file mode 100644 (file)
index 0000000..da94415
--- /dev/null
@@ -0,0 +1 @@
+uri_default="test:///default?good_uri"