Skip to content

Commit

Permalink
tests: Convert bats test to shell script
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jodh-intel committed Jun 25, 2018
1 parent 5b9b69a commit 7b581c2
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 64 deletions.
5 changes: 0 additions & 5 deletions .ci/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 1 addition & 1 deletion .ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 0 additions & 2 deletions .ci/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
190 changes: 134 additions & 56 deletions tests/image_creation.bats → tests/image_creation.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,26 +1,75 @@
#!/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"
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"

exit_handler()
{
if [ "$?" -eq 0 ]
then
# Rootfs and images are owned by root
sudo -E rm -rf "${tmp_rootfs}"
sudo -E rm -rf "${images_dir}"

rm -rf "${tmp_dir}"

return
fi

# The test failed so dump what we can

info "AGENT_INIT: '${AGENT_INIT}'"

info "images:"
sudo -E ls -l "${images_dir}" >&2

info "rootfs:"
sudo -E ls -l "${tmp_rootfs}" >&2

info "local runtime config:"
cat /etc/kata-containers/configuration.toml >&2

info "main runtime config:"
cat /usr/share/defaults/kata-containers/configuration.toml >&2

info "collect script output:"
sudo -E kata-collect-data.sh >&2

info "processes:"
sudo -E ps -efwww | egrep "docker|kata" >&2
}

trap exit_handler EXIT ERR

die()
{
msg="$*"
echo "ERROR: $msg" >&2
exit 1
}

info()
{
s="$*"
Expand All @@ -31,6 +80,8 @@ set_runtime()
{
local name="$1"

[ -z "$name" ] && die "need name"

# Travis doesn't support VT-x
[ -n "$TRAVIS" ] && return

Expand All @@ -42,6 +93,7 @@ set_runtime()

setup()
{
[ -z "$images_dir" ] && die "need images directory"
mkdir -p "${images_dir}"

export USE_DOCKER=true
Expand All @@ -58,47 +110,14 @@ setup()
set_runtime "${docker_build_runtime}"
}

teardown()
{
if [ "$BATS_ERROR_STATUS" -eq 0 ]
then
# Rootfs and images are owned by root
sudo -E rm -rf "${tmp_rootfs}"
sudo -E rm -rf "${images_dir}"

rm -rf "${tmp_dir}"

return
fi

# The test failed so dump what we can

info "AGENT_INIT: '${AGENT_INIT}'"

info "images:"
sudo -E ls -l "${images_dir}" >&2

info "rootfs:"
sudo -E ls -l "${tmp_rootfs}" >&2

info "local runtime config:"
cat /etc/kata-containers/configuration.toml >&2

info "main runtime config:"
cat /usr/share/defaults/kata-containers/configuration.toml >&2

info "collect script output:"
sudo -E kata-collect-data.sh >&2

info "processes:"
sudo -E ps -efwww | egrep "docker|kata" >&2
}

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
Expand All @@ -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':"
Expand All @@ -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':"
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
test_centos()
{
local -r name="Can create and run centos image"
run_test "${name}" "" "centos" "service" "no"
}

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"

create_and_run euleros "service" "no"
run_test "${name}" "$skip" "euleros" "service" "no"
}

@test "Can create and run alpine image" {
create_and_run alpine "no" "init"
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 "$@"

0 comments on commit 7b581c2

Please sign in to comment.