[libvirt] [PATCH v2] 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> --- in v2: - fix typo in XDG_RUNTIME_HOME [Andrea] - fix 'make distcheck' by using actual per-test tempdir (tests/lv-TEST.XXX/) rather than tests/ as the XDG root [Andrea] tests/test-lib.sh | 13 +++++++++++++ tests/virsh-snapshot | 2 ++ tests/virsh-uriprecedence | 12 +----------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/tests/test-lib.sh b/tests/test-lib.sh index 49e8d22095..ef5a47b565 100644 --- a/tests/test-lib.sh +++ b/tests/test-lib.sh @@ -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,.*/,,'; } diff --git a/tests/virsh-snapshot b/tests/virsh-snapshot index fb8a99dd43..cb498cf54e 100755 --- a/tests/virsh-snapshot +++ b/tests/virsh-snapshot @@ -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 ' diff --git a/tests/virsh-uriprecedence b/tests/virsh-uriprecedence index 564e3dc42c..fd6ce108c0 100755 --- a/tests/virsh-uriprecedence +++ b/tests/virsh-uriprecedence @@ -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() { -- 2.20.1

On Thu, 2019-04-04 at 09:41 -0500, Eric Blake wrote:
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> ---
in v2: - fix typo in XDG_RUNTIME_HOME [Andrea] - fix 'make distcheck' by using actual per-test tempdir (tests/lv-TEST.XXX/) rather than tests/ as the XDG root [Andrea]
tests/test-lib.sh | 13 +++++++++++++ tests/virsh-snapshot | 2 ++ tests/virsh-uriprecedence | 12 +----------- 3 files changed, 16 insertions(+), 11 deletions(-)
Reviewed-by: Andrea Bolognani <abologna@redhat.com> -- Andrea Bolognani / Red Hat / Virtualization
participants (2)
-
Andrea Bolognani
-
Eric Blake