rpm-ostree/tests/common/libvm.sh
Jonathan Lebon 7697702aaa vmcheck: strengthen test harness
We now make the test harness handle restoring the VM to the original
state. The wonderful thing about ostree here is that it's a perfect
shoo-in for this. We make a 'backup' of the current ref, and just have
to make sure that the VM is back on that ref after running each test.

This will allow us to write tests without worrying as much about
cleaning up in the event of an error.

Closes: #360
Approved by: cgwalters
2016-06-29 19:17:38 +00:00

122 lines
2.9 KiB
Bash

# Source library for installed virtualized shell script tests
#
# Copyright (C) 2016 Jonathan Lebon <jlebon@redhat.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# run command in vm
# - $@ command to run
vm_cmd() {
$SSH "$@"
}
# copy files to a directory in the vm
# - $1 target directory
# - $2.. files & dirs to copy
vm_send() {
dir=$1; shift
vm_cmd mkdir -p $dir
$SCP -r "$@" vmcheck:$dir
}
# wait until ssh is available on the vm
# - $1 timeout in second (optional)
vm_ssh_wait() {
timeout=${1:-0}
while [ $timeout -gt 0 ]; do
if vm_cmd true &> /dev/null; then
return 0
fi
timeout=$((timeout - 1))
sleep 1
done
# final check at the timeout mark
vm_cmd true &> /dev/null
}
# reboot the vm
vm_reboot() {
vm_cmd systemctl reboot || :
sleep 2 # give time for port to go down
vm_ssh_wait 10
}
# check that the given files exist on the VM
# - $@ packages to check for
vm_has_files() {
for file in "$@"; do
if ! vm_cmd test -e $file; then
return 1
fi
done
}
# check that the packages are installed
# - $@ packages to check for
vm_has_packages() {
for pkg in "$@"; do
if ! vm_cmd rpm -q $pkg; then
return 1
fi
done
}
# retrieve info from the booted deployment
# - $1 key to retrieve
vm_get_booted_deployment_info() {
key=$1
vm_cmd rpm-ostree status --json | \
python -c "
import sys, json
deployments = json.load(sys.stdin)[\"deployments\"]
booted = None
for deployment in deployments:
if deployment[\"booted\"]:
booted = deployment
break
if not booted:
print \"Failed to determine currently booted deployment\"
exit(1)
if \"$key\" in booted:
data = booted[\"$key\"]
if type(data) is list:
print \" \".join(data)
else:
print data
"
}
# print the layered packages
vm_get_layered_packages() {
vm_get_booted_deployment_info packages
}
# check that the packages are currently layered
# - $@ packages to check for
vm_has_layered_packages() {
pkgs=$(vm_get_layered_packages)
for pkg in "$@"; do
if [[ " $pkgs " != *$pkg* ]]; then
return 1
fi
done
}
# retrieve the checksum of the currently booted deployment
vm_get_booted_csum() {
vm_get_booted_deployment_info checksum
}