Киселев Егор Владимирович #294
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Docker Compose Test | ||
| on: | ||
| pull_request: | ||
| branches: [ main ] | ||
| jobs: | ||
| test-compose: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| - name: Start services | ||
| run: docker compose up -d | ||
| - name: Wait for init-db to complete | ||
| run: | | ||
| timeout=60 | ||
| while docker inspect -f '{{.State.Running}}' devops-project-init-db-1 2>/dev/null | grep -q 'true'; do | ||
| sleep 1 | ||
| ((timeout--)) | ||
| if [ $timeout -eq 0 ]; then | ||
| echo "init-db did not finish in time" | ||
| docker compose logs init-db | ||
| exit 1 | ||
| fi | ||
| done | ||
| echo "init-db completed" | ||
| - name: Wait for web to be ready | ||
| run: | | ||
| timeout=30 | ||
| while ! curl -f http://localhost:5000 >/dev/null 2>&1; do | ||
| ((timeout--)) | ||
| if [ $timeout -eq 0 ]; then | ||
| echo "Web service failed to start" | ||
| exit 1 | ||
| fi | ||
| sleep 1 | ||
| done | ||
| - name: Test web response | ||
| run: | | ||
| response=$(curl -s http://localhost:5000/ping) | ||
| if [[ "$response" != "pong" ]]; then | ||
| echo "Expected 'pong', got: $response" | ||
| exit 1 | ||
| fi | ||
| echo "Web test passed!" | ||
| - name: Test PostgreSQL connection | ||
| run: | | ||
| docker compose exec -T db pg_isready -U user -d pingdb | ||
| - name: Stop services | ||
| run: docker compose down | ||
| - name: Start DEV services | ||
| run: docker compose -f docker-compose.yml -f docker-compose.dev.yml up -d | ||
| - name: Wait for DEV init-db | ||
| run: | | ||
| timeout=60 | ||
| while docker inspect -f '{{.State.Running}}' devops-project-init-db-1 2>/dev/null | grep -q 'true'; do | ||
| sleep 1 | ||
| ((timeout--)) | ||
| if [ $timeout -eq 0 ]; then | ||
| echo "DEV init-db did not finish" | ||
| docker compose logs init-db | ||
| exit 1 | ||
| fi | ||
| done | ||
| - name: Wait for DEV web | ||
| run: | | ||
| timeout=30 | ||
| while ! curl -f http://localhost:5000 >/dev/null 2>&1; do | ||
| ((timeout--)) | ||
| if [ $timeout -eq 0 ]; then | ||
| echo "DEV web failed to start" | ||
| exit 1 | ||
| fi | ||
| sleep 1 | ||
| done | ||
| - name: Test DEV mode | ||
| run: | | ||
| response=$(curl -s http://localhost:5000/visits) | ||
| if [[ "$response" != "-1" ]]; then | ||
| echo "Expected -1 in DEV mode, got: $response" | ||
| exit 1 | ||
| fi | ||
| echo "DEV mode test passed!" | ||
| - name: Show logs on failure | ||
| if: failure() | ||
| run: docker compose logs | ||
| - name: Run tests with coverage | ||
| run: | | ||
| docker compose run --rm app python -m coverage run --source='.' manage.py test | ||
| docker compose run --rm app python -m coverage report | ||
| coverage_value=$(docker compose run --rm app python -c " | ||
| import coverage; c = coverage.Coverage(); c.load(); | ||
| total = c.report(file=open('/dev/null', 'w')); | ||
| print(int(round(total))) | ||
| ") | ||
| if [ "$coverage_value" -lt 60 ]; then | ||
| echo "Coverage is $coverage_value% < 60% — FAIL" | ||
| exit 1 | ||
| else | ||
| echo "Coverage is $coverage_value% >= 60% — OK" | ||
| fi | ||