Skip to content

Commit

Permalink
ctr.bats: fix jq checks
Browse files Browse the repository at this point in the history
Commits a2ec1d4 and 247d465 added a few checks
using jq in the following test cases:

 * ctr lifecycle
 * ctr execsync should not overwrite initial spec args
 * privileged ctr -- check for rw mounts

Alas, those checks do not work (and never worked); jq always succeeds.

This happened because

1. in `run cmd1 ... | cmd2` the part starting with the pipe
   character is not part of `run` statement;

2. `run` eats `cmd1 ...` output (into `$output` variable);

so `cmd2` is provided with empty input.

Now,

3. `jq` with empty input does not run any filters and thus succeeds
   (even with `-e`, see [1]).

The fix is to add a separate check that the output is not empty.

While at it, remove `run` where it's not needed from the other places
in those three tests we fix.

[1] jqlang/jq#1628

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Oct 9, 2020
1 parent ec6e62a commit 4c40f29
Showing 1 changed file with 43 additions and 88 deletions.
131 changes: 43 additions & 88 deletions test/ctr.bats
Original file line number Diff line number Diff line change
Expand Up @@ -222,71 +222,38 @@ function wait_until_exit() {

@test "ctr lifecycle" {
start_crio
run crictl runp "$TESTDATA"/sandbox_config.json
echo "$output"
[ "$status" -eq 0 ]
pod_id="$output"
run crictl pods --quiet
echo "$output"
[ "$status" -eq 0 ]
pod_id=$(crictl runp "$TESTDATA"/sandbox_config.json)
output=$(crictl pods --quiet)
[[ "$output" == "$pod_id" ]]
run crictl create "$pod_id" "$TESTDATA"/container_redis.json "$TESTDATA"/sandbox_config.json
echo "$output"
[ "$status" -eq 0 ]
ctr_id="$output"
run crictl ps --quiet --state created
echo "$output"
[ "$status" -eq 0 ]

ctr_id=$(crictl create "$pod_id" "$TESTDATA"/container_redis.json "$TESTDATA"/sandbox_config.json)
output=$(crictl ps --quiet --state created)
[[ "$output" == "$ctr_id" ]]
run crictl inspect "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run crictl inspect "$ctr_id" | jq -e ".info.privileged == false"
echo "$output"
[ "$status" -eq 0 ]
run crictl start "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run crictl inspect "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run crictl ps --quiet --state running
echo "$output"
[ "$status" -eq 0 ]

output=$(crictl inspect "$ctr_id")
[ -n "$output" ]
echo "$output" | jq -e ".info.privileged == false"

crictl start "$ctr_id"
crictl inspect "$ctr_id"
output=$(crictl ps --quiet --state running)
[[ "$output" == "$ctr_id" ]]
run crictl stop "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run crictl inspect "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run crictl ps --quiet --state exited
echo "$output"
[ "$status" -eq 0 ]

crictl stop "$ctr_id"
crictl inspect "$ctr_id"
output=$(crictl ps --quiet --state exited)
[[ "$output" == "$ctr_id" ]]
run crictl rm "$ctr_id"
echo "$output"
[ "$status" -eq 0 ]
run crictl ps --quiet
echo "$output"
[ "$status" -eq 0 ]
run crictl stopp "$pod_id"
echo "$output"
[ "$status" -eq 0 ]
run crictl pods --quiet
echo "$output"
[ "$status" -eq 0 ]

crictl rm "$ctr_id"
crictl ps --quiet
crictl stopp "$pod_id"
output=$(crictl pods --quiet)
[[ "$output" == "$pod_id" ]]
run crictl ps --quiet
echo "$output"
[ "$status" -eq 0 ]
output=$(crictl ps --quiet)
[[ "$output" == "" ]]
run crictl rmp "$pod_id"
echo "$output"
[ "$status" -eq 0 ]
run crictl pods --quiet
echo "$output"
[ "$status" -eq 0 ]

crictl rmp "$pod_id"
output=$(crictl pods --quiet)
[[ "$output" == "" ]]
}

Expand Down Expand Up @@ -751,23 +718,21 @@ function wait_until_exit() {
}

@test "ctr execsync should not overwrite initial spec args" {
start_crio
start_crio

run crictl run "$TESTDATA"/container_redis.json "$TESTDATA"/sandbox_config.json
[ "$status" -eq 0 ]
CTR="$output"
ctr_id=$(crictl run "$TESTDATA"/container_redis.json "$TESTDATA"/sandbox_config.json)

run crictl inspect $CTR | jq -e '.info.runtimeSpec.process.args[2] == "redis-server"'
[ "$status" -eq 0 ]
output=$(crictl inspect "$ctr_id")
[ -n "$output" ]
echo "$output" | jq -e '.info.runtimeSpec.process.args[2] == "redis-server"'

run crictl exec --sync $CTR echo Hello
[ "$status" -eq 0 ]
crictl exec --sync "$ctr_id" echo Hello

run crictl inspect $CTR | jq -e '.info.runtimeSpec.process.args[2] == "redis-server"'
[ "$status" -eq 0 ]
output=$(crictl inspect "$ctr_id")
[ -n "$output" ]
echo "$output" | jq -e '.info.runtimeSpec.process.args[2] == "redis-server"'

run crictl rm -f $CTR
[ "$status" -eq 0 ]
crictl rm -f "$ctr_id"
}

@test "ctr device add" {
Expand Down Expand Up @@ -1386,30 +1351,20 @@ function wait_until_exit() {
[ "$status" -eq 0 ]
}


@test "privileged ctr -- check for rw mounts" {
start_crio

run crictl runp "$TESTDATA"/sandbox_config_privileged.json
echo "$output"
[ "$status" -eq 0 ]
pod_id="$output"

pod_id=$(crictl runp "$TESTDATA"/sandbox_config_privileged.json)
edit_json '.linux.security_context.privileged |= true' \
"$TESTDATA"/container_redis.json "$TESTDIR"/ctr_config.json
run crictl create "$pod_id" "$TESTDIR"/ctr_config.json "$TESTDATA"/sandbox_config_privileged.json
echo "$output"
[ "$status" -eq 0 ]
ctr_id="$output"
run crictl start "$ctr_id"
[ "$status" -eq 0 ]
ctr_id=$(crictl create "$pod_id" "$TESTDIR"/ctr_config.json "$TESTDATA"/sandbox_config_privileged.json)
crictl start "$ctr_id"

run crictl inspect "$ctr_id" | jq -e ".info.privileged == true"
echo "$output"
[ "$status" -eq 0 ]
output=$(crictl inspect "$ctr_id")
[ -n "$output" ]
echo "$output" | jq -e ".info.privileged == true"

run crictl exec "$ctr_id" grep rw\, /proc/mounts
[ "$status" -eq 0 ]
output=$(crictl exec "$ctr_id" grep rw\, /proc/mounts)
if is_cgroup_v2; then
[[ "$output" == *"/sys/fs/cgroup cgroup2"* ]]
else
Expand Down

0 comments on commit 4c40f29

Please sign in to comment.