2017-05-19 20:24:42 +03:00
# Core source library for shell script tests; this
# file is intended to be the canonical source, which at
# is copied at least into:
#
# - https://github.com/projectatomic/rpm-ostree
#
# Copyright (C) 2017 Colin Walters <walters@verbum.org>
#
# 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.
fatal( ) {
2019-12-14 19:21:03 +03:00
echo error: $@ 1>& 2; exit 1
2017-05-19 20:24:42 +03:00
}
# fatal() is shorter to type, but retain this alias
assert_not_reached ( ) {
fatal " $@ "
}
# Some tests look for specific English strings. Use a UTF-8 version
# of the C (POSIX) locale if we have one, or fall back to POSIX
# (https://sourceware.org/glibc/wiki/Proposals/C.UTF-8)
if locale -a | grep C.UTF-8 >/dev/null; then
export LC_ALL = C.UTF-8
else
export LC_ALL = C
fi
# This should really be the default IMO
export G_DEBUG = fatal-warnings
assert_streq ( ) {
test " $1 " = " $2 " || fatal " $1 != $2 "
}
assert_str_match ( ) {
if ! echo " $1 " | grep -E -q " $2 " ; then
fatal " $1 does not match regexp $2 "
fi
}
assert_not_streq ( ) {
( ! test " $1 " = " $2 " ) || fatal " $1 == $2 "
}
assert_has_file ( ) {
test -f " $1 " || fatal " Couldn't find ' $1 ' "
}
assert_has_dir ( ) {
test -d " $1 " || fatal " Couldn't find ' $1 ' "
}
# Dump ls -al + file contents to stderr, then fatal()
_fatal_print_file( ) {
file = " $1 "
shift
ls -al " $file " >& 2
sed -e 's/^/# /' < " $file " >& 2
fatal " $@ "
}
assert_not_has_file ( ) {
if test -f " $1 " ; then
_fatal_print_file " $1 " " File ' $1 ' exists "
fi
}
assert_not_file_has_content ( ) {
fpath = $1
shift
for re in " $@ " ; do
if grep -q -e " $re " " $fpath " ; then
_fatal_print_file " $fpath " " File ' $fpath ' matches regexp ' $re ' "
fi
done
}
assert_not_has_dir ( ) {
if test -d " $1 " ; then
fatal " Directory ' $1 ' exists "
fi
}
assert_file_has_content ( ) {
fpath = $1
shift
for re in " $@ " ; do
if ! grep -q -e " $re " " $fpath " ; then
_fatal_print_file " $fpath " " File ' $fpath ' doesn't match regexp ' $re ' "
fi
done
}
assert_file_has_content_literal ( ) {
2018-01-11 21:36:27 +03:00
fpath = $1 ; shift
for s in " $@ " ; do
if ! grep -q -F -e " $s " " $fpath " ; then
_fatal_print_file " $fpath " " File ' $fpath ' doesn't match fixed string list ' $s ' "
fi
done
2017-05-19 20:24:42 +03:00
}
2020-02-05 06:22:08 +03:00
assert_not_file_has_content_literal ( ) {
fpath = $1 ; shift
for s in " $@ " ; do
if grep -q -F -e " $s " " $fpath " ; then
_fatal_print_file " $fpath " " File ' $fpath ' matches fixed string list ' $s ' "
fi
done
}
2017-05-19 20:24:42 +03:00
assert_symlink_has_content ( ) {
if ! test -L " $1 " ; then
fatal " File ' $1 ' is not a symbolic link "
fi
if ! readlink " $1 " | grep -q -e " $2 " ; then
_fatal_print_file " $1 " " Symbolic link ' $1 ' doesn't match regexp ' $2 ' "
fi
}
assert_file_empty( ) {
if test -s " $1 " ; then
_fatal_print_file " $1 " " File ' $1 ' is not empty "
fi
}
# Use to skip all of these tests
skip( ) {
echo "1..0 # SKIP" " $@ "
exit 0
}
tests/compose: Target FCOS 31, move off of PAPR
Again, a lot going on here, but essentially, we adapt the compose tests
to run either privileged or fully unprivileged via supermin, just like
cosa.
I actually got more than halfway through this initially using `cosa
build` directly for testing. But in the end, we simply need more
flexibility than that. We want to be able to manipulate exactly how
rpm-ostree is called, and cosa is very opinionated about this (and may
also change from under us in the future).
(Another big difference for example is that cosa doesn't care about
non-unified mode, whereas we *need* to have coverage for this until we
fully kill it.)
Really, the most important bit we want from there is the
unprivileged-via-supermin bits. So we copy and adapt that here. One
obvious improvement then is sharing this code more easily (e.g. a
`cosa runasroot` or something?)
However, we still use the FCOS manifest (frozen at a specific tag). It's
a realistic example, and because of the lockfiles and pool, we get good
reproducibility.
2019-12-22 01:42:09 +03:00
# https://github.com/coreos/coreos-assembler/pull/632. Ideally, we'd also cap
# based on memory available to us, but that's notoriously difficult to do for
# containers (see:
# https://fabiokung.com/2014/03/13/memory-inside-linux-containers/). We make an
# assumption here that we have at least e.g. 1G of RAM we can use per CPU
# available to us.
ncpus( ) {
if ! grep -q kubepods /proc/1/cgroup; then
# this might be a developer laptop; leave one cpu free to be nice
echo $(( $( nproc) - 1 ))
return 0
fi
quota = $( cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us)
period = $( cat /sys/fs/cgroup/cpu/cpu.cfs_period_us)
if [ [ ${ quota } != -1 ] ] && [ [ ${ period } -gt 0 ] ] ; then
echo $(( " ${ quota } " / " ${ period } " ))
fi
# just fallback to 1
echo 1
}
filter_tests( ) {
local tests_dir = $1 ; shift
local skipped = 0
local selected_tests = ( )
for tf in $( find " ${ tests_dir } " -name 'test-*.sh' | shuf) ; do
tfbn = $( basename " $tf " .sh)
tfbn = " ${ tfbn #test- } "
if [ -n " ${ TESTS + } " ] ; then
if [ [ " $TESTS " != *$tfbn * ] ] ; then
skipped = $(( skipped + 1 ))
continue
fi
fi
selected_tests += ( " ${ tfbn } " )
done
if [ ${ skipped } -gt 0 ] ; then
echo " Skipping ${ skipped } tests " >& 2
fi
echo " ${ selected_tests [*] } "
}