This repository was archived by the owner on Mar 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_entrypoint_update.sh
More file actions
executable file
·479 lines (402 loc) · 13.7 KB
/
test_entrypoint_update.sh
File metadata and controls
executable file
·479 lines (402 loc) · 13.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/bin/sh
# Test script for entrypoint.sh update logic
# This tests the update_plural_binary function in isolation
set -e
# Colors for test output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
TESTS_PASSED=0
TESTS_FAILED=0
ORIG_PATH="$PATH"
# Test helper functions
pass() {
TESTS_PASSED=$((TESTS_PASSED + 1))
echo "${GREEN}✓${NC} $1"
}
fail() {
TESTS_FAILED=$((TESTS_FAILED + 1))
echo "${RED}✗${NC} $1"
}
info() {
echo "${YELLOW}ℹ${NC} $1"
}
# Mock plural binary for testing
create_mock_plural() {
VERSION="$1"
cat > /tmp/test-plural <<EOF
#!/bin/sh
echo "plural version $VERSION"
EOF
chmod +x /tmp/test-plural
}
# Mock GitHub API response
create_mock_api_response() {
VERSION="$1"
ARCH="$2"
cat > /tmp/mock-api.json <<EOF
{
"tag_name": "$VERSION",
"assets": [
{
"name": "plural_Linux_${ARCH}.tar.gz",
"browser_download_url": "https://example.com/plural_Linux_${ARCH}.tar.gz"
}
]
}
EOF
}
# Create a mock curl script that returns local files instead of making network requests.
# This is more portable than using file:// URLs, which some curl builds don't support.
setup_mock_curl() {
mkdir -p /tmp/plural-test-mock
cat > /tmp/plural-test-mock/curl <<'MOCK_EOF'
#!/bin/sh
# Mock curl: returns local mock data based on URL patterns.
# Extract the URL from arguments (last non-flag, non-flag-value argument).
URL=""
SKIP_NEXT=0
for arg in "$@"; do
if [ "$SKIP_NEXT" = "1" ]; then
SKIP_NEXT=0
continue
fi
case "$arg" in
--connect-timeout|--max-time) SKIP_NEXT=1 ;;
-*) ;;
*) URL="$arg" ;;
esac
done
case "$URL" in
*api.github.com*|*mock-api*)
cat /tmp/mock-api.json 2>/dev/null
;;
*.tar.gz)
cat /tmp/mock-tarball.tar.gz 2>/dev/null
;;
*)
echo "" >&2
exit 1
;;
esac
MOCK_EOF
chmod +x /tmp/plural-test-mock/curl
}
teardown_mock_curl() {
rm -rf /tmp/plural-test-mock
export PATH="$ORIG_PATH"
}
# Create a mock tarball containing a fake plural binary
create_mock_tarball() {
NEW_VERSION="$1"
mkdir -p /tmp/plural-test-tarball
cat > /tmp/plural-test-tarball/plural <<EOF
#!/bin/sh
echo "plural version $NEW_VERSION"
EOF
chmod +x /tmp/plural-test-tarball/plural
tar -czf /tmp/mock-tarball.tar.gz -C /tmp/plural-test-tarball plural
rm -rf /tmp/plural-test-tarball
}
# Extract the update function from entrypoint.sh for testing
# We'll create a testable version
create_test_function() {
cat > /tmp/update_test.sh <<'EOF'
#!/bin/sh
set -e
# Test version of update function with injectable dependencies
update_plural_binary() {
if [ -n "$PLURAL_SKIP_UPDATE" ]; then
echo "[plural-update] Auto-update disabled via PLURAL_SKIP_UPDATE"
return 0
fi
echo "[plural-update] Checking for updates..."
# Get current version (use mock binary path for testing)
PLURAL_BIN="${PLURAL_BIN:-/usr/local/bin/plural}"
CURRENT_VERSION=$($PLURAL_BIN --version 2>&1 | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' || echo "unknown")
echo "[plural-update] Current version: $CURRENT_VERSION"
# Fetch latest release info with timeout
# For testing, allow override of the API endpoint
API_URL="${GITHUB_API_URL:-https://api.github.com/repos/zhubert/plural/releases/latest}"
LATEST_INFO=$(curl -sL --connect-timeout 5 --max-time 10 "$API_URL" 2>/dev/null || echo "")
if [ -z "$LATEST_INFO" ]; then
echo "[plural-update] Failed to fetch latest release info (network issue or timeout), skipping update"
return 0
fi
LATEST_VERSION=$(echo "$LATEST_INFO" | jq -r '.tag_name // "unknown"' 2>/dev/null || echo "unknown")
if [ "$LATEST_VERSION" = "unknown" ] || [ -z "$LATEST_VERSION" ]; then
echo "[plural-update] Could not determine latest version, skipping update"
return 0
fi
echo "[plural-update] Latest version: $LATEST_VERSION"
# Compare versions (skip if same)
if [ "$CURRENT_VERSION" = "$LATEST_VERSION" ]; then
echo "[plural-update] Already running latest version"
return 0
fi
echo "[plural-update] New version available, updating from $CURRENT_VERSION to $LATEST_VERSION..."
# Determine architecture (map Docker arch to GoReleaser arch)
# Only x86_64 and aarch64/arm64 are supported by releases
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
GOARCH="x86_64"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
GOARCH="arm64"
else
echo "[plural-update] Unsupported architecture: $ARCH, skipping update"
return 0
fi
# Find download URL for this architecture
DOWNLOAD_URL=$(echo "$LATEST_INFO" | \
jq -r ".assets[] | select(.name | contains(\"Linux_${GOARCH}\")) | .browser_download_url" 2>/dev/null | head -n1 || echo "")
if [ -z "$DOWNLOAD_URL" ]; then
echo "[plural-update] Could not find download URL for architecture $GOARCH, skipping update"
return 0
fi
echo "[plural-update] Downloading from: $DOWNLOAD_URL"
# For testing, we can skip the actual download
if [ -n "$SKIP_DOWNLOAD" ]; then
echo "[plural-update] Skipping download (test mode)"
return 0
fi
# Download and install new binary
if curl -sL --connect-timeout 5 --max-time 30 "$DOWNLOAD_URL" | tar -xz -C /tmp plural 2>/dev/null; then
if [ -f /tmp/plural ]; then
mv /tmp/plural "$PLURAL_BIN"
chmod +x "$PLURAL_BIN"
echo "[plural-update] Successfully updated to $LATEST_VERSION"
else
echo "[plural-update] Failed to extract binary from downloaded archive, skipping update"
return 0
fi
else
echo "[plural-update] Failed to download or extract update, skipping"
return 0
fi
}
# Run the update function
update_plural_binary
EOF
chmod +x /tmp/update_test.sh
}
# Test 1: Skip update when PLURAL_SKIP_UPDATE is set
test_skip_update() {
info "Test: Skip update when PLURAL_SKIP_UPDATE is set"
export PLURAL_SKIP_UPDATE=1
OUTPUT=$(sh /tmp/update_test.sh 2>&1)
unset PLURAL_SKIP_UPDATE
if echo "$OUTPUT" | grep -q "Auto-update disabled"; then
pass "Update skipped when PLURAL_SKIP_UPDATE is set"
else
fail "Update not skipped when PLURAL_SKIP_UPDATE is set"
fi
}
# Test 2: Handle missing version in current binary
test_missing_current_version() {
info "Test: Handle missing version in current binary"
cat > /tmp/test-plural-noversion <<'EOF'
#!/bin/sh
echo "plural: unknown version"
EOF
chmod +x /tmp/test-plural-noversion
create_mock_api_response "v0.2.0" "x86_64"
export PLURAL_BIN=/tmp/test-plural-noversion
export PATH="/tmp/plural-test-mock:$ORIG_PATH"
export SKIP_DOWNLOAD=1
OUTPUT=$(sh /tmp/update_test.sh 2>&1)
unset PLURAL_BIN SKIP_DOWNLOAD
export PATH="$ORIG_PATH"
if echo "$OUTPUT" | grep -q "Current version: unknown"; then
pass "Handles missing current version gracefully"
else
fail "Did not handle missing current version"
fi
}
# Test 3: Skip update when versions match
test_same_version() {
info "Test: Skip update when versions match"
create_mock_plural "v0.1.5"
create_mock_api_response "v0.1.5" "x86_64"
export PLURAL_BIN=/tmp/test-plural
export PATH="/tmp/plural-test-mock:$ORIG_PATH"
export SKIP_DOWNLOAD=1
OUTPUT=$(sh /tmp/update_test.sh 2>&1)
unset PLURAL_BIN SKIP_DOWNLOAD
export PATH="$ORIG_PATH"
if echo "$OUTPUT" | grep -q "Already running latest version"; then
pass "Skips update when versions match"
else
fail "Did not skip update when versions match"
fi
}
# Test 4: Detect new version available
test_new_version_available() {
info "Test: Detect new version available"
create_mock_plural "v0.1.5"
create_mock_api_response "v0.2.0" "x86_64"
export PLURAL_BIN=/tmp/test-plural
export PATH="/tmp/plural-test-mock:$ORIG_PATH"
export SKIP_DOWNLOAD=1
OUTPUT=$(sh /tmp/update_test.sh 2>&1)
unset PLURAL_BIN SKIP_DOWNLOAD
export PATH="$ORIG_PATH"
if echo "$OUTPUT" | grep -q "New version available.*v0.1.5.*v0.2.0"; then
pass "Detects new version available"
else
fail "Did not detect new version available"
fi
}
# Test 5: Handle network failure gracefully
test_network_failure() {
info "Test: Handle network failure gracefully"
create_mock_plural "v0.1.5"
# Use real curl (not mock) with an invalid URL to test network failure handling
export PLURAL_BIN=/tmp/test-plural
export PATH="$ORIG_PATH"
export GITHUB_API_URL="http://invalid.example.com/nonexistent"
export SKIP_DOWNLOAD=1
OUTPUT=$(sh /tmp/update_test.sh 2>&1)
EXITCODE=$?
unset PLURAL_BIN GITHUB_API_URL SKIP_DOWNLOAD
if [ $EXITCODE -eq 0 ] && echo "$OUTPUT" | grep -q "Failed to fetch latest release info"; then
pass "Handles network failure gracefully"
else
fail "Did not handle network failure gracefully (exit code: $EXITCODE)"
fi
}
# Test 6: Handle malformed API response
test_malformed_api_response() {
info "Test: Handle malformed API response"
create_mock_plural "v0.1.5"
echo "invalid json" > /tmp/mock-api.json
export PLURAL_BIN=/tmp/test-plural
export PATH="/tmp/plural-test-mock:$ORIG_PATH"
export SKIP_DOWNLOAD=1
OUTPUT=$(sh /tmp/update_test.sh 2>&1)
EXITCODE=$?
unset PLURAL_BIN SKIP_DOWNLOAD
export PATH="$ORIG_PATH"
if [ $EXITCODE -eq 0 ] && echo "$OUTPUT" | grep -q "Could not determine latest version"; then
pass "Handles malformed API response gracefully"
else
fail "Did not handle malformed API response gracefully"
fi
}
# Test 7: Verify architecture mapping
test_architecture_detection() {
info "Test: Architecture detection and mapping"
create_mock_plural "v0.1.5"
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
EXPECTED_ARCH="x86_64"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
EXPECTED_ARCH="arm64"
else
# Unsupported architecture - verify the script reports it
create_mock_api_response "v0.2.0" "$ARCH"
export PLURAL_BIN=/tmp/test-plural
export PATH="/tmp/plural-test-mock:$ORIG_PATH"
export SKIP_DOWNLOAD=1
OUTPUT=$(sh /tmp/update_test.sh 2>&1)
unset PLURAL_BIN SKIP_DOWNLOAD
export PATH="$ORIG_PATH"
if echo "$OUTPUT" | grep -q "Unsupported architecture"; then
pass "Correctly reports unsupported architecture $ARCH"
else
fail "Did not report unsupported architecture"
fi
return
fi
create_mock_api_response "v0.2.0" "$EXPECTED_ARCH"
export PLURAL_BIN=/tmp/test-plural
export PATH="/tmp/plural-test-mock:$ORIG_PATH"
export SKIP_DOWNLOAD=1
OUTPUT=$(sh /tmp/update_test.sh 2>&1)
unset PLURAL_BIN SKIP_DOWNLOAD
export PATH="$ORIG_PATH"
if echo "$OUTPUT" | grep -q "Downloading from.*Linux_${EXPECTED_ARCH}"; then
pass "Correctly maps architecture $ARCH to $EXPECTED_ARCH"
else
fail "Did not correctly map architecture"
fi
}
# Test 8: Full download, extract, and install flow (integration test)
test_download_extract_install() {
info "Test: Full download, extract, and install flow"
create_mock_plural "v0.1.5"
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
EXPECTED_ARCH="x86_64"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
EXPECTED_ARCH="arm64"
else
pass "Skipping download test on unsupported architecture: $ARCH"
return
fi
create_mock_api_response "v0.2.0" "$EXPECTED_ARCH"
create_mock_tarball "v0.2.0"
# Run WITHOUT SKIP_DOWNLOAD so the full download/extract/install path executes
export PLURAL_BIN=/tmp/test-plural
export PATH="/tmp/plural-test-mock:$ORIG_PATH"
OUTPUT=$(sh /tmp/update_test.sh 2>&1)
EXITCODE=$?
export PATH="$ORIG_PATH"
if [ $EXITCODE -ne 0 ]; then
fail "Download/extract/install exited with non-zero status: $EXITCODE"
unset PLURAL_BIN
return
fi
if ! echo "$OUTPUT" | grep -q "Successfully updated to v0.2.0"; then
fail "Did not report successful update"
unset PLURAL_BIN
return
fi
# Verify the binary was actually replaced with the new version
NEW_VERSION=$($PLURAL_BIN --version 2>&1 | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' || echo "unknown")
unset PLURAL_BIN
if [ "$NEW_VERSION" = "v0.2.0" ]; then
pass "Full download/extract/install flow works end-to-end"
else
fail "Binary was not updated (expected v0.2.0, got $NEW_VERSION)"
fi
}
# Main test execution
main() {
echo "================================"
echo "Entrypoint Update Logic Tests"
echo "================================"
echo ""
# Ensure cleanup on exit or interruption
trap 'teardown_mock_curl; rm -f /tmp/test-plural* /tmp/mock-api.json /tmp/mock-tarball.tar.gz /tmp/update_test.sh; rm -rf /tmp/plural-test-tarball' EXIT INT TERM
# Setup
create_test_function
setup_mock_curl
# Run tests
test_skip_update
test_missing_current_version
test_same_version
test_new_version_available
test_network_failure
test_malformed_api_response
test_architecture_detection
test_download_extract_install
# Cleanup handled by trap
# Summary
echo ""
echo "================================"
echo "Test Results"
echo "================================"
echo "${GREEN}Passed: $TESTS_PASSED${NC}"
echo "${RED}Failed: $TESTS_FAILED${NC}"
echo ""
if [ $TESTS_FAILED -gt 0 ]; then
echo "${RED}Some tests failed${NC}"
exit 1
else
echo "${GREEN}All tests passed${NC}"
exit 0
fi
}
# Run tests
main