From f638f402c53bd263642be8145f287db128fd485e Mon Sep 17 00:00:00 2001 From: southerntofu Date: Wed, 23 Feb 2022 23:38:02 +0100 Subject: [PATCH] Update forgecheck (CLI) tests --- forgecheck/test.sh | 8 +++---- forgecheck/test_tests.sh | 35 +++++++++++++++++++++++------ forgecheck/tests/gitea.bats | 6 ++--- forgecheck/tests/github.bats | 6 ++--- forgecheck/tests/gitlab.bats | 6 ++--- {tests => shared}/gitea.json | 0 {tests => shared}/github.json | 0 {tests => shared}/gitlab.json | 0 {tests => shared}/helper.bash | 7 +++++- {tests => shared}/mock-forgehook.sh | 0 10 files changed, 47 insertions(+), 21 deletions(-) rename {tests => shared}/gitea.json (100%) rename {tests => shared}/github.json (100%) rename {tests => shared}/gitlab.json (100%) rename {tests => shared}/helper.bash (92%) rename {tests => shared}/mock-forgehook.sh (100%) diff --git a/forgecheck/test.sh b/forgecheck/test.sh index 398ae1e..aa7d05a 100755 --- a/forgecheck/test.sh +++ b/forgecheck/test.sh @@ -7,12 +7,12 @@ SCRIPTDIR="$(dirname "$0")" ORIGDIR="$(pwd)" cd "$SCRIPTDIR" -export FORGEHOOK="$(pwd)/tests/mock-forgehook.sh" +export FORGEHOOK="$(realpath ../shared/mock-forgehook.sh)" -source tests/helper.bash +source ../shared/helper.bash if [ -z "$1" ]; then - output="$(findBin "$ORIGDIR"/bin/cli ../bin/cli)" + output="$(findBin "$ORIGDIR"/bin/cli ../bin/cli ../../bin/cli ../../../bin/cli)" if [ $? -eq 0 ]; then if [[ "$(echo "$output" | wc -l)" == "1" ]]; then FIND_WHCK="$output" @@ -39,7 +39,7 @@ else fi fi -bats tests/cli/*.bats +bats tests/*.bats status=$? cd "$ORIGDIR" diff --git a/forgecheck/test_tests.sh b/forgecheck/test_tests.sh index f645ce8..3bbcf1e 100755 --- a/forgecheck/test_tests.sh +++ b/forgecheck/test_tests.sh @@ -2,18 +2,36 @@ # If you give first argument it will be a path to already cloned whck +function testOrError() { + message="$1" + shift + bin="$1" + shift + output="$("$bin" "$@" 2>&1)" + if [ ! $? -eq 0 ]; then + echo "FAILURE: "$message"" + echo "$output" | sed 's/^/ /g' + return 1 + fi + echo "SUCCESS: "$message"" +} + # TODO: save exit codes of various tests so we can know how many failed function testVariousScenarios() { echo "Running various tests from "$(pwd)"" - bin/test.sh || echo "ERROR running from repo" + testOrError "bin/test.sh running from repo" bin/test.sh cd spec - ./test_cli.sh || echo "ERROR test_cli.sh failed with implicit ../cli" - ./test_cli.sh "$(../bin/cli)" || echo "ERROR test_cli with explicit result from ../cli" - ../bin/test.sh || echo "ERROR test.sh failed called from spec submodule" + testOrError "forgecheck/test.sh with implicit ../bin/cli" forgecheck/test.sh + testOrError "forgecheck/test.sh with explicit result from ../bin/cli" forgecheck/test.sh "$(../bin/cli)" + testOrError "test.sh called from spec submodule" ../bin/test.sh + cd forgecheck + testOrError "bin/test.sh from spec/forgecheck folder" ../../bin/test.sh + testOrError "forgecheck/test.sh from spec/forgecheck folder with implicit ../../bin/cli" ./test.sh + testOrError "forgecheck/test.sh from spec/forgecheck folder with explicit result from ../../bin.cli" ./test.sh "$(../../bin/cli)" cd tests - ../../bin/test.sh || echo "ERROR test.sh failed from spec/tests folder" - ../test_cli.sh || echo "ERROR test_cli.sh failed from spec/tests folder with implicit ../../cli" - ../test_cli.sh "$(../../bin/cli)" || echo "ERROR test_cli.sh failed from spec/tests folder with explicit result from ../../cli" + testOrError "bin/test.sh from spec/forgecheck/tests" ../../../bin/test.sh + testOrError "forgecheck/test.sh from spec/forgecheck/tests" ../test.sh + testOrError "forgecheck/test.sh from spec/forgehook/tests folder with explicit result from ../../../bin/cli" ../test.sh "$(../../../bin/cli)" } ORIGDIR="$(pwd)" @@ -24,6 +42,9 @@ if [ -z "$1" ]; then elif [ -x ../bin/cli ]; then cd .. testVariousScenarios + elif [ -x ../../bin/cli ]; then + cd ../.. + testVariousScenarios elif [ -x whck/bin/cli ]; then cd whck testVariousScenarios diff --git a/forgecheck/tests/gitea.bats b/forgecheck/tests/gitea.bats index cc1f887..dff2caf 100644 --- a/forgecheck/tests/gitea.bats +++ b/forgecheck/tests/gitea.bats @@ -2,7 +2,7 @@ function setup { # Load helper functions - load ../helper + load ../../shared/helper # Which forgehook implementation to use? if [ -z "$FORGEHOOK" ]; then FORGEHOOK="forgehook"; fi @@ -20,7 +20,7 @@ function teardown { @test "correct signature works" { [ -x "$WHCK" ] repo="https://tildegit.org/forge/hook.sh" - webhook="$(gen_webhook ../gitea.json "$repo")" + webhook="$(gen_webhook ../../shared/gitea.json "$repo")" secret="$($FORGEHOOK secret $repo)" echo -n "$secret" > $WHCK_DIR/identifier sig="$(hash_hmac sha256 "$webhook" "$secret")" @@ -34,7 +34,7 @@ function teardown { @test "incorrect signature fails" { [ -x "$WHCK" ] repo="https://tildegit.org/forge/hook.sh" - webhook="$(gen_webhook ../gitea.json "$repo")" + webhook="$(gen_webhook ../../shared/gitea.json "$repo")" secret="$($FORGEHOOK secret $repo)" echo -n "FAIL$secret" > $WHCK_DIR/identifier sig="$(hash_hmac sha256 "$webhook" "$secret")" diff --git a/forgecheck/tests/github.bats b/forgecheck/tests/github.bats index 6d23d99..73f0a5a 100644 --- a/forgecheck/tests/github.bats +++ b/forgecheck/tests/github.bats @@ -2,7 +2,7 @@ function setup { # Load helper functions - load ../helper + load ../../shared/helper # Which forgehook implementation to use? if [ -z "$FORGEHOOK" ]; then FORGEHOOK="forgehook"; fi @@ -20,7 +20,7 @@ function teardown { @test "correct signature works" { [ -x "$WHCK" ] repo="https://tildegit.org/forge/hook.sh" - webhook="$(gen_webhook ../github.json "$repo")" + webhook="$(gen_webhook ../../shared/github.json "$repo")" secret="$($FORGEHOOK secret $repo)" echo -n "$secret" > $WHCK_DIR/identifier sig="$(hash_hmac sha256 "$webhook" "$secret")" @@ -34,7 +34,7 @@ function teardown { @test "incorrect signature fails" { [ -x "$WHCK" ] repo="https://tildegit.org/forge/hook.sh" - webhook="$(gen_webhook ../gitea.json "$repo")" + webhook="$(gen_webhook ../../shared/gitea.json "$repo")" secret="$($FORGEHOOK secret $repo)" echo -n "FAIL$secret" > $WHCK_DIR/identifier sig="$(hash_hmac sha256 "$webhook" "$secret")" diff --git a/forgecheck/tests/gitlab.bats b/forgecheck/tests/gitlab.bats index afe193a..856180a 100644 --- a/forgecheck/tests/gitlab.bats +++ b/forgecheck/tests/gitlab.bats @@ -2,7 +2,7 @@ function setup { # Load helper functions - load ../helper + load ../../shared/helper # Which forgehook implementation to use? if [ -z "$FORGEHOOK" ]; then FORGEHOOK="forgehook"; fi @@ -20,7 +20,7 @@ function teardown { @test "correct signature works" { [ -x "$WHCK" ] repo="https://tildegit.org/forge/hook.sh" - webhook="$(gen_webhook ../gitea.json "$repo")" + webhook="$(gen_webhook ../../shared/gitlab.json "$repo")" secret="$($FORGEHOOK secret $repo)" echo -n "$secret" > $WHCK_DIR/identifier # Can't echo "$webhook" | $WHCK because of bats bug which eats STDIN @@ -33,7 +33,7 @@ function teardown { @test "incorrect signature fails" { [ -x "$WHCK" ] repo="https://tildegit.org/forge/hook.sh" - webhook="$(gen_webhook ../gitea.json "$repo")" + webhook="$(gen_webhook ../../shared/gitlab.json "$repo")" secret="$($FORGEHOOK secret $repo)" echo -n "FAIL$secret" > $WHCK_DIR/identifier run $WHCK token identifier "$secret" < <(echo -n "$webhook") diff --git a/tests/gitea.json b/shared/gitea.json similarity index 100% rename from tests/gitea.json rename to shared/gitea.json diff --git a/tests/github.json b/shared/github.json similarity index 100% rename from tests/github.json rename to shared/github.json diff --git a/tests/gitlab.json b/shared/gitlab.json similarity index 100% rename from tests/gitlab.json rename to shared/gitlab.json diff --git a/tests/helper.bash b/shared/helper.bash similarity index 92% rename from tests/helper.bash rename to shared/helper.bash index decf536..c885cb6 100755 --- a/tests/helper.bash +++ b/shared/helper.bash @@ -13,6 +13,8 @@ function find_free_port { function gen_webhook() { export repo_url="$2" envsubst < "$BATS_TEST_DIRNAME"/"$1" + # NOTE: Below does not work because dirname $0 is /usr/libexec/bats-core/ + #envsubst < "$(dirname "$0")"/"$1" } # send_webhook "ENDPOINT" "PAYLOAD" "SECRET" "HEADER" @@ -82,7 +84,10 @@ findBin () { # Resolve symlinks f="$(readlink -m "$1")" shift - if [ ! -f "$f" ]; then continue; fi + if [ ! -f "$f" ]; then + echo "DEBUG: "$f" does not exist" + continue; + fi if [ ! -x "$f" ]; then echo "WARNING: "$f" exists but is not executable" >&2 continue diff --git a/tests/mock-forgehook.sh b/shared/mock-forgehook.sh similarity index 100% rename from tests/mock-forgehook.sh rename to shared/mock-forgehook.sh