diff --git a/.github/workflows/buildandpush.yml b/.github/workflows/buildandpush.yml index 366f83a..6eef4c2 100644 --- a/.github/workflows/buildandpush.yml +++ b/.github/workflows/buildandpush.yml @@ -10,85 +10,165 @@ on: #- 'setup.sh' - 'startup.sh' - 'Dockerfile' + - 'tests/**' + - '.github/workflows/buildandpush.yml' pull_request: branches: [ "main" ] paths: #- 'setup.sh' - 'startup.sh' - 'Dockerfile' + - 'tests/**' + - '.github/workflows/buildandpush.yml' jobs: - update-version: + # Resolve the versions once, from the webservice pom, and hand them to every + # later job. Nothing downstream re-reads the pom, so all jobs are guaranteed to + # agree on which release is being built. + resolve-version: runs-on: ubuntu-latest + outputs: + bdbversion: ${{ steps.versions.outputs.bdbversion }} + wsversion: ${{ steps.versions.outputs.wsversion }} steps: - - - name: Checkout - uses: actions/checkout@v6 - - - name: Retrieve JAR version name + - name: Retrieve BridgeDb and webservice versions from the pom + id: versions run: | - wget https://raw.githubusercontent.com/bridgedb/BridgeDbWebservice/main/pom.xml - echo "BDBVERSION=$(cat pom.xml | grep -m 1 -oP "(?<=).*" | sed 's|||g')" >> $GITHUB_ENV + wget -q -O pom.xml https://raw.githubusercontent.com/bridgedb/BridgeDbWebservice/main/pom.xml + BDBVERSION=$(grep -m 1 -oP "(?<=).*" pom.xml | sed 's|||g') + WSVERSION=$(grep -m 1 -oP "(?<=).*" pom.xml | sed 's|||g') + if [ -z "$BDBVERSION" ] || [ -z "$WSVERSION" ]; then + echo "ERROR: could not parse the versions out of the webservice pom." >&2 + exit 1 + fi + echo "bdbversion=$BDBVERSION" >> $GITHUB_OUTPUT + echo "wsversion=$WSVERSION" >> $GITHUB_OUTPUT + echo "Building for BridgeDb ${BDBVERSION}, webservice ${WSVERSION}" - - - name: Retrieve WebService version name + # Fail before the (hour-long) image build rather than part-way through it + # if the release asset the image downloads has not been published yet. + - name: Verify the webservice release JAR exists run: | - wget https://raw.githubusercontent.com/bridgedb/BridgeDbWebservice/main/pom.xml - echo "BRIDGEDBWSVERSION=$(cat pom.xml | grep -m 1 -oP "(?<=).*" | sed 's|||g')" >> $GITHUB_ENV + URL="https://github.com/bridgedb/BridgeDbWebservice/releases/download/${{ steps.versions.outputs.wsversion }}/BridgeDbWebservice-${{ steps.versions.outputs.wsversion }}-jar-with-dependencies.jar" + if ! curl -sfIL -o /dev/null "$URL"; then + echo "ERROR: release asset not found: $URL" >&2 + echo "The webservice release for ${{ steps.versions.outputs.wsversion }} is probably not published yet." >&2 + exit 1 + fi + echo "Release asset is available." - - name: Update setup.sh with BDBWSVERSION + update-version: + runs-on: ubuntu-latest + needs: resolve-version + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Update setup.sh with the resolved webservice version run: | - cat setup.sh | sed 's|BRIDGEDBWSVERSION=".\+"|BRIDGEDBWSVERSION="${{ env.BRIDGEDBWSVERSION }}"|g' > setup_tmp.sh - mv setup_tmp.sh setup.sh - + sed -i 's|BRIDGEDBWSVERSION=".\+"|BRIDGEDBWSVERSION="${{ needs.resolve-version.outputs.wsversion }}"|g' setup.sh + - name: commit and push setup.sh if: github.ref == 'refs/heads/main' run: | git config --local user.name actions-user git config --local user.email "actions@github.com" git add setup.sh - git diff --cached --quiet || git commit -m "Updated setup.sh with ${{ env.BDBVERSION }}-${{ env.BRIDGEDBWSVERSION }}" + git diff --cached --quiet || git commit -m "Updated setup.sh with ${{ needs.resolve-version.outputs.bdbversion }}-${{ needs.resolve-version.outputs.wsversion }}" git push origin main - - echo New release tag: ${{ env.BDBWSVERSION }} env: REPO_KEY: ${{secrets.GITHUB_TOKEN}} username: github-actions + docker: runs-on: ubuntu-latest - needs: update-version - steps: - - - name: Checkout + needs: resolve-version + steps: + - name: Checkout uses: actions/checkout@v6 - - - name: Set up QEMU + + # The version is pinned here, in this job's own workspace, instead of + # relying on the commit that update-version makes. checkout resolves the + # SHA that triggered the run, which predates that commit, so taking the + # version from the checked-out setup.sh silently builds the PREVIOUS + # release's JAR while tagging the image with the current version. + - name: Pin setup.sh to the resolved webservice version + run: | + sed -i 's|BRIDGEDBWSVERSION=".\+"|BRIDGEDBWSVERSION="${{ needs.resolve-version.outputs.wsversion }}"|g' setup.sh + PINNED=$(grep -m1 -E '^export BRIDGEDBWSVERSION=' setup.sh | sed -e 's/.*="//' -e 's/".*//') + if [ "$PINNED" != "${{ needs.resolve-version.outputs.wsversion }}" ]; then + echo "ERROR: setup.sh pins '$PINNED' but this build is for '${{ needs.resolve-version.outputs.wsversion }}'." >&2 + exit 1 + fi + echo "setup.sh pinned to $PINNED" + + - name: Set up QEMU uses: docker/setup-qemu-action@v4 - - - name: Set up Docker Buildx + + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v4 - - - name: Login to Docker Hub + + - name: Login to Docker Hub uses: docker/login-action@v4 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Retrieve tags - run: | - wget https://raw.githubusercontent.com/bridgedb/BridgeDbWebservice/main/pom.xml - echo "BDBVERSION=$(cat pom.xml | grep -m 1 -oP "(?<=).*" | sed 's|||g')" >> $GITHUB_ENV - wget https://raw.githubusercontent.com/bridgedb/BridgeDbWebservice/main/pom.xml - echo "BRIDGEDBWSVERSION=$(cat pom.xml | grep -m 1 -oP "(?<=).*" | sed 's|||g')" >> $GITHUB_ENV - - - name: Build and push - uses: docker/build-push-action@v5 - with: - push: true - tags: bigcatum/bridgedb:${{ env.BDBVERSION }}-${{ env.BRIDGEDBWSVERSION }} - - - name: Build and push + + # One build, one digest. This previously ran twice -- once for the version + # tag and once for :latest -- so the two tags were different images and + # :latest was never verifiably the release it claimed to be. :latest is no + # longer pushed here; it is promoted in test-and-promote once tests pass. + - name: Build and push the version tag uses: docker/build-push-action@v7 with: - push: true - tags: bigcatum/bridgedb:latest + push: ${{ github.event_name != 'pull_request' }} + tags: bigcatum/bridgedb:${{ needs.resolve-version.outputs.bdbversion }}-${{ needs.resolve-version.outputs.wsversion }} + + # Verify the image that was actually published, then point :latest at it. A + # broken or mislabelled build therefore never becomes :latest. + test-and-promote: + runs-on: ubuntu-latest + needs: [resolve-version, docker] + if: github.event_name != 'pull_request' + steps: + - name: Checkout + uses: actions/checkout@v6 + + # The image unpacks to roughly 9 GB of Derby databases, which does not fit + # alongside the runner's preinstalled toolchains. + - name: Free disk space + run: | + sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /usr/local/share/boost + df -h / + + - name: Login to Docker Hub + uses: docker/login-action@v4 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Verify the published image + run: | + IMAGE=bigcatum/bridgedb:${{ needs.resolve-version.outputs.bdbversion }}-${{ needs.resolve-version.outputs.wsversion }} + docker pull "$IMAGE" + tests/verify-image.sh "$IMAGE" "${{ needs.resolve-version.outputs.wsversion }}" + + # Re-tag by digest rather than rebuilding, so :latest is bit-for-bit the + # image that was just tested. + - name: Promote the tested image to :latest + run: | + docker buildx imagetools create \ + --tag bigcatum/bridgedb:latest \ + bigcatum/bridgedb:${{ needs.resolve-version.outputs.bdbversion }}-${{ needs.resolve-version.outputs.wsversion }} + + - name: Confirm :latest and the version tag are the same image + run: | + TAG=bigcatum/bridgedb:${{ needs.resolve-version.outputs.bdbversion }}-${{ needs.resolve-version.outputs.wsversion }} + VERSION_DIGEST=$(docker buildx imagetools inspect --format '{{.Manifest.Digest}}' "$TAG") + LATEST_DIGEST=$(docker buildx imagetools inspect --format '{{.Manifest.Digest}}' bigcatum/bridgedb:latest) + echo "version tag: $VERSION_DIGEST" + echo "latest: $LATEST_DIGEST" + if [ "$VERSION_DIGEST" != "$LATEST_DIGEST" ]; then + echo "ERROR: :latest does not point at the tested image." >&2 + exit 1 + fi diff --git a/.github/workflows/verify-published-image.yml b/.github/workflows/verify-published-image.yml new file mode 100644 index 0000000..e15d3f1 --- /dev/null +++ b/.github/workflows/verify-published-image.yml @@ -0,0 +1,53 @@ +name: Verify published image + +# Checks the image that is actually on Docker Hub, independently of any build. +# This catches a tag that is mislabelled, was overwritten by hand, or has rotted +# relative to the current webservice release. + +on: + workflow_dispatch: + inputs: + image: + description: 'Image to verify (default: bigcatum/bridgedb:latest)' + required: false + default: 'bigcatum/bridgedb:latest' + expected_version: + description: 'Expected webservice version (default: read from the webservice pom)' + required: false + default: '' + schedule: + # Weekly, Monday 06:00 UTC. + - cron: '0 6 * * 1' + +jobs: + verify: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6 + + # The image unpacks to roughly 9 GB of Derby databases, which does not fit + # alongside the runner's preinstalled toolchains. + - name: Free disk space + run: | + sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /usr/local/share/boost + df -h / + + - name: Determine image and expected version + id: target + run: | + IMAGE="${{ github.event.inputs.image }}" + [ -z "$IMAGE" ] && IMAGE="bigcatum/bridgedb:latest" + EXPECTED="${{ github.event.inputs.expected_version }}" + if [ -z "$EXPECTED" ]; then + wget -q -O pom.xml https://raw.githubusercontent.com/bridgedb/BridgeDbWebservice/main/pom.xml + EXPECTED=$(grep -m 1 -oP "(?<=).*" pom.xml | sed 's|||g') + fi + echo "image=$IMAGE" >> $GITHUB_OUTPUT + echo "expected=$EXPECTED" >> $GITHUB_OUTPUT + echo "Verifying $IMAGE against webservice version $EXPECTED" + + - name: Verify the published image + run: | + docker pull "${{ steps.target.outputs.image }}" + tests/verify-image.sh "${{ steps.target.outputs.image }}" "${{ steps.target.outputs.expected }}" diff --git a/README.md b/README.md index 19b65a3..f153a09 100644 --- a/README.md +++ b/README.md @@ -55,3 +55,40 @@ To stop a container, enter the following line. This will automatically remove th docker stop bridgedb ``` +## Testing an image + +`tests/` holds the checks that CI runs, and they can be run by hand against any +image or any running service. + +To check a complete image — what is baked into it, plus its behaviour once +started: + +``` +tests/verify-image.sh bigcatum/bridgedb:3.0.31-2.1.9 +``` + +The expected webservice version is taken from the tag; pass it explicitly as a +second argument for an image whose tag does not carry it (such as `:latest`): + +``` +tests/verify-image.sh bigcatum/bridgedb:latest 2.1.9 +``` + +To test a service that is already running (a container, or a deployed host): + +``` +tests/smoke-test.sh http://localhost:8183 2.1.9 +``` + +The checks cover the version the service reports for itself, the organism and +datasource catalogues, and real Derby-backed identifier mappings for human and +mouse. Both scripts exit non-zero when a check fails. + +Note that the webservice answers **HTTP 200 for unknown paths**, returning an +"Unrecognized query" page rather than a 404, so these checks assert on response +bodies. Any new check should do the same — asserting on the status code alone +would pass against a server that resolves nothing at all. + +In CI, `verify-published-image.yml` runs the same checks weekly against the +image on Docker Hub, and can be started manually for any tag. + diff --git a/tests/smoke-test.sh b/tests/smoke-test.sh new file mode 100755 index 0000000..5c922dc --- /dev/null +++ b/tests/smoke-test.sh @@ -0,0 +1,129 @@ +#!/usr/bin/env bash +# +# Smoke-test a running BridgeDb webservice over HTTP. +# +# Usage: tests/smoke-test.sh [expected-webservice-version] +# e.g. tests/smoke-test.sh http://localhost:8183 2.1.9 +# +# When the expected version is given, the version reported by /swagger.yaml must +# match it. That is the only signal that ties the running JAR to a release +# number, so it is what catches an image built from a stale setup.sh. +# +# NOTE: the webservice answers HTTP 200 for unknown paths, returning an +# "Unrecognized query" HTML page instead of a 404. Every check here therefore +# asserts on the response body; asserting on the status code alone would pass +# against a server that resolves nothing at all. + +set -uo pipefail + +BASE_URL="${1:?usage: smoke-test.sh [expected-webservice-version]}" +EXPECTED_VERSION="${2:-}" +BASE_URL="${BASE_URL%/}" + +CURL="curl -sS -m 60" +FAILED=0 + +pass() { printf ' ok %s\n' "$1"; } +fail() { printf ' FAIL %s\n' "$1"; printf ' %s\n' "$2"; FAILED=$((FAILED + 1)); } + +# Fetch a path and fail if the body is the catch-all "Unrecognized query" page. +# +# Note: grep reads from a here-string rather than a pipe throughout this script. +# With `pipefail`, a `grep -q` that exits on its first match leaves the upstream +# writer with SIGPIPE, and the pipeline then reports failure even though the +# match succeeded -- intermittently, only once a body exceeds the pipe buffer. +get() { + local path="$1" body + body=$($CURL "${BASE_URL}${path}" 2>/dev/null) + if [ -z "$body" ] || grep -q "Unrecognized query" <<<"$body"; then + return 1 + fi + printf '%s' "$body" +} + +check_contains() { + local label="$1" path="$2" needle="$3" body + if ! body=$(get "$path"); then + fail "$label" "no usable response from ${path} (empty or 'Unrecognized query')" + return + fi + if grep -q -- "$needle" <<<"$body"; then + pass "$label" + else + fail "$label" "expected '${needle}' in response from ${path}; got: $(head -c 200 <<<"$body")" + fi +} + +echo "Smoke-testing ${BASE_URL}" + +# Wait for the server to accept requests. Cold start loads every .bridge file +# listed in gdb.config, which takes appreciably longer than the JVM itself. +echo "Waiting for the service to become ready..." +ready=0 +for _ in $(seq 1 60); do + if get /contents >/dev/null 2>&1; then + ready=1 + break + fi + sleep 5 +done +if [ "$ready" -ne 1 ]; then + echo " FAIL service did not become ready within 300s" >&2 + exit 1 +fi +pass "service is up and answering /contents" + +# --- version ----------------------------------------------------------------- +# /swagger.yaml carries the webservice version of the JAR that is actually +# running, so it is the check that detects a version/content mismatch. +if [ -n "$EXPECTED_VERSION" ]; then + served_version="" + if swagger=$(get /swagger.yaml); then + served_version=$(grep -m1 -oP '(?<=^ version: ).*' <<<"$swagger" | tr -d '[:space:]') + fi + if [ -z "$served_version" ]; then + fail "served version matches the image tag" "could not read a version from /swagger.yaml" + elif [ "$served_version" = "$EXPECTED_VERSION" ]; then + pass "served version is ${EXPECTED_VERSION}" + else + fail "served version matches the image tag" \ + "/swagger.yaml reports '${served_version}' but the image is tagged for '${EXPECTED_VERSION}' -- the image contains the wrong JAR" + fi +fi + +# --- catalogue --------------------------------------------------------------- +check_contains "organism list includes Homo sapiens" /contents "Homo sapiens" +check_contains "source datasources are listed" "/Homo%20sapiens/sourceDataSources" "supportedSourceDatasources" + +# --- mappings ---------------------------------------------------------------- +# A Derby-backed lookup. This is the check that fails outright when the JDBC +# driver is not registered and no .bridge file can be opened. +check_contains "human gene mapping (BRCA2 -> Entrez)" \ + "/Homo%20sapiens/xrefs/En/ENSG00000139618" "ncbigene:675" +check_contains "human gene mapping (BRCA2 -> HGNC symbol)" \ + "/Homo%20sapiens/xrefs/En/ENSG00000139618" "hgnc.symbol:BRCA2" +check_contains "target-restricted mapping (BRCA2 -> Wikidata)" \ + "/Homo%20sapiens/xrefs/En/ENSG00000139618/Wd" "wikidata:Q17853272" +check_contains "mouse gene mapping (Brca1)" \ + "/Mus%20musculus/xrefs/En/ENSMUSG00000017146" "mgi:Brca1" +check_contains "attributes endpoint" \ + "/Homo%20sapiens/attributes/En/ENSG00000139618" "Symbol" + +# --- negative check ---------------------------------------------------------- +# Documents the 200-for-everything behaviour: a nonsense path must come back as +# the "Unrecognized query" page. If this ever starts returning real content, the +# body-based checks above would silently pass against anything. +if body=$($CURL "${BASE_URL}/definitely-not-a-real-endpoint-xyz" 2>/dev/null) && + grep -q "Unrecognized query" <<<"$body"; then + pass "unknown paths return the 'Unrecognized query' page" +else + fail "unknown paths return the 'Unrecognized query' page" \ + "unexpected response for a nonsense path; body-based assertions may no longer be meaningful" +fi + +echo +if [ "$FAILED" -gt 0 ]; then + echo "${FAILED} check(s) failed." + exit 1 +fi +echo "All checks passed." diff --git a/tests/verify-image.sh b/tests/verify-image.sh new file mode 100755 index 0000000..9404cd4 --- /dev/null +++ b/tests/verify-image.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash +# +# Verify a built BridgeDb image: inspect what was baked into it, then start it +# and run the HTTP smoke tests against it. +# +# Usage: tests/verify-image.sh [expected-webservice-version] +# e.g. tests/verify-image.sh bigcatum/bridgedb:3.0.31-2.1.9 2.1.9 +# +# With no expected version, the version is taken from the image tag, which is of +# the form -. + +set -uo pipefail + +IMAGE="${1:?usage: verify-image.sh [expected-webservice-version]}" +EXPECTED_VERSION="${2:-}" + +if [ -z "$EXPECTED_VERSION" ]; then + tag="${IMAGE##*:}" + # Tag is -; the webservice version is the part after the last dash. + case "$tag" in + *-*) EXPECTED_VERSION="${tag##*-}" ;; + *) EXPECTED_VERSION="" ;; + esac +fi + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +CONTAINER="bridgedb-verify-$$" +PORT="${PORT:-18183}" +FAILED=0 + +cleanup() { + docker rm -f "$CONTAINER" >/dev/null 2>&1 || true +} +trap cleanup EXIT + +echo "Verifying image ${IMAGE} (expecting webservice ${EXPECTED_VERSION:-})" + +# --- static checks, before starting anything --------------------------------- +# setup.sh is copied into the image, so the version it pins is the version of +# the JAR that was downloaded at build time. If this disagrees with the tag, the +# image is mislabelled and there is no point starting it. +if [ -n "$EXPECTED_VERSION" ]; then + baked=$(docker run --rm --entrypoint /bin/sh "$IMAGE" -c \ + 'grep -m1 -E "^export BRIDGEDBWSVERSION=" /setup.sh' 2>/dev/null | + sed -e 's/.*="//' -e 's/".*//') + if [ "$baked" = "$EXPECTED_VERSION" ]; then + printf ' ok setup.sh in image pins %s\n' "$baked" + else + printf ' FAIL setup.sh in image pins "%s" but the tag says "%s"\n' "$baked" "$EXPECTED_VERSION" + printf ' the image was built from a stale setup.sh and contains the wrong JAR\n' + FAILED=$((FAILED + 1)) + fi +fi + +if docker run --rm --entrypoint /bin/sh "$IMAGE" -c \ + '[ -s /opt/bridgedb/bridgedb/BridgeDb-Webservice.jar ] && [ "$(head -c 2 /opt/bridgedb/bridgedb/BridgeDb-Webservice.jar)" = "PK" ]' 2>/dev/null; then + printf ' ok webservice JAR is present and is a valid archive\n' +else + printf ' FAIL webservice JAR is missing, empty, or not a JAR\n' + FAILED=$((FAILED + 1)) +fi + +if docker run --rm --entrypoint /bin/sh "$IMAGE" -c \ + '[ -s /opt/bridgedb-databases/gdb.config ]' 2>/dev/null; then + printf ' ok gdb.config is present\n' +else + printf ' FAIL gdb.config is missing or empty\n' + FAILED=$((FAILED + 1)) +fi + +# --- runtime checks ---------------------------------------------------------- +echo "Starting container on port ${PORT}..." +if ! docker run -d --name "$CONTAINER" -p "${PORT}:8183" "$IMAGE" >/dev/null; then + echo " FAIL container failed to start" >&2 + exit 1 +fi + +if ! "${SCRIPT_DIR}/smoke-test.sh" "http://localhost:${PORT}" "$EXPECTED_VERSION"; then + FAILED=$((FAILED + 1)) + echo + echo "--- last 40 lines of the service log ---" + docker exec "$CONTAINER" sh -c 'tail -40 /opt/bridgedb/bridgedb/bridgedb.log' 2>/dev/null || + docker logs --tail 40 "$CONTAINER" 2>&1 +fi + +echo +if [ "$FAILED" -gt 0 ]; then + echo "Image verification FAILED for ${IMAGE}." + exit 1 +fi +echo "Image verification passed for ${IMAGE}."