2016-09-20 10:23:34 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Root directory of integration tests.
|
|
|
|
INTEGRATION_ROOT=$(dirname "$(readlink -f "$BASH_SOURCE")")
|
|
|
|
|
|
|
|
# Test data path.
|
2016-09-27 07:14:31 +00:00
|
|
|
TESTDATA="${INTEGRATION_ROOT}/testdata"
|
2016-09-20 10:23:34 +00:00
|
|
|
|
|
|
|
# Root directory of the repository.
|
|
|
|
OCID_ROOT=${OCID_ROOT:-$(cd "$INTEGRATION_ROOT/../.."; pwd -P)}
|
|
|
|
|
|
|
|
# Path of the ocid binary.
|
2016-09-26 23:55:12 +00:00
|
|
|
OCID_BINARY=${OCID_BINARY:-${OCID_ROOT}/cri-o/ocid}
|
2016-09-20 10:23:34 +00:00
|
|
|
# Path of the ocic binary.
|
2016-09-26 23:55:12 +00:00
|
|
|
OCIC_BINARY=${OCIC_BINARY:-${OCID_ROOT}/cri-o/ocic}
|
2016-09-20 10:23:34 +00:00
|
|
|
# Path of the conmon binary.
|
2016-09-26 23:55:12 +00:00
|
|
|
CONMON_BINARY=${CONMON_BINARY:-${OCID_ROOT}/cri-o/conmon/conmon}
|
2016-10-02 09:11:07 +00:00
|
|
|
# Path of the pause binary.
|
|
|
|
PAUSE_BINARY=${PAUSE_BINARY:-${OCID_ROOT}/cri-o/pause/pause}
|
2016-12-01 13:15:47 +00:00
|
|
|
# Path of the default seccomp profile.
|
2016-11-23 09:41:48 +00:00
|
|
|
SECCOMP_PROFILE=${SECCOMP_PROFILE:-${OCID_ROOT}/cri-o/seccomp.json}
|
2016-12-01 13:15:47 +00:00
|
|
|
# Name of the default apparmor profile.
|
|
|
|
APPARMOR_DEFAULT_PROFILE=${SECCOMP_PROFILE:-ocid-default}
|
2016-09-21 09:03:57 +00:00
|
|
|
# Path of the runc binary.
|
|
|
|
RUNC_PATH=$(command -v runc || true)
|
|
|
|
RUNC_BINARY=${RUNC_PATH:-/usr/local/sbin/runc}
|
2016-12-01 13:15:47 +00:00
|
|
|
# Path of the apparmor_parser binary.
|
|
|
|
APPARMOR_PARSER_BINARY=${APPARMOR_PARSER_BINARY:-/sbin/apparmor_parser}
|
|
|
|
# Path of the apparmor profile for test.
|
|
|
|
APPARMOR_TEST_PROFILE_PATH=${APPARMOR_TEST_PROFILE_PATH:-${TESTDATA}/apparmor_test_deny_write}
|
|
|
|
# Name of the apparmor profile for test.
|
|
|
|
APPARMOR_TEST_PROFILE_NAME=${APPARMOR_TEST_PROFILE_NAME:-apparmor_test_deny_write}
|
2016-09-20 10:23:34 +00:00
|
|
|
|
2016-09-21 09:03:57 +00:00
|
|
|
TESTDIR=$(mktemp -d)
|
2016-10-17 12:48:14 +00:00
|
|
|
if [ -e /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
|
2016-10-14 21:26:13 +00:00
|
|
|
. /etc/selinux/config
|
|
|
|
filelabel=$(awk -F'"' '/^file.*=.*/ {print $2}' /etc/selinux/${SELINUXTYPE}/contexts/lxc_contexts)
|
|
|
|
chcon -R ${filelabel} $TESTDIR
|
|
|
|
fi
|
2016-09-21 09:03:57 +00:00
|
|
|
OCID_SOCKET="$TESTDIR/ocid.sock"
|
2016-10-10 08:22:15 +00:00
|
|
|
OCID_CONFIG="$TESTDIR/ocid.conf"
|
2016-09-21 09:03:57 +00:00
|
|
|
|
|
|
|
cp "$CONMON_BINARY" "$TESTDIR/conmon"
|
|
|
|
|
|
|
|
PATH=$PATH:$TESTDIR
|
2016-09-20 10:23:34 +00:00
|
|
|
|
|
|
|
# Run ocid using the binary specified by $OCID_BINARY.
|
|
|
|
# This must ONLY be run on engines created with `start_ocid`.
|
|
|
|
function ocid() {
|
|
|
|
"$OCID_BINARY" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Run ocic using the binary specified by $OCID_BINARY.
|
|
|
|
function ocic() {
|
2016-10-10 09:57:17 +00:00
|
|
|
"$OCIC_BINARY" --connect "$OCID_SOCKET" "$@"
|
2016-09-20 10:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Communicate with Docker on the host machine.
|
|
|
|
# Should rarely use this.
|
|
|
|
function docker_host() {
|
|
|
|
command docker "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
|
|
|
# Waits until the given ocid becomes reachable.
|
|
|
|
function wait_until_reachable() {
|
2016-09-21 09:03:57 +00:00
|
|
|
retry 15 1 ocic runtimeversion
|
2016-09-20 10:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Start ocid.
|
|
|
|
function start_ocid() {
|
2016-12-01 13:15:47 +00:00
|
|
|
"$OCID_BINARY" --conmon "$CONMON_BINARY" --pause "$PAUSE_BINARY" --listen "$OCID_SOCKET" --runtime "$RUNC_BINARY" --root "$TESTDIR/ocid" --sandboxdir "$TESTDIR/sandboxes" --containerdir "$TESTDIR/ocid/containers" --seccomp-profile "$SECCOMP_PROFILE" --apparmor-profile "$APPARMOR_PROFILE" config >$OCID_CONFIG
|
2016-11-23 09:41:48 +00:00
|
|
|
"$OCID_BINARY" --debug --config "$OCID_CONFIG" & OCID_PID=$!
|
|
|
|
wait_until_reachable
|
|
|
|
}
|
|
|
|
|
|
|
|
function start_ocid_with_seccomp_path() {
|
2016-12-01 13:15:47 +00:00
|
|
|
"$OCID_BINARY" --conmon "$CONMON_BINARY" --pause "$PAUSE_BINARY" --listen "$OCID_SOCKET" --runtime "$RUNC_BINARY" --root "$TESTDIR/ocid" --sandboxdir "$TESTDIR/sandboxes" --containerdir "$TESTDIR/ocid/containers" --seccomp-profile "$1" --apparmor-profile "$APPARMOR_PROFILE" config >$OCID_CONFIG
|
|
|
|
"$OCID_BINARY" --debug --config "$OCID_CONFIG" & OCID_PID=$!
|
|
|
|
wait_until_reachable
|
|
|
|
}
|
|
|
|
|
|
|
|
function start_ocid_with_apparmor_profile_name() {
|
|
|
|
"$OCID_BINARY" --conmon "$CONMON_BINARY" --pause "$PAUSE_BINARY" --listen "$OCID_SOCKET" --runtime "$RUNC_BINARY" --root "$TESTDIR/ocid" --sandboxdir "$TESTDIR/sandboxes" --containerdir "$TESTDIR/ocid/containers" --seccomp-profile "$SECCOMP_PROFILE" --apparmor-profile "$1" config >$OCID_CONFIG
|
2016-10-10 08:22:15 +00:00
|
|
|
"$OCID_BINARY" --debug --config "$OCID_CONFIG" & OCID_PID=$!
|
2016-09-20 10:23:34 +00:00
|
|
|
wait_until_reachable
|
|
|
|
}
|
|
|
|
|
2016-10-08 12:57:45 +00:00
|
|
|
function cleanup_ctrs() {
|
|
|
|
run ocic ctr list --quiet
|
|
|
|
if [ "$status" -eq 0 ]; then
|
|
|
|
if [ "$output" != "" ]; then
|
|
|
|
printf '%s\n' "$output" | while IFS= read -r line
|
|
|
|
do
|
2016-11-23 09:41:48 +00:00
|
|
|
ocic ctr stop --id "$line" || true
|
2016-10-08 12:57:45 +00:00
|
|
|
ocic ctr remove --id "$line"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-09-27 08:40:33 +00:00
|
|
|
function cleanup_pods() {
|
2016-10-08 12:24:16 +00:00
|
|
|
run ocic pod list --quiet
|
2016-09-27 08:54:16 +00:00
|
|
|
if [ "$status" -eq 0 ]; then
|
2016-10-08 12:24:16 +00:00
|
|
|
if [ "$output" != "" ]; then
|
|
|
|
printf '%s\n' "$output" | while IFS= read -r line
|
|
|
|
do
|
2016-11-23 09:41:48 +00:00
|
|
|
ocic pod stop --id "$line" || true
|
2016-10-08 12:24:16 +00:00
|
|
|
ocic pod remove --id "$line"
|
|
|
|
done
|
|
|
|
fi
|
2016-09-27 08:54:16 +00:00
|
|
|
fi
|
2016-09-27 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 10:23:34 +00:00
|
|
|
# Stop ocid.
|
|
|
|
function stop_ocid() {
|
2016-09-27 08:54:16 +00:00
|
|
|
if [ "$OCID_PID" != "" ]; then
|
2016-10-08 12:57:45 +00:00
|
|
|
kill "$OCID_PID" >/dev/null 2>&1
|
2016-10-10 08:22:15 +00:00
|
|
|
rm -f "$OCID_CONFIG"
|
2016-09-27 08:54:16 +00:00
|
|
|
fi
|
2016-09-21 09:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function cleanup_test() {
|
|
|
|
rm -rf "$TESTDIR"
|
2016-09-20 10:23:34 +00:00
|
|
|
}
|
2016-12-01 13:15:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
function load_apparmor_test_profile() {
|
|
|
|
"$APPARMOR_PARSER_BINARY" -r "$APPARMOR_TEST_PROFILE_PATH"
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_apparmor_test_profile() {
|
|
|
|
"$APPARMOR_PARSER_BINARY" -R "$APPARMOR_TEST_PROFILE_PATH"
|
|
|
|
}
|