diff --git a/forgejo-test-helper.sh b/forgejo-test-helper.sh index 39ccca9..a6bec88 100755 --- a/forgejo-test-helper.sh +++ b/forgejo-test-helper.sh @@ -5,8 +5,8 @@ set -ex export DEBIAN_FRONTEND=noninteractive -: ${LOOPS:=40} -: ${LOOP_DELAY:=10} +: ${LOOPS:=80} +: ${LOOP_DELAY:=5} DIR=$(mktemp -d) trap "rm -fr $DIR" EXIT @@ -65,25 +65,48 @@ function check_status() { local url="$1" local repo="$2" local sha="$3" + local expected_status="$4" + local expected_description="$5" - local state=$(get_status $url $repo $sha | jq --raw-output .state) - echo $state - test "$state" != "" -a "$state" != "pending" -a "$state" != "running" -a "$state" != "null" + get_status $url $repo $sha > /tmp/status.json + local status="$(jq --raw-output .state < /tmp/status.json)" + local description="$(jq --raw-output .statuses[0].description < /tmp/status.json)" + if test "$status" = "$expected_status" && test -z "$expected_description" -o "$description" = "$expected_description"; then + echo OK + elif test "$status" = "failure" -o "$status" = "success"; then + echo NOK + else + echo RETRY + fi } function wait_success() { - local url="$1" - local repo="$2" - local sha="$3" + wait_status success "$@" +} + +function wait_failure() { + wait_status failure "$@" +} + +function wait_running() { + wait_status pending "$@" "Has started running" +} + +function wait_status() { + local status="$1" + local url="$2" + local repo="$3" + local sha="$4" + local description="$5" for i in $(seq $LOOPS); do - if check_status "$url" "$repo" "$sha"; then + if test $(check_status "$url" "$repo" "$sha" "$status" "$description") != RETRY ; then break fi test "$FORGEJO_RUNNER_LOGS" && tail $FORGEJO_RUNNER_LOGS sleep $LOOP_DELAY done - if ! test "$(check_status "$url" "$repo" "$sha")" = "success" ; then + if test $(check_status "$url" "$repo" "$sha" "$status" "$description") != "OK" ; then get_status $url $repo $sha | jq .statuses test "$FORGEJO_RUNNER_LOGS" && cat $FORGEJO_RUNNER_LOGS return 1 diff --git a/testdata/example-push-cancel/.forgejo/workflows/test.yml b/testdata/example-push-cancel/.forgejo/workflows/test.yml new file mode 100644 index 0000000..556f5e8 --- /dev/null +++ b/testdata/example-push-cancel/.forgejo/workflows/test.yml @@ -0,0 +1,6 @@ +on: [push] +jobs: + test: + runs-on: docker + steps: + - run: sleep infinity diff --git a/testdata/example-push-cancel/run.sh b/testdata/example-push-cancel/run.sh new file mode 100755 index 0000000..aeb6c66 --- /dev/null +++ b/testdata/example-push-cancel/run.sh @@ -0,0 +1,20 @@ +url=http://root:admin1234@$(cat forgejo-ip):3000 +token=$(cat forgejo-token) +repo=root/example-$example +# +# push the repository +# +forgejo-test-helper.sh push_workflow testdata/example-$example $url root example-$example setup-forgejo $token +sha=$(forgejo-test-helper.sh branch_tip $url $repo main) +# +# wait for the workflow (sleep infinity) to start running +# +forgejo-test-helper.sh wait_running $url $repo $sha +# +# push to the same branch +# +forgejo-test-helper.sh push_workflow testdata/example-$example $url root example-$example setup-forgejo $token +# +# wait for the workflow to be canceld as a result of the previous push +# +forgejo-test-helper.sh wait_failure $url $repo $sha 'Has been cancelled' diff --git a/testdata/example-push-cancel/teardown.sh b/testdata/example-push-cancel/teardown.sh new file mode 100644 index 0000000..ae9987a --- /dev/null +++ b/testdata/example-push-cancel/teardown.sh @@ -0,0 +1,4 @@ +# +# this will effectively discard any linger workflow so they do not interfere with other tests +# +forgejo-runner.sh reload