Skip to content

Commit e311cf2

Browse files
committed
tests: Normalize json before comparing it.
The conmon-v3 produces slightly different json. It is still valid, but it cannot be matched with the very same strings as the one produced by the conmon-v2. This commit normalizes the JSON using `jq` before comparing it and as a result, these conmon-v2 tests also pass against conmon-v3. Signed-off-by: Jan Kaluza <jkaluza@redhat.com>
1 parent 8ed050c commit e311cf2

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

test/04-runtime.bats

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ teardown() {
8989
assert_file_exists $TEST_TMPDIR/syncpipe-output
9090
run cat $TEST_TMPDIR/syncpipe-output
9191
CONTAINER_PID=$(cat "$PID_FILE")
92-
assert "${output}" =~ "\"pid\": $CONTAINER_PID"
92+
assert_json "${output}" =~ "\"pid\": $CONTAINER_PID"
9393
}
9494

9595
@test "runtime: runc error with _OCI_SYNCPIPE defined" {
@@ -121,7 +121,7 @@ teardown() {
121121
# Check that the error is sent to the sync pipe.
122122
assert_file_exists $TEST_TMPDIR/syncpipe-output
123123
run cat $TEST_TMPDIR/syncpipe-output
124-
assert "${output}" =~ "\"pid\": -1"
125-
assert "${output}" =~ "\"message\":"
126-
assert "${output}" =~ "runc create failed"
124+
assert_json "${output}" =~ "\"pid\": -1"
125+
assert_json "${output}" =~ "\"message\":"
126+
assert_json "${output}" =~ "runc create failed"
127127
}

test/08-exec.bats

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ teardown() {
8686
# Check that the conmon wrote something back.
8787
assert_file_exists $TEST_TMPDIR/attachpipe-output
8888
run cat $TEST_TMPDIR/attachpipe-output
89-
assert "${output}" =~ '"data": 0'
89+
assert_json "${output}" =~ '"data": 0'
9090
}
9191

9292
@test "exec: --exec-attach with _OCI_STARTPIPE" {
@@ -156,7 +156,7 @@ teardown() {
156156
# Check that the conmon wrote something back.
157157
assert_file_exists $TEST_TMPDIR/syncpipe-output
158158
run cat $TEST_TMPDIR/syncpipe-output
159-
assert "${output}" =~ '"exit_code": 0'
159+
assert_json "${output}" =~ '"exit_code": 0'
160160
}
161161

162162
@test "exec: --exec --api-version=1 with _OCI_SYNCPIPE defined" {
@@ -178,7 +178,7 @@ teardown() {
178178
assert_file_exists $TEST_TMPDIR/syncpipe-output
179179
run cat $TEST_TMPDIR/syncpipe-output
180180
CONTAINER_PID=$(cat "$PID_FILE")
181-
assert "${output}" =~ "\"data\": $CONTAINER_PID"
182-
assert "${output}" =~ '"data": 0'
181+
assert_json "${output}" =~ "\"data\": $CONTAINER_PID"
182+
assert_json "${output}" =~ '"data": 0'
183183
}
184184

test/test_helper.bash

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,3 +643,15 @@ function die() {
643643
echo "#\\^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" >&2
644644
bail-now
645645
}
646+
647+
# Helper function wrapping the `assert`. It expects json as a first
648+
# argument and normalizes it so it is exactly the same no matter what
649+
# generated it.
650+
assert_json() {
651+
echo "$1"
652+
if ! normalized_json=$(printf '%s' "$1" | jq -S .); then
653+
die "Invalid JSON passed to assert_json: $normalized_json"
654+
fi
655+
echo "$normalized_json"
656+
assert "$normalized_json" "$2" "$3" "$4"
657+
}

0 commit comments

Comments
 (0)