From 65527da361f8b2ee3b89377a86ddc27e7c831695 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Wed, 22 Mar 2017 23:07:15 +1100 Subject: [PATCH] test: add logging tests The main purpose of these tests is to make sure that the log actually contains output from the container. We don't test the timestamps or the stream that's stated at the moment. Signed-off-by: Aleksa Sarai --- server/config.go | 2 - test/ctr.bats | 97 +++++++++++++++++++ test/helpers.bash | 17 ++++ test/testdata/container_config.json | 4 +- .../testdata/container_config_by_imageid.json | 2 +- test/testdata/container_config_logging.json | 82 ++++++++++++++++ test/testdata/container_config_seccomp.json | 2 +- test/testdata/container_exit_test.json | 2 +- test/testdata/container_redis.json | 2 +- test/testdata/sandbox_config_hostnet.json | 2 +- test/testdata/sandbox_config_seccomp.json | 2 +- 11 files changed, 203 insertions(+), 11 deletions(-) create mode 100644 test/testdata/container_config_logging.json diff --git a/server/config.go b/server/config.go index 014edb06..59233772 100644 --- a/server/config.go +++ b/server/config.go @@ -56,8 +56,6 @@ type RootConfig struct { // LogDir is the default log directory were all logs will go unless kubelet // tells us to put them somewhere else. - // - // TODO: This is currently unused until the conmon logging rewrite is done. LogDir string `toml:"log_dir"` } diff --git a/test/ctr.bats b/test/ctr.bats index ba92f0f7..0798577c 100644 --- a/test/ctr.bats +++ b/test/ctr.bats @@ -99,6 +99,103 @@ function teardown() { stop_ocid } +@test "ctr logging" { + start_ocid + run ocic pod run --config "$TESTDATA"/sandbox_config.json + echo "$output" + [ "$status" -eq 0 ] + pod_id="$output" + run ocic pod list + echo "$output" + [ "$status" -eq 0 ] + + # Create a new container. + newconfig=$(mktemp --tmpdir ocid-config.XXXXXX.json) + cp "$TESTDATA"/container_config_logging.json "$newconfig" + sed -i 's|"%echooutput%"|"here", "is", "some", "output"|' "$newconfig" + run ocic ctr create --config "$newconfig" --pod "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + ctr_id="$output" + run ocic ctr start --id "$ctr_id" + echo "$output" + [ "$status" -eq 0 ] + run ocic ctr stop --id "$ctr_id" + echo "$output" + # Ignore errors on stop. + run ocic ctr status --id "$ctr_id" + [ "$status" -eq 0 ] + run ocic ctr remove --id "$ctr_id" + echo "$output" + [ "$status" -eq 0 ] + + # Check that the output is what we expect. + run cat "$DEFAULT_LOG_PATH/$pod_id/$ctr_id.log" + echo "$DEFAULT_LOG_PATH/$pod_id/$ctr_id.log ::" "$output" + [ "$status" -eq 0 ] + [[ "$output" == *"here is some output" ]] + + run ocic pod stop --id "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + run ocic pod remove --id "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + + cleanup_ctrs + cleanup_pods + stop_ocid +} + +@test "ctr logging [tty=true]" { + start_ocid + run ocic pod run --config "$TESTDATA"/sandbox_config.json + echo "$output" + [ "$status" -eq 0 ] + pod_id="$output" + run ocic pod list + echo "$output" + [ "$status" -eq 0 ] + + # Create a new container. + newconfig=$(mktemp --tmpdir ocid-config.XXXXXX.json) + cp "$TESTDATA"/container_config_logging.json "$newconfig" + sed -i 's|"%echooutput%"|"here", "is", "some", "output"|' "$newconfig" + sed -i 's|"tty": false,|"tty": true,|' "$newconfig" + run ocic ctr create --config "$newconfig" --pod "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + ctr_id="$output" + run ocic ctr start --id "$ctr_id" + echo "$output" + [ "$status" -eq 0 ] + run ocic ctr stop --id "$ctr_id" + echo "$output" + # Ignore errors on stop. + run ocic ctr status --id "$ctr_id" + [ "$status" -eq 0 ] + run ocic ctr remove --id "$ctr_id" + echo "$output" + [ "$status" -eq 0 ] + + # Check that the output is what we expect. + run cat "$DEFAULT_LOG_PATH/$pod_id/$ctr_id.log" + echo "$DEFAULT_LOG_PATH/$pod_id/$ctr_id.log ::" "$output" + [ "$status" -eq 0 ] + [[ "$output" == *"here is some output" ]] + + run ocic pod stop --id "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + run ocic pod remove --id "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + + cleanup_ctrs + cleanup_pods + stop_ocid +} + # regression test for #127 @test "ctrs status for a pod" { start_ocid diff --git a/test/helpers.bash b/test/helpers.bash index 3bff7e19..be4c56c1 100644 --- a/test/helpers.bash +++ b/test/helpers.bash @@ -45,6 +45,8 @@ COPYIMG_BINARY=${COPYIMG_BINARY:-${OCID_ROOT}/cri-o/test/copyimg/copyimg} ARTIFACTS_PATH=${ARTIFACTS_PATH:-${OCID_ROOT}/cri-o/.artifacts} # Path of the checkseccomp binary. CHECKSECCOMP_BINARY=${CHECKSECCOMP_BINARY:-${OCID_ROOT}/cri-o/test/checkseccomp/checkseccomp} +# XXX: This is hardcoded inside cri-o at the moment. +DEFAULT_LOG_PATH=/var/log/ocid/pods TESTDIR=$(mktemp -d) if [ -e /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then @@ -75,6 +77,16 @@ if ! [ -d "$ARTIFACTS_PATH"/redis-image ]; then fi fi +# 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 + # Run ocid using the binary specified by $OCID_BINARY. # This must ONLY be run on engines created with `start_ocid`. function ocid() { @@ -145,6 +157,11 @@ function start_ocid() { ocic image pull redis:latest fi REDIS_IMAGEID=$(ocic image status --id=redis | head -1 | sed -e "s/ID: //g") + 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") } function cleanup_ctrs() { diff --git a/test/testdata/container_config.json b/test/testdata/container_config.json index 1c38e111..3ab8fb8d 100644 --- a/test/testdata/container_config.json +++ b/test/testdata/container_config.json @@ -7,11 +7,9 @@ "image": "docker://redis:latest" }, "command": [ - "/bin/bash" - ], - "args": [ "/bin/ls" ], + "args": [], "working_dir": "/", "envs": [ { diff --git a/test/testdata/container_config_by_imageid.json b/test/testdata/container_config_by_imageid.json index 6cfb3556..5c87e7a5 100644 --- a/test/testdata/container_config_by_imageid.json +++ b/test/testdata/container_config_by_imageid.json @@ -41,7 +41,7 @@ }, "privileged": true, "readonly_rootfs": true, - "log_path": "container.log", + "log_path": "", "stdin": false, "stdin_once": false, "tty": false, diff --git a/test/testdata/container_config_logging.json b/test/testdata/container_config_logging.json new file mode 100644 index 00000000..05035e82 --- /dev/null +++ b/test/testdata/container_config_logging.json @@ -0,0 +1,82 @@ +{ + "metadata": { + "name": "container1", + "attempt": 1 + }, + "image": { + "image": "docker://busybox:latest" + }, + "command": [ + "/bin/echo" + ], + "args": [ + "%echooutput%" + ], + "working_dir": "/", + "envs": [ + { + "key": "PATH", + "value": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + }, + { + "key": "TERM", + "value": "xterm" + }, + { + "key": "TESTDIR", + "value": "test/dir1" + }, + { + "key": "TESTFILE", + "value": "test/file1" + } + ], + "labels": { + "type": "small", + "batch": "no" + }, + "annotations": { + "owner": "dragon", + "daemon": "ocid" + }, + "privileged": true, + "readonly_rootfs": true, + "log_path": "", + "stdin": false, + "stdin_once": false, + "tty": false, + "linux": { + "resources": { + "cpu_period": 10000, + "cpu_quota": 20000, + "cpu_shares": 512, + "memory_limit_in_bytes": 88000000, + "oom_score_adj": 30 + }, + "capabilities": { + "add_capabilities": [ + "setuid", + "setgid" + ], + "drop_capabilities": [ + "audit_write", + "audit_read" + ] + }, + "selinux_options": { + "user": "system_u", + "role": "system_r", + "type": "container_t", + "level": "s0:c4,c5" + }, + "user": { + "uid": 5, + "gid": 300, + "additional_gids": [ + 400, + 401, + 402 + ] + } + } +} diff --git a/test/testdata/container_config_seccomp.json b/test/testdata/container_config_seccomp.json index 6213c515..027c25e1 100644 --- a/test/testdata/container_config_seccomp.json +++ b/test/testdata/container_config_seccomp.json @@ -41,7 +41,7 @@ }, "privileged": true, "readonly_rootfs": true, - "log_path": "container.log", + "log_path": "", "stdin": false, "stdin_once": false, "tty": false, diff --git a/test/testdata/container_exit_test.json b/test/testdata/container_exit_test.json index 8780faed..bca99fb7 100644 --- a/test/testdata/container_exit_test.json +++ b/test/testdata/container_exit_test.json @@ -15,7 +15,7 @@ } ], "readonly_rootfs": true, - "log_path": "container.log", + "log_path": "", "stdin": false, "stdin_once": false, "tty": false, diff --git a/test/testdata/container_redis.json b/test/testdata/container_redis.json index 17e8de93..839ca746 100644 --- a/test/testdata/container_redis.json +++ b/test/testdata/container_redis.json @@ -39,7 +39,7 @@ "pod": "podsandbox1" }, "readonly_rootfs": false, - "log_path": "container.log", + "log_path": "", "stdin": false, "stdin_once": false, "tty": false, diff --git a/test/testdata/sandbox_config_hostnet.json b/test/testdata/sandbox_config_hostnet.json index fad50e42..44a72166 100644 --- a/test/testdata/sandbox_config_hostnet.json +++ b/test/testdata/sandbox_config_hostnet.json @@ -6,7 +6,7 @@ "attempt": 1 }, "hostname": "ocic_host", - "log_directory": ".", + "log_directory": "", "dns_options": { "servers": [ "server1.redhat.com", diff --git a/test/testdata/sandbox_config_seccomp.json b/test/testdata/sandbox_config_seccomp.json index 9a55f51a..de666222 100644 --- a/test/testdata/sandbox_config_seccomp.json +++ b/test/testdata/sandbox_config_seccomp.json @@ -6,7 +6,7 @@ "attempt": 1 }, "hostname": "ocic_host", - "log_directory": ".", + "log_directory": "", "dns_options": { "servers": [ "server1.redhat.com",