-
Notifications
You must be signed in to change notification settings - Fork 1
575 lines (498 loc) · 19.6 KB
/
Copy pathpath-based-testing.yml
File metadata and controls
575 lines (498 loc) · 19.6 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
name: Path-based Testing
on:
pull_request:
branches: [main, develop]
push:
branches: [main, develop]
concurrency:
group: path-testing-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
# Detect which paths have changed
detect-changes:
name: Detect Changes
runs-on: ubuntu-latest
outputs:
api: ${{ steps.changes.outputs.api }}
app: ${{ steps.changes.outputs.app }}
infra: ${{ steps.changes.outputs.infra }}
opa: ${{ steps.changes.outputs.opa }}
k8s: ${{ steps.changes.outputs.k8s }}
docs: ${{ steps.changes.outputs.docs }}
root: ${{ steps.changes.outputs.root }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check for changes
uses: dorny/paths-filter@v2
id: changes
with:
filters: |
api:
- 'projects/consumer-accounts-internal-api/**'
app:
- 'projects/consumer-accounts-internal-app/**'
infra:
- 'infra/**'
opa:
- 'infra/opa/**'
k8s:
- 'infra/k8s/**'
docs:
- '**/*.md'
- 'docs/**'
root:
- 'package.json'
- 'bun.lock'
- 'tsconfig.json'
- '.github/**'
# API-specific tests
test-api:
name: Test API
runs-on: ubuntu-latest
needs: detect-changes
if: needs.detect-changes.outputs.api == 'true' || needs.detect-changes.outputs.root == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Environment
uses: ./.github/actions/setup-environment
- name: Setup Test Services
uses: ./.github/actions/setup-services
with:
setup-opa: "true"
- name: Load OPA test data
run: |
cd infra/opa
# Load user data into OPA
curl -X PUT http://localhost:8181/v1/data/users \
-H "Content-Type: application/json" \
-d "$(jq '.users' data/users.json)"
curl -X PUT http://localhost:8181/v1/data/user_accounts \
-H "Content-Type: application/json" \
-d "$(jq '.user_accounts' data/users.json)"
curl -X PUT http://localhost:8181/v1/data/roles \
-H "Content-Type: application/json" \
-d "$(jq '.roles' data/users.json)"
curl -X PUT http://localhost:8181/v1/data/departments \
-H "Content-Type: application/json" \
-d "$(jq '.departments' data/users.json)"
echo "✅ OPA test data loaded"
- name: Type check API
working-directory: ./projects/consumer-accounts-internal-api
run: bun run tsc --noEmit
- name: Run Biome checks on API
working-directory: ./projects/consumer-accounts-internal-api
run: |
# Run Biome check on API-specific files
bunx biome check src/ tests/
- name: Run API unit tests
working-directory: ./projects/consumer-accounts-internal-api
run: bun run test:coverage
env:
NODE_ENV: test
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/ea_financial_test
REDIS_URL: redis://localhost:6379
# TODO: Re-enable integration tests once issues are resolved
# - name: Run API integration tests
# working-directory: ./projects/consumer-accounts-internal-api
# run: bun run test:offline
# env:
# NODE_ENV: test
- name: Build API
working-directory: ./projects/consumer-accounts-internal-api
run: bun run build
- name: Upload API coverage
uses: codecov/codecov-action@v3
with:
file: ./projects/consumer-accounts-internal-api/coverage/lcov.info
flags: api
name: api-coverage
# App-specific tests
test-app:
name: Test App
runs-on: ubuntu-latest
needs: detect-changes
if: needs.detect-changes.outputs.app == 'true' || needs.detect-changes.outputs.root == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Environment
uses: ./.github/actions/setup-environment
- name: Type check App
working-directory: ./projects/consumer-accounts-internal-app
run: bun run typecheck
- name: Run Biome checks on App
working-directory: ./projects/consumer-accounts-internal-app
run: |
# Run Biome check on app-specific files
bunx biome check src/
- name: Build App
working-directory: ./projects/consumer-accounts-internal-app
run: bun run build
- name: Test App build artifacts
working-directory: ./projects/consumer-accounts-internal-app
run: |
if [ ! -d "dist" ]; then
echo "Build failed - dist directory not found"
exit 1
fi
if [ ! -f "dist/index.html" ]; then
echo "Build failed - index.html not found"
exit 1
fi
echo "App build artifacts validated successfully"
# Infrastructure tests
test-infrastructure:
name: Test Infrastructure
runs-on: ubuntu-latest
needs: detect-changes
if: needs.detect-changes.outputs.infra == 'true' || needs.detect-changes.outputs.k8s == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup kubeconform
run: |
curl -L https://github.com/yannh/kubeconform/releases/latest/download/kubeconform-linux-amd64.tar.gz | tar xz
sudo mv kubeconform /usr/local/bin/
kubeconform -v
- name: Setup Helm
uses: azure/setup-helm@v3
with:
version: "3.12.0"
- name: Validate Kubernetes manifests
run: |
cd infra/k8s
# Validate all YAML files with kubeconform
find . -name "*.yaml" -o -name "*.yml" | xargs kubeconform -summary -output json
- name: Lint Helm charts
run: |
cd infra/k8s
if [ -d "helm-chart" ]; then
helm lint helm-chart/
fi
- name: Test Docker Compose
run: |
cd infra
if [ -f "docker-compose.dev.yml" ]; then
docker compose -f docker-compose.dev.yml config
fi
# OPA policy tests
test-opa-policies:
name: Test OPA Policies
runs-on: ubuntu-latest
needs: detect-changes
if: needs.detect-changes.outputs.opa == 'true' || needs.detect-changes.outputs.api == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Environment
uses: ./.github/actions/setup-environment
- name: Install OPA CLI
run: |
curl -L -o opa https://github.com/open-policy-agent/opa/releases/latest/download/opa_linux_amd64_static
chmod +x opa
sudo mv opa /usr/local/bin/
- name: Validate OPA policies
run: |
cd infra/opa
opa fmt --diff policies/
opa test policies/
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Start OPA server with policies
run: |
cd infra/opa
# Start OPA server with policies in background
opa run --server --addr localhost:8181 policies/ &
OPA_PID=$!
echo "OPA PID: $OPA_PID"
# Wait for OPA to be healthy
timeout 30 bash -c 'until curl -s http://localhost:8181/health > /dev/null; do sleep 1; done' || (echo "OPA failed to start" && exit 1)
echo "OPA server started successfully"
# Load user data into OPA (load each section separately)
curl -X PUT http://localhost:8181/v1/data/users \
-H "Content-Type: application/json" \
-d "$(jq '.users' data/users.json)"
curl -X PUT http://localhost:8181/v1/data/user_accounts \
-H "Content-Type: application/json" \
-d "$(jq '.user_accounts' data/users.json)"
curl -X PUT http://localhost:8181/v1/data/roles \
-H "Content-Type: application/json" \
-d "$(jq '.roles' data/users.json)"
curl -X PUT http://localhost:8181/v1/data/departments \
-H "Content-Type: application/json" \
-d "$(jq '.departments' data/users.json)"
echo "OPA data loaded successfully"
- name: Run API server in background
working-directory: ./projects/consumer-accounts-internal-api
run: |
bun run start &
API_PID=$!
echo "API PID: $API_PID"
# Wait for API to be healthy
timeout 30 bash -c 'until curl -s http://localhost:3001/health > /dev/null; do sleep 1; done' || (echo "API failed to start" && exit 1)
echo "API server started successfully"
env:
NODE_ENV: test
OPA_URL: http://localhost:8181
- name: Verify services are ready
run: |
echo "Checking OPA health..."
curl -v http://localhost:8181/health || echo "OPA health check failed"
echo "Checking API health..."
curl -v http://localhost:3001/health || echo "API health check failed"
echo "Checking OPA data..."
curl -s http://localhost:8181/v1/data/users | jq '.' || echo "OPA data check failed"
- name: Run OPA integration tests
working-directory: ./projects/consumer-accounts-internal-api
run: bun run test:opa
# Container builds for changed components
build-containers:
name: Build Containers
runs-on: ubuntu-latest
needs: detect-changes
if: |
needs.detect-changes.outputs.api == 'true' ||
needs.detect-changes.outputs.app == 'true' ||
needs.detect-changes.outputs.root == 'true'
strategy:
matrix:
include:
- component: api
condition: ${{ needs.detect-changes.outputs.api == 'true' || needs.detect-changes.outputs.root == 'true' }}
context: .
dockerfile: ./projects/consumer-accounts-internal-api/Dockerfile
- component: app
condition: ${{ needs.detect-changes.outputs.app == 'true' || needs.detect-changes.outputs.root == 'true' }}
context: .
dockerfile: ./projects/consumer-accounts-internal-app/Dockerfile
steps:
- name: Checkout code
if: matrix.condition
uses: actions/checkout@v4
- name: Set up Docker Buildx
if: matrix.condition
uses: docker/setup-buildx-action@v3
- name: Debug - Verify repository structure
if: matrix.condition
run: |
echo "=== Building ${{ matrix.component }} ==="
echo "Context: ${{ matrix.context }}"
echo "Dockerfile: ${{ matrix.dockerfile }}"
echo ""
echo "=== Current directory ==="
pwd
echo ""
echo "=== Root directory contents ==="
ls -la
echo ""
echo "=== Projects directory ==="
ls -la projects/ || echo "projects/ directory not found!"
echo ""
echo "=== ${{ matrix.component }} project ==="
ls -la projects/consumer-accounts-internal-${{ matrix.component }}/ || echo "${{ matrix.component }} directory not found!"
- name: Build container image
if: matrix.condition
uses: docker/build-push-action@v5
with:
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
tags: ea-financial/${{ matrix.component }}:test-${{ github.sha }}
push: false
load: true
cache-from: type=gha,scope=${{ matrix.component }}
cache-to: type=gha,mode=max,scope=${{ matrix.component }}
- name: Test container startup
if: matrix.condition
run: |
# Test that the container can start successfully
echo "Starting ${{ matrix.component }} container..."
CONTAINER_ID=$(docker run -d --name test-${{ matrix.component }} \
ea-financial/${{ matrix.component }}:test-${{ github.sha }})
echo "Container ID: $CONTAINER_ID"
# Wait a moment for container to initialize
sleep 5
# Check if container is still running (didn't crash)
CONTAINER_STATUS=$(docker inspect --format='{{.State.Status}}' $CONTAINER_ID)
if [ "$CONTAINER_STATUS" = "running" ]; then
echo "✅ ${{ matrix.component }} container started successfully"
docker stop test-${{ matrix.component }}
docker rm test-${{ matrix.component }}
else
echo "❌ ${{ matrix.component }} container failed to start (status: $CONTAINER_STATUS)"
echo "Container logs:"
docker logs test-${{ matrix.component }} 2>&1 || echo "Could not retrieve logs"
echo "Container status details:"
docker inspect $CONTAINER_ID | jq '.[0].State' || true
docker rm test-${{ matrix.component }} 2>/dev/null || true
exit 1
fi
# Documentation checks
check-documentation:
name: Check Documentation
runs-on: ubuntu-latest
needs: detect-changes
if: needs.detect-changes.outputs.docs == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Environment
uses: ./.github/actions/setup-environment
- name: Install markdown linter
run: bun install -g markdownlint-cli
- name: Lint markdown files
run: bunx markdownlint **/*.md --ignore node_modules
- name: Check for broken links
run: |
bunx markdown-link-check README.md
find . -name "*.md" -not -path "./node_modules/*" -not -name "README.md" | head -5 | xargs -I {} bunx markdown-link-check {}
- name: Validate README structure
run: |
if [ ! -f "README.md" ]; then
echo "Missing root README.md"
exit 1
fi
if [ ! -f "projects/consumer-accounts-internal-api/README.md" ]; then
echo "Missing API README.md"
exit 1
fi
if [ ! -f "projects/consumer-accounts-internal-app/README.md" ]; then
echo "Missing App README.md"
exit 1
fi
# Security scan for changed components
security-scan-changes:
name: Security Scan Changes
runs-on: ubuntu-latest
needs: detect-changes
if: |
needs.detect-changes.outputs.api == 'true' ||
needs.detect-changes.outputs.app == 'true' ||
needs.detect-changes.outputs.root == 'true'
permissions:
contents: read
security-events: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run Trivy filesystem scan
uses: aquasecurity/trivy-action@v0.35.0
with:
scan-type: "fs"
scan-ref: "."
format: "sarif"
output: "trivy-results.sarif"
severity: "CRITICAL,HIGH"
- name: Upload Trivy scan results
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: "trivy-results.sarif"
- name: Check for sensitive files
run: |
echo "Checking for accidentally committed sensitive files..."
if find . -name "*.pem" -o -name "*.key" -o -name "*.p12" -o -name "*.jks" | grep -v node_modules; then
echo "❌ Potential sensitive files found!"
exit 1
fi
echo "✅ No sensitive files detected"
# Performance impact analysis
performance-impact:
name: Performance Impact Analysis
runs-on: ubuntu-latest
needs: [detect-changes, test-api]
if: needs.detect-changes.outputs.api == 'true' && needs.test-api.result == 'success'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Environment
uses: ./.github/actions/setup-environment
- name: Start API server
working-directory: ./projects/consumer-accounts-internal-api
run: |
bun run start &
sleep 10
env:
NODE_ENV: test
- name: Install Artillery
run: bun install -g artillery@latest
- name: Run lightweight performance test
run: |
cat > quick-perf-test.yml << 'EOF'
config:
target: 'http://localhost:3001'
phases:
- duration: 30
arrivalRate: 5
scenarios:
- name: "Quick health check"
flow:
- get:
url: "/health"
EOF
artillery run quick-perf-test.yml --output perf-results.json
- name: Analyze performance results
run: |
artillery report perf-results.json --output perf-report.html
# Extract key metrics and comment on PR if significant changes
echo "Performance test completed. Check artifacts for detailed report."
- name: Upload performance report
uses: actions/upload-artifact@v4
with:
name: performance-impact-${{ github.sha }}
path: perf-report.html
# Summary job
path-based-testing-summary:
name: Path-based Testing Summary
runs-on: ubuntu-latest
needs:
[
detect-changes,
test-api,
test-app,
test-infrastructure,
test-opa-policies,
build-containers,
check-documentation,
security-scan-changes,
performance-impact,
]
if: always()
steps:
- name: Generate summary
run: |
echo "## Path-based Testing Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Changes Detected:" >> $GITHUB_STEP_SUMMARY
echo "- API: ${{ needs.detect-changes.outputs.api }}" >> $GITHUB_STEP_SUMMARY
echo "- App: ${{ needs.detect-changes.outputs.app }}" >> $GITHUB_STEP_SUMMARY
echo "- Infrastructure: ${{ needs.detect-changes.outputs.infra }}" >> $GITHUB_STEP_SUMMARY
echo "- OPA Policies: ${{ needs.detect-changes.outputs.opa }}" >> $GITHUB_STEP_SUMMARY
echo "- K8s: ${{ needs.detect-changes.outputs.k8s }}" >> $GITHUB_STEP_SUMMARY
echo "- Documentation: ${{ needs.detect-changes.outputs.docs }}" >> $GITHUB_STEP_SUMMARY
echo "- Root files: ${{ needs.detect-changes.outputs.root }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Test Results:" >> $GITHUB_STEP_SUMMARY
echo "- API Tests: ${{ needs.test-api.result || 'skipped' }}" >> $GITHUB_STEP_SUMMARY
echo "- App Tests: ${{ needs.test-app.result || 'skipped' }}" >> $GITHUB_STEP_SUMMARY
echo "- Infrastructure Tests: ${{ needs.test-infrastructure.result || 'skipped' }}" >> $GITHUB_STEP_SUMMARY
echo "- OPA Policy Tests: ${{ needs.test-opa-policies.result || 'skipped' }}" >> $GITHUB_STEP_SUMMARY
echo "- Container Builds: ${{ needs.build-containers.result || 'skipped' }}" >> $GITHUB_STEP_SUMMARY
echo "- Documentation Checks: ${{ needs.check-documentation.result || 'skipped' }}" >> $GITHUB_STEP_SUMMARY
echo "- Security Scan: ${{ needs.security-scan-changes.result || 'skipped' }}" >> $GITHUB_STEP_SUMMARY
echo "- Performance Impact: ${{ needs.performance-impact.result || 'skipped' }}" >> $GITHUB_STEP_SUMMARY
- name: Check critical failures
run: |
if [[ "${{ needs.test-api.result }}" == "failure" ||
"${{ needs.test-app.result }}" == "failure" ||
"${{ needs.security-scan-changes.result }}" == "failure" ]]; then
echo "❌ Critical tests failed!"
exit 1
else
echo "✅ All critical path-based tests passed or were skipped appropriately"
fi