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