|
| 1 | +name: Go Build and Test |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + branches: [main] |
| 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 "CallbackSecret=my-test-secret" >> backup.conf |
| 40 | + echo "ScriptPath=./test-script.sh" >> backup.conf |
| 41 | + echo "#!/bin/bash" > test-script.sh |
| 42 | + echo 'echo "script output"' >> test-script.sh |
| 43 | + chmod +x test-script.sh |
| 44 | +
|
| 45 | + - name: Run integration test |
| 46 | + run: | |
| 47 | + # Start the application in the background |
| 48 | + nohup ./myapp > app.log 2>&1 & |
| 49 | +
|
| 50 | + # Health check - wait for the server to be ready (max 30 seconds) |
| 51 | + for i in {1..30}; do |
| 52 | + if curl -s http://localhost:8080 >/dev/null; then |
| 53 | + echo "Server is ready" |
| 54 | + break |
| 55 | + fi |
| 56 | + echo "Waiting for server to be ready..." |
| 57 | + sleep 1 |
| 58 | + done |
| 59 | +
|
| 60 | + # Prepare and send API request |
| 61 | + SIGNATURE=$(echo -n '{"args":[]}' | openssl dgst -sha256 -hmac "my-test-secret" | sed 's/^.* //') |
| 62 | + RESPONSE=$(curl -s -w "\n%{http_code}" -X POST http://localhost:8080/backup \ |
| 63 | + -H "Content-Type: application/json" \ |
| 64 | + -H "X-Signature: sha256=$SIGNATURE" \ |
| 65 | + -d '{"args":[]}') |
| 66 | +
|
| 67 | + # Extract response body and status code |
| 68 | + RESPONSE_BODY=$(echo "$RESPONSE" | head -n -1) |
| 69 | + STATUS_CODE=$(echo "$RESPONSE" | tail -n 1) |
| 70 | +
|
| 71 | + # Verify response |
| 72 | + if [ "$STATUS_CODE" != "200" ]; then |
| 73 | + echo "Test failed: Expected status code 200, got $STATUS_CODE" |
| 74 | + echo "Response: $RESPONSE_BODY" |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | +
|
| 78 | + if [[ "$RESPONSE_BODY" != *"Backup initiated successfully"* ]]; then |
| 79 | + echo "Test failed: Expected response body to contain 'Backup initiated successfully'" |
| 80 | + echo "Response: $RESPONSE_BODY" |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | +
|
| 84 | + echo "Integration test passed" |
| 85 | +
|
| 86 | + # Cleanup |
| 87 | + pkill -f ./myapp || true |
| 88 | +
|
| 89 | + - name: Output application logs |
| 90 | + if: always() |
| 91 | + run: cat app.log |
0 commit comments