Update forgecheck (CLI) tests

This commit is contained in:
southerntofu 2022-02-23 23:38:02 +01:00
parent ebaacae60d
commit f638f402c5
10 changed files with 47 additions and 21 deletions

View File

@ -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"

View File

@ -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

View File

@ -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")"

View File

@ -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")"

View File

@ -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")

View File

@ -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