2011-10-13 00:46:23 +04:00
# Source library for shell script tests
#
# Copyright (C) 2011 Colin Walters <walters@verbum.org>
#
2011-11-10 22:17:04 +04:00
# 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.
2011-10-13 00:46:23 +04:00
#
2011-11-10 22:17:04 +04:00
# This library is distributed in the hope that it will be useful,
2011-10-13 00:46:23 +04:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2011-11-10 22:17:04 +04:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
2011-10-13 00:46:23 +04:00
#
2011-11-10 22:17:04 +04:00
# 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.
2011-10-13 00:46:23 +04:00
2016-03-31 20:29:44 +03:00
if [ -n " ${ G_TEST_SRCDIR :- } " ] ; then
test_srcdir = " ${ G_TEST_SRCDIR } /tests "
else
test_srcdir = $( dirname $0 )
fi
if [ -n " ${ G_TEST_BUILDDIR :- } " ] ; then
test_builddir = " ${ G_TEST_BUILDDIR } /tests "
else
test_builddir = $( dirname $0 )
fi
2016-03-02 18:28:04 +03:00
2016-03-02 18:05:08 +03:00
assert_not_reached ( ) {
echo $@ 1>& 2; exit 1
}
2016-03-02 18:28:04 +03:00
2013-05-01 23:26:21 +04:00
test_tmpdir = $( pwd )
2011-10-13 00:46:23 +04:00
2016-03-18 15:43:53 +03:00
# Sanity check that we're in a tmpdir that has
# just .testtmp (created by tap-driver for `make check`,
# or nothing at all (as ginstest-runner does)
if ! test -f .testtmp; then
files = $( ls)
if test -n " ${ files } " ; then
2016-03-31 19:52:57 +03:00
ls -l
2016-03-18 15:43:53 +03:00
assert_not_reached " test tmpdir= ${ test_tmpdir } is not empty; run this test via \`make check TESTS=\`, not directly "
fi
2016-03-31 19:52:57 +03:00
# Remember that this is an acceptable test $(pwd), for the benefit of
# C and JS tests which may source this file again
touch .testtmp
2016-03-02 18:05:08 +03:00
fi
2011-11-04 23:58:32 +04:00
export G_DEBUG = fatal-warnings
2016-03-02 18:28:04 +03:00
# Also, unbreak `tar` inside `make check`...Automake will inject
# TAR_OPTIONS: --owner=0 --group=0 --numeric-owner presumably so that
# tarballs are predictable, except we don't want this in our tests.
unset TAR_OPTIONS
2016-02-19 19:58:08 +03:00
# Don't flag deployments as immutable so that test harnesses can
# easily clean up.
export OSTREE_SYSROOT_DEBUG = mutable-deployments
2015-03-17 18:22:27 +03:00
export TEST_GPG_KEYID_1 = "472CDAFA"
export TEST_GPG_KEYID_2 = "CA950D41"
export TEST_GPG_KEYID_3 = "DF444D67"
2015-02-23 21:02:37 +03:00
# GPG when creating signatures demands a writable
# homedir in order to create lockfiles. Work around
# this by copying locally.
2016-03-02 18:28:04 +03:00
echo " Copying gpghome to ${ test_tmpdir } "
2016-03-31 20:29:44 +03:00
cp -a " ${ test_srcdir } /gpghome " ${ test_tmpdir }
2015-02-23 21:02:37 +03:00
export TEST_GPG_KEYHOME = ${ test_tmpdir } /gpghome
export OSTREE_GPG_HOME = ${ test_tmpdir } /gpghome/trusted
2013-09-03 05:43:49 +04:00
2016-01-27 19:44:10 +03:00
if test -n " ${ OT_TESTS_DEBUG :- } " ; then
2011-11-09 14:28:13 +04:00
set -x
fi
2016-01-27 19:44:10 +03:00
if test -n " ${ OT_TESTS_VALGRIND :- } " ; then
2016-05-11 22:54:48 +03:00
CMD_PREFIX = " env G_SLICE=always-malloc OSTREE_SUPPRESS_SYNCFS=1 valgrind -q --leak-check=full --num-callers=30 --suppressions= ${ test_srcdir } /glib.supp --suppressions= ${ test_srcdir } /ostree.supp "
2015-06-02 05:34:14 +03:00
else
2016-03-31 20:29:44 +03:00
CMD_PREFIX = " env LD_PRELOAD= ${ test_builddir } /libreaddir-rand.so "
2012-05-04 18:04:32 +04:00
fi
2011-11-01 16:49:01 +04:00
assert_streq ( ) {
2011-11-04 20:31:40 +04:00
test " $1 " = " $2 " || ( echo 1>& 2 " $1 != $2 " ; exit 1)
2011-11-01 16:49:01 +04:00
}
2016-04-19 22:19:46 +03:00
assert_str_match ( ) {
if ! echo " $1 " | grep -E -q " $2 " ; then
( echo 1>& 2 " $1 does not match regexp $2 " ; exit 1)
fi
}
2013-06-29 19:45:53 +04:00
assert_not_streq ( ) {
( ! test " $1 " = " $2 " ) || ( echo 1>& 2 " $1 == $2 " ; exit 1)
}
2011-10-15 09:22:42 +04:00
assert_has_file ( ) {
2011-11-04 20:31:40 +04:00
test -f " $1 " || ( echo 1>& 2 " Couldn't find ' $1 ' " ; exit 1)
2011-10-15 17:56:31 +04:00
}
2013-06-29 19:45:53 +04:00
assert_has_dir ( ) {
test -d " $1 " || ( echo 1>& 2 " Couldn't find ' $1 ' " ; exit 1)
}
2011-10-15 17:56:31 +04:00
assert_not_has_file ( ) {
if test -f " $1 " ; then
2016-04-17 11:57:06 +03:00
sed -e 's/^/# /' < " $1 " >& 2
echo 1>& 2 " File ' $1 ' exists "
exit 1
2011-10-15 17:56:31 +04:00
fi
2011-10-15 09:22:42 +04:00
}
2013-07-01 18:18:26 +04:00
assert_not_file_has_content ( ) {
if grep -q -e " $2 " " $1 " ; then
2016-04-17 11:57:06 +03:00
sed -e 's/^/# /' < " $1 " >& 2
echo 1>& 2 " File ' $1 ' incorrectly matches regexp ' $2 ' "
exit 1
2013-07-01 18:18:26 +04:00
fi
}
2013-06-29 19:45:53 +04:00
assert_not_has_dir ( ) {
if test -d " $1 " ; then
echo 1>& 2 " Directory ' $1 ' exists " ; exit 1
fi
}
2011-10-15 21:07:26 +04:00
assert_file_has_content ( ) {
2011-12-19 02:35:30 +04:00
if ! grep -q -e " $2 " " $1 " ; then
2016-04-17 11:57:06 +03:00
sed -e 's/^/# /' < " $1 " >& 2
echo 1>& 2 " File ' $1 ' doesn't match regexp ' $2 ' "
exit 1
2011-10-15 21:07:26 +04:00
fi
}
2016-04-22 19:39:54 +03:00
assert_symlink_has_content ( ) {
if ! test -L " $1 " ; then
echo 1>& 2 " File ' $1 ' is not a symbolic link "
exit 1
fi
if ! readlink " $1 " | grep -q -e " $2 " ; then
sed -e 's/^/# /' < " $1 " >& 2
echo 1>& 2 " Symbolic link ' $1 ' doesn't match regexp ' $2 ' "
exit 1
fi
}
2014-09-25 11:05:45 +04:00
assert_file_empty( ) {
if test -s " $1 " ; then
2016-04-17 11:57:06 +03:00
sed -e 's/^/# /' < " $1 " >& 2
echo 1>& 2 " File ' $1 ' is not empty "
exit 1
2014-09-25 11:05:45 +04:00
fi
}
2016-04-22 19:39:54 +03:00
assert_files_hardlinked( ) {
f1 = $( stat -c %i $1 )
f2 = $( stat -c %i $2 )
if [ " $f1 " != " $f2 " ] ; then
echo 1>& 2 " Files ' $1 ' and ' $2 ' are not hardlinked "
exit 1
fi
}
2011-11-01 06:42:14 +04:00
setup_test_repository ( ) {
mode = $1
shift
2011-10-15 08:45:07 +04:00
2011-11-01 06:42:14 +04:00
oldpwd = ` pwd `
2011-10-15 09:22:42 +04:00
2011-11-09 03:11:42 +04:00
cd ${ test_tmpdir }
mkdir repo
cd repo
ot_repo = "--repo=`pwd`"
2012-05-04 18:04:32 +04:00
export OSTREE = " ${ CMD_PREFIX } ostree ${ ot_repo } "
2012-09-15 20:44:57 +04:00
if test -n " $mode " ; then
$OSTREE init --mode= ${ mode }
2011-11-09 03:11:42 +04:00
else
$OSTREE init
fi
cd ${ test_tmpdir }
2011-10-15 09:22:42 +04:00
mkdir files
cd files
2011-11-01 06:42:14 +04:00
ot_files = ` pwd `
2011-10-15 09:22:42 +04:00
export ht_files
2011-11-01 06:42:14 +04:00
ln -s nosuchfile somelink
2011-10-15 09:22:42 +04:00
echo first > firstfile
2011-11-09 03:11:42 +04:00
cd ${ test_tmpdir } /files
$OSTREE commit -b test2 -s "Test Commit 1" -m "Commit body first"
2011-10-15 09:22:42 +04:00
mkdir baz
echo moo > baz/cow
echo alien > baz/saucer
mkdir baz/deeper
echo hi > baz/deeper/ohyeah
2011-11-01 06:42:14 +04:00
ln -s nonexistent baz/alink
2011-10-15 09:22:42 +04:00
mkdir baz/another/
echo x > baz/another/y
2011-11-09 03:11:42 +04:00
cd ${ test_tmpdir } /files
$OSTREE commit -b test2 -s "Test Commit 2" -m "Commit body second"
2011-11-04 07:08:28 +04:00
$OSTREE fsck -q
2011-11-01 06:42:14 +04:00
cd $oldpwd
2011-10-15 08:45:07 +04:00
}
2011-11-01 04:24:38 +04:00
setup_fake_remote_repo1( ) {
2012-09-15 20:44:57 +04:00
mode = $1
2016-01-27 19:44:10 +03:00
commit_opts = ${ 2 :- }
args = ${ 3 :- }
2012-09-15 20:44:57 +04:00
shift
2011-11-01 04:24:38 +04:00
oldpwd = ` pwd `
mkdir ostree-srv
cd ostree-srv
mkdir gnomerepo
2012-09-15 20:44:57 +04:00
${ CMD_PREFIX } ostree --repo= gnomerepo init --mode= $mode
2011-11-01 04:24:38 +04:00
mkdir gnomerepo-files
cd gnomerepo-files
echo first > firstfile
mkdir baz
echo moo > baz/cow
echo alien > baz/saucer
2015-04-30 10:36:27 +03:00
${ CMD_PREFIX } ostree --repo= ${ test_tmpdir } /ostree-srv/gnomerepo commit $commit_opts --add-metadata-string version = 3.0 -b main -s "A remote commit" -m "Some Commit body"
2011-11-01 04:24:38 +04:00
mkdir baz/deeper
2015-04-30 10:36:27 +03:00
${ CMD_PREFIX } ostree --repo= ${ test_tmpdir } /ostree-srv/gnomerepo commit $commit_opts --add-metadata-string version = 3.1 -b main -s "Add deeper"
2011-11-01 04:24:38 +04:00
echo hi > baz/deeper/ohyeah
mkdir baz/another/
echo x > baz/another/y
2015-04-30 10:36:27 +03:00
${ CMD_PREFIX } ostree --repo= ${ test_tmpdir } /ostree-srv/gnomerepo commit $commit_opts --add-metadata-string version = 3.2 -b main -s "The rest"
2011-11-01 04:24:38 +04:00
cd ..
rm -rf gnomerepo-files
cd ${ test_tmpdir }
mkdir ${ test_tmpdir } /httpd
cd httpd
2013-07-06 00:12:10 +04:00
ln -s ${ test_tmpdir } /ostree-srv ostree
2015-03-03 15:13:54 +03:00
${ CMD_PREFIX } ostree trivial-httpd --autoexit --daemonize -p ${ test_tmpdir } /httpd-port $args
2013-07-06 00:12:10 +04:00
port = $( cat ${ test_tmpdir } /httpd-port)
echo " http://127.0.0.1: ${ port } " > ${ test_tmpdir } /httpd-address
2011-11-01 04:24:38 +04:00
cd ${ oldpwd }
2011-11-04 07:08:28 +04:00
2015-03-03 15:13:54 +03:00
export OSTREE = " ${ CMD_PREFIX } ostree --repo=repo "
2011-11-01 04:24:38 +04:00
}
2013-06-29 19:45:53 +04:00
2013-09-01 01:04:48 +04:00
setup_os_boot_syslinux( ) {
# Stub syslinux configuration
mkdir -p sysroot/boot/loader.0
ln -s loader.0 sysroot/boot/loader
touch sysroot/boot/loader/syslinux.cfg
# And a compatibility symlink
mkdir -p sysroot/boot/syslinux
ln -s ../loader/syslinux.cfg sysroot/boot/syslinux/syslinux.cfg
}
2013-09-01 15:07:17 +04:00
setup_os_boot_uboot( ) {
# Stub U-Boot configuration
mkdir -p sysroot/boot/loader.0
ln -s loader.0 sysroot/boot/loader
touch sysroot/boot/loader/uEnv.txt
# And a compatibility symlink
ln -s loader/uEnv.txt sysroot/boot/uEnv.txt
}
2016-04-01 14:51:18 +03:00
setup_os_boot_grub2( ) {
grub2_options = $1
mkdir -p sysroot/boot/grub2/
ln -s ../loader/grub.cfg sysroot/boot/grub2/grub.cfg
export OSTREE_BOOT_PARTITION = "/boot"
case " $grub2_options " in
*ostree-grub-generator*)
cp ${ test_srcdir } /ostree-grub-generator ${ test_tmpdir }
chmod +x ${ test_tmpdir } /ostree-grub-generator
export OSTREE_GRUB2_EXEC = ${ test_tmpdir } /ostree-grub-generator
; ;
esac
}
2013-06-29 19:45:53 +04:00
setup_os_repository ( ) {
mode = $1
2013-09-01 01:04:48 +04:00
bootmode = $2
2013-06-29 19:45:53 +04:00
shift
oldpwd = ` pwd `
cd ${ test_tmpdir }
mkdir testos-repo
if test -n " $mode " ; then
2015-03-03 15:13:54 +03:00
${ CMD_PREFIX } ostree --repo= testos-repo init --mode= ${ mode }
2013-06-29 19:45:53 +04:00
else
2015-03-03 15:13:54 +03:00
${ CMD_PREFIX } ostree --repo= testos-repo init
2013-06-29 19:45:53 +04:00
fi
cd ${ test_tmpdir }
mkdir osdata
cd osdata
mkdir -p boot usr/bin usr/lib/modules/3.6.0 usr/share usr/etc
echo "a kernel" > boot/vmlinuz-3.6.0
echo "an initramfs" > boot/initramfs-3.6.0
2013-08-31 02:12:31 +04:00
bootcsum = $( cat boot/vmlinuz-3.6.0 boot/initramfs-3.6.0 | sha256sum | cut -f 1 -d ' ' )
2013-06-29 19:45:53 +04:00
export bootcsum
mv boot/vmlinuz-3.6.0 boot/vmlinuz-3.6.0-${ bootcsum }
mv boot/initramfs-3.6.0 boot/initramfs-3.6.0-${ bootcsum }
echo "an executable" > usr/bin/sh
echo "some shared data" > usr/share/langs.txt
echo "a library" > usr/lib/libfoo.so.0
ln -s usr/bin bin
cat > usr/etc/os-release <<EOF
NAME = TestOS
VERSION = 42
ID = testos
VERSION_ID = 42
PRETTY_NAME = "TestOS 42"
EOF
echo "a config file" > usr/etc/aconfigfile
mkdir -p usr/etc/NetworkManager
echo "a default daemon file" > usr/etc/NetworkManager/nm.conf
2013-10-15 22:11:43 +04:00
mkdir -p usr/etc/testdirectory
echo "a default daemon file" > usr/etc/testdirectory/test
2013-06-29 19:45:53 +04:00
2015-03-03 15:13:54 +03:00
${ CMD_PREFIX } ostree --repo= ${ test_tmpdir } /testos-repo commit --add-metadata-string version = 1.0.9 -b testos/buildmaster/x86_64-runtime -s "Build"
2013-06-29 19:45:53 +04:00
2014-02-21 03:25:56 +04:00
# Ensure these commits have distinct second timestamps
sleep 2
2013-06-29 19:45:53 +04:00
echo "a new executable" > usr/bin/sh
2015-03-03 15:13:54 +03:00
${ CMD_PREFIX } ostree --repo= ${ test_tmpdir } /testos-repo commit --add-metadata-string version = 1.0.10 -b testos/buildmaster/x86_64-runtime -s "Build"
2013-06-29 19:45:53 +04:00
2014-01-19 02:42:24 +04:00
cd ${ test_tmpdir }
cp -a osdata osdata-devel
cd osdata-devel
mkdir -p usr/include
echo "a development header" > usr/include/foo.h
2015-03-03 15:13:54 +03:00
${ CMD_PREFIX } ostree --repo= ${ test_tmpdir } /testos-repo commit --add-metadata-string version = 1.0.9 -b testos/buildmaster/x86_64-devel -s "Build"
2014-01-19 02:42:24 +04:00
2015-03-03 15:13:54 +03:00
${ CMD_PREFIX } ostree --repo= ${ test_tmpdir } /testos-repo fsck -q
2013-06-29 19:45:53 +04:00
cd ${ test_tmpdir }
mkdir sysroot
2015-07-14 20:30:05 +03:00
export OSTREE_SYSROOT = sysroot
${ CMD_PREFIX } ostree admin init-fs sysroot
${ CMD_PREFIX } ostree admin os-init testos
2013-06-29 19:45:53 +04:00
2013-09-01 15:07:17 +04:00
case $bootmode in
"syslinux" )
setup_os_boot_syslinux
; ;
"uboot" )
setup_os_boot_uboot
; ;
2016-04-01 14:51:18 +03:00
*grub2*)
setup_os_boot_grub2 " ${ bootmode } "
; ;
2013-09-01 15:07:17 +04:00
esac
2014-02-21 03:25:56 +04:00
cd ${ test_tmpdir }
mkdir ${ test_tmpdir } /httpd
cd httpd
ln -s ${ test_tmpdir } ostree
2016-01-27 19:44:10 +03:00
${ CMD_PREFIX } ostree trivial-httpd --autoexit --daemonize -p ${ test_tmpdir } /httpd-port
2014-02-21 03:25:56 +04:00
port = $( cat ${ test_tmpdir } /httpd-port)
echo " http://127.0.0.1: ${ port } " > ${ test_tmpdir } /httpd-address
cd ${ oldpwd }
2013-06-29 19:45:53 +04:00
}
os_repository_new_commit ( )
{
2016-01-27 19:44:10 +03:00
boot_checksum_iteration = ${ 1 :- 0 }
content_iteration = ${ 2 :- 0 }
2013-09-20 16:09:06 +04:00
echo " BOOT ITERATION: $boot_checksum_iteration "
2013-06-29 19:45:53 +04:00
cd ${ test_tmpdir } /osdata
rm boot/*
2013-09-20 16:09:06 +04:00
echo " new: a kernel ${ boot_checksum_iteration } " > boot/vmlinuz-3.6.0
echo " new: an initramfs ${ boot_checksum_iteration } " > boot/initramfs-3.6.0
2013-08-31 02:12:31 +04:00
bootcsum = $( cat boot/vmlinuz-3.6.0 boot/initramfs-3.6.0 | sha256sum | cut -f 1 -d ' ' )
2013-06-29 19:45:53 +04:00
export bootcsum
mv boot/vmlinuz-3.6.0 boot/vmlinuz-3.6.0-${ bootcsum }
mv boot/initramfs-3.6.0 boot/initramfs-3.6.0-${ bootcsum }
echo "a new default config file" > usr/etc/a-new-default-config-file
mkdir -p usr/etc/new-default-dir
echo "a new default dir and file" > usr/etc/new-default-dir/moo
2013-10-04 02:33:18 +04:00
echo " content iteration ${ content_iteration } " > usr/bin/content-iteration
2014-10-22 19:07:44 +04:00
version = $( date " +%Y%m%d. ${ content_iteration } " )
2015-03-03 15:13:54 +03:00
${ CMD_PREFIX } ostree --repo= ${ test_tmpdir } /testos-repo commit --add-metadata-string " version= ${ version } " -b testos/buildmaster/x86_64-runtime -s "Build"
2013-06-29 19:45:53 +04:00
cd ${ test_tmpdir }
}
2016-03-31 13:06:05 +03:00
skip_without_user_xattrs ( ) {
touch test-xattrs
if ! setfattr -n user.testvalue -v somevalue test-xattrs; then
echo "1..0 # SKIP this test requires xattr support"
exit 0
fi
}
2016-03-31 15:44:27 +03:00
skip_without_fuse ( ) {
if ! fusermount --version >/dev/null 2>& 1; then
echo "1..0 # SKIP no fusermount"
exit 0
fi
2016-04-18 17:25:32 +03:00
if ! capsh --print | grep -q 'Bounding set.*[^a-z]cap_sys_admin' ; then
echo "1..0 # SKIP No cap_sys_admin in bounding set, can't use FUSE"
exit 0
fi
2016-03-31 15:44:27 +03:00
if ! [ -w /dev/fuse ] ; then
echo "1..0 # SKIP no write access to /dev/fuse"
exit 0
fi
if ! [ -e /etc/mtab ] ; then
echo "1..0 # SKIP no /etc/mtab"
exit 0
fi
}
2016-03-31 20:46:16 +03:00
libtest_cleanup_gpg ( ) {
gpg-connect-agent --homedir ${ test_tmpdir } /gpghome killagent /bye || true
}