gitops-status-api/test_api.sh
dvirlabs 4ab7f51c62
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Update json schema
2026-04-26 03:00:42 +03:00

341 lines
9.6 KiB
Bash

#!/bin/bash
# GitOps Status API - Test Script
# Tests all endpoints with realistic examples
set -e
API_URL="${API_URL:-http://localhost:5000}"
echo "=========================================="
echo "GitOps Status API Test Script"
echo "API URL: $API_URL"
echo "=========================================="
echo ""
# Color codes
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
function print_test() {
echo -e "${BLUE}[TEST]${NC} $1"
}
function print_success() {
echo -e "${GREEN}[OK]${NC} $1"
}
function print_section() {
echo ""
echo -e "${YELLOW}========================================${NC}"
echo -e "${YELLOW}$1${NC}"
echo -e "${YELLOW}========================================${NC}"
echo ""
}
# Test 1: Health checks
print_section "1. Health Checks"
print_test "GET /health"
curl -s "$API_URL/health" | jq .
print_success "Health check passed"
echo ""
print_test "GET /ready"
curl -s "$API_URL/ready" | jq .
print_success "Readiness check passed"
echo ""
print_test "GET / (API info)"
curl -s "$API_URL/" | jq .
print_success "API info retrieved"
echo ""
# Test 2: Pipeline updates (rsyslog servers)
print_section "2. Pipeline Updates - rsyslog Servers"
print_test "POST /status/pipeline - rsyslog-prod/rsyslog-01 (synced)"
curl -s -X POST "$API_URL/status/pipeline" \
-H "Content-Type: application/json" \
-d '{
"project_name": "gitops-for-servers",
"repo_name": "rsyslog",
"server_group": "rsyslog-prod",
"server_type": "rsyslog",
"environment": "prod",
"host": "rsyslog-01",
"status": "synced",
"changed_files": [],
"validation_status": "passed",
"validation_message": "rsyslog syntax validation passed",
"commit_sha": "abc1234567890",
"branch": "master",
"updated_by": "woodpecker",
"last_pipeline_run": "2026-04-26T10:30:00Z"
}' | jq .
print_success "rsyslog-01 status updated"
echo ""
print_test "POST /status/pipeline - rsyslog-prod/rsyslog-02 (synced)"
curl -s -X POST "$API_URL/status/pipeline" \
-H "Content-Type: application/json" \
-d '{
"project_name": "gitops-for-servers",
"repo_name": "rsyslog",
"server_group": "rsyslog-prod",
"server_type": "rsyslog",
"environment": "prod",
"host": "rsyslog-02",
"status": "synced",
"changed_files": [],
"validation_status": "passed",
"validation_message": "rsyslog syntax validation passed",
"commit_sha": "abc1234567890",
"branch": "master",
"updated_by": "woodpecker",
"last_pipeline_run": "2026-04-26T10:30:00Z"
}' | jq .
print_success "rsyslog-02 status updated"
echo ""
# Test 3: Pipeline updates (other server types)
print_section "3. Pipeline Updates - Other Server Types"
print_test "POST /status/pipeline - Splunk indexer"
curl -s -X POST "$API_URL/status/pipeline" \
-H "Content-Type: application/json" \
-d '{
"project_name": "gitops-for-servers",
"repo_name": "splunk",
"server_group": "splunk-prod",
"server_type": "splunk",
"environment": "prod",
"host": "splunk-indexer-01",
"status": "synced",
"changed_files": [],
"validation_status": "passed",
"validation_message": "Splunk configuration validated",
"commit_sha": "def4567890123",
"branch": "main",
"updated_by": "jenkins",
"last_pipeline_run": "2026-04-26T10:35:00Z"
}' | jq .
print_success "Splunk indexer status updated"
echo ""
print_test "POST /status/pipeline - nginx web server"
curl -s -X POST "$API_URL/status/pipeline" \
-H "Content-Type: application/json" \
-d '{
"project_name": "gitops-for-servers",
"repo_name": "nginx",
"server_group": "nginx-prod",
"server_type": "nginx",
"environment": "prod",
"host": "nginx-web-01",
"status": "synced",
"changed_files": [],
"validation_status": "passed",
"validation_message": "nginx -t passed",
"commit_sha": "ghi7890123456",
"branch": "master",
"updated_by": "gitlab-ci",
"last_pipeline_run": "2026-04-26T10:40:00Z"
}' | jq .
print_success "nginx web server status updated"
echo ""
print_test "POST /status/pipeline - IBM ITNM"
curl -s -X POST "$API_URL/status/pipeline" \
-H "Content-Type: application/json" \
-d '{
"project_name": "gitops-for-servers",
"repo_name": "ibm-itnm",
"server_group": "itnm-prod",
"server_type": "ibm-itnm",
"environment": "prod",
"host": "itnm-server-01",
"status": "synced",
"changed_files": [],
"validation_status": "passed",
"validation_message": "ITNM config validation successful",
"commit_sha": "jkl0123456789",
"branch": "production",
"updated_by": "woodpecker",
"last_pipeline_run": "2026-04-26T10:45:00Z"
}' | jq .
print_success "IBM ITNM status updated"
echo ""
# Test 4: Pipeline update with failure
print_section "4. Pipeline Update - Failed Deployment"
print_test "POST /status/pipeline - rsyslog-staging/rsyslog-test-01 (failed)"
curl -s -X POST "$API_URL/status/pipeline" \
-H "Content-Type: application/json" \
-d '{
"project_name": "gitops-for-servers",
"repo_name": "rsyslog",
"server_group": "rsyslog-staging",
"server_type": "rsyslog",
"environment": "staging",
"host": "rsyslog-test-01",
"status": "failed",
"changed_files": ["/etc/rsyslog.conf"],
"validation_status": "failed",
"validation_message": "rsyslog: syntax error in /etc/rsyslog.conf line 42",
"commit_sha": "bad1234567890",
"branch": "feature/new-config",
"updated_by": "developer",
"last_pipeline_run": "2026-04-26T10:50:00Z"
}' | jq .
print_success "Failed deployment recorded"
echo ""
# Test 5: Cron updates (drift detection)
print_section "5. Cron Updates - Drift Detection"
print_test "POST /status/cron - rsyslog-01 (no drift)"
curl -s -X POST "$API_URL/status/cron" \
-H "Content-Type: application/json" \
-d '{
"server_group": "rsyslog-prod",
"host": "rsyslog-01",
"status": "synced",
"changed_files": [],
"last_cron_check": "2026-04-26T11:00:00Z"
}' | jq .
print_success "Cron check - no drift"
echo ""
print_test "POST /status/cron - rsyslog-02 (drift detected)"
curl -s -X POST "$API_URL/status/cron" \
-H "Content-Type: application/json" \
-d '{
"server_group": "rsyslog-prod",
"host": "rsyslog-02",
"status": "out_of_sync",
"changed_files": ["/etc/rsyslog.conf", "/etc/rsyslog.d/50-default.conf"],
"last_cron_check": "2026-04-26T11:00:00Z"
}' | jq .
print_success "Cron check - drift detected"
echo ""
# Test 6: Query endpoints
print_section "6. Query Endpoints - GET Status"
print_test "GET /status (all servers)"
curl -s "$API_URL/status" | jq .
print_success "All servers retrieved"
echo ""
print_test "GET /status/rsyslog-prod (server group)"
curl -s "$API_URL/status/rsyslog-prod" | jq .
print_success "rsyslog-prod group retrieved"
echo ""
print_test "GET /status/rsyslog-prod/rsyslog-01 (specific server)"
curl -s "$API_URL/status/rsyslog-prod/rsyslog-01" | jq .
print_success "Specific server retrieved"
echo ""
print_test "GET /status/splunk-prod (splunk group)"
curl -s "$API_URL/status/splunk-prod" | jq .
print_success "splunk-prod group retrieved"
echo ""
# Test 7: Legacy API (backward compatibility)
print_section "7. Legacy API - Backward Compatibility"
print_test "POST /api/status (old format)"
curl -s -X POST "$API_URL/api/status" \
-H "Content-Type: application/json" \
-d '{
"repo": "rsyslog",
"server": "rsyslog-legacy",
"sync_status": "SYNCED",
"drift_count": 0,
"deployed_files": [{"name": "rsyslog.conf"}],
"drifted_files": [],
"last_check": "2026-04-26T11:15:00Z"
}' | jq .
print_success "Legacy format accepted"
echo ""
print_test "GET /api/status (old format response)"
curl -s "$API_URL/api/status" | jq .
print_success "Legacy format returned"
echo ""
# Test 8: Error cases
print_section "8. Error Handling"
print_test "POST /status/pipeline - missing required fields"
curl -s -X POST "$API_URL/status/pipeline" \
-H "Content-Type: application/json" \
-d '{
"server_group": "test"
}' | jq .
print_success "Validation error handled"
echo ""
print_test "POST /status/cron - server not found"
curl -s -X POST "$API_URL/status/cron" \
-H "Content-Type: application/json" \
-d '{
"server_group": "nonexistent",
"host": "ghost-server"
}' | jq .
print_success "Server not found error handled"
echo ""
print_test "GET /status/nonexistent-group"
curl -s "$API_URL/status/nonexistent-group" | jq .
print_success "Group not found error handled"
echo ""
# Test 9: Grafana-friendly output check
print_section "9. Grafana Infinity Output Validation"
print_test "GET /status - Check output is flat array"
RESPONSE=$(curl -s "$API_URL/status")
IS_ARRAY=$(echo "$RESPONSE" | jq -r 'if type == "array" then "true" else "false" end')
if [ "$IS_ARRAY" == "true" ]; then
print_success "Output is a flat array (Grafana compatible)"
# Check if each element has required fields
HAS_FIELDS=$(echo "$RESPONSE" | jq -r '.[0] | has("server_group") and has("host") and has("status")')
if [ "$HAS_FIELDS" == "true" ]; then
print_success "Array elements have required fields"
else
echo "Warning: Array elements missing required fields"
fi
else
echo "Error: Output is not an array"
fi
echo ""
echo "$RESPONSE" | jq '.'
echo ""
# Summary
print_section "Test Summary"
echo "All tests completed!"
echo ""
echo "Servers created:"
curl -s "$API_URL/status" | jq -r '.[] | "\(.server_group)/\(.host) - \(.status)"'
echo ""
print_success "API is working correctly with multi-server support"
echo ""
echo "Next steps:"
echo " 1. Update your pipelines to use POST /status/pipeline"
echo " 2. Configure cron jobs to use POST /status/cron"
echo " 3. Set up Grafana Infinity datasource pointing to GET /status"
echo ""