libvirt/tests/virsh-uriprecedence
Cole Robinson 856e84a516 tests: consistently source test-lib.sh in scripts
This unifies the test scripts to all use the similar pattern added for
schematests in ace4aecd. This gives the following

- Enables running all tests from outside of tests/ dir
- Drops redundant abs_* definitions, which are set by test-lib.sh
- Drops unnecessary srcdir variable which was only used for sourcing
    test-lib.sh

Behavior changes:

- srcdir can no longer be overwritten, but I don't know why anyone would
    really need to...
- Script VERBOSE setting no longer prints commands executed by test-lib.sh.
    if anyone cares I suggest handling this in test-lib.sh which already
    has other verbose style handling
2016-04-23 15:41:40 -04:00

80 lines
2.2 KiB
Bash
Executable File

#!/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"
counter=1
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"
# Main function checking for the proper uri being returned
test_uri()
{
result=0
if [ "$($virsh_bin uri)" != "$good_uri" ]; then
result=1
ret=1
fi
test_result "$counter" "$1" "$result"
counter="$((counter+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
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_bin="$virsh_bin --connect $good_uri"
test_uri "Parameter"
# test_uri() increases $counter even for the last test, so we must
# decrement it
test_final "$((counter-1))" "$ret"
(exit "$ret"); exit "$ret"