tests: Avoid writing into $HOME during virsh-snapshot

In a constrained CI environment, where it is intentional that attempts
to write outside the current directory will fail, virsh-snapshot was
failing:

@@ -1,2 +1,3 @@
 error: invalid argument: parent s3 for snapshot s2 not found
 error: marker
+error: Failed to create '/home/travis/.cache/libvirt/virsh': Permission denied
FAIL virsh-snapshot (exit status: 1)

But we've already solved the problem in virsh-uriprecedence: tell
virsh to use XDG locations pointing to somewhere we can write rather
than its default of falling back to $HOME with the test being at risk
of breaking due to the user's environment and/or unacceptably altering
the user's normal cache.  Hoist that solution into test-lib.sh, so
that all scripts can use it as needed. While at it, fix a latent typo
where XDG_RUNTIME_HOME was set to a literal relative directory name
"XDG_CACHE_HOME" (the typo did not affect virsh-uriprecedence, but
could matter to other clients).

Fixes: 280a2b41
Fixes: 398de147
Reported-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Eric Blake 2019-03-27 13:42:45 -05:00
parent fc22d3b6d1
commit b188660865
3 changed files with 16 additions and 11 deletions

View File

@ -222,6 +222,19 @@ mkfifo_or_skip_()
fi
}
# Create mock XDG files/directories to avoid permission problems.
# As it points inside $test_dir_, it is automatically cleaned.
mock_xdg_()
{
export XDG_CONFIG_HOME="$t_/.config"
export XDG_CACHE_HOME="$t_/.cache"
export XDG_RUNTIME_HOME="$XDG_CACHE_HOME"
mkdir -p "$XDG_CONFIG_HOME/libvirt" "$XDG_CONFIG_HOME/virsh"
mkdir -p "$XDG_CACHE_HOME/libvirt" "$XDG_CACHE_HOME/virsh"
mkdir -p "$XDG_RUNTIME_HOME/libvirt" "$XDG_RUNTIME_HOME/virsh"
}
test_dir_=$(pwd)
this_test_() { echo "./$0" | sed 's,.*/,,'; }

View File

@ -26,6 +26,8 @@ fi
fail=0
mock_xdg_ || framework_failure
# The test driver loses states between restarts, so we perform a script
# with some convenient markers for later post-processing of output.
$abs_top_builddir/tools/virsh --connect test:///default >out 2>err '

View File

@ -11,17 +11,7 @@ virsh_cmd="$virsh_bin"
counter=0
ret=0
cleanup_() { rm -rf "$tmphome"; }
# Create all mock files/directories to avoid permission problems
tmphome="$PWD/tmp_home"
export XDG_CONFIG_HOME="$tmphome/.config"
export XDG_CACHE_HOME="$tmphome/.cache"
export XDG_RUNTIME_HOME="XDG_CACHE_HOME"
mkdir -p "$XDG_CONFIG_HOME/libvirt" "$XDG_CONFIG_HOME/virsh"
mkdir -p "$XDG_CACHE_HOME/libvirt" "$XDG_CACHE_HOME/virsh"
mkdir -p "$XDG_RUNTIME_HOME/libvirt" "$XDG_RUNTIME_HOME/virsh"
mock_xdg_ || framework_failure
is_uri_good()
{