-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_tests.sh
More file actions
executable file
·240 lines (197 loc) · 7.04 KB
/
Copy pathrun_tests.sh
File metadata and controls
executable file
·240 lines (197 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/bash
# run_tests.sh - Enhanced test runner with coverage collection and robust Docker service management
# Supports both unit tests and integration tests based on SLOW_TESTS environment variable
# Note: This script assumes Docker is installed and running
# This script enforces the test environment through MINEXUS_ENV variable
set -e
# Configuration
COVERAGE_FILE="coverage.out"
COVERAGE_HTML="coverage.html"
DOCKER_COMPOSE_FILE="docker-compose.yml"
MAX_RETRIES=60
RETRY_INTERVAL=2
export MINEXUS_ENV="test"
NEXUS_MINION_PORT=${NEXUS_MINION_PORT:-11972}
DB_PORT=5432
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if a port is open
check_port() {
local host=$1
local port=$2
local timeout=${3:-2}
# Use netcat with built-in timeout for macOS compatibility
if nc -z -w "$timeout" "$host" "$port" 2>/dev/null; then
return 0
else
return 1
fi
}
# Wait for service to be ready
wait_for_service() {
local service_name=$1
local host=$2
local port=$3
local max_attempts=${4:-$MAX_RETRIES}
log_info "Waiting for $service_name to be ready on $host:$port..."
for ((i=1; i<=max_attempts; i++)); do
if check_port "$host" "$port"; then
log_info "$service_name is ready (attempt $i/$max_attempts)"
return 0
fi
if [ $i -eq $max_attempts ]; then
log_error "$service_name failed to start within $((max_attempts * RETRY_INTERVAL)) seconds"
return 1
fi
log_info "Waiting for $service_name... (attempt $i/$max_attempts)"
sleep $RETRY_INTERVAL
done
}
# Check Docker Compose service status
check_service_status() {
local service_name=$1
if docker compose ps --format json | grep -q "\"Service\":\"$service_name\".*\"State\":\"running\""; then
return 0
else
return 1
fi
}
# Load environment variables from the appropriate .env file
load_environment_variables() {
local env_file=".env.${MINEXUS_ENV:-dev}"
if [ -f "$env_file" ]; then
log_info "Loading environment variables from $env_file..."
# Export variables from the environment file
set -a # automatically export all variables
source "$env_file"
set +a # disable automatic export
log_info "Environment variables loaded successfully"
else
log_error "Environment file $env_file not found!"
log_error "Integration tests require proper environment configuration."
log_error "Please create $env_file with the required variables."
log_error "See docker-compose.yml for the expected configuration."
exit 1
fi
}
# Setup Docker services with proper dependency management
setup_docker_services() {
log_info "Setting up Docker services for integration tests..."
# Load environment variables from .env.test
load_environment_variables
# Check if services are already running
local services_to_start=()
if ! check_service_status "nexus_db"; then
services_to_start+=("nexus_db")
fi
if ! check_service_status "nexus"; then
services_to_start+=("nexus")
fi
if ! check_service_status "minion"; then
services_to_start+=("minion")
fi
if [ ${#services_to_start[@]} -gt 0 ]; then
log_info "Starting services: ${services_to_start[*]}"
# Start database first if needed
if [[ " ${services_to_start[*]} " =~ " nexus_db " ]]; then
log_info "Starting database service..."
docker compose up -d nexus_db
wait_for_service "PostgreSQL Database" "localhost" "$DB_PORT" 30
fi
# Start nexus server if needed
if [[ " ${services_to_start[*]} " =~ " nexus " ]]; then
log_info "Starting Nexus server..."
docker compose up -d nexus
wait_for_service "Nexus Server" "localhost" "$NEXUS_MINION_PORT" 45
fi
# Start minion if needed
if [[ " ${services_to_start[*]} " =~ " minion " ]]; then
log_info "Starting Minion service..."
docker compose up -d minion
sleep 5 # Give minion time to register
fi
else
log_info "All required services are already running"
# Still verify they're accessible
wait_for_service "PostgreSQL Database" "localhost" "$DB_PORT" 10
wait_for_service "Nexus Server" "localhost" "$NEXUS_MINION_PORT" 10
fi
}
# Build console executable with proper error handling
build_console() {
log_info "Building console executable..."
if [ ! -f ./console ] || [ cmd/console/console.go -nt ./console ]; then
if go build -o console ./cmd/console; then
log_info "Console built successfully"
else
log_error "Failed to build console"
return 1
fi
else
log_info "Console executable is up to date"
fi
}
# Check if integration tests should be included
if [ -n "$SLOW_TESTS" ]; then
log_info "Running all tests including integration tests (SLOW_TESTS is set)..."
# Verify Docker is running
if ! docker info >/dev/null 2>&1; then
log_error "Docker is not running or not accessible"
exit 1
fi
# Setup services and build console
setup_docker_services
build_console
# Final verification
log_info "Running final verification..."
wait_for_service "PostgreSQL Database" "localhost" "$DB_PORT" 5
wait_for_service "Nexus Server" "localhost" "$NEXUS_MINION_PORT" 5
log_info "Test environment setup completed successfully!"
else
log_info "Running unit tests only (set SLOW_TESTS=1 to include integration tests)..."
fi
# Run tests with coverage
go test -coverprofile=$COVERAGE_FILE ./... -v
# Check if coverage file was generated
if [ -f "$COVERAGE_FILE" ]; then
echo ""
echo "Coverage results:"
echo "================"
# Show coverage summary
go tool cover -func=$COVERAGE_FILE
# Generate HTML coverage report
go tool cover -html=$COVERAGE_FILE -o $COVERAGE_HTML
echo ""
echo "Coverage report generated: $COVERAGE_HTML"
echo "Coverage data saved to: $COVERAGE_FILE"
# Calculate total coverage percentage
TOTAL_COVERAGE=$(go tool cover -func=$COVERAGE_FILE | grep "total:" | awk '{print $3}')
if [ -n "$TOTAL_COVERAGE" ]; then
echo "Total coverage: $TOTAL_COVERAGE"
fi
# Show test summary
if [ -n "$SLOW_TESTS" ]; then
echo ""
echo "✓ Unit tests and integration tests completed"
echo " Run 'make test' for unit tests only"
echo " Run 'SLOW_TESTS=1 make test' for all tests"
else
echo ""
echo "✓ Unit tests completed"
echo " Run 'SLOW_TESTS=1 make test' to include integration tests"
fi
else
echo "Warning: Coverage file not generated"
fi