From c61878253b402e19bdbd3a4c904406fee0984888 Mon Sep 17 00:00:00 2001 From: Nathan Vasse Date: Tue, 26 Aug 2025 11:50:12 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7(conf)=20setup=20handy=20local=20e2?= =?UTF-8?q?e=20env?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to be able to quickly start a local isolated env for the e2e testing, we don't want to share to same db with the classic dev env because the e2e db should be empty before tests execution. And we don't want to clear our dev db every time. Also provide a new makefile handy command to launch the e2e env and the tests in one command, great. --- Makefile | 29 +++++++++++++++++++++++++++++ bin/clear_db_e2e.sql | 9 +++++++++ bin/postgres_e2e | 12 ++++++++++++ compose.yaml | 6 ++++-- env.d/development/postgresql.e2e | 11 +++++++++++ 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 bin/clear_db_e2e.sql create mode 100755 bin/postgres_e2e create mode 100644 env.d/development/postgresql.e2e diff --git a/Makefile b/Makefile index 4b05b126..8ae1bfaa 100644 --- a/Makefile +++ b/Makefile @@ -48,6 +48,7 @@ COMPOSE_RUN_CROWDIN = $(COMPOSE_RUN) crowdin crowdin # -- Backend MANAGE = $(COMPOSE_RUN_APP) python manage.py MAIL_YARN = $(COMPOSE_RUN) -w /app/src/mail node yarn +PSQL_E2E = ./bin/postgres_e2e # -- Frontend PATH_FRONT = ./src/frontend @@ -105,6 +106,7 @@ build-frontend: ## build the frontend container .PHONY: build-frontend-development down: ## stop and remove containers, networks, images, and volumes + rm -rf data/postgresql.* @$(COMPOSE) down .PHONY: down @@ -117,6 +119,33 @@ run-backend: ## start the backend container @$(COMPOSE) up --force-recreate -d nginx .PHONY: run-backend +bootstrap-e2e: ## bootstrap the backend container for e2e tests, without frontend +bootstrap-e2e: \ + data/media \ + data/static \ + create-env-local-files \ + build-backend \ + back-i18n-compile \ + run-backend-e2e +.PHONY: bootstrap-e2e + +clear-db-e2e: ## quickly clears the database for e2e tests, used in the e2e tests + $(PSQL_E2E) -c "$$(cat bin/clear_db_e2e.sql)" +.PHONY: clear-db-e2e + +run-backend-e2e: ## start the backend container for e2e tests, always remove the postgresql.e2e volume first + @$(MAKE) stop + rm -rf data/postgresql.e2e + @ENV_OVERRIDE=e2e $(MAKE) run-backend + @ENV_OVERRIDE=e2e $(MAKE) migrate +.PHONY: run-backend-e2e + +run-tests-e2e: ## run the e2e tests, example: make run-tests-e2e -- --project chromium --headed + @$(MAKE) run-backend-e2e + @args="$(filter-out $@,$(MAKECMDGOALS))" && \ + cd src/frontend/apps/e2e && yarn test $${args:-${1}} +.PHONY: run-tests-e2e + run: ## start the development server and frontend development run: @$(MAKE) run-backend diff --git a/bin/clear_db_e2e.sql b/bin/clear_db_e2e.sql new file mode 100644 index 00000000..c830d253 --- /dev/null +++ b/bin/clear_db_e2e.sql @@ -0,0 +1,9 @@ +DO $$ +DECLARE r RECORD; +BEGIN + FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') + LOOP + RAISE NOTICE 'Truncating table %', r.tablename; + EXECUTE 'TRUNCATE TABLE ' || quote_ident(r.tablename) || ' CASCADE'; + END LOOP; +END $$; diff --git a/bin/postgres_e2e b/bin/postgres_e2e new file mode 100755 index 00000000..8d2f6e44 --- /dev/null +++ b/bin/postgres_e2e @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +# shellcheck source=bin/_config.sh +source "$(dirname "${BASH_SOURCE[0]}")/_config.sh" + +# Get database credentials from environment file +ENV_FILE="${REPO_DIR}/env.d/development/postgresql.e2e" +POSTGRES_USER=$(grep POSTGRES_USER "$ENV_FILE" | cut -d'=' -f2) +POSTGRES_DB=$(grep POSTGRES_DB "$ENV_FILE" | cut -d'=' -f2) + +# Execute PostgreSQL command (run as postgres user, not host user) +_docker_compose exec -T postgresql psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -v ON_ERROR_STOP=1 "$@" diff --git a/compose.yaml b/compose.yaml index 032ecada..1589868c 100644 --- a/compose.yaml +++ b/compose.yaml @@ -12,7 +12,9 @@ services: retries: 300 env_file: - env.d/development/postgresql - - env.d/development/postgresql.local + - env.d/development/postgresql.${ENV_OVERRIDE:-local} + volumes: + - ./data/postgresql.${ENV_OVERRIDE:-local}:/var/lib/postgresql/data redis: image: redis:5 @@ -71,7 +73,7 @@ services: - env.d/development/common - env.d/development/common.local - env.d/development/postgresql - - env.d/development/postgresql.local + - env.d/development/postgresql.${ENV_OVERRIDE:-local} ports: - "8071:8000" volumes: diff --git a/env.d/development/postgresql.e2e b/env.d/development/postgresql.e2e new file mode 100644 index 00000000..6fa3e91a --- /dev/null +++ b/env.d/development/postgresql.e2e @@ -0,0 +1,11 @@ +# Postgresql db container configuration +POSTGRES_DB=drive_e2e +POSTGRES_USER=dinum +POSTGRES_PASSWORD=pass + +# App database configuration +DB_HOST=postgresql +DB_NAME=drive_e2e +DB_USER=dinum +DB_PASSWORD=pass +DB_PORT=5432