From c227bec62dad8e1d7911fa99a15576790f3fe2e6 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 5 Apr 2025 17:00:54 -0500 Subject: [PATCH] run-tests: Add harness for integration tests The `run-tests.sh` script sets up a full environment for the integration tests. This includes starting Meilisearch (with a master key to enable authentication) and generating an ephemeral JWT secret. After the tests are run, the environment is cleaned up. ```sh just test just unit-tests just integration-tests ``` --- Justfile | 9 +++++++ run-tests.sh | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 Justfile create mode 100755 run-tests.sh diff --git a/Justfile b/Justfile new file mode 100644 index 0000000..9164410 --- /dev/null +++ b/Justfile @@ -0,0 +1,9 @@ +# vim: set et : + +integration-tests: + ./run-tests.sh --test=integration + +unit-tests: + cargo test --lib + +test: unit-tests integration-tests diff --git a/run-tests.sh b/run-tests.sh new file mode 100755 index 0000000..314b1f7 --- /dev/null +++ b/run-tests.sh @@ -0,0 +1,66 @@ +#!/bin/sh +# vim: set ts=4 sts=4 sw=4 et : + +cleanup() { + if [ -n "${cid}" ]; then + echo 'Stopping Meilisearch ...' >&2 + podman stop "${cid}" >/dev/null + fi + unset cid + if [ -f "${meilisearch_key}" ]; then + rm "${meilisearch_key}" + fi + unset meilisearch_key + if [ -f "${jwt_key}" ]; then + rm "${jwt_key}" + fi + unset jwt_key + if [ -f "${ROCKET_CONFIG}" ]; then + rm "${ROCKET_CONFIG}" + fi + unset ROCKET_CONFIG +} + +trap cleanup INT TERM QUIT EXIT + +echo 'Generating Meilisearch master key ...' >&2 +MEILI_MASTER_KEY=$(tr -cd 0-9A-Za-z_- < /dev/urandom | head -c 32) + +echo 'Starting Meilisearch ...' >&2 +cid=$( + export MEILI_MASTER_KEY + podman run --rm -d -P \ + -e MEILI_NO_ANALYTICS=true \ + -e MEILI_MASTER_KEY \ + docker.io/getmeili/meilisearch:v1.13 +) + +port=$(podman port ${cid} 7700/tcp | cut -d: -f2) +printf 'Waiting for Meilisearch on port %d ...' "${port}" >&2 +until curl -fs -o /dev/null 127.0.0.1:${port}; do + printf '.' + sleep 0.25 +done +echo ' ready' >&2 + +meilisearch_key=$(mktemp meilisearch.token.XXXXXX) +curl -s 127.0.0.1:${port}/keys -H "Authorization: Bearer ${MEILI_MASTER_KEY}" \ + | jq -r '.results[] | select(.name == "Default Admin API Key") | .key' \ + > "${meilisearch_key}" + +echo 'Generating JWT secret key ...' >&2 +jwt_key=$(mktemp jwt.secret.XXXXXX) +tr -cd 0-9A-Za-z_- < /dev/urandom | head -c 32 > "${jwt_key}" + +ROCKET_CONFIG=$(mktemp Rocket.toml.XXXXXX) +cat > "${ROCKET_CONFIG}" <