|
| 1 | +name: Go Callback Handler Test |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, go-callback] |
| 6 | + pull_request: |
| 7 | + branches: [main, go-callback] |
| 8 | + |
| 9 | +jobs: |
| 10 | + build-and-test: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + steps: |
| 14 | + - name: Checkout code |
| 15 | + uses: actions/checkout@v4 |
| 16 | + |
| 17 | + - name: Set up Go |
| 18 | + uses: actions/setup-go@v5 |
| 19 | + with: |
| 20 | + go-version: "1.22" |
| 21 | + |
| 22 | + - name: Cache Go modules and build |
| 23 | + uses: actions/cache@v4 |
| 24 | + with: |
| 25 | + path: | |
| 26 | + ~/go/pkg/mod |
| 27 | + ~/.cache/go-build |
| 28 | + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} |
| 29 | + |
| 30 | + - name: Install dependencies |
| 31 | + run: go mod download |
| 32 | + |
| 33 | + - name: Build application |
| 34 | + run: go build -v -o ./myapp ./go/callback/main.go |
| 35 | + |
| 36 | + - name: Create test files |
| 37 | + run: | |
| 38 | + echo "port=8080" > backup.conf |
| 39 | + echo "callback_secret=my-test-secret" >> backup.conf |
| 40 | + echo "scriptpath=./docker-backup.sh" >> backup.conf |
| 41 | +
|
| 42 | + - name: Run integration test |
| 43 | + run: | |
| 44 | + # Start a test container for backup |
| 45 | + echo "Starting a test Docker container..." |
| 46 | + docker run -d --name test-container nginx:alpine |
| 47 | + echo "Test container started." |
| 48 | +
|
| 49 | + # Start the callback handler application in the background |
| 50 | + nohup ./myapp > app.log 2>&1 & |
| 51 | +
|
| 52 | + # Health check - wait for the server to be ready (max 30 seconds) |
| 53 | + for i in {1..30}; do |
| 54 | + if curl -s http://localhost:8080 >/dev/null; then |
| 55 | + echo "Server is ready" |
| 56 | + break |
| 57 | + fi |
| 58 | + echo "Waiting for server to be ready..." |
| 59 | + sleep 1 |
| 60 | + done |
| 61 | +
|
| 62 | + # Prepare and send a simulated external callback (POST request) |
| 63 | + # Include the test container name in the args |
| 64 | + SIGNATURE=$(echo -n '{"args":["test-container"]}' | openssl dgst -sha256 -hmac "my-test-secret" | sed 's/^.* //') |
| 65 | + RESPONSE=$(curl -s -w "\n%{http_code}" -X POST http://localhost:8080/backup \ |
| 66 | + -H "Content-Type: application/json" \ |
| 67 | + -H "X-Signature: sha256=$SIGNATURE" \ |
| 68 | + -d '{"args":["test-container"]}') |
| 69 | +
|
| 70 | + # Extract response body and status code |
| 71 | + RESPONSE_BODY=$(echo "$RESPONSE" | head -n -1) |
| 72 | + STATUS_CODE=$(echo "$RESPONSE" | tail -n 1) |
| 73 | +
|
| 74 | + # Verify response |
| 75 | + if [ "$STATUS_CODE" != "200" ]; then |
| 76 | + echo "Test failed: Expected status code 200, got $STATUS_CODE" |
| 77 | + echo "Response: $RESPONSE_BODY" |
| 78 | + # Output app logs for debugging |
| 79 | + echo "=== App logs ===" |
| 80 | + cat app.log |
| 81 | + echo "=== End of app logs ===" |
| 82 | + # Output Docker logs for the test container |
| 83 | + echo "=== Test container logs ===" |
| 84 | + docker logs test-container || true |
| 85 | + echo "=== End of test container logs ===" |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | +
|
| 89 | + if [[ "$RESPONSE_BODY" != *"Backup initiated successfully"* ]]; then |
| 90 | + echo "Test failed: Expected response body to contain 'Backup initiated successfully'" |
| 91 | + echo "Response: $RESPONSE_BODY" |
| 92 | + # Output app logs for debugging |
| 93 | + echo "=== App logs ===" |
| 94 | + cat app.log |
| 95 | + echo "=== End of app logs ===" |
| 96 | + # Output Docker logs for the test container |
| 97 | + echo "=== Test container logs ===" |
| 98 | + docker logs test-container || true |
| 99 | + echo "=== End of test container logs ===" |
| 100 | + exit 1 |
| 101 | + fi |
| 102 | +
|
| 103 | + echo "Callback handler test passed" |
| 104 | +
|
| 105 | + # Cleanup |
| 106 | + killall myapp || true |
| 107 | + docker rm -f test-container || true |
| 108 | +
|
| 109 | + - name: Output application logs |
| 110 | + if: always() |
| 111 | + run: cat app.log |
| 112 | + |
| 113 | + - name: List backup artifacts (if any) |
| 114 | + if: always() |
| 115 | + run: | |
| 116 | + echo "=== Listing Docker backup artifacts ===" |
| 117 | + # List directories that match the backup pattern created by docker-backup.sh |
| 118 | + echo "Backup directories:" |
| 119 | + ls -la /tmp/docker-backups/ 2>/dev/null || echo "No backup directory found at /tmp/docker-backups/" |
| 120 | +
|
| 121 | + # If backup directories exist, show their contents |
| 122 | + if [[ -d "/tmp/docker-backups" ]]; then |
| 123 | + for backup_dir in /tmp/docker-backups/*/; do |
| 124 | + if [[ -d "$backup_dir" ]]; then |
| 125 | + echo "Contents of $backup_dir:" |
| 126 | + ls -la "$backup_dir" |
| 127 | + echo "---" |
| 128 | + fi |
| 129 | + done |
| 130 | + fi |
| 131 | + echo "=== End of Docker backup artifact listing ===" |
0 commit comments