forked from mtusi2026/mtusi2026lab2
add tests
This commit is contained in:
@@ -1,22 +1,47 @@
|
||||
-include .env
|
||||
|
||||
PYTHON ?= python3
|
||||
PYTHON ?= $(if $(wildcard venv/bin/python),venv/bin/python,python3)
|
||||
BASE_URL ?= http://localhost:8080
|
||||
CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
TEST_DATABASE_URL ?= postgresql+asyncpg://$(DB_USER):$(DB_PASSWORD)@localhost:5432/$(DB_NAME)_test
|
||||
export TEST_DATABASE_URL
|
||||
COMPOSE = docker compose --env-file .env
|
||||
|
||||
TEST_PROJECT ?= $(notdir $(CURDIR))-test
|
||||
TEST_API_PORT ?= 18080
|
||||
TEST_DB_PORT ?= 15432
|
||||
TEST_REDIS_PORT ?= 16379
|
||||
TEST_BASE_URL = http://localhost:$(TEST_API_PORT)
|
||||
TEST_ENV = COMPOSE_PROJECT_NAME=$(TEST_PROJECT) API_HOST_PORT=$(TEST_API_PORT) \
|
||||
DB_HOST_PORT=$(TEST_DB_PORT) REDIS_HOST_PORT=$(TEST_REDIS_PORT)
|
||||
TEST_COMPOSE = $(TEST_ENV) $(COMPOSE)
|
||||
|
||||
TEST_DATABASE_URL ?= postgresql+asyncpg://$(DB_USER):$(DB_PASSWORD)@localhost:$(TEST_DB_PORT)/$(DB_NAME)_test
|
||||
TEST_REDIS_HOST ?= localhost
|
||||
TEST_REDIS_PASSWORD ?= $(REDIS_PASSWORD)
|
||||
TEST_REDIS_DB ?= 15
|
||||
export TEST_DATABASE_URL TEST_REDIS_HOST TEST_REDIS_PORT TEST_REDIS_PASSWORD TEST_REDIS_DB
|
||||
|
||||
define wait_ready
|
||||
@for i in $$(seq 1 90); do \
|
||||
curl -sf "$(1)/health/ready" >/dev/null && exit 0; sleep 1; \
|
||||
done; echo "API $(1) не готов за 90 секунд" >&2; exit 1
|
||||
endef
|
||||
|
||||
.PHONY: help check validate test load test-persistence \
|
||||
up down logs db-up migrate migration \
|
||||
test-own test-limits test-concurrency test-migrations
|
||||
up down restart ps logs migrate migration \
|
||||
test-stand-up test-stand-down test-stand-destroy test-stand-ps \
|
||||
test-stand-smoke test-stand-persistence db-up \
|
||||
test-own test-docker test-limits test-concurrency test-migrations test-all
|
||||
|
||||
help:
|
||||
@echo "Курс: check | validate | test (smoke по BASE_URL) | load | test-persistence ACTION_SCRIPT=scripts/restart.sh"
|
||||
@echo "Стенд: up | down | logs | db-up (только PostgreSQL + тестовая БД) | migrate"
|
||||
@echo "Свои: test-own | test-limits | test-concurrency | test-migrations | migration m='описание'"
|
||||
@echo "Курс: check | validate | test (smoke по BASE_URL) | load | test-persistence ACTION_SCRIPT=scripts/restart.sh"
|
||||
@echo "Основной: up | down | restart | ps | logs | migrate | migration m='описание'"
|
||||
@echo "Тестовый: test-stand-up | test-stand-down | test-stand-destroy | test-stand-ps"
|
||||
@echo " test-stand-smoke | test-stand-persistence"
|
||||
@echo "Тесты: test-own | test-docker | test-limits | test-concurrency | test-migrations"
|
||||
@echo "Всё: test-all (стенд + pytest + smoke + persistence на тестовом стенде)"
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
@@ -35,27 +60,58 @@ test-persistence:
|
||||
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
up:
|
||||
docker compose --env-file .env up -d --build
|
||||
$(COMPOSE) up -d --build
|
||||
$(call wait_ready,$(BASE_URL))
|
||||
|
||||
down:
|
||||
docker compose down
|
||||
$(COMPOSE) down
|
||||
|
||||
restart:
|
||||
scripts/restart.sh
|
||||
$(call wait_ready,$(BASE_URL))
|
||||
|
||||
ps:
|
||||
$(COMPOSE) ps
|
||||
|
||||
logs:
|
||||
docker compose logs -f api
|
||||
|
||||
db-up:
|
||||
docker compose up -d --wait postgres
|
||||
docker compose exec -T postgres sh -c 'createdb -U "$$POSTGRES_USER" "$$POSTGRES_DB"_test 2>&1 | grep -v "already exists"; true'
|
||||
$(COMPOSE) logs -f api
|
||||
|
||||
migrate:
|
||||
docker compose run --rm migrate
|
||||
$(COMPOSE) run --rm --build migrate
|
||||
|
||||
migration:
|
||||
$(PYTHON) -m alembic revision --autogenerate -m "$(m)"
|
||||
DB_HOST=localhost $(PYTHON) -m alembic revision --autogenerate -m "$(m)"
|
||||
|
||||
test-stand-up:
|
||||
$(TEST_COMPOSE) up -d --build
|
||||
$(call wait_ready,$(TEST_BASE_URL))
|
||||
$(TEST_COMPOSE) exec -T postgres sh -c 'createdb -U "$$POSTGRES_USER" "$$POSTGRES_DB"_test 2>&1 | grep -v "already exists"; true'
|
||||
|
||||
db-up: test-stand-up
|
||||
|
||||
test-stand-down:
|
||||
$(TEST_COMPOSE) down
|
||||
|
||||
test-stand-destroy:
|
||||
$(TEST_COMPOSE) down -v
|
||||
|
||||
test-stand-ps:
|
||||
$(TEST_COMPOSE) ps
|
||||
|
||||
test-stand-smoke:
|
||||
$(PYTHON) tests/smoke.py --base-url "$(TEST_BASE_URL)"
|
||||
|
||||
test-stand-persistence:
|
||||
$(TEST_COMPOSE) ps
|
||||
$(TEST_ENV) $(PYTHON) tests/resilience.py --mode persistence --action-script scripts/restart.sh --base-url "$(TEST_BASE_URL)"
|
||||
$(TEST_COMPOSE) ps
|
||||
|
||||
test-own:
|
||||
$(PYTHON) -m pytest tests -q
|
||||
|
||||
test-docker:
|
||||
$(TEST_ENV) DOCKER_TESTS=1 $(PYTHON) -m pytest tests -q
|
||||
|
||||
test-limits:
|
||||
$(PYTHON) -m pytest tests/test_limits.py -q
|
||||
|
||||
@@ -64,3 +120,5 @@ test-concurrency:
|
||||
|
||||
test-migrations:
|
||||
$(PYTHON) -m pytest tests/test_migrations.py -q
|
||||
|
||||
test-all: test-stand-up test-docker test-stand-smoke test-stand-persistence
|
||||
|
||||
Reference in New Issue
Block a user