test-50-dissect: move minimal image setup to common setup function

So that it can be re-used for other tests by simply setting
TEST_INSTALL_VERITY_MINIMAL=1 in test.sh, without having to
replicate the setup commands.
This commit is contained in:
Luca Boccassi 2020-08-28 14:17:31 +01:00
parent 46a906f414
commit 9785c44dd8
3 changed files with 54 additions and 21 deletions

View File

@ -5,11 +5,10 @@ set -e
TEST_DESCRIPTION="test systemd-dissect"
IMAGE_NAME="dissect"
TEST_NO_NSPAWN=1
TEST_INSTALL_VERITY_MINIMAL=1
. $TEST_BASE_DIR/test-functions
command -v mksquashfs >/dev/null 2>&1 || exit 0
command -v veritysetup >/dev/null 2>&1 || exit 0
command -v sfdisk >/dev/null 2>&1 || exit 0
# Need loop devices for systemd-dissect
@ -21,23 +20,7 @@ test_append_files() {
install_dmevent
generate_module_dependencies
inst_binary losetup
BASICTOOLS=(
bash
cat
mount
)
oldinitdir=$initdir
export initdir=$TESTDIR/minimal
mkdir -p $initdir/usr/lib $initdir/etc
setup_basic_dirs
install_basic_tools
cp $os_release $initdir/usr/lib/os-release
ln -s ../usr/lib/os-release $initdir/etc/os-release
echo MARKER=1 >> $initdir/usr/lib/os-release
mksquashfs $initdir $oldinitdir/usr/share/minimal.raw
veritysetup format $oldinitdir/usr/share/minimal.raw $oldinitdir/usr/share/minimal.verity | grep '^Root hash:' | cut -f2 | tr -d '\n' > $oldinitdir/usr/share/minimal.roothash
export initdir=$oldinitdir
install_verity_minimal
)
}

View File

@ -462,6 +462,53 @@ run_nspawn() {
return 0
}
# Build two very minimal root images, with two units, one is the same and one is different across them
install_verity_minimal() {
if [ -e $initdir/usr/share/minimal.raw ]; then
return
fi
if ! command -v mksquashfs >/dev/null 2>&1; then
dfatal "mksquashfs not found"
exit 1
fi
if ! command -v veritysetup >/dev/null 2>&1; then
dfatal "veritysetup not found"
exit 1
fi
(
BASICTOOLS=(
bash
cat
mount
sleep
)
oldinitdir=$initdir
rm -rfv $TESTDIR/minimal
export initdir=$TESTDIR/minimal
mkdir -p $initdir/usr/lib/systemd/system $initdir/etc
setup_basic_dirs
install_basic_tools
cp $os_release $initdir/usr/lib/os-release
ln -s ../usr/lib/os-release $initdir/etc/os-release
touch $initdir/etc/machine-id $initdir/etc/resolv.conf
echo MARKER=1 >> $initdir/usr/lib/os-release
echo -e "[Service]\nExecStartPre=cat /usr/lib/os-release\nExecStart=sleep 120" > $initdir/usr/lib/systemd/system/app0.service
cp $initdir/usr/lib/systemd/system/app0.service $initdir/usr/lib/systemd/system/app0-foo.service
mksquashfs $initdir $oldinitdir/usr/share/minimal_0.raw
veritysetup format $oldinitdir/usr/share/minimal_0.raw $oldinitdir/usr/share/minimal_0.verity | \
grep '^Root hash:' | cut -f2 | tr -d '\n' > $oldinitdir/usr/share/minimal_0.roothash
sed -i "s/MARKER=1/MARKER=2/g" $initdir/usr/lib/os-release
rm $initdir/usr/lib/systemd/system/app0-foo.service
cp $initdir/usr/lib/systemd/system/app0.service $initdir/usr/lib/systemd/system/app0-bar.service
mksquashfs $initdir $oldinitdir/usr/share/minimal_1.raw
veritysetup format $oldinitdir/usr/share/minimal_1.raw $oldinitdir/usr/share/minimal_1.verity | \
grep '^Root hash:' | cut -f2 | tr -d '\n' > $oldinitdir/usr/share/minimal_1.roothash
)
}
setup_basic_environment() {
# create the basic filesystem layout
setup_basic_dirs
@ -492,6 +539,9 @@ setup_basic_environment() {
if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
create_asan_wrapper
fi
if [ -n "$TEST_INSTALL_VERITY_MINIMAL" ]; then
install_verity_minimal
fi
}
setup_selinux() {

View File

@ -24,8 +24,8 @@ fi
trap cleanup EXIT
cp /usr/share/minimal.* "${image_dir}/"
image="${image_dir}/minimal"
cp /usr/share/minimal* "${image_dir}/"
image="${image_dir}/minimal_0"
roothash="$(cat ${image}.roothash)"
os_release=$(test -e /etc/os-release && echo /etc/os-release || echo /usr/lib/os-release)