From 8266e14adc2eda1a81a5fdb15efa504e74c2e5eb Mon Sep 17 00:00:00 2001 From: Gregory Haynes Date: Tue, 31 Mar 2015 04:53:30 +0000 Subject: [PATCH] Add functional smoke test for disk-image-create We do not have any testing inside DIB for testing disk-image-create logic. Lets do some smoke testing for all our supported image formats. Also adding a run_functests.sh so we can extend this later without editing the jenkins job. Change-Id: Ie491e27f00bde54f73af6b47c9696ec04d973b14 --- README.rst | 3 +++ bin/disk-image-create | 1 + tests/elements/fake-os/README.rst | 6 +++++ tests/elements/fake-os/element-provides | 1 + tests/elements/fake-os/root.d/10-fake-os | 16 +++++++++++ tests/image_output_formats.bash | 21 +++++++++++++++ tests/run_functests.sh | 8 ++++++ tests/test_functions.bash | 34 ++++++++++++++++++++++++ 8 files changed, 90 insertions(+) create mode 100644 tests/elements/fake-os/README.rst create mode 100644 tests/elements/fake-os/element-provides create mode 100755 tests/elements/fake-os/root.d/10-fake-os create mode 100755 tests/image_output_formats.bash create mode 100755 tests/run_functests.sh create mode 100644 tests/test_functions.bash 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 +}