Skip to content

Commit f1fa3dc

Browse files
authored
chores(x2a): run local tests against PostgreSQL (#2656)
Signed-off-by: Marek Libra <marek.libra@gmail.com>
1 parent 53a0ccf commit f1fa3dc

4 files changed

Lines changed: 78 additions & 2 deletions

File tree

workspaces/x2a/README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,39 @@ The plugin's `KubeService` provides methods to interact with Kubernetes resource
282282
Loaded Kubernetes configuration from ~/.kube/config
283283
```
284284

285+
## Running Tests with PostgreSQL
286+
287+
By default, `yarn test` runs database tests against SQLite only. The backend
288+
tests are written to also exercise PostgreSQL (via `TestDatabases` from
289+
`@backstage/backend-test-utils`), but PostgreSQL is skipped locally because the
290+
Backstage test tooling disables Docker when the `CI` environment variable is not
291+
set.
292+
293+
### Quick start — testcontainers (Docker/Podman)
294+
295+
Run tests against both SQLite and PostgreSQL with a single command:
296+
297+
```sh
298+
yarn test:pg # unit tests (SQLite + PostgreSQL)
299+
yarn test:all:pg # full suite including lint, prettier, coverage
300+
```
301+
302+
These scripts set `CI=true` so that `testcontainers` automatically pulls and
303+
starts a `postgres:18` container. **Docker or Podman must be running.**
304+
305+
On Fedora/RHEL with Podman, enable the Docker-compatible socket first:
306+
307+
```sh
308+
systemctl --user enable --now podman.socket
309+
export DOCKER_HOST=unix:///run/user/$(id -u)/podman/podman.sock
310+
```
311+
312+
The first run downloads the `postgres:18` image.
313+
285314
## Additional Commands
286315

287-
- `yarn test` - Run tests
316+
- `yarn test` - Run tests (SQLite only)
317+
- `yarn test:pg` - Run tests (SQLite + PostgreSQL via testcontainers)
288318
- `yarn lint` - Run linter
289319
- `yarn prettier:fix` - Fix code formatting
290320
- `yarn build:all` - Build all packages

workspaces/x2a/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@
1919
"tsc:full": "tsc --skipLibCheck true --incremental false",
2020
"clean": "backstage-cli repo clean",
2121
"test": "backstage-cli repo test",
22+
"test:pg": "CI=true backstage-cli repo test",
2223
"test:all": "yarn openapi-generate && yarn prettier:check && yarn lint:all && backstage-cli repo test --coverage",
24+
"test:all:pg": "yarn openapi-generate && yarn prettier:check && yarn lint:all && CI=true backstage-cli repo test --coverage",
2325
"test:e2e": "echo Skipping until we have tests: playwright test",
2426
"fix": "backstage-cli repo fix",
2527
"lint": "backstage-cli repo lint --since origin/main",
2628
"lint:all": "backstage-cli repo lint",
2729
"prettier:check": "prettier --check .",
2830
"prettier:fix": "prettier --write .",
29-
"chores": "yarn prettier:fix && yarn lint:all --fix && yarn tsc:full && yarn build:api-reports && yarn test:all",
31+
"chores": "yarn prettier:fix && yarn lint:all --fix && yarn tsc:full && yarn build:api-reports && yarn test:all:pg",
3032
"new": "backstage-cli new --scope @red-hat-developer-hub",
3133
"postinstall": "cd ../../ && yarn install"
3234
},

workspaces/x2a/plugins/x2a-backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
"build": "backstage-cli package build",
3030
"lint": "backstage-cli package lint",
3131
"test": "NODE_OPTIONS='--experimental-vm-modules' backstage-cli package test",
32+
"test:pg": "NODE_OPTIONS='--experimental-vm-modules' CI=true backstage-cli package test",
3233
"test:all": "NODE_OPTIONS='--experimental-vm-modules' backstage-cli package test --coverage",
34+
"test:all:pg": "NODE_OPTIONS='--experimental-vm-modules' CI=true backstage-cli package test --coverage",
3335
"clean": "backstage-cli package clean",
3436
"prepack": "backstage-cli package prepack",
3537
"postpack": "backstage-cli package postpack",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bash
2+
# Start a disposable PostgreSQL 18 container for local test runs.
3+
#
4+
# Usage:
5+
# eval "$(./scripts/start-test-postgres.sh)"
6+
# yarn test # now runs against the external PostgreSQL instance
7+
#
8+
# The container is removed automatically when stopped:
9+
# podman stop x2a-test-postgres # or: docker stop x2a-test-postgres
10+
11+
set -euo pipefail
12+
13+
CONTAINER_NAME="x2a-test-postgres"
14+
POSTGRES_PASSWORD="testpassword"
15+
HOST_PORT="${X2A_TEST_POSTGRES_PORT:-5433}"
16+
IMAGE="${BACKSTAGE_TEST_DOCKER_REGISTRY:-}postgres:18"
17+
18+
runtime="docker"
19+
if command -v podman &>/dev/null && ! command -v docker &>/dev/null; then
20+
runtime="podman"
21+
fi
22+
23+
if "$runtime" ps --format '{{.Names}}' 2>/dev/null | grep -qx "$CONTAINER_NAME"; then
24+
>&2 echo "Container '$CONTAINER_NAME' is already running on port $HOST_PORT."
25+
else
26+
>&2 echo "Starting PostgreSQL 18 container '$CONTAINER_NAME' on port $HOST_PORT"
27+
"$runtime" run -d --rm \
28+
--name "$CONTAINER_NAME" \
29+
-e POSTGRES_PASSWORD="$POSTGRES_PASSWORD" \
30+
-p "${HOST_PORT}:5432" \
31+
"$IMAGE" >/dev/null
32+
33+
>&2 echo "Waiting for PostgreSQL to accept connections …"
34+
for _ in $(seq 1 30); do
35+
if "$runtime" exec "$CONTAINER_NAME" pg_isready -U postgres &>/dev/null; then
36+
break
37+
fi
38+
sleep 1
39+
done
40+
fi
41+
42+
echo "export BACKSTAGE_TEST_DATABASE_POSTGRES18_CONNECTION_STRING=\"postgresql://postgres:${POSTGRES_PASSWORD}@localhost:${HOST_PORT}/postgres\""

0 commit comments

Comments
 (0)