feat: CI/CD integration testing with network access #23

Closed
poimen wants to merge 5 commits from feat/ci-integration-testing-workflow into main
Showing only changes of commit fa9938df8e - Show all commits
+37 -37
View File
@@ -43,13 +43,6 @@ jobs:
REGISTRY_USER: ${{ secrets.FORGEJO_REGISTRY_USER }}
REGISTRY_TOKEN: ${{ secrets.FORGEJO_REGISTRY_TOKEN }}
- name: Test build only (unit tests)
run: |
echo "Running unit tests..."
go test ./...
echo "Running static analysis..."
go vet ./...
- name: Build Docker image
run: |
docker build --no-cache \
@@ -62,7 +55,7 @@ jobs:
docker push "${IMAGE}:${{ steps.sha.outputs.short_sha }}"
echo "✓ Pushed test image: ${IMAGE}:${{ steps.sha.outputs.short_sha }}"
- name: Setup kubeconfig for test pod
- name: Setup kubeconfig for integration test job
run: |
mkdir -p ~/.kube
echo "${KUBECONFIG_B64}" | base64 -d > ~/.kube/config
@@ -70,37 +63,42 @@ jobs:
KUBECONFIG_B64: ${{ secrets.KUBECONFIG_B64 }}
continue-on-error: true
- name: Verify kubectl availability
- name: Run integration tests via Kubernetes Job
run: |
kubectl version --client || (echo "ERROR: kubectl not available" && exit 1)
echo "Running integration tests via Kubernetes Job..."
echo "Image: ${IMAGE}:${{ steps.sha.outputs.short_sha }}"
- name: Deploy test pod from new image
run: |
echo "Deploying test pod with new image: ${IMAGE}:${{ steps.sha.outputs.short_sha }}"
kubectl run api-gateway-test-${{ steps.sha.outputs.short_sha }} \
--image="${IMAGE}:${{ steps.sha.outputs.short_sha }}" \
--namespace=api \
--restart=Never \
--port=8080 \
--labels="app=api-gateway,managed-by=argocd,role=test,test-run=${{ steps.sha.outputs.short_sha }}" \
--overrides='{"spec":{"containers":[{"name":"gateway","securityContext":{"runAsNonRoot":true,"runAsUser":65532,"allowPrivilegeEscalation":false}}]}}'
# Apply job template from repo
kubectl apply -f k8s/integration-test-job.yaml
echo "Waiting for test pod to be ready..."
kubectl wait --for=condition=ready pod -l run=api-gateway-test-${{ steps.sha.outputs.short_sha }} -n api --timeout=60s
continue-on-error: true
# Update job to use new image
kubectl set image job/api-gateway-integration-test \
api-gateway="${IMAGE}:${{ steps.sha.outputs.short_sha }}" \
-n api --record
- name: Run integration tests inside test pod
run: |
echo "Running integration tests inside test pod..."
echo "Test pod: api-gateway-test-${{ steps.sha.outputs.short_sha }}"
sleep 5
# Wait for job to complete (max 5 minutes)
echo "Waiting for job to complete..."
kubectl wait --for=condition=complete job/api-gateway-integration-test \
-n api --timeout=5m || true
# Run tests from inside the pod
# Pod has network access to all services via network policy labels
kubectl exec -n api pod/api-gateway-test-${{ steps.sha.outputs.short_sha }} -- \
go test -v -tags=integration -timeout=5m ./internal/integration/...
env:
GATEWAY_URL: http://localhost:8080
# Stream logs
echo "Job logs:"
kubectl logs -n api job/api-gateway-integration-test --all-containers=true --timestamps=true || true
# Check if job succeeded
SUCCEEDED=$(kubectl get job api-gateway-integration-test -n api -o jsonpath='{.status.succeeded}')
FAILED=$(kubectl get job api-gateway-integration-test -n api -o jsonpath='{.status.failed}')
echo ""
echo "Job Status: Succeeded=$SUCCEEDED, Failed=$FAILED"
if [ "$SUCCEEDED" = "1" ]; then
echo "✓ Integration tests PASSED"
exit 0
else
echo "✗ Integration tests FAILED"
exit 1
fi
continue-on-error: false
- name: Promote image to latest (only if tests passed)
@@ -111,11 +109,13 @@ jobs:
docker push "${IMAGE}:latest"
echo "✓ Promoted ${IMAGE}:${{ steps.sha.outputs.short_sha }} to latest"
- name: Cleanup test pod
- name: Cleanup integration test job
if: always()
run: |
kubectl delete pod api-gateway-test-${{ steps.sha.outputs.short_sha }} -n api 2>/dev/null || true
echo "Cleaning up test job..."
kubectl delete job api-gateway-integration-test -n api --ignore-not-found=true
continue-on-error: true
- name: Prune unused images
- name: Cleanup docker
if: always()
run: docker image prune -a --force 2>&1 | tail -3 || true