diff --git a/tests/image_creation.sh b/tests/image_creation.sh index 5c1eae421f..242b1dbc1a 100755 --- a/tests/image_creation.sh +++ b/tests/image_creation.sh @@ -7,6 +7,7 @@ set -e readonly script_dir="$(dirname $(readlink -f $0))" +readonly script_name=${0##*/} readonly rootfs_sh="${script_dir}/../rootfs-builder/rootfs.sh" readonly image_builder_sh="${script_dir}/../image-builder/image_builder.sh" @@ -23,9 +24,42 @@ readonly mgr="${tests_repo_dir}/cmd/kata-manager/kata-manager.sh" readonly RUNTIME=${RUNTIME:-kata-runtime} readonly MACHINE_TYPE=`uname -m` +# all distro tests must have this prefix +readonly test_func_prefix="test_distro_" + # "docker build" does not work with a VM-based runtime readonly docker_build_runtime="runc" +test_images_only="false" +test_initrds_only="false" + +usage() +{ + cat <] + $script_name [options] + +Options: + + -h | --help # Show usage. + --distro # Only run tests for specified distro. + --list # List all distros that can be tested. + --test-images-only # Only run images tests for the list of distros under test. + --test-initrds-only # Only run initrds tests for the list of distros under test. + +Parameters: + + +help : Show usage. + : Only run tests for specified distro. + +Notes: + +- If no options or parameters are specified, all tests will be run. + +EOT +} + exit_handler() { if [ "$?" -eq 0 ] @@ -284,12 +318,22 @@ create_and_run() if [ "$image_options" != "no" ] then - handle_options "$distro" "image" "$image_options" + if [ "${test_initrds_only}" = "true" ] + then + info "only testing initrds: skipping image test for distro $distro" + else + handle_options "$distro" "image" "$image_options" + fi fi if [ "$initrd_options" != "no" ] then - handle_options "$distro" "initrd" "$initrd_options" + if [ "${test_images_only}" = "true" ] + then + info "only testing images: skipping initrd test for distro $distro" + else + handle_options "$distro" "initrd" "$initrd_options" + fi fi } @@ -347,6 +391,47 @@ test_distro_alpine() run_test "${name}" "" "alpine" "no" "init" } +# Displays a list of all distro test functions +get_distro_test_names() +{ + typeset -F | awk '{print $3}' |\ + grep "^${test_func_prefix}" | sort +} + +# Displays a list of distros which can be tested +list_distros() +{ + get_distro_test_names | sed "s/${test_func_prefix}//g" +} + +test_single_distro() +{ + local -r distro="$1" + + [ -z "$distro" ] && die "distro cannot be blank" + + local -r expected_func="${test_func_prefix}${distro}" + + local test_funcs + test_funcs=$(get_distro_test_names) + + local defined_func + defined_func=$(echo "$test_funcs" | grep "^${expected_func}$" || true) + + if [ -z "$defined_func" ] + then + local distros + + # make a comma-separated list + distros=$(list_distros | tr '\n' ',' | sed 's/,$//g') + + die "no test for distro '$distro' (try one of $distros)" + fi + + # run the test + $defined_func +} + test_all_distros() { test_distro_fedora @@ -364,9 +449,59 @@ test_all_distros() main() { + local args=$(getopt \ + -n "$script_name" \ + -a \ + --options="h" \ + --longoptions="help distro: list test-images-only test-initrds-only" \ + -- "$@") + + eval set -- "$args" + [ $? -ne 0 ] && { usage >&2; exit 1; } + + local distro= + + while [ $# -gt 1 ] + do + case "$1" in + --distro) distro="$2";; + + -h|--help) usage; exit 0 ;; + + --list) list_distros; exit 0;; + + --test-images-only) + test_images_only="true" + test_initrds_only="false" + ;; + + --test-initrds-only) + test_initrds_only="true" + test_images_only="false" + ;; + + --) shift; break ;; + esac + + shift + done + + # Consume getopt cruft + [ "$1" = "--" ] && shift + + case "$1" in + help) usage && exit 0;; + *) distro="$1";; + esac + setup - test_all_distros + if [ -n "$distro" ] + then + test_single_distro "$distro" + else + test_all_distros + fi } main "$@"