Debian Codegraph @

testing/e2e/ntfy/setup.sh

raw symbols SCIP
1 #!/bin/bash
2 #
3 # ntfy E2E Test Environment Setup Script
4 #
5 # This script starts a local ntfy server for end-to-end testing
6 # of Shoutrrr's ntfy service.
7 #
8 # Usage:
9 # ./setup.sh [OPTIONS] [COMMAND]
10 #
11 # Commands:
12 # start-server Start the ntfy server using docker compose
13 # stop-server Stop the ntfy server
14 # status Check the status of the ntfy server
15 # setup-all Start the server and wait for it to be ready (default)
16 #
17 # Options:
18 # --help, -h Show this help message
19 # --verbose, -v Enable verbose output
20 #
21 # Environment:
22 # The script automatically loads .env file from its directory if present.
23 # Required variables:
24 # None (uses defaults: localhost:8080)
25 #
26
27 set -euo pipefail
28
29 # =============================================================================
30 # Configuration and Constants
31 # =============================================================================
32
33 # Color codes for output
34 readonly RED='\033[0;31m'
35 readonly GREEN='\033[0;32m'
36 readonly YELLOW='\033[1;33m'
37 readonly BLUE='\033[0;34m'
38 readonly NC='\033[0m' # No Color
39
40 # Script configuration
41 SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
42 ENV_FILE="${SCRIPT_DIR}/.env"
43 COMPOSE_FILE="${SCRIPT_DIR}/docker-compose.yaml"
44
45 # Default values
46 DEFAULT_NTFY_HOST="localhost:8080"
47
48 # Verbose mode
49 VERBOSE=false
50
51 # Docker compose command (detected in check_requirements)
52 COMPOSE_CMD=""
53
54 # =============================================================================
55 # Helper Functions
56 # =============================================================================
57
58 # Print an error message and exit
59 error() {
60 echo -e "${RED}[ERROR]${NC} $1" >&2
61 exit 1
62 }
63
64 # Print an info message
65 info() {
66 echo -e "${GREEN}[INFO]${NC} $1"
67 }
68
69 # Print a warning message
70 warn() {
71 echo -e "${YELLOW}[WARN]${NC} $1"
72 }
73
74 # Print a debug message (only in verbose mode)
75 debug() {
76 if [[ "$VERBOSE" == "true" ]]; then
77 echo -e "${BLUE}[DEBUG]${NC} $1"
78 fi
79 }
80
81 # Print usage information
82 usage() {
83 echo "ntfy E2E Test Environment Setup Script"
84 echo ""
85 echo "This script starts a local ntfy server for end-to-end testing."
86 echo ""
87 echo "Usage:"
88 echo " ./setup.sh [OPTIONS] [COMMAND]"
89 echo ""
90 echo "Commands:"
91 echo " start-server Start the ntfy server using docker compose"
92 echo " stop-server Stop the ntfy server"
93 echo " status Check the status of the ntfy server"
94 echo " setup-all Start the server and wait for it to be ready (default)"
95 echo ""
96 echo "Options:"
97 echo " --help, -h Show this help message"
98 echo " --verbose, -v Enable verbose output"
99 echo ""
100 echo "Examples:"
101 echo " ./setup.sh # Start server and wait for readiness"
102 echo " ./setup.sh start-server # Start server only"
103 echo " ./setup.sh stop-server # Stop server"
104 echo " ./setup.sh --verbose setup-all # Run with verbose output"
105 }
106
107 # Load environment variables from .env file
108 load_env_file() {
109 if [[ -f "$ENV_FILE" ]]; then
110 debug "Loading environment variables from ${ENV_FILE}"
111 set -a
112 source "$ENV_FILE"
113 set +a
114 info "Loaded environment variables from ${ENV_FILE}"
115 else
116 debug "No .env file found at ${ENV_FILE}"
117 fi
118 }
119
120 # Check if required commands are available
121 check_requirements() {
122 local missing_cmds=()
123
124 # Check for docker
125 if ! command -v docker &> /dev/null; then
126 missing_cmds+=("docker")
127 fi
128
129 # Check for docker compose (plugin or standalone)
130 if docker compose version &> /dev/null 2>&1; then
131 COMPOSE_CMD="docker compose"
132 elif command -v docker-compose &> /dev/null; then
133 COMPOSE_CMD="docker-compose"
134 else
135 missing_cmds+=("docker-compose")
136 fi
137
138 # Check for curl
139 if ! command -v curl &> /dev/null; then
140 missing_cmds+=("curl")
141 fi
142
143 if [[ ${#missing_cmds[@]} -gt 0 ]]; then
144 error "Missing required commands: ${missing_cmds[*]}"
145 fi
146
147 debug "All required commands are available"
148 debug "Using compose command: ${COMPOSE_CMD}"
149 }
150
151 # Wait for the ntfy server to be ready
152 wait_for_server() {
153 local base="${1:-http://${DEFAULT_NTFY_HOST}}"
154 local max_attempts="${2:-30}"
155 local attempt=1
156
157 info "Waiting for ntfy server at ${base} to be ready..."
158
159 while [[ $attempt -le $max_attempts ]]; do
160 if curl -s -o /dev/null -w "%{http_code}" "${base}/v1/health" 2>/dev/null | grep -q "200"; then
161 info "ntfy server is ready!"
162 return 0
163 fi
164
165 debug "Attempt ${attempt}/${max_attempts}: Server not ready yet..."
166 sleep 2
167 ((attempt++))
168 done
169
170 error "ntfy server did not become ready after ${max_attempts} attempts"
171 }
172
173 # =============================================================================
174 # Setup Functions
175 # =============================================================================
176
177 # Start the ntfy server using docker compose
178 start_server() {
179 info "Starting ntfy server..."
180
181 # Check if docker compose file exists
182 if [[ ! -f "$COMPOSE_FILE" ]]; then
183 error "Docker compose file not found at ${COMPOSE_FILE}"
184 fi
185
186 # Ensure COMPOSE_CMD is set
187 if [[ -z "$COMPOSE_CMD" ]]; then
188 check_requirements
189 fi
190
191 # Change to the script directory
192 cd "$SCRIPT_DIR"
193
194 # Stop any existing container
195 if $COMPOSE_CMD ps --status running 2>/dev/null | grep -q ntfy; then
196 info "Stopping existing ntfy container..."
197 $COMPOSE_CMD down || true
198 fi
199
200 # Start the server
201 if $COMPOSE_CMD up -d; then
202 info "ntfy server started successfully"
203 info "Server is available at http://${DEFAULT_NTFY_HOST}"
204 info "Use 'docker logs ntfy -f' to watch the logs"
205 else
206 error "Failed to start ntfy server"
207 fi
208 }
209
210 # Stop the ntfy server
211 stop_server() {
212 info "Stopping ntfy server..."
213
214 # Ensure COMPOSE_CMD is set
215 if [[ -z "$COMPOSE_CMD" ]]; then
216 check_requirements
217 fi
218
219 # Change to the script directory
220 cd "$SCRIPT_DIR"
221
222 if $COMPOSE_CMD down; then
223 info "ntfy server stopped successfully"
224 else
225 error "Failed to stop ntfy server"
226 fi
227 }
228
229 # Check the status of the ntfy server
230 check_status() {
231 # Ensure COMPOSE_CMD is set
232 if [[ -z "$COMPOSE_CMD" ]]; then
233 check_requirements
234 fi
235
236 # Change to the script directory
237 cd "$SCRIPT_DIR"
238
239 info "Checking ntfy server status..."
240 $COMPOSE_CMD ps
241 }
242
243 # Run all setup steps in order
244 setup_all() {
245 info "Starting ntfy E2E test environment setup..."
246 echo ""
247
248 # Check requirements
249 check_requirements
250
251 # Step 1: Start server
252 echo ""
253 info "=== Step 1/2: Starting ntfy server ==="
254 start_server
255
256 # Step 2: Wait for server to be ready
257 echo ""
258 info "=== Step 2/2: Waiting for server to be ready ==="
259 wait_for_server "http://${DEFAULT_NTFY_HOST}"
260
261 echo ""
262 info "=========================================="
263 info "ntfy E2E test environment setup complete!"
264 info "=========================================="
265 info ""
266 info "You can now run the E2E tests with:"
267 info " go test -v ./testing/e2e/ntfy/..."
268 info ""
269 info "To stop the server, run:"
270 info " ${SCRIPT_DIR}/setup.sh stop-server"
271 }
272
273 # =============================================================================
274 # Main Entry Point
275 # =============================================================================
276
277 main() {
278 load_env_file
279
280 # Parse command-line arguments
281 local command="setup-all"
282
283 while [[ $# -gt 0 ]]; do
284 case "$1" in
285 --help|-h)
286 usage
287 exit 0
288 ;;
289 --verbose|-v)
290 VERBOSE=true
291 shift
292 ;;
293 start-server|stop-server|status|setup-all)
294 command="$1"
295 shift
296 ;;
297 *)
298 error "Unknown option or command: $1"
299 ;;
300 esac
301 done
302
303 # Execute the requested command
304 case "$command" in
305 start-server)
306 check_requirements
307 start_server
308 ;;
309 stop-server)
310 check_requirements
311 stop_server
312 ;;
313 status)
314 check_requirements
315 check_status
316 ;;
317 setup-all)
318 setup_all
319 ;;
320 esac
321 }
322
323 # Run main function with all arguments
324 main "$@"