From 69403136917131b968ca34290714394f73ee59d6 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Wed, 12 Apr 2017 08:13:26 +1000 Subject: [PATCH] test: add tests for split std{err,out} Now that we have support for split std{out,err}, make sure that execsync will correctly handle the split stdio properly. In addition, extend the ctr logging test to make sure that the regular container logging is also split correctly. We can't test !terminal containers because we only have a single console for both std{out,err}. Signed-off-by: Aleksa Sarai --- test/ctr.bats | 77 ++++++++++++++++++--- test/testdata/container_config_logging.json | 4 +- 2 files changed, 69 insertions(+), 12 deletions(-) diff --git a/test/ctr.bats b/test/ctr.bats index 0798577c..2c936add 100644 --- a/test/ctr.bats +++ b/test/ctr.bats @@ -112,7 +112,7 @@ function teardown() { # 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|"%shellcommand%"|"echo here is some output \&\& echo and some from stderr >\&2"|' "$newconfig" run ocic ctr create --config "$newconfig" --pod "$pod_id" echo "$output" [ "$status" -eq 0 ] @@ -130,10 +130,11 @@ function teardown() { [ "$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" ]] + logpath="$DEFAULT_LOG_PATH/$pod_id/$ctr_id.log" + [ -f "$logpath" ] + echo "$logpath :: $(cat "$logpath")" + grep -E "^[^\n]+ stdout here is some output$" "$logpath" + grep -E "^[^\n]+ stderr and some from stderr$" "$logpath" run ocic pod stop --id "$pod_id" echo "$output" @@ -160,7 +161,7 @@ function teardown() { # 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|"%shellcommand%"|"echo here is some output"|' "$newconfig" sed -i 's|"tty": false,|"tty": true,|' "$newconfig" run ocic ctr create --config "$newconfig" --pod "$pod_id" echo "$output" @@ -179,10 +180,10 @@ function teardown() { [ "$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" ]] + logpath="$DEFAULT_LOG_PATH/$pod_id/$ctr_id.log" + [ -f "$logpath" ] + echo "$logpath :: $(cat "$logpath")" + grep -E "^[^\n]+ stdout here is some output$" "$logpath" run ocic pod stop --id "$pod_id" echo "$output" @@ -450,6 +451,62 @@ function teardown() { stop_ocid } +@test "ctr execsync exit code" { + start_ocid + run ocic pod run --config "$TESTDATA"/sandbox_config.json + echo "$output" + [ "$status" -eq 0 ] + pod_id="$output" + run ocic ctr create --config "$TESTDATA"/container_redis.json --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 execsync --id "$ctr_id" false + echo "$output" + [ "$status" -eq 0 ] + [[ "$output" =~ "Exit code: 1" ]] + cleanup_ctrs + cleanup_pods + stop_ocid +} + +@test "ctr execsync std{out,err}" { + start_ocid + run ocic pod run --config "$TESTDATA"/sandbox_config.json + echo "$output" + [ "$status" -eq 0 ] + pod_id="$output" + run ocic ctr create --config "$TESTDATA"/container_redis.json --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 execsync --id "$ctr_id" "echo hello0 stdout" + echo "$output" + [ "$status" -eq 0 ] + [[ "$output" == *"$(printf "Stdout:\nhello0 stdout")"* ]] + run ocic ctr execsync --id "$ctr_id" "echo hello1 stderr >&2" + echo "$output" + [ "$status" -eq 0 ] + [[ "$output" == *"$(printf "Stderr:\nhello1 stderr")"* ]] + run ocic ctr execsync --id "$ctr_id" "echo hello2 stderr >&2; echo hello3 stdout" + echo "$output" + [ "$status" -eq 0 ] + [[ "$output" == *"$(printf "Stderr:\nhello2 stderr")"* ]] + [[ "$output" == *"$(printf "Stdout:\nhello3 stdout")"* ]] + run ocic pod remove --id "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + cleanup_ctrs + cleanup_pods + stop_ocid +} + @test "ctr stop idempotent" { start_ocid run ocic pod run --config "$TESTDATA"/sandbox_config.json diff --git a/test/testdata/container_config_logging.json b/test/testdata/container_config_logging.json index 05035e82..2d48747c 100644 --- a/test/testdata/container_config_logging.json +++ b/test/testdata/container_config_logging.json @@ -7,10 +7,10 @@ "image": "docker://busybox:latest" }, "command": [ - "/bin/echo" + "/bin/sh", "-c" ], "args": [ - "%echooutput%" + "%shellcommand%" ], "working_dir": "/", "envs": [