From 5e029670eb8707ca7e1e3b59df93faaeb62c9317 Mon Sep 17 00:00:00 2001 From: Joao P Dubas Date: Mon, 7 Oct 2024 12:16:07 +0000 Subject: [PATCH] feat: execute test with docker compose (#101) When we changed the application to be executed as distributed nodes (PR #96), performing tests with `docker-compose` became impossible. To fix this situation in this PR, two new targets are added to `Makefile`: * `compose_test` * `compose_test_shell` These new targets allow one to execute tests and enter the container shell responsible for running these tests. Reviewed-on: https://gitea.dubas.dev/joao.dubas/ex_trainer/pulls/101 Co-authored-by: Joao P Dubas Co-committed-by: Joao P Dubas --- Makefile | 24 ++++++++++++++++-------- docker-compose.yml | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 9e7f75f..9034144 100644 --- a/Makefile +++ b/Makefile @@ -28,14 +28,6 @@ static_code_analysis: check_format credo dialyzer ## run static code analysis test: ## run tests @mix test --cover --trace --slowest 10 -.PHONY: compose_up -compose_up: ## start containers for this service - @$(COMPOSE) up -d - -.PHONY: compose_watch -compose_watch: ## start containers for this service watching for updates in filesystem - @$(COMPOSE) up -w - .PHONY: compose_database_create compose_database_create: @$(COMPOSE) run --rm --entrypoint mix app ecto.create @@ -55,6 +47,22 @@ compose_ps: ## status of containers compose_remote: ## connect to remote node @$(COMPOSE) exec app ./priv/docker/service/docker-remote.sh +.PHONY: compose_test +compose_test: ## execute test on docker environment + @$(COMPOSE) --profile test run --rm test + +.PHONY: compose_test_shell +compose_test_shell: ## enter test environment shell + @$(COMPOSE) --profile test run --rm --entrypoint bash test -c bash + +.PHONY: compose_up +compose_up: ## start containers for this service + @$(COMPOSE) up -d + +.PHONY: compose_watch +compose_watch: ## start containers for this service watching for updates in filesystem + @$(COMPOSE) up -w + .PHONY: help help: @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/docker-compose.yml b/docker-compose.yml index 21b55ec..2ca1931 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,6 +19,7 @@ services: context: . args: BUILD_MIX_ENV: dev + pull_policy: never hostname: &app_host ex_trainer depends_on: - db @@ -53,6 +54,39 @@ services: scale: 3 entrypoint: ./priv/docker/service/docker-entrypoint.sh command: local-cookie + test: + image: 'joaodubas/ex_trainer:test' + build: + target: builder + context: . + args: + BUILD_MIX_ENV: test + pull_policy: never + profiles: + - test + hostname: ex_trainer_test + depends_on: + - db + init: true + volumes: + - './mix.exs:/opt/app/mix.exs:ro' + - './mix.lock:/opt/app/mix.lock:ro' + - './students.csv:/opt/app/students.csv:ro' + - './config:/opt/app/config' + - './cover:/opt/app/cover' + - './lib:/opt/app/lib' + - './priv:/opt/app/priv' + - './report:/opt/app/report' + - './test:/opt/app/test' + environment: + POSTGRES_HOST: *db_host + POSTGRES_USER: *db_user + POSTGRES_PASS: *db_pass + POSTGRES_NAME: wabanex_test + working_dir: /opt/app + restart: never + entrypoint: mix test + command: --trace --cover --slowest 10 volumes: db_data: {}