diff --git a/test/04-runtime.bats b/test/04-runtime.bats index 60cc07b1..9e03a5cb 100644 --- a/test/04-runtime.bats +++ b/test/04-runtime.bats @@ -89,7 +89,7 @@ teardown() { assert_file_exists $TEST_TMPDIR/syncpipe-output run cat $TEST_TMPDIR/syncpipe-output CONTAINER_PID=$(cat "$PID_FILE") - assert "${output}" =~ "\"pid\": $CONTAINER_PID" + assert_json "${output}" =~ "\"pid\": $CONTAINER_PID" } @test "runtime: runc error with _OCI_SYNCPIPE defined" { @@ -121,7 +121,7 @@ teardown() { # Check that the error is sent to the sync pipe. assert_file_exists $TEST_TMPDIR/syncpipe-output run cat $TEST_TMPDIR/syncpipe-output - assert "${output}" =~ "\"pid\": -1" - assert "${output}" =~ "\"message\":" - assert "${output}" =~ "runc create failed" + assert_json "${output}" =~ "\"pid\": -1" + assert_json "${output}" =~ "\"message\":" + assert_json "${output}" =~ "runc create failed" } diff --git a/test/08-exec.bats b/test/08-exec.bats index e9e6de5c..6d0057fe 100644 --- a/test/08-exec.bats +++ b/test/08-exec.bats @@ -86,7 +86,7 @@ teardown() { # Check that the conmon wrote something back. assert_file_exists $TEST_TMPDIR/attachpipe-output run cat $TEST_TMPDIR/attachpipe-output - assert "${output}" =~ '"data": 0' + assert_json "${output}" =~ '"data": 0' } @test "exec: --exec-attach with _OCI_STARTPIPE" { @@ -156,7 +156,7 @@ teardown() { # Check that the conmon wrote something back. assert_file_exists $TEST_TMPDIR/syncpipe-output run cat $TEST_TMPDIR/syncpipe-output - assert "${output}" =~ '"exit_code": 0' + assert_json "${output}" =~ '"exit_code": 0' } @test "exec: --exec --api-version=1 with _OCI_SYNCPIPE defined" { @@ -178,7 +178,7 @@ teardown() { assert_file_exists $TEST_TMPDIR/syncpipe-output run cat $TEST_TMPDIR/syncpipe-output CONTAINER_PID=$(cat "$PID_FILE") - assert "${output}" =~ "\"data\": $CONTAINER_PID" - assert "${output}" =~ '"data": 0' + assert_json "${output}" =~ "\"data\": $CONTAINER_PID" + assert_json "${output}" =~ '"data": 0' } diff --git a/test/test_helper.bash b/test/test_helper.bash index 3da0677d..3ea2ee21 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -643,3 +643,15 @@ function die() { echo "#\\^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" >&2 bail-now } + +# Helper function wrapping the `assert`. It expects json as a first +# argument and normalizes it so it is exactly the same no matter what +# generated it. +assert_json() { + echo "$1" + if ! normalized_json=$(printf '%s' "$1" | jq -S .); then + die "Invalid JSON passed to assert_json: $normalized_json" + fi + echo "$normalized_json" + assert "$normalized_json" "$2" "$3" "$4" +}