libvirt/tests/virsh-uriprecedence
Martin Kletzander 0c56d94318 tools: Fix connect command
The man page says: "(Re)-Connect to the hypervisor. When the shell is
first started, this is automatically run with the URI parameter
requested by the "-c" option on the command line."  However, if you run:

  virsh -c 'test://default' 'connect; uri'

the output will not be 'test://default'.  That's because the 'connect'
command does not care about any virsh-only related settings and if it is
run without parameters, it connects with @uri == NULL.  Not only that
doesn't comply to what the man page describes, but it also doesn't make
sense.  It also means you aren't able to reconnect to whatever you are
connected currently.

So let's fix that in both virsh and virt-admin add a test case for it.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2016-05-02 07:18:25 +02:00

108 lines
2.5 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"
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"
is_uri_good()
{
echo "$1" | grep -q -F "$good_uri"
}
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"