Skip to content

Commit

Permalink
Merge pull request #74 from buildkite-plugins/ivannalisetska/SUP-1821
Browse files Browse the repository at this point in the history
Test Collector Plugin Issue  - error parsing report url
  • Loading branch information
ivannalisetska authored May 13, 2024
2 parents 6d4447d + c163479 commit 8ebff14
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ The number of concurrent file uploads to perform to the Buildkite analytics API.

Default value: `1`

## Requirements

This plugin requires `jq` for parsing JSON data. If `jq` is not found on the agent, `sed` will be used as a fallback. Ensure that `sed` is also available to handle scenarios where `jq` cannot be used.

## Fallback Behavior

If `jq` is unavailable, the plugin will attempt to parse the results using `sed`. This ensures that the plugin remains functional even if the preferred JSON parser is missing.

## Examples

### Upload a JUnit file
Expand Down
29 changes: 21 additions & 8 deletions hooks/pre-exit
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,36 @@ save-report-url() {
echo "Could not get the tests report URL from $json_file. File not found."
return
fi
# Could be easier with jq, but we don't want to require it

if which jq >/dev/null; then
echo "Using jq to parse the report URL"
report_url=$(jq -r '.run_url' "${json_file}")
echo "Using jq to parse the report URL"
report_url=$(jq -r '.run_url' "${json_file}" 2>&1) # Capture stderr for error reporting
if [[ "$report_url" == "null" || "$report_url" == "" || "$report_url" =~ "parse error" ]]; then
echo "jq parsing failed with the message: $report_url"
echo "Contents of $json_file:"
cat "$json_file"
return
fi
else
echo "Not using jq to parse the report URL"
report_url=$(sed 's/.*"run_url" *: *"\([^"]*\)".*/\1/g' "${json_file}")
echo "jq not installed, attempting to parse with sed"
report_url=$(sed 's/.*"run_url" *: *"\([^"]*\)".*/\1/g' "${json_file}")
if [[ "$report_url" == "null" || "$report_url" == "" ]]; then
echo "sed parsing failed, no valid URL extracted."
echo "Contents of $json_file:"
cat "$json_file"
return
fi
fi

if [ "$report_url" == "null" ]; then
echo "Could not get the tests report URL from $json_file. 'run_url' property not found."
if [ -z "$report_url" ]; then
echo "No report URL found or extracted. Unable to save."
return
fi

echo "$report_url" >> "$REPORT_URLS_FILE"
}


# Uploads files to the Test Analytics API
#
# Upload failures should not fail the build, and should have a sensible timeout,
Expand Down Expand Up @@ -239,4 +252,4 @@ else
fi
if [ "$ANNOTATE" != "false" ]; then
annotation-link "${REPORT_URLS_FILE}"
fi
fi
1 change: 1 addition & 0 deletions tests/fixtures/response_missing_url.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id": "2","run_id": "2","queued": 130,"skipped": 0,"errors": []}
1 change: 1 addition & 0 deletions tests/fixtures/response_null_url.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id": "4","run_id": "4","queued": 150,"skipped": 0,"errors": [],"run_url": null}
55 changes: 51 additions & 4 deletions tests/pre-exit-report-link.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bats

# To debug stubs, uncomment these lines:
# export CURL_STUB_DEBUG=/dev/tty
# export GIT_STUB_DEBUG=/dev/tty
#export CURL_STUB_DEBUG=/dev/tty
#export GIT_STUB_DEBUG=/dev/tty

setup() {
load "$BATS_PLUGIN_PATH/load.bash"
Expand Down Expand Up @@ -53,7 +53,7 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \*
assert_success
assert_output --partial "Uploading './tests/fixtures/junit-1.xml'..."
assert_output --partial "curl success"
assert_output --partial "Not using jq"
assert_output --partial "jq not installed, attempting to parse with sed"
assert_output --partial "annotation success"

unstub which
Expand Down Expand Up @@ -117,6 +117,7 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \*
unstub jq
unstub curl
}

@test "Annotates report link absorbs empty file error" {
export CURL_RESP_FILE="response.json"
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'"
Expand All @@ -141,8 +142,54 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \*
assert_success
assert_output --partial "Uploading './tests/fixtures/junit-1.xml'..."
assert_output --partial "curl success"
assert_output --partial "'run_url' property not found"
assert_output --partial "jq parsing failed with the message: "
assert_output --partial "Contents of ./tests/fixtures/response_no_url.json:"
assert_output --partial "There are no report URLs to annotate"

unstub curl
}

@test "No annotation when 'run_url' property is missing in JSON response" {
export CURL_RESP_FILE="./tests/fixtures/response_missing_url.json"
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'"

run "$PWD/hooks/pre-exit"

assert_success
assert_output --partial "jq parsing failed with the message:"
assert_output --partial "Contents of ./tests/fixtures/response_missing_url.json:"
assert_output --partial "There are no report URLs to annotate"

unstub curl
}

@test "No annotation when 'run_url' is null in JSON response" {
export CURL_RESP_FILE="./tests/fixtures/response_null_url.json"
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'"

run "$PWD/hooks/pre-exit"

assert_success
assert_output --partial "jq parsing failed with the message: null"
assert_output --partial "Contents of ./tests/fixtures/response_null_url.json:"
assert_output --partial "There are no report URLs to annotate"

unstub curl
}

@test "Fallback to sed when jq is missing" {
stub which "jq : exit 1"
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'"
stub buildkite-agent "annotate --style info --context \"test-collector\" --append : echo 'annotation success'"

run "$PWD/hooks/pre-exit"

assert_success
assert_output --partial "jq not installed, attempting to parse with sed"
assert_output --partial "curl success"
assert_output --partial "annotation success"

unstub which
unstub curl
}

0 comments on commit 8ebff14

Please sign in to comment.