diff --git a/README.rst b/README.rst index 31180572..e19a8598 100644 --- a/README.rst +++ b/README.rst @@ -45,6 +45,9 @@ What tools are there? * element-info : Extract information about elements. +* tests/run_functests.sh + This runs a set of functional tests for diskimage-builder. + Why? ---- diff --git a/bin/disk-image-create b/bin/disk-image-create index 8a9ee7f1..3b9e5c5f 100755 --- a/bin/disk-image-create +++ b/bin/disk-image-create @@ -205,6 +205,7 @@ fi mk_build_dir create_base # This variable needs to be propagated into the chroot +mkdir -p $TMP_HOOKS_PATH/environment.d echo "export DIB_DEFAULT_INSTALLTYPE=\"${DIB_DEFAULT_INSTALLTYPE}\"" > $TMP_HOOKS_PATH/environment.d/11-dib-install-type.bash run_d extra-data # Run pre-install scripts. These do things that prepare the chroot for package installs diff --git a/tests/elements/fake-os/README.rst b/tests/elements/fake-os/README.rst new file mode 100644 index 00000000..cd449c08 --- /dev/null +++ b/tests/elements/fake-os/README.rst @@ -0,0 +1,6 @@ +fake-os +======= + +This is not the element you are looking for. + +This element does not provide an operating system, it provides a fake for testing purposes only. diff --git a/tests/elements/fake-os/element-provides b/tests/elements/fake-os/element-provides new file mode 100644 index 00000000..a72e0496 --- /dev/null +++ b/tests/elements/fake-os/element-provides @@ -0,0 +1 @@ +operating-system diff --git a/tests/elements/fake-os/root.d/10-fake-os b/tests/elements/fake-os/root.d/10-fake-os new file mode 100755 index 00000000..e522cb17 --- /dev/null +++ b/tests/elements/fake-os/root.d/10-fake-os @@ -0,0 +1,16 @@ +#!/bin/bash + +set -eux +set -o pipefail + +sudo touch $TARGET_ROOT/fake-os + +sudo mkdir -p $TARGET_ROOT/etc +sudo mkdir -p $TARGET_ROOT/mnt +sudo mkdir -p $TARGET_ROOT/proc +sudo mkdir -p $TARGET_ROOT/dev +sudo mkdir -p $TARGET_ROOT/sys + +# We need some size so the mkfs does not fail when making an fs of size 0 +# We also need to account for the journal size on the FS +dd if=/dev/zero of=$TARGET_ROOT/fake-data bs=1M count=42 diff --git a/tests/image_output_formats.bash b/tests/image_output_formats.bash new file mode 100755 index 00000000..57ffa72e --- /dev/null +++ b/tests/image_output_formats.bash @@ -0,0 +1,21 @@ +#!/bin/bash + +set -eux +set -o pipefail + +source $(dirname $0)/test_functions.bash + +test_formats="tar raw qcow2" +if [ -z "$(which qemu-img)" ]; then + echo "Warning: No qemu-img binary found, cowardly refusing to run tests." + exit 0 +fi + +for format in '' $test_formats; do + build_test_image $format + echo "Test passed for output formats '$format'." +done + +combined_format=$(echo $test_formats | tr ' ' ',') +build_test_image $combined_format +echo "Test passed for output format '$combined_format'." diff --git a/tests/run_functests.sh b/tests/run_functests.sh new file mode 100755 index 00000000..d7fa3d08 --- /dev/null +++ b/tests/run_functests.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +set -eux +set -o pipefail + +$(dirname $0)/image_output_formats.bash + +echo "Tests passed!" diff --git a/tests/test_functions.bash b/tests/test_functions.bash new file mode 100644 index 00000000..f26d5c22 --- /dev/null +++ b/tests/test_functions.bash @@ -0,0 +1,34 @@ +export TEST_ELEMENTS=$(dirname $0)/elements +export DIB_ELEMENTS=$(dirname $0)/../elements +export DIB_CMD=$(dirname $0)/../bin/disk-image-create + +function build_test_image() { + format=${1:-} + + if [ -n "$format" ]; then + type_arg="-t $format" + else + type_arg= + format="qcow2" + fi + dest_dir=$(mktemp -d) + + trap "rm -rf $dest_dir" EXIT + + ELEMENTS_PATH=$DIB_ELEMENTS:$TEST_ELEMENTS \ + $DIB_CMD $type_arg -o $dest_dir/image -n fake-os + + format=$(echo $format | tr ',' ' ') + for format in $format; do + img_path="$dest_dir/image.$format" + if ! [ -f "$img_path" ]; then + echo "Error: No image with name $img_path found!" + exit 1 + else + echo "Found image $img_path." + fi + done + + trap EXIT + rm -rf $dest_dir +}