Skip to content

Commit

Permalink
fix(utils): remove hacbs prefix from result
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Sztuka <[email protected]>
  • Loading branch information
jsztuka authored and dirgim committed May 2, 2023
1 parent 6a731c9 commit 391ed3a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions test/selftest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ check_return_code
echo "Testing shell function parse_hacbs_test_output"
. /utils.sh
conftest test --namespace optional_checks --policy $POLICY_PATH/image/inherited-labels.rego label.json --output=json > unittest.json
HACBS_TEST_OUTPUT=
TEST_OUTPUT=
parse_hacbs_test_output sanity_label_check conftest unittest.json
[ "$(echo $HACBS_TEST_OUTPUT | jq -r '.result')" == "SUCCESS" ] && echo "test_parse_hacbs_test_output PASSED" || exit 1
[ "$(echo $TEST_OUTPUT | jq -r '.result')" == "SUCCESS" ] && echo "test_parse_hacbs_test_output PASSED" || exit 1

echo "Starting Integeration-Tests"
bats $POLICY_PATH/conftest.sh
Expand Down
12 changes: 6 additions & 6 deletions test/utils.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash


# returns HACBS_TEST_OUTPUT json with predefined default. Function accepts optional args to modify result
# returns TEST_OUTPUT json with predefined default. Function accepts optional args to modify result
# see make_result_json_usage for usage
make_result_json() {
local RESULT=""
Expand Down Expand Up @@ -83,7 +83,7 @@ make_result_json() {
}


# Parse test result and genarate HACBS_TEST_OUTPUT
# Parse test result and genarate TEST_OUTPUT
parse_hacbs_test_output() {
# The name of test
TEST_NAME=$1
Expand Down Expand Up @@ -112,13 +112,13 @@ parse_hacbs_test_output() {

# Handle the test result with format of sarif
if [ "$TEST_RESULT_FORMAT" = "sarif" ]; then
HACBS_TEST_OUTPUT=$(make_result_json \
TEST_OUTPUT=$(make_result_json \
-r "$(jq -rce '(if (.runs[].results | length > 0) then "FAILURE" else "SUCCESS" end)' "${TEST_RESULT_FILE}" || echo 'ERROR')" \
-f "$(jq -rce '(.runs[].results | length)' "${TEST_RESULT_FILE}")" \
)

# Log out the failing runs
if [ "$(echo "$HACBS_TEST_OUTPUT" | jq '.failures')" -gt 0 ]
if [ "$(echo "$TEST_OUTPUT" | jq '.failures')" -gt 0 ]
then
echo "Task $TEST_NAME failed because of the following issues:"
jq '.runs[].results // []|map(.message.text) | unique' "$TEST_RESULT_FILE"
Expand All @@ -134,15 +134,15 @@ parse_hacbs_test_output() {
exit 1
fi

HACBS_TEST_OUTPUT=$(make_result_json \
TEST_OUTPUT=$(make_result_json \
-r "$(jq -rce '.[] | (if (.failures | length > 0) then "FAILURE" else "SUCCESS" end)' "${TEST_RESULT_FILE}" || echo 'ERROR')" \
-n "$(jq -rce '.[] | .namespace' "${TEST_RESULT_FILE}")" \
-s "$(jq -rce '.[] | .successes' "${TEST_RESULT_FILE}")" \
-f "$(jq -rce '.[] | (.failures | length)' "${TEST_RESULT_FILE}")" \
)

# Log out the failing runs
if [ "$(echo "$HACBS_TEST_OUTPUT" | jq '.failures')" -gt 0 ]
if [ "$(echo "$TEST_OUTPUT" | jq '.failures')" -gt 0 ]
then
echo "Task $TEST_NAME failed because of the following issues:"
jq '.[].failures // []|map(.metadata.details.name) | unique' "$TEST_RESULT_FILE"
Expand Down
16 changes: 8 additions & 8 deletions unittests_bash/test_utils.bats
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ setup() {
}

@test "Conftest input: successful tests" {
HACBS_TEST_OUTPUT=""
TEST_OUTPUT=""
parse_hacbs_test_output testname conftest unittests_bash/data/conftest_successes.json
EXPECTED_JSON='{"result":"SUCCESS","timestamp":"whatever","note":"For details, check Tekton task log.","namespace":"image_labels","successes":19,"failures":0,"warnings":0}'
test_json_eq "${EXPECTED_JSON}" "${HACBS_TEST_OUTPUT}"
test_json_eq "${EXPECTED_JSON}" "${TEST_OUTPUT}"
}

@test "Conftest input: failed tests" {
HACBS_TEST_OUTPUT=""
TEST_OUTPUT=""
parse_hacbs_test_output testname conftest unittests_bash/data/conftest_failures.json
EXPECTED_JSON='{"result":"FAILURE","timestamp":"whatever","note":"For details, check Tekton task log.","namespace":"image_labels","successes":19,"failures":1,"warnings":0}'
test_json_eq "${EXPECTED_JSON}" "${HACBS_TEST_OUTPUT}"
test_json_eq "${EXPECTED_JSON}" "${TEST_OUTPUT}"
}

@test "Conftest input: more than 1 result" {
Expand All @@ -84,15 +84,15 @@ setup() {
}

@test "Sarif input: successful tests" {
HACBS_TEST_OUTPUT=""
TEST_OUTPUT=""
parse_hacbs_test_output testname sarif unittests_bash/data/sarif_successes.json
EXPECTED_JSON='{"result":"SUCCESS","timestamp":"whatever","note":"For details, check Tekton task log.","namespace":"default","successes":0,"failures":0,"warnings":0}'
test_json_eq "${EXPECTED_JSON}" "${HACBS_TEST_OUTPUT}"
test_json_eq "${EXPECTED_JSON}" "${TEST_OUTPUT}"
}

@test "Sarif input: failed tests" {
HACBS_TEST_OUTPUT=""
TEST_OUTPUT=""
parse_hacbs_test_output testname sarif unittests_bash/data/sarif_failures.json
EXPECTED_JSON='{"result":"FAILURE","timestamp":"whatever","note":"For details, check Tekton task log.","namespace":"default","successes":0,"failures":1,"warnings":0}'
test_json_eq "${EXPECTED_JSON}" "${HACBS_TEST_OUTPUT}"
test_json_eq "${EXPECTED_JSON}" "${TEST_OUTPUT}"
}

0 comments on commit 391ed3a

Please sign in to comment.