From 7b581c25d80dc028dc8a1d0a1a3f3ec1ab828982 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Mon, 25 Jun 2018 10:51:43 +0100 Subject: [PATCH] tests: Convert bats test to shell script The tests perform a lot of configuration and call other commands. Since the tests are running under BATS, any "stdout pollution" results in the test failing. This is too rigid for current purposes so convert the BATS test into a `set -e` test. This will still fail if any command fails, but does not impose the output pollution restriction. It also makes debugging easier. Signed-off-by: James O. D. Hunt --- .ci/lib.sh | 5 - .ci/run.sh | 2 +- .ci/setup.sh | 2 - ...{image_creation.bats => image_creation.sh} | 198 ++++++++++++------ 4 files changed, 139 insertions(+), 68 deletions(-) rename tests/{image_creation.bats => image_creation.sh} (68%) mode change 100644 => 100755 diff --git a/.ci/lib.sh b/.ci/lib.sh index 25d30fb552..5f0db45615 100644 --- a/.ci/lib.sh +++ b/.ci/lib.sh @@ -23,8 +23,3 @@ run_static_checks() clone_tests_repo bash "$tests_repo_dir/.ci/static-checks.sh" "github.com/kata-containers/osbuilder" } - -install_bats() -{ - bash "$tests_repo_dir/.ci/install_bats.sh" -} diff --git a/.ci/run.sh b/.ci/run.sh index 57448a4345..a5d2ffc622 100755 --- a/.ci/run.sh +++ b/.ci/run.sh @@ -12,4 +12,4 @@ export GOPATH="${GOPATH:-/tmp/go}" script_dir="$(dirname $(readlink -f $0))" -sudo -E PATH="$PATH" bats "${script_dir}/../tests/image_creation.bats" +sudo -E PATH="$PATH" bash "${script_dir}/../tests/image_creation.sh" diff --git a/.ci/setup.sh b/.ci/setup.sh index 3aae750805..b014198b2f 100755 --- a/.ci/setup.sh +++ b/.ci/setup.sh @@ -15,8 +15,6 @@ bash "${cidir}/static-checks.sh" #Note: If add clearlinux as supported CI use a stateless os-release file source /etc/os-release -install_bats - if [ "$ID" == fedora ];then sudo -E dnf -y install automake yamllint coreutils moreutils elif [ "$ID" == centos ];then diff --git a/tests/image_creation.bats b/tests/image_creation.sh old mode 100644 new mode 100755 similarity index 68% rename from tests/image_creation.bats rename to tests/image_creation.sh index be90cd6e38..a0dbd7490b --- a/tests/image_creation.bats +++ b/tests/image_creation.sh @@ -1,12 +1,16 @@ -#!/usr/bin/env bats +#!/bin/bash # # Copyright (c) 2018 Intel Corporation # # SPDX-License-Identifier: Apache-2.0 -readonly rootfs_sh="$BATS_TEST_DIRNAME/../rootfs-builder/rootfs.sh" -readonly image_builder_sh="$BATS_TEST_DIRNAME/../image-builder/image_builder.sh" -readonly initrd_builder_sh="$BATS_TEST_DIRNAME/../initrd-builder/initrd_builder.sh" +set -e + +readonly script_dir="$(dirname $(readlink -f $0))" + +readonly rootfs_sh="${script_dir}/../rootfs-builder/rootfs.sh" +readonly image_builder_sh="${script_dir}/../image-builder/image_builder.sh" +readonly initrd_builder_sh="${script_dir}/../initrd-builder/initrd_builder.sh" readonly tmp_dir=$(mktemp -t -d osbuilder-test.XXXXXXX) readonly tmp_rootfs="${tmp_dir}/rootfs-osbuilder" readonly images_dir="${tmp_dir}/images" @@ -14,53 +18,16 @@ readonly osbuilder_file="/var/lib/osbuilder/osbuilder.yaml" readonly docker_image="busybox" readonly docker_config_file="/etc/systemd/system/docker.service.d/kata-containers.conf" readonly tests_repo="github.com/kata-containers/tests" -readonly tests_repo_dir="$BATS_TEST_DIRNAME/../../tests" +readonly tests_repo_dir="${script_dir}/../../tests" readonly mgr="${tests_repo_dir}/cmd/kata-manager/kata-manager.sh" readonly RUNTIME=${RUNTIME:-kata-runtime} # "docker build" does not work with a VM-based runtime readonly docker_build_runtime="runc" -info() +exit_handler() { - s="$*" - echo -e "INFO: $s\n" >&2 -} - -set_runtime() -{ - local name="$1" - - # Travis doesn't support VT-x - [ -n "$TRAVIS" ] && return - - sudo -E sed -i "s/--default-runtime=[^ ][^ ]*/--default-runtime=${name}/g" \ - "${docker_config_file}" - sudo -E systemctl daemon-reload - sudo -E systemctl restart docker -} - -setup() -{ - mkdir -p "${images_dir}" - - export USE_DOCKER=true - - # Travis doesn't support VT-x - [ -n "$TRAVIS" ] && return - - [ ! -d "${tests_repo_dir}" ] && git clone "https://${tests_repo}" "${tests_repo_dir}" - - chronic $mgr install-docker-system - chronic $mgr enable-debug - - # Ensure "docker build" works - set_runtime "${docker_build_runtime}" -} - -teardown() -{ - if [ "$BATS_ERROR_STATUS" -eq 0 ] + if [ "$?" -eq 0 ] then # Rootfs and images are owned by root sudo -E rm -rf "${tmp_rootfs}" @@ -94,11 +61,63 @@ teardown() sudo -E ps -efwww | egrep "docker|kata" >&2 } +trap exit_handler EXIT ERR + +die() +{ + msg="$*" + echo "ERROR: $msg" >&2 + exit 1 +} + +info() +{ + s="$*" + echo -e "INFO: $s\n" >&2 +} + +set_runtime() +{ + local name="$1" + + [ -z "$name" ] && die "need name" + + # Travis doesn't support VT-x + [ -n "$TRAVIS" ] && return + + sudo -E sed -i "s/--default-runtime=[^ ][^ ]*/--default-runtime=${name}/g" \ + "${docker_config_file}" + sudo -E systemctl daemon-reload + sudo -E systemctl restart docker +} + +setup() +{ + [ -z "$images_dir" ] && die "need images directory" + mkdir -p "${images_dir}" + + export USE_DOCKER=true + + # Travis doesn't support VT-x + [ -n "$TRAVIS" ] && return + + [ ! -d "${tests_repo_dir}" ] && git clone "https://${tests_repo}" "${tests_repo_dir}" + + chronic $mgr install-docker-system + chronic $mgr enable-debug + + # Ensure "docker build" works + set_runtime "${docker_build_runtime}" +} + build_rootfs() { local distro="$1" local rootfs="$2" + [ -z "$distro" ] && die "need distro" + [ -z "$rootfs" ] && die "need rootfs" + local full="${rootfs}${osbuilder_file}" # clean up from any previous runs @@ -118,6 +137,9 @@ build_image() local file="$1" local rootfs="$2" + [ -z "$file" ] && die "need file" + [ -z "$rootfs" ] && die "need rootfs" + sudo -E ${image_builder_sh} -o "${file}" "${rootfs}" info "built image file '$file' for rootfs '$rootfs':" @@ -129,6 +151,9 @@ build_initrd() local file="$1" local rootfs="$2" + [ -z "$file" ] && die "need file" + [ -z "$rootfs" ] && die "need rootfs" + sudo -E ${initrd_builder_sh} -o "${file}" "${rootfs}" info "built initrd file '$file' for rootfs '$rootfs':" @@ -156,6 +181,9 @@ install_image_create_container() { local file="$1" + [ -z "$file" ] && die "need file" + [ ! -e "$file" ] && die "file does not exist: $file" + # Travis doesn't support VT-x [ -n "$TRAVIS" ] && return @@ -168,6 +196,9 @@ install_initrd_create_container() { local file="$1" + [ -z "$file" ] && die "need file" + [ ! -e "$file" ] && die "file does not exist: $file" + # Travis doesn't support VT-x [ -n "$TRAVIS" ] && return @@ -182,6 +213,9 @@ handle_options() local type="$2" local options="$3" + [ -z "$distro" ] && die "need distro" + [ -z "$type" ] && die "need type" + local opt local rootfs @@ -241,7 +275,9 @@ create_and_run() local image_options="$2" local initrd_options="$3" - [ -n "$distro" ] + [ -z "$distro" ] && die "need distro" + [ -z "$image_options" ] && die "need image options" + [ -z "$initrd_options" ] && die "need initrd options" local opt @@ -256,27 +292,69 @@ create_and_run() fi } -@test "Can create and run fedora image" { - create_and_run fedora "service" "no" +run_test() +{ + local -r name="$1" + local -r skip="$2" + local -r distro="$3" + local -r image_options="$4" + local -r initrd_options="$5" + + [ -z "$name" ] && die "need name" + [ -z "$distro" ] && die "need distro" + [ -z "$image_options" ] && die "need image options" + [ -z "$initrd_options" ] && die "need initrd options" + + [ -n "$skip" ] && info "Skipping test $name: $skip" && return + + info "Running test: ${name}" + + create_and_run "${distro}" "${image_options}" "${initrd_options}" } -@test "Can create and run clearlinux image" { - create_and_run clearlinux "service" "no" +test_fedora() +{ + local -r name="Can create and run fedora image" + run_test "${name}" "" "fedora" "service" "no" } -@test "Can create and run centos image" { - create_and_run centos "service" "no" +test_clearlinux() +{ + local -r name="Can create and run clearlinux image" + + run_test "${name}" "" "clearlinux" "service" "no" } -@test "Can create and run euleros image" { - if [ "$TRAVIS" = true ] - then - skip "travis timeout, see: https://github.com/kata-containers/osbuilder/issues/46" - fi - - create_and_run euleros "service" "no" +test_centos() +{ + local -r name="Can create and run centos image" + run_test "${name}" "" "centos" "service" "no" } -@test "Can create and run alpine image" { - create_and_run alpine "no" "init" +test_euleros() +{ + local -r name="Can create and run euleros image" + + [ "$TRAVIS" = true ] && skip="travis timeout, see: https://github.com/kata-containers/osbuilder/issues/46" + + run_test "${name}" "$skip" "euleros" "service" "no" } + +test_alpine() +{ + local -r name="Can create and run alpine image" + run_test "${name}" "" "alpine" "no" "init" +} + +main() +{ + setup + + test_fedora + test_clearlinux + test_centos + test_euleros + test_alpine +} + +main "$@"