-
Notifications
You must be signed in to change notification settings - Fork 0
121 lines (104 loc) · 3.43 KB
/
api-contract-tests.yml
File metadata and controls
121 lines (104 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
name: API Contract Tests
# This workflow tests your API against the OpenAPI spec using Schemathesis.
# It only runs the "examples" phase (fast & deterministic).
# For thorough fuzzing, run locally: make schemathesis
on:
pull_request:
paths:
- '**.go'
- 'go.mod'
- 'go.sum'
- 'openapi.yaml'
- 'cmd/**'
- 'internal/**'
- 'pkg/**'
- '.github/workflows/api-contract-tests.yml'
concurrency:
group: api-contract-tests-${{ github.head_ref }}
cancel-in-progress: true
jobs:
api-contract-tests:
name: API Contract Tests
runs-on: ubuntu-24.04
timeout-minutes: 10
services:
postgres:
image: pgvector/pgvector:pg18
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test_db
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/test_db?sslmode=disable
API_KEY: test-api-key-for-ci
PORT: 8080
steps:
- name: Checkout
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5
- name: Set up Go
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff
with:
go-version: '1.25.6'
- name: Cache Go modules
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install dependencies
run: go mod download
- name: Initialize database schema
run: make init-db
- name: Build application
run: make build
- name: Start API server
run: |
./bin/api &
API_PID=$!
echo "API_PID=$API_PID" >> $GITHUB_ENV
# Wait for server to be healthy (up to ~60s)
for i in {1..30}; do
if curl -s http://localhost:8080/health | grep -q "OK"; then
echo "Server is ready"
break
fi
echo "Waiting for server... ($i/30)"
sleep 2
done
# Verify we actually became ready
if ! curl -s http://localhost:8080/health | grep -q "OK"; then
echo "Server failed to start. Checking logs..."
ps aux | grep api || true
exit 1
fi
- name: Run API contract tests
uses: schemathesis/action@1f15936316e0742005bf69657b5909ac68f04cb3
with:
schema: ./openapi.yaml
base-url: http://localhost:8080
# Pin version to avoid regressions from newer Schemathesis releases
version: "4.4.4"
# Only test schema conformance
checks: not_a_server_error,status_code_conformance,content_type_conformance,response_schema_conformance
# Only run examples phase for fast, deterministic CI testing.
# For deeper testing (stateful, fuzzing), run locally: make schemathesis
args: >-
--phases examples
-H "Authorization: Bearer test-api-key-for-ci"
- name: Stop API server
if: always()
run: |
if [ ! -z "$API_PID" ]; then
kill $API_PID || true
fi
pkill -f "./bin/api" || true