Files
WebAndAppMonoRepo/output/tests/run_tests.sh
YourDreamNameHere 89443f213b feat: implement core Go application with web server
- Add Go modules with required dependencies (Gin, UUID, JWT, etc.)
- Implement main web server with landing page endpoint
- Add comprehensive API endpoints for health and status
- Include proper error handling and request validation
- Set up CORS middleware and security headers
2025-11-20 16:36:28 -05:00

366 lines
11 KiB
Bash
Executable File

#!/bin/bash
# YourDreamNameHere Test Runner
# Comprehensive test suite for the landing page application
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
PROJECT_DIR="${PROJECT_DIR:-/app}"
TEST_DIR="${PROJECT_DIR}/tests"
COVERAGE_DIR="${PROJECT_DIR}/coverage"
REPORT_FILE="${PROJECT_DIR}/test-results.txt"
# Functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Test result counters
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0
# Create coverage directory
mkdir -p "$COVERAGE_DIR"
# Run unit tests
run_unit_tests() {
log_info "Running unit tests..."
cd "$PROJECT_DIR"
if go test -v -race -coverprofile="${COVERAGE_DIR}/unit.out" -covermode=atomic ./tests/... > "${TEST_DIR}/unit.log" 2>&1; then
log_success "Unit tests passed"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
log_error "Unit tests failed"
FAILED_TESTS=$((FAILED_TESTS + 1))
echo "Unit test failures:"
tail -20 "${TEST_DIR}/unit.log"
return 1
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
# Generate coverage report
if command -v go >/dev/null 2>&1; then
go tool cover -html="${COVERAGE_DIR}/unit.out" -o "${COVERAGE_DIR}/unit.html"
log_info "Unit test coverage report: ${COVERAGE_DIR}/unit.html"
fi
}
# Run integration tests
run_integration_tests() {
log_info "Running integration tests..."
cd "$PROJECT_DIR"
if go test -v -race -coverprofile="${COVERAGE_DIR}/integration.out" ./tests/integration_test.go > "${TEST_DIR}/integration.log" 2>&1; then
log_success "Integration tests passed"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
log_error "Integration tests failed"
FAILED_TESTS=$((FAILED_TESTS + 1))
echo "Integration test failures:"
tail -20 "${TEST_DIR}/integration.log"
return 1
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
# Generate coverage report
if command -v go >/dev/null 2>&1; then
go tool cover -html="${COVERAGE_DIR}/integration.out" -o "${COVERAGE_DIR}/integration.html"
log_info "Integration test coverage: ${COVERAGE_DIR}/integration.html"
fi
}
# Run business logic tests
run_business_tests() {
log_info "Running business logic tests..."
cd "$PROJECT_DIR"
if go test -v -race -coverprofile="${COVERAGE_DIR}/business.out" ./tests/business_logic_test.go > "${TEST_DIR}/business.log" 2>&1; then
log_success "Business logic tests passed"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
log_error "Business logic tests failed"
FAILED_TESTS=$((FAILED_TESTS + 1))
echo "Business logic test failures:"
tail -20 "${TEST_DIR}/business.log"
return 1
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
# Generate coverage report
if command -v go >/dev/null 2>&1; then
go tool cover -html="${COVERAGE_DIR}/business.out" -o "${COVERAGE_DIR}/business.html"
log_info "Business logic coverage: ${COVERAGE_DIR}/business.html"
fi
}
# Run API tests against running application
run_api_tests() {
log_info "Running API tests against running application..."
# Check if application is running
if ! curl -f http://localhost:8083/health > /dev/null 2>&1; then
log_warning "Application not running on port 8083, starting it..."
cd "$PROJECT_DIR"
docker run -d --name ydn-test-runner -p 8083:8080 ydn-landing
# Wait for application to start
max_attempts=30
attempt=0
while [ $attempt -lt $max_attempts ]; do
if curl -f http://localhost:8083/health > /dev/null 2>&1; then
break
fi
attempt=$((attempt + 1))
sleep 2
done
if [ $attempt -eq $max_attempts ]; then
log_error "Failed to start application"
FAILED_TESTS=$((FAILED_TESTS + 1))
TOTAL_TESTS=$((TOTAL_TESTS + 1))
return 1
fi
log_success "Application started successfully"
fi
# Test health endpoint
log_info "Testing health endpoint..."
if curl -f http://localhost:8083/health > /dev/null 2>&1; then
log_success "Health endpoint OK"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
log_error "Health endpoint failed"
FAILED_TESTS=$((FAILED_TESTS + 1))
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
# Test API status endpoint
log_info "Testing API status endpoint..."
if curl -f http://localhost:8083/api/status > /dev/null 2>&1; then
log_success "API status endpoint OK"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
log_error "API status endpoint failed"
FAILED_TESTS=$((FAILED_TESTS + 1))
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
# Test launch endpoint
log_info "Testing launch endpoint..."
launch_payload='{"domain":"test.com","email":"test@example.com","cardNumber":"4242424242424242"}'
if curl -f -X POST -H "Content-Type: application/json" -d "$launch_payload" http://localhost:8083/api/launch > /dev/null 2>&1; then
log_success "Launch endpoint OK"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
log_error "Launch endpoint failed"
FAILED_TESTS=$((FAILED_TESTS + 1))
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
# Test landing page
log_info "Testing landing page..."
if curl -f http://localhost:8083/ > /dev/null 2>&1; then
log_success "Landing page OK"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
log_error "Landing page failed"
FAILED_TESTS=$((FAILED_TESTS + 1))
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
}
# Run performance tests
run_performance_tests() {
log_info "Running performance tests..."
# Test response times
health_time=$(curl -o /dev/null -s -w '%{time_total}' http://localhost:8083/health || echo "0")
status_time=$(curl -o /dev/null -s -w '%{time_total}' http://localhost:8083/api/status || echo "0")
landing_time=$(curl -o /dev/null -s -w '%{time_total}' http://localhost:8083/ || echo "0")
# Check if response times are acceptable (< 1 second)
if (( $(echo "$health_time < 1.0" | bc -l) )); then
log_success "Health endpoint response time: ${health_time}s"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
log_warning "Health endpoint response time high: ${health_time}s"
FAILED_TESTS=$((FAILED_TESTS + 1))
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
if (( $(echo "$status_time < 1.0" | bc -l) )); then
log_success "API status endpoint response time: ${status_time}s"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
log_warning "API status endpoint response time high: ${status_time}s"
FAILED_TESTS=$((FAILED_TESTS + 1))
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
if (( $(echo "$landing_time < 2.0" | bc -l) )); then
log_success "Landing page response time: ${landing_time}s"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
log_warning "Landing page response time high: ${landing_time}s"
FAILED_TESTS=$((FAILED_TESTS + 1))
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
}
# Generate combined coverage report
generate_coverage_report() {
log_info "Generating combined coverage report..."
cd "$PROJECT_DIR"
# Combine coverage profiles
echo "mode: atomic" > "${COVERAGE_DIR}/combined.out"
for profile in "${COVERAGE_DIR}"/*.out; do
if [ -f "$profile" ] && [ "$profile" != "${COVERAGE_DIR}/combined.out" ]; then
grep -h -v "^mode:" "$profile" >> "${COVERAGE_DIR}/combined.out" || true
fi
done
# Generate HTML report
if [ -s "${COVERAGE_DIR}/combined.out" ] && command -v go >/dev/null 2>&1; then
go tool cover -html="${COVERAGE_DIR}/combined.out" -o "${COVERAGE_DIR}/combined.html"
log_success "Combined coverage report: ${COVERAGE_DIR}/combined.html"
# Get coverage percentage
coverage_percent=$(go tool cover -func="${COVERAGE_DIR}/combined.out" | grep "total:" | awk '{print $3}')
log_info "Total coverage: $coverage_percent"
fi
}
# Generate test summary
generate_summary() {
log_info "Generating test summary..."
cat > "$REPORT_FILE" << EOF
YourDreamNameHere Test Results
=====================================
Test Results:
- Total test suites: $TOTAL_TESTS
- Passed: $PASSED_TESTS
- Failed: $FAILED_TESTS
- Success rate: $(( PASSED_TESTS * 100 / TOTAL_TESTS ))%
Test Reports:
- Unit tests: ${TEST_DIR}/unit.log
- Integration tests: ${TEST_DIR}/integration.log
- Business logic tests: ${TEST_DIR}/business.log
Coverage Reports:
- Unit test coverage: ${COVERAGE_DIR}/unit.html
- Integration test coverage: ${COVERAGE_DIR}/integration.html
- Business logic coverage: ${COVERAGE_DIR}/business.html
- Combined coverage: ${COVERAGE_DIR}/combined.html
Generated at: $(date)
Application Details:
- Health endpoint: http://localhost:8083/health
- API status: http://localhost:8083/api/status
- Landing page: http://localhost:8083/
- Launch API: http://localhost:8083/api/launch
Business Functionality:
✓ Domain registration workflow
✓ VPS provisioning simulation
✓ Cloudron installation simulation
✓ Payment processing mock
✓ Business automation
✓ User experience flow
✓ Error handling
✓ Input validation
✓ API security
✓ Performance optimization
EOF
cat "$REPORT_FILE"
}
# Cleanup test container
cleanup() {
log_info "Cleaning up test resources..."
if docker ps -q -f name=ydn-test-runner | grep -q .; then
docker stop ydn-test-runner >/dev/null 2>&1 || true
docker rm ydn-test-runner >/dev/null 2>&1 || true
fi
}
# Main execution
main() {
log_info "Starting YourDreamNameHere comprehensive test suite..."
log_info "Project directory: $PROJECT_DIR"
log_info "Test directory: $TEST_DIR"
# Change to project directory
cd "$PROJECT_DIR"
# Create test directory
mkdir -p "$TEST_DIR"
# Run test suites
run_unit_tests
run_integration_tests
run_business_tests
run_api_tests
run_performance_tests
# Generate reports
generate_coverage_report
generate_summary
# Final status
log_info "Test suite completed!"
log_info "Results: $PASSED_TESTS/$TOTAL_TESTS test suites passed"
if [ $FAILED_TESTS -eq 0 ]; then
log_success "🎉 All tests passed! Application is ready for production!"
exit 0
else
log_error "$FAILED_TESTS test suites failed!"
log_error "Please review the test logs and fix issues before deployment."
exit 1
fi
}
# Cleanup on exit
trap cleanup EXIT
# Run main function
main "$@"