From 7e8c8064af1cb457519fd88d36b3673bbfeb9914 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 24 Sep 2023 20:32:40 +0200 Subject: [PATCH] add example of cron task --- .forgejo/workflows/integration.yml | 57 ++++++++----------- forgejo-test-helper.sh | 13 ++++- forgejo.sh | 3 +- .../example-cron/.forgejo/workflows/test.yml | 14 +++++ testdata/example-cron/run.sh | 7 +++ testdata/example-cron/runner-config.yaml | 30 ++++++++++ testdata/example-cron/setup.sh | 2 + testdata/example-cron/teardown.sh | 1 + testdata/example-if-fail/expected-to-fail.sh | 5 -- testdata/example-if-fail/run.sh | 7 +++ testdata/example-service/setup.sh | 3 + testdata/example-service/teardown.sh | 1 + 12 files changed, 102 insertions(+), 41 deletions(-) create mode 100644 testdata/example-cron/.forgejo/workflows/test.yml create mode 100755 testdata/example-cron/run.sh create mode 100644 testdata/example-cron/runner-config.yaml create mode 100755 testdata/example-cron/setup.sh create mode 100755 testdata/example-cron/teardown.sh delete mode 100755 testdata/example-if-fail/expected-to-fail.sh create mode 100755 testdata/example-if-fail/run.sh create mode 100755 testdata/example-service/setup.sh create mode 100755 testdata/example-service/teardown.sh diff --git a/.forgejo/workflows/integration.yml b/.forgejo/workflows/integration.yml index f137501..dfdc3bd 100644 --- a/.forgejo/workflows/integration.yml +++ b/.forgejo/workflows/integration.yml @@ -1,31 +1,24 @@ on: [ push, pull_request ] -env: - # - # List of tests to run as found in testdata/example-$test. The - # directory will be used to create a git repository uploaded to a - # Forgejo instance. - # - # The test will be a success once the status of - # the commit is success, as set by the Forgejo Action run. - # - TESTS: 'echo service container expression local-action docker-action if' - # - # The test is expected to fail the workflow and verify the expected - # side effect of the failure with testdata/example-$test/expected-to-fail.sh - # - TESTS_FAILING: 'if-fail' - jobs: integration: runs-on: self-hosted + strategy: + matrix: + info: + - version: "1.21.0-0-rc0" + image: codeberg.org/forgejo-experimental/forgejo + tests: "echo cron service container expression local-action docker-action if if-fail" + - version: "1.20" + image: codeberg.org/forgejo/forgejo + tests: "echo service container expression local-action docker-action if if-fail" steps: - uses: actions/checkout@v3 - run: | set -x LXC_IP_PREFIX=10.0.10 ./forgejo-dependencies.sh export PATH=$(pwd):$PATH - forgejo.sh setup root admin1234 codeberg.org/forgejo/forgejo 1.20 + forgejo.sh setup root admin1234 ${{ matrix.info.image }} ${{ matrix.info.version }} # # Uncomment the following for a shortcut to debugging the Forgejo runner. # It will build the runner from a designated repository and branch instead of @@ -34,33 +27,29 @@ jobs: #./forgejo-test-helper.sh build_runner http://code.forgejo.org/earl-warren/runner wip-sync #export PATH=$(pwd)/forgejo-runner:$PATH # - ./forgejo-runner.sh setup + forgejo-runner.sh setup export FORGEJO_RUNNER_LOGS=forgejo-runner.log - for example in $TESTS $TESTS_FAILING ; do + for example in ${{ matrix.info.tests }} ; do + export EXAMPLE_DIR=$(pwd)/testdata/example-$example - if test $example = service ; then - > /srv/example-service-volume-valid - > /srv/example-service-volume-invalid - fi - - config=$(pwd)/testdata/example-$example/runner-config.yaml - if test -f $config ; then - FORGEJO_RUNNER_CONFIG=$config ./forgejo-runner.sh reload + if test -f $EXAMPLE_DIR/setup.sh ; then + source $EXAMPLE_DIR/setup.sh fi echo "============================ BEGIN example-$example ===================" - if ./forgejo-test-helper.sh run_workflow testdata/example-$example http://root:admin1234@$(cat forgejo-ip):3000 root example-$example setup-forgejo $(cat forgejo-token) >& /tmp/run.out ; then - cat /tmp/run.out - ! test -f testdata/example-$example/expected-to-fail.sh + if test -f $EXAMPLE_DIR/run.sh ; then + source $EXAMPLE_DIR/run.sh else - cat /tmp/run.out - test -f testdata/example-$example/expected-to-fail.sh + if ! forgejo-test-helper.sh run_workflow testdata/example-$example http://root:admin1234@$(cat forgejo-ip):3000 root example-$example setup-forgejo $(cat forgejo-token) >& /tmp/run.out ; then + cat /tmp/run.out + false + fi fi echo "============================ END example-$example ===================" - if test -f $config ; then - ./forgejo-runner.sh reload + if test -f $EXAMPLE_DIR/teardown.sh ; then + source $EXAMPLE_DIR/teardown.sh fi done echo "============================ demo ===================" diff --git a/forgejo-test-helper.sh b/forgejo-test-helper.sh index d4dd760..39ad372 100755 --- a/forgejo-test-helper.sh +++ b/forgejo-test-helper.sh @@ -105,7 +105,7 @@ function push() { ) } -function run_workflow() { +function push_workflow() { local directory="$1" local url="$2" local owner="$3" @@ -119,6 +119,17 @@ function run_workflow() { fi push "$directory" "$url" "$owner" "$project" "$self_action" "$token" +} + +function run_workflow() { + local directory="$1" + local url="$2" + local owner="$3" + local project="$4" + local self_action="$5" + local token="$6" + + push_workflow "$directory" "$url" "$owner" "$project" "$self_action" "$token" wait_success "$url" "$owner/$project" $(cat $DIR/$project/SHA) } diff --git a/forgejo.sh b/forgejo.sh index 95889b0..ee9ac61 100755 --- a/forgejo.sh +++ b/forgejo.sh @@ -6,11 +6,12 @@ set -e DIR=/tmp if ${VERBOSE:-false}; then set -x; fi : ${CONTAINER:=forgejo} +: ${RETRY_DELAYS:=1 1 5 5 15 30} function retry() { rm -f $DIR/retry.out success=false - for delay in 1 1 5 5 15 30 ; do + for delay in $RETRY_DELAYS ; do if "$@" >> $DIR/retry.out 2>&1 ; then success=true break diff --git a/testdata/example-cron/.forgejo/workflows/test.yml b/testdata/example-cron/.forgejo/workflows/test.yml new file mode 100644 index 0000000..e1c2b87 --- /dev/null +++ b/testdata/example-cron/.forgejo/workflows/test.yml @@ -0,0 +1,14 @@ +on: + schedule: + - cron: '* * * * *' + +jobs: + test: + runs-on: docker + container: + image: debian:bookworm + options: "--volume /srv/example-cron-volume:/srv/example-cron-volume" + + steps: + - run: | + touch /srv/example-cron-volume/DONE diff --git a/testdata/example-cron/run.sh b/testdata/example-cron/run.sh new file mode 100755 index 0000000..d43bc6b --- /dev/null +++ b/testdata/example-cron/run.sh @@ -0,0 +1,7 @@ +forgejo-test-helper.sh push_workflow testdata/example-$example http://root:admin1234@$(cat forgejo-ip):3000 root example-$example setup-forgejo $(cat forgejo-token) +# cron runs once per minute, give it three minutes max before declaring failure +if ! RETRY_DELAYS="30 30 30 30 30 30" forgejo.sh retry test -f /srv/example-cron-volume/DONE ; then + cat $FORGEJO_RUNNER_LOGS + false +fi + diff --git a/testdata/example-cron/runner-config.yaml b/testdata/example-cron/runner-config.yaml new file mode 100644 index 0000000..bc5d862 --- /dev/null +++ b/testdata/example-cron/runner-config.yaml @@ -0,0 +1,30 @@ + +log: + level: info + +runner: + file: .runner + capacity: 1 + env_file: .env + timeout: 3h + insecure: false + fetch_timeout: 5s + fetch_interval: 2s + labels: [] + +cache: + enabled: false + dir: "" + host: "" + port: 0 + +container: + network: "" + privileged: false + options: + workdir_parent: + valid_volumes: ["/srv/example-cron-volume"] + docker_host: "" + +host: + workdir_parent: diff --git a/testdata/example-cron/setup.sh b/testdata/example-cron/setup.sh new file mode 100755 index 0000000..db8208f --- /dev/null +++ b/testdata/example-cron/setup.sh @@ -0,0 +1,2 @@ +mkdir /srv/example-cron-volume +FORGEJO_RUNNER_CONFIG=$EXAMPLE_DIR/runner-config.yaml forgejo-runner.sh reload diff --git a/testdata/example-cron/teardown.sh b/testdata/example-cron/teardown.sh new file mode 100755 index 0000000..b410c51 --- /dev/null +++ b/testdata/example-cron/teardown.sh @@ -0,0 +1 @@ +forgejo-runner.sh reload diff --git a/testdata/example-if-fail/expected-to-fail.sh b/testdata/example-if-fail/expected-to-fail.sh deleted file mode 100755 index c4485df..0000000 --- a/testdata/example-if-fail/expected-to-fail.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -set -ex -grep --quiet 'IF TEST FAILURE' $FORGEJO_RUNNER_LOGS -grep --quiet 'IF TEST ALWAYS' $FORGEJO_RUNNER_LOGS diff --git a/testdata/example-if-fail/run.sh b/testdata/example-if-fail/run.sh new file mode 100755 index 0000000..a077ee2 --- /dev/null +++ b/testdata/example-if-fail/run.sh @@ -0,0 +1,7 @@ +if forgejo-test-helper.sh run_workflow testdata/example-$example http://root:admin1234@$(cat forgejo-ip):3000 root example-$example setup-forgejo $(cat forgejo-token) >& /tmp/run.out ; then + cat /tmp/run.out + false +fi + +grep --quiet 'IF TEST FAILURE' $FORGEJO_RUNNER_LOGS +grep --quiet 'IF TEST ALWAYS' $FORGEJO_RUNNER_LOGS diff --git a/testdata/example-service/setup.sh b/testdata/example-service/setup.sh new file mode 100755 index 0000000..3a9674e --- /dev/null +++ b/testdata/example-service/setup.sh @@ -0,0 +1,3 @@ +> /srv/example-service-volume-valid +> /srv/example-service-volume-invalid +FORGEJO_RUNNER_CONFIG=$EXAMPLE_DIR/runner-config.yaml forgejo-runner.sh reload diff --git a/testdata/example-service/teardown.sh b/testdata/example-service/teardown.sh new file mode 100755 index 0000000..b410c51 --- /dev/null +++ b/testdata/example-service/teardown.sh @@ -0,0 +1 @@ +forgejo-runner.sh reload