ci(workflow): 更新测试工作流以使用 docker-backup 脚本并列出备份产物 #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Go Callback Handler Test | |
on: | |
push: | |
branches: [main, go-callback] | |
pull_request: | |
branches: [main, go-callback] | |
jobs: | |
build-and-test: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Go | |
uses: actions/setup-go@v5 | |
with: | |
go-version: "1.22" | |
- name: Cache Go modules and build | |
uses: actions/cache@v4 | |
with: | |
path: | | |
~/go/pkg/mod | |
~/.cache/go-build | |
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | |
- name: Install dependencies | |
run: go mod download | |
- name: Build application | |
run: go build -v -o ./myapp ./go/callback/main.go | |
- name: Create test files | |
run: | | |
echo "port=8080" > backup.conf | |
echo "callback_secret=my-test-secret" >> backup.conf | |
echo "scriptpath=./docker-backup.sh" >> backup.conf | |
- name: Run integration test | |
run: | | |
# Start the callback handler application in the background | |
nohup ./myapp > app.log 2>&1 & | |
# Health check - wait for the server to be ready (max 30 seconds) | |
for i in {1..30}; do | |
if curl -s http://localhost:8080 >/dev/null; then | |
echo "Server is ready" | |
break | |
fi | |
echo "Waiting for server to be ready..." | |
sleep 1 | |
done | |
# Prepare and send a simulated external callback (POST request) | |
SIGNATURE=$(echo -n '{"args":[]}' | openssl dgst -sha256 -hmac "my-test-secret" | sed 's/^.* //') | |
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST http://localhost:8080/backup \ | |
-H "Content-Type: application/json" \ | |
-H "X-Signature: sha256=$SIGNATURE" \ | |
-d '{"args":[]}') | |
# Extract response body and status code | |
RESPONSE_BODY=$(echo "$RESPONSE" | head -n -1) | |
STATUS_CODE=$(echo "$RESPONSE" | tail -n 1) | |
# Verify response | |
if [ "$STATUS_CODE" != "200" ]; then | |
echo "Test failed: Expected status code 200, got $STATUS_CODE" | |
echo "Response: $RESPONSE_BODY" | |
exit 1 | |
fi | |
if [[ "$RESPONSE_BODY" != *"Backup initiated successfully"* ]]; then | |
echo "Test failed: Expected response body to contain 'Backup initiated successfully'" | |
echo "Response: $RESPONSE_BODY" | |
exit 1 | |
fi | |
echo "Callback handler test passed" | |
# Cleanup | |
killall myapp || true | |
- name: Output application logs | |
if: always() | |
run: cat app.log | |
- name: List backup artifacts (if any) | |
if: always() | |
run: | | |
echo "=== Listing Docker backup artifacts ===" | |
# List directories that match the backup pattern created by docker-backup.sh | |
echo "Backup directories:" | |
ls -la /tmp/docker-backups/ 2>/dev/null || echo "No backup directory found at /tmp/docker-backups/" | |
# If backup directories exist, show their contents | |
if [[ -d "/tmp/docker-backups" ]]; then | |
for backup_dir in /tmp/docker-backups/*/; do | |
if [[ -d "$backup_dir" ]]; then | |
echo "Contents of $backup_dir:" | |
ls -la "$backup_dir" | |
echo "---" | |
fi | |
done | |
fi | |
echo "=== End of Docker backup artifact listing ===" |