From 1f8283325199a8ac7a61a225fffbb6e06e3c1b1d Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 22 Mar 2024 16:23:57 +0100 Subject: [PATCH] virshtest: Adapt virsh-uriprecedence test case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Ján Tomko --- tests/meson.build | 1 - tests/virsh-uriprecedence | 97 -------------- tests/virshtest.c | 126 ++++++++++++++++-- .../uriprecedence-LIBVIRT_DEFAULT_URI.out | 5 + ...riprecedence-VIRSH_DEFAULT_CONNECT_URI.out | 5 + tests/virshtestdata/uriprecedence-param.out | 5 + .../uriprecedence-xdg-config.out | 5 + .../bad/libvirt/libvirt.conf | 1 + .../good/libvirt/libvirt.conf | 1 + 9 files changed, 140 insertions(+), 106 deletions(-) delete mode 100755 tests/virsh-uriprecedence create mode 100644 tests/virshtestdata/uriprecedence-LIBVIRT_DEFAULT_URI.out create mode 100644 tests/virshtestdata/uriprecedence-VIRSH_DEFAULT_CONNECT_URI.out create mode 100644 tests/virshtestdata/uriprecedence-param.out create mode 100644 tests/virshtestdata/uriprecedence-xdg-config.out create mode 100644 tests/virshtestdata/uriprecedence-xdg/bad/libvirt/libvirt.conf create mode 100644 tests/virshtestdata/uriprecedence-xdg/good/libvirt/libvirt.conf diff --git a/tests/meson.build b/tests/meson.build index 4da1b099e4..3f8f3dee7c 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -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 index f141d08dfd..0000000000 --- a/tests/virsh-uriprecedence +++ /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" diff --git a/tests/virshtest.c b/tests/virshtest.c index 72c6302a54..14a96f2d35 100644 --- a/tests/virshtest.c +++ b/tests/virshtest.c @@ -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 index 0000000000..1d665485ad --- /dev/null +++ b/tests/virshtestdata/uriprecedence-LIBVIRT_DEFAULT_URI.out @@ -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 index 0000000000..1d665485ad --- /dev/null +++ b/tests/virshtestdata/uriprecedence-VIRSH_DEFAULT_CONNECT_URI.out @@ -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 index 0000000000..1d665485ad --- /dev/null +++ b/tests/virshtestdata/uriprecedence-param.out @@ -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 index 0000000000..1d665485ad --- /dev/null +++ b/tests/virshtestdata/uriprecedence-xdg-config.out @@ -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 index 0000000000..bcc732efd5 --- /dev/null +++ b/tests/virshtestdata/uriprecedence-xdg/bad/libvirt/libvirt.conf @@ -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 index 0000000000..da94415a7a --- /dev/null +++ b/tests/virshtestdata/uriprecedence-xdg/good/libvirt/libvirt.conf @@ -0,0 +1 @@ +uri_default="test:///default?good_uri"