Skip to content

Commit

Permalink
tests/int: rework/simplify setup and teardown
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kolyshkin committed Feb 10, 2021
1 parent c34a9b1 commit 41670e2
Show file tree
Hide file tree
Showing 29 changed files with 69 additions and 123 deletions.
5 changes: 1 addition & 4 deletions tests/integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
7 changes: 1 addition & 6 deletions tests/integration/cgroups.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 1 addition & 3 deletions tests/integration/checkpoint.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/create.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
load helpers

function setup() {
teardown_busybox
setup_busybox
}

function teardown() {
teardown_busybox
teardown_bundle
}

@test "runc create" {
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/cwd.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/debug.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
load helpers

function setup() {
teardown_hello
setup_hello
}

function teardown() {
teardown_hello
teardown_bundle
}

@test "global --debug" {
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/delete.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/dev.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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]" {
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/events.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
load helpers

function setup() {
teardown_busybox
setup_busybox
}

function teardown() {
teardown_busybox
teardown_bundle
}

@test "events --stats" {
Expand Down Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions tests/integration/exec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
load helpers

function setup() {
teardown_busybox
setup_busybox
}

function teardown() {
teardown_busybox
teardown_bundle
}

@test "runc exec" {
Expand Down
81 changes: 34 additions & 47 deletions tests/integration/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

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

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

Expand Down Expand Up @@ -428,68 +418,65 @@ 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"

runc_spec
}

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"
}
3 changes: 1 addition & 2 deletions tests/integration/hooks.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)" {
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/kill.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
load helpers

function setup() {
teardown_busybox
setup_busybox
}

function teardown() {
teardown_busybox
teardown_bundle
}

@test "kill detached busybox" {
Expand Down
12 changes: 4 additions & 8 deletions tests/integration/list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/mask.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
load helpers

function setup() {
teardown_busybox
setup_busybox

# Create fake rootfs.
Expand All @@ -15,7 +14,7 @@ function setup() {
}

function teardown() {
teardown_busybox
teardown_bundle
}

@test "mask paths [file]" {
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/mounts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
load helpers

function setup() {
teardown_busybox
setup_busybox
}

function teardown() {
teardown_busybox
teardown_bundle
}

@test "runc run [bind mount]" {
Expand Down
Loading

0 comments on commit 41670e2

Please sign in to comment.