2016-06-21 19:37:02 +03:00
# 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.
2016-07-15 20:30:56 +03:00
# prepares the VM and library for action
vm_setup( ) {
2016-10-31 23:26:56 +03:00
# We assume that there's already a configured ssh-config
# file available to tell us how to connect to the VM.
if [ ! -f " ${ topsrcdir } /ssh-config " ] ; then
echo "ERROR: No ssh-config found."
exit 1
2016-07-15 20:30:56 +03:00
fi
local sshopts = " -F ${ topsrcdir } /ssh-config \
2016-11-14 20:07:43 +03:00
-o User = root \
2016-07-15 20:30:56 +03:00
-o ControlMaster = auto \
-o ControlPath = ${ topsrcdir } /ssh.sock \
-o ControlPersist = yes"
export SSH = " ssh $sshopts vmcheck "
export SCP = " scp $sshopts "
2016-11-14 20:07:43 +03:00
if grep -q 'User.*vagrant' ${ topsrcdir } /ssh-config; then
export using_sshfs = yes
else
export using_sshfs = no
fi
2016-07-15 20:30:56 +03:00
}
vm_rsync( ) {
2016-11-14 20:07:43 +03:00
if test ${ using_sshfs } = yes; then
return
fi
2016-07-15 20:30:56 +03:00
pushd ${ topsrcdir }
rsync -az --no-owner --no-group --filter ":- .gitignore" \
2016-11-14 20:07:43 +03:00
-e "ssh -F ssh-config" --exclude .git/ . vmcheck:/root/sync
2016-07-15 20:30:56 +03:00
popd
}
2016-06-22 00:35:51 +03:00
# run command in vm
# - $@ command to run
2016-06-21 19:37:02 +03:00
vm_cmd( ) {
$SSH " $@ "
}
2016-06-22 00:35:51 +03:00
# 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
}
2016-07-04 18:43:02 +03:00
# copy the test repo to the vm
vm_send_test_repo( ) {
vm_send /tmp/vmcheck ${ commondir } /compose/yum/repo
cat > vmcheck.repo << EOF
[ test-repo]
name = test-repo
baseurl = file:///tmp/vmcheck/repo
EOF
vm_send /etc/yum.repos.d vmcheck.repo
}
2016-06-22 00:35:51 +03:00
# wait until ssh is available on the vm
2016-06-24 23:40:20 +03:00
# - $1 timeout in second (optional)
2016-06-22 00:35:51 +03:00
vm_ssh_wait( ) {
2016-06-24 23:40:20 +03:00
timeout = ${ 1 :- 0 }
while [ $timeout -gt 0 ] ; do
if vm_cmd true & > /dev/null; then
return 0
fi
timeout = $(( timeout - 1 ))
2016-06-22 00:35:51 +03:00
sleep 1
done
2016-06-24 23:40:20 +03:00
# final check at the timeout mark
2016-11-14 20:07:43 +03:00
set -x
vm_cmd true
2016-06-22 00:35:51 +03:00
}
# reboot the vm
vm_reboot( ) {
vm_cmd systemctl reboot || :
sleep 2 # give time for port to go down
2016-06-24 23:40:20 +03:00
vm_ssh_wait 10
2016-06-22 00:35:51 +03:00
}
# 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
}
2016-06-29 00:23:53 +03:00
# retrieve info from the booted deployment
# - $1 key to retrieve
vm_get_booted_deployment_info( ) {
key = $1
2016-06-22 00:35:51 +03:00
vm_cmd rpm-ostree status --json | \
2016-06-29 00:23:53 +03:00
python -c "
2016-06-22 00:35:51 +03:00
import sys, json
2016-06-29 00:23:53 +03:00
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
2016-06-22 00:35:51 +03:00
}
# 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
}
2016-06-29 00:23:53 +03:00
# retrieve the checksum of the currently booted deployment
vm_get_booted_csum( ) {
vm_get_booted_deployment_info checksum
}
2016-07-04 18:43:02 +03:00
# make multiple consistency checks on a test pkg
# - $1 package to check for
# - $2 either "present" or "absent"
vm_assert_layered_pkg( ) {
pkg = $1 ; shift
policy = $1 ; shift
set +e
vm_has_packages $pkg ; pkg_in_rpmdb = $?
vm_has_layered_packages $pkg ; pkg_is_layered = $?
[ $pkg_in_rpmdb = = 0 ] && [ $pkg_is_layered = = 0 ] ; pkg_present = $?
[ $pkg_in_rpmdb != 0 ] && [ $pkg_is_layered != 0 ] ; pkg_absent = $?
set -e
if [ $policy = = present ] && [ $pkg_present != 0 ] ; then
assert_not_reached " pkg $pkg is not present "
fi
if [ $policy = = absent ] && [ $pkg_absent != 0 ] ; then
assert_not_reached " pkg $pkg is not absent "
fi
}