|
| 1 | +#!/bin/bash |
| 2 | +# Test script for CloudEvents webhook |
| 3 | +# Usage: ./test-webhook.sh <webhook-url> <secret> |
| 4 | + |
| 5 | +set -e |
| 6 | + |
| 7 | +WEBHOOK_URL="${1:-http://localhost:8080/webhook?type=cloudevents}" |
| 8 | +SECRET="${2:-test-secret}" |
| 9 | + |
| 10 | +echo "Testing CloudEvents webhook..." |
| 11 | +echo "URL: ${WEBHOOK_URL}" |
| 12 | +echo "" |
| 13 | + |
| 14 | +# Test 1: ECR event |
| 15 | +echo "Test 1: AWS ECR push event" |
| 16 | +curl -X POST "${WEBHOOK_URL}" \ |
| 17 | + -H "Content-Type: application/json" \ |
| 18 | + -H "X-Webhook-Secret: ${SECRET}" \ |
| 19 | + -d '{ |
| 20 | + "specversion": "1.0", |
| 21 | + "id": "test-ecr-event-001", |
| 22 | + "type": "com.amazon.ecr.image.push", |
| 23 | + "source": "urn:aws:ecr:us-east-1:123456789012:repository/my-app", |
| 24 | + "subject": "my-app:v1.2.3", |
| 25 | + "time": "2025-11-27T10:00:00Z", |
| 26 | + "datacontenttype": "application/json", |
| 27 | + "data": { |
| 28 | + "repositoryName": "my-app", |
| 29 | + "imageDigest": "sha256:abcdef1234567890", |
| 30 | + "imageTag": "v1.2.3", |
| 31 | + "registryId": "123456789012" |
| 32 | + } |
| 33 | + }' \ |
| 34 | + -w "\nHTTP Status: %{http_code}\n\n" |
| 35 | + |
| 36 | +# Test 2: Generic container event |
| 37 | +echo "Test 2: Generic container push event" |
| 38 | +curl -X POST "${WEBHOOK_URL}" \ |
| 39 | + -H "Content-Type: application/cloudevents+json" \ |
| 40 | + -H "X-Webhook-Secret: ${SECRET}" \ |
| 41 | + -d '{ |
| 42 | + "specversion": "1.0", |
| 43 | + "id": "test-generic-event-001", |
| 44 | + "type": "container.image.push", |
| 45 | + "source": "https://registry.example.com", |
| 46 | + "subject": "myapp/backend:v2.0.0", |
| 47 | + "time": "2025-11-27T10:05:00Z", |
| 48 | + "datacontenttype": "application/json", |
| 49 | + "data": { |
| 50 | + "repository": "myapp/backend", |
| 51 | + "tag": "v2.0.0", |
| 52 | + "digest": "sha256:fedcba0987654321", |
| 53 | + "registryUrl": "registry.example.com" |
| 54 | + } |
| 55 | + }' \ |
| 56 | + -w "\nHTTP Status: %{http_code}\n\n" |
| 57 | + |
| 58 | +# Test 3: Missing secret (should fail) |
| 59 | +echo "Test 3: Missing secret (expected to fail)" |
| 60 | +curl -X POST "${WEBHOOK_URL}" \ |
| 61 | + -H "Content-Type: application/json" \ |
| 62 | + -d '{ |
| 63 | + "specversion": "1.0", |
| 64 | + "id": "test-fail-event-001", |
| 65 | + "type": "com.amazon.ecr.image.push", |
| 66 | + "source": "urn:aws:ecr:us-east-1:123456789012:repository/test", |
| 67 | + "subject": "test:latest", |
| 68 | + "data": { |
| 69 | + "repositoryName": "test", |
| 70 | + "imageTag": "latest", |
| 71 | + "registryId": "123456789012" |
| 72 | + } |
| 73 | + }' \ |
| 74 | + -w "\nHTTP Status: %{http_code}\n\n" || echo "Expected failure" |
| 75 | + |
| 76 | +# Test 4: Invalid event type (should fail) |
| 77 | +echo "Test 4: Invalid event type (expected to fail)" |
| 78 | +curl -X POST "${WEBHOOK_URL}" \ |
| 79 | + -H "Content-Type: application/json" \ |
| 80 | + -H "X-Webhook-Secret: ${SECRET}" \ |
| 81 | + -d '{ |
| 82 | + "specversion": "1.0", |
| 83 | + "id": "test-invalid-event-001", |
| 84 | + "type": "com.example.database.updated", |
| 85 | + "source": "https://db.example.com", |
| 86 | + "data": { |
| 87 | + "table": "users" |
| 88 | + } |
| 89 | + }' \ |
| 90 | + -w "\nHTTP Status: %{http_code}\n\n" || echo "Expected failure" |
| 91 | + |
| 92 | +echo "Testing complete!" |
0 commit comments