Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ on:
branches: [ main ]

schedule:
- cron: '01 07 * * *'
- cron: '25 08 * * *'

workflow_dispatch:
inputs:
debug_enabled:
description: 'Debug with tmate set "debug_enabled"'
type: boolean
description: Debug with tmate
required: false
default: "false"
default: false

# Permissions required for keepalive-workflow in ddev/github-action-add-on-test
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

# This is required for "gautamkrishnar/keepalive-workflow", see "ddev/github-action-add-on-test"
permissions:
actions: write

Expand All @@ -28,10 +33,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: ddev/github-action-add-on-test@v2
with:
ddev_version: ${{ matrix.ddev_version }}
token: ${{ secrets.GITHUB_TOKEN }}
debug_enabled: ${{ github.event.inputs.debug_enabled }}
addon_repository: ${{ env.GITHUB_REPOSITORY }}
addon_ref: ${{ env.GITHUB_REF }}
- uses: ddev/github-action-add-on-test@v2
with:
ddev_version: ${{ matrix.ddev_version }}
token: ${{ secrets.GITHUB_TOKEN }}
debug_enabled: ${{ github.event.inputs.debug_enabled }}
addon_repository: ${{ env.GITHUB_REPOSITORY }}
addon_ref: ${{ env.GITHUB_REF }}
89 changes: 60 additions & 29 deletions tests/test.bats
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
#!/usr/bin/env bats

# Bats is a testing framework for Bash
# Documentation https://bats-core.readthedocs.io/en/stable/
# Bats libraries documentation https://github.com/ztombol/bats-docs

# For local tests, install bats-core, bats-assert, bats-file, bats-support
# And run this in the add-on root directory:
# bats ./tests/test.bats
# To exclude release tests:
# bats ./tests/test.bats --filter-tags '!release'
# For debugging:
# bats ./tests/test.bats --show-output-of-passing-tests --verbose-run --print-output-on-failure

setup() {
set -eu -o pipefail

export DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )/.."
export TESTDIR=~/tmp/ddev-cron
mkdir -p $TESTDIR
export PROJNAME=ddev-cron
export DDEV_NON_INTERACTIVE=true
ddev delete -Oy ${PROJNAME} || true
# Override this variable for your add-on:
export GITHUB_REPO=ddev/ddev-cron

TEST_BREW_PREFIX="$(brew --prefix 2>/dev/null || true)"
export BATS_LIB_PATH="${BATS_LIB_PATH}:${TEST_BREW_PREFIX}/lib:/usr/lib/bats"
bats_load_library bats-assert
bats_load_library bats-file
bats_load_library bats-support

export DIR="$(cd "$(dirname "${BATS_TEST_FILENAME}")/.." >/dev/null 2>&1 && pwd)"
export PROJNAME="test-$(basename "${GITHUB_REPO}")"
mkdir -p ~/tmp
export TESTDIR=$(mktemp -d ~/tmp/${PROJNAME}.XXXXXX)
export DDEV_NONINTERACTIVE=true
export DDEV_NO_INSTRUMENTATION=true
ddev delete -Oy "${PROJNAME}" >/dev/null 2>&1 || true
cd "${TESTDIR}"
ddev config --project-name=${PROJNAME}
ddev start -y
run ddev config --project-name="${PROJNAME}" --project-tld=ddev.site
assert_success
run ddev start -y
assert_success
echo "# ddev started at $(date)" >&3
}

teardown() {
set -eu -o pipefail
cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 )
ddev delete -Oy ${PROJNAME}
[ "${TESTDIR}" != "" ] && rm -rf ${TESTDIR}
}

health_checks() {
# Make sure cron process is running.
# We use `time_cron_checks` to check the example cron job is actually correctly implemented.
# This is due to the need to test the health-check when there are no jobs added.
ddev exec 'sudo killall -0 cron'
run ddev exec 'sudo killall -0 cron'
assert_success
}

time_cron_checks() {
Expand All @@ -35,14 +55,22 @@ time_cron_checks() {
ddev exec crontab -l | grep '* * * * * date | tee -a /var/www/html/time.log'
}

teardown() {
set -eu -o pipefail
ddev delete -Oy ${PROJNAME} >/dev/null 2>&1
[ "${TESTDIR}" != "" ] && rm -rf ${TESTDIR}
}

@test "install from directory" {
set -eu -o pipefail
cd ${TESTDIR}
echo "# ddev add-on get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev add-on get ${DIR}
echo "# ddev add-on get ${DIR} with project ${PROJNAME} in $(pwd)" >&3
run ddev add-on get "${DIR}"

# Set the example cron job as an actual cron job.
mv ./.ddev/web-build/time.cron.example ./.ddev/web-build/time.cron
ddev restart

run ddev restart -y
assert_success

# The example runs every minute so we should wait at least the length.
sleep 61
Expand All @@ -57,12 +85,14 @@ time_cron_checks() {
# bats test_tags=release
@test "install from release" {
set -eu -o pipefail
cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 )
echo "# ddev add-on get ddev/ddev-cron with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev add-on get ddev/ddev-cron
echo "# ddev add-on get ${GITHUB_REPO} with project ${PROJNAME} in $(pwd)" >&3
run ddev add-on get "${GITHUB_REPO}"

# Set the example cron job as an actual cron job.
mv ./.ddev/web-build/time.cron.example ./.ddev/web-build/time.cron
ddev restart

run ddev restart -y
assert_success

# The example runs every minute so we should wait at least the length.
sleep 61
Expand All @@ -76,14 +106,15 @@ time_cron_checks() {

@test "services work when no valid jobs are present" {
set -eu -o pipefail
cd ${TESTDIR}
echo "# ddev add-on get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev add-on get ${DIR}
ddev restart
echo "# ddev add-on get ${DIR} with project ${PROJNAME} in $(pwd)" >&3
run ddev add-on get "${DIR}"
assert_success
run ddev restart -y
assert_success

# We should wait at least one cycle.
sleep 61

# Check service works
# Check service works
health_checks
}