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.
|
2017-01-24 13:07:05 +00:00
|
|
|
OCID_BINARY=${OCID_BINARY:-${OCID_ROOT}/cri-o/ocid}
|
2016-09-20 10:23:34 +00:00
|
|
|
# Path of the ocic binary.
|
2017-01-24 13:07:05 +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.
|
2016-12-02 07:13:41 +00:00
|
|
|
APPARMOR_PROFILE=${APPARMOR_PROFILE:-ocid-default}
|
2017-01-25 23:58:13 +00:00
|
|
|
# Runtime
|
|
|
|
RUNTIME=${RUNTIME:-runc}
|
|
|
|
RUNTIME_PATH=$(command -v $RUNTIME || true)
|
|
|
|
RUNTIME_BINARY=${RUNTIME_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}
|
2016-12-07 11:32:50 +00:00
|
|
|
# Path of the apparmor profile for unloading ocid-default.
|
|
|
|
FAKE_OCID_DEFAULT_PROFILE_PATH=${FAKE_OCID_DEFAULT_PROFILE_PATH:-${TESTDATA}/fake_ocid_default}
|
2016-12-01 13:15:47 +00:00
|
|
|
# Name of the apparmor profile for test.
|
2016-12-02 07:13:41 +00:00
|
|
|
APPARMOR_TEST_PROFILE_NAME=${APPARMOR_TEST_PROFILE_NAME:-apparmor-test-deny-write}
|
|
|
|
# Path of boot config.
|
|
|
|
BOOT_CONFIG_FILE_PATH=${BOOT_CONFIG_FILE_PATH:-/boot/config-`uname -r`}
|
|
|
|
# Path of apparmor parameters file.
|
|
|
|
APPARMOR_PARAMETERS_FILE_PATH=${APPARMOR_PARAMETERS_FILE_PATH:-/sys/module/apparmor/parameters/enabled}
|
2016-12-16 23:34:51 +00:00
|
|
|
# Path of the bin2img binary.
|
|
|
|
BIN2IMG_BINARY=${BIN2IMG_BINARY:-${OCID_ROOT}/cri-o/test/bin2img/bin2img}
|
2017-01-10 22:57:22 +00:00
|
|
|
# Path of the copyimg binary.
|
|
|
|
COPYIMG_BINARY=${COPYIMG_BINARY:-${OCID_ROOT}/cri-o/test/copyimg/copyimg}
|
2017-01-19 17:06:34 +00:00
|
|
|
# Path of tests artifacts.
|
|
|
|
ARTIFACTS_PATH=${ARTIFACTS_PATH:-${OCID_ROOT}/cri-o/.artifacts}
|
2017-01-19 17:11:02 +00:00
|
|
|
# Path of the checkseccomp binary.
|
|
|
|
CHECKSECCOMP_BINARY=${CHECKSECCOMP_BINARY:-${OCID_ROOT}/cri-o/test/checkseccomp/checkseccomp}
|
2017-03-22 12:07:15 +00:00
|
|
|
# XXX: This is hardcoded inside cri-o at the moment.
|
|
|
|
DEFAULT_LOG_PATH=/var/log/ocid/pods
|
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-12-17 11:29:27 +00:00
|
|
|
OCID_CNI_CONFIG="$TESTDIR/cni/net.d/"
|
2016-12-17 14:21:11 +00:00
|
|
|
OCID_CNI_PLUGIN="/opt/cni/bin/"
|
2016-12-17 11:29:27 +00:00
|
|
|
POD_CIDR="10.88.0.0/16"
|
|
|
|
POD_CIDR_MASK="10.88.*.*"
|
2016-09-21 09:03:57 +00:00
|
|
|
|
|
|
|
cp "$CONMON_BINARY" "$TESTDIR/conmon"
|
|
|
|
|
2016-12-17 11:29:27 +00:00
|
|
|
mkdir -p $OCID_CNI_CONFIG
|
|
|
|
|
2016-09-21 09:03:57 +00:00
|
|
|
PATH=$PATH:$TESTDIR
|
2016-09-20 10:23:34 +00:00
|
|
|
|
2017-01-10 22:57:22 +00:00
|
|
|
# Make sure we have a copy of the redis:latest image.
|
2017-01-19 17:06:34 +00:00
|
|
|
if ! [ -d "$ARTIFACTS_PATH"/redis-image ]; then
|
|
|
|
mkdir -p "$ARTIFACTS_PATH"/redis-image
|
|
|
|
if ! "$COPYIMG_BINARY" --import-from=docker://redis --export-to=dir:"$ARTIFACTS_PATH"/redis-image --signature-policy="$INTEGRATION_ROOT"/policy.json ; then
|
2017-01-10 22:57:22 +00:00
|
|
|
echo "Error pulling docker://redis"
|
2017-01-19 17:06:34 +00:00
|
|
|
rm -fr "$ARTIFACTS_PATH"/redis-image
|
2017-01-10 22:57:22 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2017-03-22 12:07:15 +00:00
|
|
|
# Make sure we have a copy of the busybox:latest image.
|
|
|
|
if ! [ -d "$ARTIFACTS_PATH"/busybox-image ]; then
|
|
|
|
mkdir -p "$ARTIFACTS_PATH"/busybox-image
|
|
|
|
if ! "$COPYIMG_BINARY" --import-from=docker://busybox --export-to=dir:"$ARTIFACTS_PATH"/busybox-image --signature-policy="$INTEGRATION_ROOT"/policy.json ; then
|
|
|
|
echo "Error pulling docker://busybox"
|
|
|
|
rm -fr "$ARTIFACTS_PATH"/busybox-image
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
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() {
|
2016-10-18 14:48:33 +00:00
|
|
|
"$OCID_BINARY" --listen "$OCID_SOCKET" "$@"
|
2016-09-20 10:23:34 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 13:01:47 +00:00
|
|
|
# Run ocic using the binary specified by $OCIC_BINARY.
|
2016-09-20 10:23:34 +00:00
|
|
|
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-02 07:13:41 +00:00
|
|
|
if [[ -n "$1" ]]; then
|
|
|
|
seccomp="$1"
|
|
|
|
else
|
|
|
|
seccomp="$SECCOMP_PROFILE"
|
|
|
|
fi
|
2016-11-23 09:41:48 +00:00
|
|
|
|
2016-12-02 07:13:41 +00:00
|
|
|
if [[ -n "$2" ]]; then
|
|
|
|
apparmor="$2"
|
|
|
|
else
|
|
|
|
apparmor="$APPARMOR_PROFILE"
|
|
|
|
fi
|
2016-12-01 13:15:47 +00:00
|
|
|
|
2017-01-10 22:57:22 +00:00
|
|
|
# Don't forget: bin2img, copyimg, and ocid have their own default drivers, so if you override any, you probably need to override them all
|
2016-12-16 23:34:51 +00:00
|
|
|
if ! [ "$3" = "--no-pause-image" ] ; then
|
|
|
|
"$BIN2IMG_BINARY" --root "$TESTDIR/ocid" --runroot "$TESTDIR/ocid-run" --source-binary "$PAUSE_BINARY"
|
|
|
|
fi
|
2017-01-26 13:01:47 +00:00
|
|
|
"$COPYIMG_BINARY" --root "$TESTDIR/ocid" --runroot "$TESTDIR/ocid-run" --image-name=redis --import-from=dir:"$ARTIFACTS_PATH"/redis-image --add-name=docker://docker.io/library/redis:latest --signature-policy="$INTEGRATION_ROOT"/policy.json
|
2017-02-03 22:19:29 +00:00
|
|
|
"$OCID_BINARY" --conmon "$CONMON_BINARY" --listen "$OCID_SOCKET" --runtime "$RUNTIME_BINARY" --root "$TESTDIR/ocid" --runroot "$TESTDIR/ocid-run" --seccomp-profile "$seccomp" --apparmor-profile "$apparmor" --cni-config-dir "$OCID_CNI_CONFIG" --signature-policy "$INTEGRATION_ROOT"/policy.json --config /dev/null 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
|
2017-01-16 18:19:44 +00:00
|
|
|
|
|
|
|
run ocic image status --id=redis
|
|
|
|
if [ "$status" -ne 0 ] ; then
|
2017-01-31 15:32:27 +00:00
|
|
|
ocic image pull redis:latest
|
2017-01-16 18:19:44 +00:00
|
|
|
fi
|
2017-01-31 15:32:27 +00:00
|
|
|
REDIS_IMAGEID=$(ocic image status --id=redis | head -1 | sed -e "s/ID: //g")
|
2017-03-22 12:07:15 +00:00
|
|
|
run ocic image status --id=busybox
|
|
|
|
if [ "$status" -ne 0 ] ; then
|
|
|
|
ocic image pull busybox:latest
|
|
|
|
fi
|
|
|
|
BUSYBOX_IMAGEID=$(ocic image status --id=busybox | head -1 | sed -e "s/ID: //g")
|
2016-09-20 10:23:34 +00:00
|
|
|
}
|
|
|
|
|
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-10-18 14:48:33 +00:00
|
|
|
function cleanup_images() {
|
|
|
|
run ocic image list --quiet
|
|
|
|
if [ "$status" -eq 0 ]; then
|
|
|
|
if [ "$output" != "" ]; then
|
|
|
|
printf '%s\n' "$output" | while IFS= read -r line
|
|
|
|
do
|
|
|
|
ocic image 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-18 14:48:33 +00:00
|
|
|
wait "$OCID_PID"
|
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
|
|
|
}
|
|
|
|
|
2016-12-09 10:56:25 +00:00
|
|
|
function restart_ocid() {
|
|
|
|
if [ "$OCID_PID" != "" ]; then
|
|
|
|
kill "$OCID_PID" >/dev/null 2>&1
|
2017-02-03 22:18:26 +00:00
|
|
|
wait "$OCID_PID"
|
2016-12-09 10:56:25 +00:00
|
|
|
start_ocid
|
|
|
|
else
|
|
|
|
echo "you must start ocid first"
|
|
|
|
exit 1
|
|
|
|
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
|
|
|
|
|
|
|
|
2016-12-07 11:32:50 +00:00
|
|
|
function load_apparmor_profile() {
|
|
|
|
"$APPARMOR_PARSER_BINARY" -r "$1"
|
2016-12-01 13:15:47 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 11:32:50 +00:00
|
|
|
function remove_apparmor_profile() {
|
|
|
|
"$APPARMOR_PARSER_BINARY" -R "$1"
|
2016-12-01 13:15:47 +00:00
|
|
|
}
|
2016-12-02 07:13:41 +00:00
|
|
|
|
|
|
|
function is_seccomp_enabled() {
|
2017-01-19 17:11:02 +00:00
|
|
|
if ! "$CHECKSECCOMP_BINARY" ; then
|
|
|
|
echo 0
|
|
|
|
return
|
2016-12-02 07:13:41 +00:00
|
|
|
fi
|
2017-01-19 17:11:02 +00:00
|
|
|
echo 1
|
2016-12-02 07:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function is_apparmor_enabled() {
|
|
|
|
if [[ -f "$APPARMOR_PARAMETERS_FILE_PATH" ]]; then
|
|
|
|
out=$(cat "$APPARMOR_PARAMETERS_FILE_PATH")
|
|
|
|
if [[ "$out" =~ "Y" ]]; then
|
|
|
|
echo 1
|
2016-12-06 11:10:32 +00:00
|
|
|
return
|
2016-12-02 07:13:41 +00:00
|
|
|
fi
|
|
|
|
fi
|
2016-12-06 11:10:32 +00:00
|
|
|
echo 0
|
2016-12-02 07:13:41 +00:00
|
|
|
}
|
2016-12-17 11:29:27 +00:00
|
|
|
|
|
|
|
function prepare_network_conf() {
|
|
|
|
cat >$OCID_CNI_CONFIG/10-ocid.conf <<-EOF
|
|
|
|
{
|
|
|
|
"cniVersion": "0.2.0",
|
|
|
|
"name": "ocidnet",
|
|
|
|
"type": "bridge",
|
|
|
|
"bridge": "cni0",
|
|
|
|
"isGateway": true,
|
|
|
|
"ipMasq": true,
|
|
|
|
"ipam": {
|
|
|
|
"type": "host-local",
|
|
|
|
"subnet": "$1",
|
|
|
|
"routes": [
|
|
|
|
{ "dst": "0.0.0.0/0" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat >$OCID_CNI_CONFIG/99-loopback.conf <<-EOF
|
|
|
|
{
|
|
|
|
"cniVersion": "0.2.0",
|
|
|
|
"type": "loopback"
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
echo 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_pod_cidr() {
|
|
|
|
fullnetns=`ocic pod status --id $1 | grep namespace | cut -d ' ' -f 3`
|
|
|
|
netns=`basename $fullnetns`
|
|
|
|
|
|
|
|
ip netns exec $netns ip addr show dev eth0 scope global | grep $POD_CIDR_MASK
|
|
|
|
|
|
|
|
echo $?
|
|
|
|
}
|
|
|
|
|
|
|
|
function parse_pod_ip() {
|
2016-12-21 11:26:18 +00:00
|
|
|
for arg
|
|
|
|
do
|
|
|
|
cidr=`echo "$arg" | grep $POD_CIDR_MASK`
|
|
|
|
if [ "$cidr" == "$arg" ]
|
|
|
|
then
|
|
|
|
echo `echo "$arg" | sed "s/\/[0-9][0-9]//"`
|
|
|
|
fi
|
|
|
|
done
|
2016-12-17 11:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ping_pod() {
|
2016-12-21 11:26:18 +00:00
|
|
|
netns=`ocic pod status --id $1 | grep namespace | cut -d ' ' -f 3`
|
2016-12-17 11:29:27 +00:00
|
|
|
inet=`ip netns exec \`basename $netns\` ip addr show dev eth0 scope global | grep inet`
|
|
|
|
|
|
|
|
IFS=" "
|
|
|
|
ip=`parse_pod_ip $inet`
|
|
|
|
|
|
|
|
ping -W 1 -c 5 $ip
|
|
|
|
|
|
|
|
echo $?
|
|
|
|
}
|
|
|
|
|
2016-12-21 10:28:33 +00:00
|
|
|
function ping_pod_from_pod() {
|
|
|
|
pod_ip=`ocic pod status --id $1 | grep "IP Address" | cut -d ' ' -f 3`
|
|
|
|
netns=`ocic pod status --id $2 | grep namespace | cut -d ' ' -f 3`
|
|
|
|
|
|
|
|
ip netns exec `basename $netns` ping -W 1 -c 2 $pod_ip
|
|
|
|
|
|
|
|
echo $?
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-17 11:29:27 +00:00
|
|
|
function cleanup_network_conf() {
|
|
|
|
rm -rf $OCID_CNI_CONFIG
|
|
|
|
|
|
|
|
echo 0
|
|
|
|
}
|
2016-12-21 10:28:33 +00:00
|
|
|
|
|
|
|
function temp_sandbox_conf() {
|
|
|
|
sed -e s/\"namespace\":.*/\"namespace\":\ \"$1\",/g "$TESTDATA"/sandbox_config.json > $TESTDIR/sandbox_config_$1.json
|
|
|
|
}
|