test_intro "virsh-uriprecedence"
virsh_bin="$abs_top_builddir/tools/virsh"
-counter=1
+virsh_cmd="$virsh_bin"
+counter=0
ret=0
cleanup_() { rm -rf "$tmphome"; }
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
+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()
{
- result=0
- if [ "$($virsh_bin uri)" != "$good_uri" ]; then
- result=1
- ret=1
- fi
- test_result "$counter" "$1" "$result"
- counter="$((counter+1))"
+ test_uri_connect "$1"
+ test_uri_noconnect "$1"
}
# Precedence is the following (lowest priority first):
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"
test_uri "VIRSH_DEFAULT_CONNECT_URI"
export VIRSH_DEFAULT_CONNECT_URI="$bad_uri"
-virsh_bin="$virsh_bin --connect $good_uri"
+virsh_cmd="$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"
+test_final "$counter" "$ret"
(exit "$ret"); exit "$ret"
*
*/
static void
-virshReconnect(vshControl *ctl)
+virshReconnect(vshControl *ctl, const char *name, bool readonly, bool force)
{
bool connected = false;
virshControlPtr priv = ctl->privData;
+ bool ro = name ? readonly : priv->readonly;
if (priv->conn) {
int ret;
"disconnect from the hypervisor"));
}
- priv->conn = virshConnect(ctl, ctl->connname, priv->readonly);
+ priv->conn = virshConnect(ctl, name ? name : ctl->connname, ro);
if (!priv->conn) {
if (disconnected)
else
vshError(ctl, "%s", _("failed to connect to the hypervisor"));
} else {
+ if (name) {
+ VIR_FREE(ctl->connname);
+ ctl->connname = vshStrdup(ctl, name);
+ priv->readonly = readonly;
+ }
if (virConnectRegisterCloseCallback(priv->conn, virshCatchDisconnect,
ctl, NULL) < 0)
vshError(ctl, "%s", _("Unable to register disconnect callback"));
- if (connected)
+ if (connected && !force)
vshError(ctl, "%s", _("Reconnected to the hypervisor"));
}
disconnected = 0;
{
bool ro = vshCommandOptBool(cmd, "readonly");
const char *name = NULL;
- virshControlPtr priv = ctl->privData;
- virConnectPtr conn;
if (vshCommandOptStringReq(ctl, cmd, "name", &name) < 0)
return false;
- conn = virshConnect(ctl, name, ro);
-
- if (!conn) {
- vshError(ctl, "%s", _("Failed to connect to the hypervisor"));
- return false;
- }
-
- if (priv->conn) {
- int ret;
-
- virConnectUnregisterCloseCallback(priv->conn, virshCatchDisconnect);
- ret = virConnectClose(priv->conn);
- if (ret < 0)
- vshError(ctl, "%s", _("Failed to disconnect from the hypervisor"));
- else if (ret > 0)
- vshError(ctl, "%s", _("One or more references were leaked after "
- "disconnect from the hypervisor"));
- }
- priv->conn = conn;
-
- VIR_FREE(ctl->connname);
- ctl->connname = vshStrdup(ctl, name);
-
- priv->useGetInfo = false;
- priv->useSnapshotOld = false;
- priv->blockJobNoBytes = false;
- priv->readonly = ro;
-
- if (virConnectRegisterCloseCallback(priv->conn, virshCatchDisconnect,
- ctl, NULL) < 0)
- vshError(ctl, "%s", _("Unable to register disconnect callback"));
+ virshReconnect(ctl, name, ro, true);
return true;
}
virshControlPtr priv = ctl->privData;
if (!priv->conn || disconnected)
- virshReconnect(ctl);
+ virshReconnect(ctl, NULL, false, false);
if (virshConnectionUsability(ctl, priv->conn))
return priv->conn;
return false;
if (ctl->connname) {
- virshReconnect(ctl);
+ virshReconnect(ctl, NULL, false, false);
/* Connecting to a named connection must succeed, but we delay
* connecting to the default connection until we need it
* (since the first command might be 'connect' which allows a