-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathtest-install-script.sh
More file actions
executable file
·412 lines (372 loc) · 11.7 KB
/
test-install-script.sh
File metadata and controls
executable file
·412 lines (372 loc) · 11.7 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env bash
# Test script for installTorrServerLinux.sh
# This script runs inside Docker containers to test the installation script
set -e
# Colors for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly NC='\033[0m' # No Color
# Test configuration
readonly SCRIPT_NAME="installTorrServerLinux.sh"
readonly INSTALL_DIR="/opt/torrserver"
readonly GLIBC_LIMITED_VERSION="135"
readonly MIN_GLIBC_VERSION="2.32"
readonly MAX_RETRIES="${MAX_RETRIES:-3}"
readonly RETRY_DELAY="${RETRY_DELAY:-2}"
# Helper functions
log_info() {
echo -e "${GREEN}✓${NC} $1"
}
log_error() {
echo -e "${RED}✗${NC} $1"
}
log_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
log_test() {
echo "Test $1: $2"
}
# Check if OS requires glibc-limited version
is_glibc_limited_os() {
local os="$1"
local glibc_limited_oses="$2"
echo "$glibc_limited_oses" | grep -qE "(^|\|)$os(\||$)"
}
# Get glibc version message for OS
get_glibc_message() {
local os="$1"
case "$os" in
debian-11)
echo "Note: Debian 11 has glibc 2.31, installing version $GLIBC_LIMITED_VERSION (version 136+ requires glibc >= $MIN_GLIBC_VERSION)"
;;
almalinux-8)
echo "Note: AlmaLinux 8 has glibc 2.28, installing version $GLIBC_LIMITED_VERSION (version 136+ requires glibc >= $MIN_GLIBC_VERSION)"
;;
rocky-8)
echo "Note: Rocky 8 has glibc 2.28, installing version $GLIBC_LIMITED_VERSION (version 136+ requires glibc >= $MIN_GLIBC_VERSION)"
;;
amazonlinux-2)
echo "Note: Amazon Linux 2 has glibc 2.26, installing version $GLIBC_LIMITED_VERSION (version 136+ requires glibc >= $MIN_GLIBC_VERSION)"
;;
esac
}
# Install RPM packages (dnf/yum)
install_rpm_packages() {
local pkg_manager="$1"
shift
local packages=("$@")
"$pkg_manager" makecache -q || true
# Always remove curl-minimal first to avoid conflicts
"$pkg_manager" remove -y -q curl-minimal 2>/dev/null || true
# Check if curl package is installed (not just curl-minimal)
if rpm -qa curl >/dev/null 2>&1; then
# curl package is already installed, just install other packages
"$pkg_manager" install -y -q "${packages[@]}" || true
else
# curl package not installed, install curl with --allowerasing
"$pkg_manager" install -y -q --allowerasing curl "${packages[@]}" || true
fi
}
# Install dependencies based on OS
install_dependencies() {
if command -v apt-get >/dev/null 2>&1; then
retry_command "apt-get update" "apt-get update -qq" 3 1 || true
retry_command "apt-get install" "apt-get install -y -qq curl iputils-ping dnsutils" 3 1 || true
elif command -v dnf >/dev/null 2>&1; then
retry_command "dnf install" "install_rpm_packages dnf iputils bind-utils" 3 1 || true
elif command -v yum >/dev/null 2>&1; then
retry_command "yum install" "install_rpm_packages yum iputils bind-utils" 3 1 || true
fi
}
# Verify curl installation
verify_curl_installation() {
if command -v rpm >/dev/null 2>&1; then
if ! rpm -qa curl >/dev/null 2>&1; then
log_error "curl package is not installed after dependency installation"
exit 1
fi
# Verify curl-minimal is not present (it should have been removed)
if rpm -qa curl-minimal >/dev/null 2>&1; then
log_warning "curl-minimal is still installed, removing it..."
rpm -e --nodeps curl-minimal 2>/dev/null || true
fi
elif command -v dpkg >/dev/null 2>&1; then
if ! dpkg -s curl >/dev/null 2>&1; then
log_error "curl package is not installed after dependency installation"
exit 1
fi
fi
}
# Retry a command with exponential backoff
retry_command() {
local test_name="$1"
local test_command="$2"
local max_attempts="${3:-$MAX_RETRIES}"
local delay="${4:-$RETRY_DELAY}"
local attempt=1
local last_error=0
# Print command before first attempt
if [[ $attempt -eq 1 ]]; then
echo "Executing: $test_command"
fi
while [[ $attempt -le $max_attempts ]]; do
if [[ $attempt -gt 1 ]]; then
echo "Retry attempt $attempt/$max_attempts: $test_command"
fi
if eval "$test_command"; then
if [[ $attempt -gt 1 ]]; then
log_info "$test_name (succeeded on attempt $attempt)"
fi
return 0
else
last_error=$?
if [[ $attempt -lt $max_attempts ]]; then
log_warning "$test_name failed (attempt $attempt/$max_attempts), retrying in ${delay}s..."
sleep "$delay"
delay=$((delay * 2))
fi
attempt=$((attempt + 1))
fi
done
log_error "$test_name (failed after $max_attempts attempts)"
return $last_error
}
# Run a test command and handle errors
run_test() {
local test_name="$1"
local test_command="$2"
local skip_on_error="${3:-false}"
local use_retry="${4:-true}"
if [[ "$use_retry" == "true" ]]; then
if retry_command "$test_name" "$test_command"; then
log_info "$test_name"
return 0
else
if [[ "$skip_on_error" == "true" ]]; then
log_warning "$test_name (skipped after retries)"
return 0
else
log_error "$test_name"
return 1
fi
fi
else
if eval "$test_command"; then
log_info "$test_name"
return 0
else
if [[ "$skip_on_error" == "true" ]]; then
log_warning "$test_name (skipped)"
return 0
else
log_error "$test_name"
return 1
fi
fi
fi
}
# Main test execution
main() {
local matrix_os="${MATRIX_OS:-}"
local test_user="${TEST_USER:-default}"
local glibc_limited_oses="${GLIBC_LIMITED_OSES:-}"
# Determine root flag
local root_flag=''
if [[ "$test_user" == 'root' ]]; then
root_flag='--root'
fi
# Check if OS requires glibc-limited version
local is_glibc_limited=false
if is_glibc_limited_os "$matrix_os" "$glibc_limited_oses"; then
is_glibc_limited=true
fi
echo "========================================"
echo "Testing $SCRIPT_NAME"
echo "OS: $matrix_os"
echo "User: $test_user"
echo "Retry settings: max=$MAX_RETRIES, initial_delay=${RETRY_DELAY}s"
echo "========================================"
echo ""
# Test 1: Check script syntax
echo "::group::Test 1: Check script syntax"
log_test "1" "Checking script syntax..."
echo "Executing: bash -n $SCRIPT_NAME"
if bash -n "$SCRIPT_NAME"; then
log_info "Script syntax is valid"
else
log_error "Script syntax check failed"
echo "::endgroup::"
exit 1
fi
echo "::endgroup::"
echo ""
# Test 2: Show help
echo "::group::Test 2: Test help command"
log_test "2" "Testing help command..."
echo "Executing: ./$SCRIPT_NAME --help"
if ./"$SCRIPT_NAME" --help > /dev/null; then
log_info "Help command works"
else
log_error "Help command failed"
echo "::endgroup::"
exit 1
fi
echo "::endgroup::"
echo ""
# Test 3: Install in silent mode
echo "::group::Test 3: Install TorrServer"
log_test "3" "Installing TorrServer (silent mode)..."
if [[ "$is_glibc_limited" == "true" ]]; then
local glibc_msg
glibc_msg=$(get_glibc_message "$matrix_os")
if [[ -n "$glibc_msg" ]]; then
echo "$glibc_msg"
fi
if retry_command "Installation" "./$SCRIPT_NAME --install $GLIBC_LIMITED_VERSION --silent $root_flag"; then
log_info "Installation completed"
else
log_error "Installation failed after retries"
echo "::endgroup::"
exit 1
fi
else
if retry_command "Installation" "./$SCRIPT_NAME --install --silent $root_flag"; then
log_info "Installation completed"
else
log_error "Installation failed after retries"
echo "::endgroup::"
exit 1
fi
fi
echo "::endgroup::"
echo ""
# Test 4: Check installation
echo "::group::Test 4: Verify installation"
log_test "4" "Checking installation..."
echo "Executing: ls $INSTALL_DIR/TorrServer-linux-*"
if ls "$INSTALL_DIR"/TorrServer-linux-* >/dev/null 2>&1; then
log_info "Binary file exists"
else
log_error "Binary file not found"
echo "::endgroup::"
exit 1
fi
echo "::endgroup::"
echo ""
# Test 5: Check version
echo "::group::Test 5: Check version"
log_test "5" "Checking for updates..."
if [[ "$is_glibc_limited" == "true" ]]; then
echo "Note: Skipping version check (latest version requires glibc >= $MIN_GLIBC_VERSION)"
log_info "Version check skipped (expected)"
else
if retry_command "Version check" "./$SCRIPT_NAME --check --silent $root_flag"; then
log_info "Version check completed"
else
log_error "Version check failed after retries"
echo "::endgroup::"
exit 1
fi
fi
echo "::endgroup::"
echo ""
# Test 6: Update (if available)
echo "::group::Test 6: Test update command"
log_test "6" "Testing update command..."
if [[ "$is_glibc_limited" == "true" ]]; then
echo "Note: Skipping update test (latest version requires glibc >= $MIN_GLIBC_VERSION)"
log_info "Update check skipped (expected)"
else
if retry_command "Update check" "./$SCRIPT_NAME --update --silent $root_flag"; then
log_info "Update check completed"
else
log_error "Update check failed after retries"
echo "::endgroup::"
exit 1
fi
fi
echo "::endgroup::"
echo ""
# Test 7: Reconfigure
echo "::group::Test 7: Test reconfigure command"
log_test "7" "Testing reconfigure command..."
if retry_command "Reconfigure" "./$SCRIPT_NAME --reconfigure --silent $root_flag"; then
log_info "Reconfigure completed"
else
log_error "Reconfigure failed after retries"
echo "::endgroup::"
exit 1
fi
echo "::endgroup::"
echo ""
# Test 8: Change user (if not already root)
if [[ "$test_user" == 'default' ]]; then
echo "::group::Test 8: Test change-user to root"
log_test "8" "Testing change-user to root..."
if retry_command "User change to root" "./$SCRIPT_NAME --change-user root --silent"; then
log_info "User change to root completed"
else
log_error "User change to root failed after retries"
echo "::endgroup::"
exit 1
fi
echo "::endgroup::"
echo ""
# Test 8b: Change user back to default (only for Ubuntu to test full flow)
if [[ "$matrix_os" == 'ubuntu-22.04' ]] || [[ "$matrix_os" == 'ubuntu-24.04' ]]; then
echo "::group::Test 8b: Test change-user back to default"
log_test "8b" "Testing change-user back to default..."
if retry_command "User change back to default" "./$SCRIPT_NAME --change-user torrserver --silent"; then
log_info "User change back to default completed"
else
log_error "User change back to default failed after retries"
echo "::endgroup::"
exit 1
fi
echo "::endgroup::"
echo ""
fi
fi
# Test 9: Cleanup - Uninstall
echo "::group::Test 9: Uninstall TorrServer"
log_test "9" "Uninstalling TorrServer..."
if retry_command "Uninstallation" "./$SCRIPT_NAME --remove --silent"; then
log_info "Uninstallation completed"
else
log_error "Uninstallation failed after retries"
echo "::endgroup::"
exit 1
fi
echo "::endgroup::"
echo ""
# Test 10: Verify cleanup
echo "::group::Test 10: Verify cleanup"
log_test "10" "Verifying cleanup..."
echo "Executing: Checking if $INSTALL_DIR is empty or doesn't exist"
if [[ ! -d "$INSTALL_DIR" ]] || [[ -z "$(ls -A "$INSTALL_DIR" 2>/dev/null)" ]]; then
log_info "Cleanup verified"
else
log_warning "Installation directory still exists (may be expected)"
fi
echo "::endgroup::"
echo ""
echo "========================================"
echo "All tests passed! ✓"
echo "========================================"
}
# Setup and run tests
setup() {
echo "::group::Setup: Install dependencies"
# Install dependencies
install_dependencies
# Verify curl installation
verify_curl_installation
# Make script executable
chmod +x "$SCRIPT_NAME"
echo "::endgroup::"
echo ""
}
# Run setup and main
setup
main