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
```
This commit is contained in:
2025-04-05 17:00:54 -05:00
parent 76cf57ebe0
commit c227bec62d
2 changed files with 75 additions and 0 deletions

9
Justfile Normal file
View File

@@ -0,0 +1,9 @@
# vim: set et :
integration-tests:
./run-tests.sh --test=integration
unit-tests:
cargo test --lib
test: unit-tests integration-tests

66
run-tests.sh Executable file
View File

@@ -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}" <<EOF
[test.meilisearch]
url = "http://127.0.0.1:${port}"
token = "${meilisearch_key}"
[test.auth]
jwt_secret = "${jwt_key}"
EOF
export ROCKET_PROFILE=test
export ROCKET_CONFIG
cargo test "$@"