tests/int: rework/simplify setup and teardown

1. Get rid of fixed ROOT, *_BUNDLE, and CONSOLE_SOCKET dirs.
   Now they are temporary directories created in setup_bundle.

2. Automate containers cleanup: instead of having to specify all
   containers to be removed, list and destroy everything (which is
   now possible since every test case has its own unique root).

3. Randomize cgroup paths so two tests running in parallel won't
   use the same cgroup.

Now it's theoretically possible to execute tests in parallel.
Practically it's not possible yet because bats uses GNU parallel,
which do not provide a terminal for whatever it executes, and
many runc tests (all those that run containers with terminal:
true) needs a tty. This may possibly be addressed later.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2021-01-21 15:25:52 -08:00
parent c34a9b1025
commit 41670e21f0
29 changed files with 69 additions and 123 deletions

View File

@@ -61,15 +61,12 @@ load helpers
# setup is called at the beginning of every test.
function setup() {
# see functions teardown_hello and setup_hello in helpers.bash, used to
# create a pristine environment for running your tests
teardown_hello
setup_hello
}
# teardown is called at the end of every test.
function teardown() {
teardown_hello
teardown_bundle
}
@test "this is a simple test" {

View File

@@ -3,15 +3,10 @@
load helpers
function teardown() {
teardown_running_container test_cgroups_kmem
teardown_running_container test_cgroups_permissions
teardown_running_container test_cgroups_group
teardown_running_container test_cgroups_unified
teardown_busybox
teardown_bundle
}
function setup() {
teardown
setup_busybox
}

View File

@@ -6,13 +6,11 @@ function setup() {
# XXX: currently criu require root containers.
requires criu root
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_running_container test_busybox_restore
teardown_bundle
}
function setup_pipes() {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "runc create" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
# Test case for https://github.com/opencontainers/runc/pull/2086

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_hello
setup_hello
}
function teardown() {
teardown_hello
teardown_bundle
}
@test "global --debug" {

View File

@@ -3,13 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_running_container testbusyboxdelete
teardown_bundle
}
@test "runc delete" {

View File

@@ -3,13 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_running_container test_dev
teardown_bundle
}
@test "runc run [redundant default /dev/tty]" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "events --stats" {
@@ -48,7 +47,7 @@ function test_events() {
# test_busybox container which causes the event logger to exit.
(
retry 10 "$retry_every" grep -q test_busybox events.log
teardown_running_container test_busybox
__runc delete -f test_busybox
) &
wait # for both subshells to finish

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "runc exec" {

View File

@@ -20,11 +20,6 @@ RECVTTY="${INTEGRATION_ROOT}/../../contrib/cmd/recvtty/recvtty"
# Test data path.
TESTDATA="${INTEGRATION_ROOT}/testdata"
# Destinations for test containers.
BUSYBOX_BUNDLE="$BATS_RUN_TMPDIR/busyboxtest"
HELLO_BUNDLE="$BATS_RUN_TMPDIR/hello-world"
DEBIAN_BUNDLE="$BATS_RUN_TMPDIR/debiantest"
# CRIU PATH
CRIU="$(which criu 2>/dev/null || true)"
@@ -34,12 +29,6 @@ KERNEL_MAJOR="${KERNEL_VERSION%%.*}"
KERNEL_MINOR="${KERNEL_VERSION#$KERNEL_MAJOR.}"
KERNEL_MINOR="${KERNEL_MINOR%%.*}"
# Root state path.
ROOT=$(mktemp -d "$BATS_RUN_TMPDIR/runc.XXXXXX")
# Path to console socket.
CONSOLE_SOCKET="$BATS_RUN_TMPDIR/console.sock"
# Check if we're in rootless mode.
ROOTLESS=$(id -u)
@@ -55,7 +44,7 @@ function runc() {
# Raw wrapper for runc.
function __runc() {
"$RUNC" ${RUNC_USE_SYSTEMD+--systemd-cgroup} --root "$ROOT" "$@"
"$RUNC" ${RUNC_USE_SYSTEMD+--systemd-cgroup} --root "$ROOT/state" "$@"
}
# Wrapper for runc spec, which takes only one argument (the bundle path).
@@ -123,18 +112,19 @@ function init_cgroup_paths() {
# init once
test -n "$CGROUP_UNIFIED" && return
local rnd="$RANDOM"
if [ -n "${RUNC_USE_SYSTEMD}" ]; then
SD_UNIT_NAME="runc-cgroups-integration-test.scope"
SD_UNIT_NAME="runc-cgroups-integration-test-${rnd}.scope"
if [ $(id -u) = "0" ]; then
REL_CGROUPS_PATH="/machine.slice/$SD_UNIT_NAME"
OCI_CGROUPS_PATH="machine.slice:runc-cgroups:integration-test"
OCI_CGROUPS_PATH="machine.slice:runc-cgroups:integration-test-${rnd}"
else
REL_CGROUPS_PATH="/user.slice/user-$(id -u).slice/user@$(id -u).service/machine.slice/$SD_UNIT_NAME"
# OCI path doesn't contain "/user.slice/user-$(id -u).slice/user@$(id -u).service/" prefix
OCI_CGROUPS_PATH="machine.slice:runc-cgroups:integration-test"
OCI_CGROUPS_PATH="machine.slice:runc-cgroups:integration-test-${rnd}"
fi
else
REL_CGROUPS_PATH="/runc-cgroups-integration-test/test-cgroup"
REL_CGROUPS_PATH="/runc-cgroups-integration-test/test-cgroup-${rnd}"
OCI_CGROUPS_PATH=$REL_CGROUPS_PATH
fi
@@ -428,28 +418,38 @@ function testcontainer() {
}
function setup_recvtty() {
[ -z "$ROOT" ] && return 1 # must not be called without ROOT set
local dir="$ROOT/tty"
mkdir $dir
export CONSOLE_SOCKET="$dir/sock"
# We need to start recvtty in the background, so we double fork in the shell.
("$RECVTTY" --pid-file "$BATS_RUN_TMPDIR/recvtty.pid" --mode null "$CONSOLE_SOCKET" &) &
("$RECVTTY" --pid-file "$dir/pid" --mode null "$CONSOLE_SOCKET" &) &
}
function teardown_recvtty() {
[ -z "$ROOT" ] && return 0 # nothing to teardown
local dir="$ROOT/tty"
# When we kill recvtty, the container will also be killed.
if [ -f "$BATS_RUN_TMPDIR/recvtty.pid" ]; then
kill -9 $(cat "$BATS_RUN_TMPDIR/recvtty.pid")
if [ -f "$dir/pid" ]; then
kill -9 $(cat "$dir/pid")
fi
# Clean up the files that might be left over.
rm -f "$BATS_RUN_TMPDIR/recvtty.pid"
rm -f "$CONSOLE_SOCKET"
rm -rf "$dir"
}
function setup_bundle() {
local image="$1"
local bundle="$2"
# Root for various container directories (state, tty, bundle).
export ROOT=$(mktemp -d "$BATS_RUN_TMPDIR/runc.XXXXXX")
mkdir -p "$ROOT/state" "$ROOT/bundle/rootfs"
setup_recvtty
mkdir -p "$bundle"/rootfs
cd "$bundle"
cd "$ROOT/bundle"
tar --exclude './dev/*' -C rootfs -xf "$image"
@@ -457,39 +457,26 @@ function setup_bundle() {
}
function setup_busybox() {
setup_bundle "$BUSYBOX_IMAGE" "$BUSYBOX_BUNDLE"
setup_bundle "$BUSYBOX_IMAGE"
}
function setup_hello() {
setup_bundle "$HELLO_IMAGE" "$HELLO_BUNDLE"
setup_bundle "$HELLO_IMAGE"
update_config '(.. | select(.? == "sh")) |= "/hello"'
}
function setup_debian() {
setup_bundle "$DEBIAN_IMAGE" "$DEBIAN_BUNDLE"
setup_bundle "$DEBIAN_IMAGE"
}
function teardown_running_container() {
__runc delete -f "$1"
}
function teardown_bundle() {
[ -z "$ROOT" ] && return 0 # nothing to teardown
function teardown_busybox() {
cd "$INTEGRATION_ROOT"
teardown_recvtty
teardown_running_container test_busybox
rm -f -r "$BUSYBOX_BUNDLE"
}
function teardown_hello() {
cd "$INTEGRATION_ROOT"
teardown_recvtty
teardown_running_container test_hello
rm -f -r "$HELLO_BUNDLE"
}
function teardown_debian() {
cd "$INTEGRATION_ROOT"
teardown_recvtty
teardown_running_container test_debian
rm -f -r "$DEBIAN_BUNDLE"
local ct
for ct in $(__runc list -q); do
__runc delete -f "$ct"
done
rm -rf "$ROOT"
}

View File

@@ -5,7 +5,6 @@ load helpers
function setup() {
requires root no_systemd
teardown
setup_debian
# CR = CreateRuntime, CC = CreataContainer
HOOKLIBCR=librunc-hooks-create-runtime.so
@@ -19,7 +18,7 @@ function teardown() {
umount "$LIBPATH"/$HOOKLIBCC.1.0.0 &>/dev/null || true
rm -f $HOOKLIBCR.1.0.0 $HOOKLIBCC.1.0.0
fi
teardown_debian
teardown_bundle
}
@test "runc run (hooks library tests)" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "kill detached busybox" {

View File

@@ -3,20 +3,16 @@
load helpers
function setup() {
unset ALT_ROOT
teardown
setup_busybox
ALT_ROOT=$(mktemp -d "$BATS_RUN_TMPDIR/runc-2.XXXXXX")
ALT_ROOT="$ROOT/alt"
mkdir -p "$ALT_ROOT/state"
}
function teardown() {
if [ -n "$ALT_ROOT" ]; then
ROOT="$ALT_ROOT" teardown_running_container test_box1
ROOT="$ALT_ROOT" teardown_running_container test_box2
ROOT="$ALT_ROOT" teardown_running_container test_box3
rm -rf "$ALT_ROOT"
ROOT="$ALT_ROOT" teardown_bundle
fi
teardown_busybox
teardown_bundle
}
@test "list" {

View File

@@ -3,7 +3,6 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
# Create fake rootfs.
@@ -15,7 +14,7 @@ function setup() {
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "mask paths [file]" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "runc run [bind mount]" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "runc run --no-pivot must not expose bare /proc" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "runc pause and resume" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "ps" {

View File

@@ -3,18 +3,17 @@
load helpers
function setup() {
unset ALT_ROOT
teardown
setup_busybox
ALT_ROOT=$(mktemp -d "$BATS_RUN_TMPDIR/runc-2.XXXXXX")
ALT_ROOT="$ROOT/alt"
mkdir -p "$ALT_ROOT/state"
}
function teardown() {
if [ -n "$ALT_ROOT" ]; then
ROOT=$ALT_ROOT teardown_running_container test_dotbox
ROOT=$ALT_ROOT __runc delete -f test_dotbox
rm -rf "$ALT_ROOT"
fi
teardown_busybox
teardown_bundle
}
@test "global --root" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "runc run [seccomp -ENOSYS handling]" {

View File

@@ -7,7 +7,7 @@ function setup() {
}
function teardown() {
teardown_hello
teardown_bundle
}
@test "spec generation cwd" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "runc start" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "runc run detached" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_hello
setup_hello
}
function teardown() {
teardown_hello
teardown_bundle
}
@test "runc run" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "state (kill + delete)" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "runc run [stdin not a tty]" {

View File

@@ -3,12 +3,11 @@
load helpers
function setup() {
teardown_busybox
setup_busybox
}
function teardown() {
teardown_busybox
teardown_bundle
}
@test "umask" {

View File

@@ -4,13 +4,10 @@ load helpers
function teardown() {
rm -f "$BATS_RUN_TMPDIR"/runc-cgroups-integration-test.json
teardown_running_container test_update
teardown_running_container test_update_rt
teardown_busybox
teardown_bundle
}
function setup() {
teardown
setup_busybox
set_cgroups_path