2016-03-15 03:55:05 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Root directory of integration tests.
|
|
|
|
INTEGRATION_ROOT=$(dirname "$(readlink -f "$BASH_SOURCE")")
|
|
|
|
RUNC="${INTEGRATION_ROOT}/../../runc"
|
|
|
|
GOPATH="${INTEGRATION_ROOT}/../../../.."
|
|
|
|
|
|
|
|
# Test data path.
|
|
|
|
TESTDATA="${INTEGRATION_ROOT}/testdata"
|
|
|
|
|
2016-05-16 16:21:52 +08:00
|
|
|
# Busybox image
|
2016-03-15 03:55:05 +08:00
|
|
|
BUSYBOX_IMAGE="$BATS_TMPDIR/busybox.tar"
|
|
|
|
BUSYBOX_BUNDLE="$BATS_TMPDIR/busyboxtest"
|
|
|
|
|
2016-05-16 16:21:52 +08:00
|
|
|
# hello-world in tar format
|
2016-03-15 03:55:05 +08:00
|
|
|
HELLO_IMAGE="$TESTDATA/hello-world.tar"
|
|
|
|
HELLO_BUNDLE="$BATS_TMPDIR/hello-world"
|
|
|
|
|
|
|
|
# CRIU PATH
|
|
|
|
CRIU="/usr/local/sbin/criu"
|
|
|
|
|
2016-05-18 08:51:02 +08:00
|
|
|
# Kernel version
|
|
|
|
KERNEL_VERSION="$(uname -r)"
|
|
|
|
KERNEL_MAJOR="${KERNEL_VERSION%%.*}"
|
|
|
|
KERNEL_MINOR="${KERNEL_VERSION#$KERNEL_MAJOR.}"
|
|
|
|
KERNEL_MINOR="${KERNEL_MINOR%%.*}"
|
|
|
|
|
2016-05-09 21:06:42 +08:00
|
|
|
# Root state path.
|
|
|
|
ROOT="$BATS_TMPDIR/runc"
|
|
|
|
|
|
|
|
# Wrapper for runc.
|
|
|
|
function runc() {
|
|
|
|
run __runc "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
function __runc() {
|
|
|
|
"$RUNC" --root "$ROOT" "$@"
|
|
|
|
}
|
|
|
|
|
2016-03-15 03:55:05 +08:00
|
|
|
# Retry a command $1 times until it succeeds. Wait $2 seconds between retries.
|
|
|
|
function retry() {
|
|
|
|
local attempts=$1
|
|
|
|
shift
|
|
|
|
local delay=$1
|
|
|
|
shift
|
|
|
|
local i
|
|
|
|
|
|
|
|
for ((i=0; i < attempts; i++)); do
|
|
|
|
run "$@"
|
|
|
|
if [[ "$status" -eq 0 ]] ; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
sleep $delay
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "Command \"$@\" failed $attempts times. Output: $output"
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
# retry until the given container has state
|
|
|
|
function wait_for_container() {
|
|
|
|
local attempts=$1
|
|
|
|
local delay=$2
|
|
|
|
local cid=$3
|
|
|
|
local i
|
|
|
|
|
|
|
|
for ((i=0; i < attempts; i++)); do
|
2016-05-09 21:06:42 +08:00
|
|
|
runc state $cid
|
2016-03-15 03:55:05 +08:00
|
|
|
if [[ "$status" -eq 0 ]] ; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
sleep $delay
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "runc state failed to return state $statecheck $attempts times. Output: $output"
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
# retry until the given container has state
|
|
|
|
function wait_for_container_inroot() {
|
|
|
|
local attempts=$1
|
|
|
|
local delay=$2
|
|
|
|
local cid=$3
|
|
|
|
local i
|
|
|
|
|
|
|
|
for ((i=0; i < attempts; i++)); do
|
2016-05-09 21:06:42 +08:00
|
|
|
ROOT=$4 runc state $cid
|
2016-03-15 03:55:05 +08:00
|
|
|
if [[ "$status" -eq 0 ]] ; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
sleep $delay
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "runc state failed to return state $statecheck $attempts times. Output: $output"
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
function testcontainer() {
|
2016-05-16 16:21:52 +08:00
|
|
|
# test state of container
|
2016-05-09 21:06:42 +08:00
|
|
|
runc state $1
|
2016-03-15 03:55:05 +08:00
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
[[ "${output}" == *"$2"* ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
function setup_busybox() {
|
2016-05-16 16:21:52 +08:00
|
|
|
run mkdir "$BUSYBOX_BUNDLE"
|
2016-03-15 03:55:05 +08:00
|
|
|
run mkdir "$BUSYBOX_BUNDLE"/rootfs
|
|
|
|
if [ -e "/testdata/busybox.tar" ]; then
|
|
|
|
BUSYBOX_IMAGE="/testdata/busybox.tar"
|
|
|
|
fi
|
|
|
|
if [ ! -e $BUSYBOX_IMAGE ]; then
|
2016-05-16 16:21:52 +08:00
|
|
|
curl -o $BUSYBOX_IMAGE -sSL 'https://github.com/jpetazzo/docker-busybox/raw/buildroot-2014.11/rootfs.tar'
|
2016-03-15 03:55:05 +08:00
|
|
|
fi
|
|
|
|
tar -C "$BUSYBOX_BUNDLE"/rootfs -xf "$BUSYBOX_IMAGE"
|
|
|
|
cd "$BUSYBOX_BUNDLE"
|
2016-05-09 21:06:42 +08:00
|
|
|
runc spec
|
2016-03-15 03:55:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function setup_hello() {
|
2016-05-16 16:21:52 +08:00
|
|
|
run mkdir "$HELLO_BUNDLE"
|
2016-03-15 03:55:05 +08:00
|
|
|
run mkdir "$HELLO_BUNDLE"/rootfs
|
|
|
|
tar -C "$HELLO_BUNDLE"/rootfs -xf "$HELLO_IMAGE"
|
|
|
|
cd "$HELLO_BUNDLE"
|
2016-05-09 21:06:42 +08:00
|
|
|
runc spec
|
2016-03-15 03:55:05 +08:00
|
|
|
sed -i 's;"sh";"/hello";' config.json
|
|
|
|
}
|
|
|
|
|
|
|
|
function teardown_running_container() {
|
2016-05-09 21:06:42 +08:00
|
|
|
runc list
|
2016-03-15 03:55:05 +08:00
|
|
|
if [[ "${output}" == *"$1"* ]]; then
|
2016-05-09 21:06:42 +08:00
|
|
|
runc kill $1 KILL
|
|
|
|
retry 10 1 eval "__runc state '$1' | grep -q 'destroyed'"
|
|
|
|
runc delete $1
|
2016-03-15 03:55:05 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function teardown_running_container_inroot() {
|
2016-05-09 21:06:42 +08:00
|
|
|
ROOT=$2 runc list
|
2016-03-15 03:55:05 +08:00
|
|
|
if [[ "${output}" == *"$1"* ]]; then
|
2016-05-09 21:06:42 +08:00
|
|
|
ROOT=$2 runc kill $1 KILL
|
|
|
|
retry 10 1 eval "ROOT='$2' __runc state '$1' | grep -q 'destroyed'"
|
|
|
|
ROOT=$2 runc delete $1
|
2016-03-15 03:55:05 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function teardown_busybox() {
|
|
|
|
cd "$INTEGRATION_ROOT"
|
|
|
|
teardown_running_container test_busybox
|
|
|
|
run rm -f -r "$BUSYBOX_BUNDLE"
|
|
|
|
}
|
|
|
|
|
|
|
|
function teardown_hello() {
|
|
|
|
cd "$INTEGRATION_ROOT"
|
|
|
|
teardown_running_container test_hello
|
|
|
|
run rm -f -r "$HELLO_BUNDLE"
|
|
|
|
}
|