-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.bats
751 lines (624 loc) · 18.9 KB
/
install.bats
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#!/usr/bin/env bats
setup() {
# Create temp test directory first
export TEST_DIR="$(mktemp -d)"
[ -d "$TEST_DIR" ] || fail "Failed to create temp directory"
# Set test environment
export TEST_MODE=true
export CONTRIBUTOOR_PATH="$TEST_DIR/.contributoor"
export VERSION="1.0.0"
export INSTALL_MODE="docker"
export CUSTOM_PATH=""
# Source the script without running main
source './install.sh'
}
teardown() {
# Clean up test directory if it exists
[ -d "$TEST_DIR" ] && rm -rf "$TEST_DIR"
}
@test "detect_architecture returns valid architecture" {
run detect_architecture
[ "$status" -eq 0 ]
[[ "$output" =~ ^(amd64|arm64)$ ]]
}
@test "detect_platform returns valid platform" {
run detect_platform
[ "$status" -eq 0 ]
[[ "$output" =~ ^(linux|darwin)$ ]]
}
@test "setup directories creates expected structure" {
run mkdir -p "$CONTRIBUTOOR_PATH"
[ "$status" -eq 0 ]
run mkdir -p "$CONTRIBUTOOR_PATH/bin"
[ "$status" -eq 0 ]
run mkdir -p "$CONTRIBUTOOR_PATH/logs"
[ "$status" -eq 0 ]
[ -d "$CONTRIBUTOOR_PATH" ]
[ -d "$CONTRIBUTOOR_PATH/bin" ]
[ -d "$CONTRIBUTOOR_PATH/logs" ]
}
@test "update_config_file creates valid yaml" {
# Setup directory first
mkdir -p "$CONTRIBUTOOR_PATH"
local config_file="$CONTRIBUTOOR_PATH/config.yaml"
# Create initial config with some custom settings
cat > "$config_file" << EOF
customSetting: value
anotherSetting: true
version: old-version
contributoorDirectory: /old/path
runMethod: old-method
yetAnotherSetting: 123
EOF
# Debug output
echo "Initial config:"
cat "$config_file"
run update_config_file "$config_file"
echo "Status: $status"
echo "Output: $output"
[ "$status" -eq 0 ]
[ -f "$config_file" ]
# Debug final config
echo "Final config:"
cat "$config_file"
# Verify custom settings were preserved first
run grep "customSetting: value" "$config_file"
[ "$status" -eq 0 ]
run grep "anotherSetting: true" "$config_file"
[ "$status" -eq 0 ]
run grep "yetAnotherSetting: 123" "$config_file"
[ "$status" -eq 0 ]
# Now verify our updated settings
run grep -F "version: $VERSION" "$config_file"
[ "$status" -eq 0 ] || {
echo "Expected version: $VERSION"
echo "Config contents:"
cat "$config_file"
}
run grep -F "contributoorDirectory: $CONTRIBUTOOR_PATH" "$config_file"
[ "$status" -eq 0 ] || {
echo "Expected path: $CONTRIBUTOOR_PATH"
echo "Config contents:"
cat "$config_file"
}
run grep -F "runMethod: $INSTALL_MODE" "$config_file"
[ "$status" -eq 0 ] || {
echo "Expected mode: $INSTALL_MODE"
echo "Config contents:"
cat "$config_file"
}
}
@test "get_latest_version returns valid version" {
# Mock curl to return a valid GitHub API response
function curl() {
echo '{"tag_name": "v1.2.3"}'
return 0
}
export -f curl
run get_latest_version
[ "$status" -eq 0 ]
[[ "$output" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]
}
@test "validate_version accepts valid version" {
# Mock curl to return a releases response that includes version 1.0.0
function curl() {
echo '{"tag_name": "v1.0.0"}'
return 0
}
export -f curl
run validate_version "1.0.0"
[ "$status" -eq 0 ]
}
@test "validate_version fails on invalid version" {
# Mock curl to return a releases response that doesn't include version 99.99.99
function curl() {
echo '{
"tag_name": "v1.0.1",
"name": "Release 1.0.1"
}'
return 0
}
export -f curl
run validate_version "99.99.99"
[ "$status" -eq 1 ]
echo "$output" | grep -q "not found"
}
@test "validate_version fails when bad response from GitHub is returned" {
function curl() {
return 1
}
export -f curl
run validate_version "1.0.0"
[ "$status" -eq 1 ]
echo "$output" | grep -F -q "**ERROR**"
echo "$output" | grep -q "Last 5 available versions:"
echo "$output" | grep -q "Provided version 1.0.0 not found"
}
@test "validate_version shows last 5 versions when version not found" {
# Mock curl to return multiple versions
function curl() {
cat << EOF
{
"data": [
{"tag_name": "v2.0.0"},
{"tag_name": "v1.9.0"},
{"tag_name": "v1.8.0"},
{"tag_name": "v1.7.0"},
{"tag_name": "v1.6.0"},
{"tag_name": "v1.5.0"}
]
}
EOF
return 0
}
export -f curl
run validate_version "1.0.0"
[ "$status" -eq 1 ]
echo "$output" | grep -q "2.0.0,1.9.0,1.8.0,1.7.0,1.6.0"
echo "$output" | grep -q "Provided version 1.0.0 not found"
}
@test "get_latest_version fails when bad response from GitHub is returned" {
function curl() {
return 1
}
export -f curl
run get_latest_version
echo "Status: $status"
echo "Output: '$output'"
[ "$status" -eq 1 ]
echo "$output" | grep -F -q "**ERROR**"
echo "$output" | grep -q "Failed to determine latest version"
}
@test "add_to_path adds directory to PATH" {
# Setup test shell environment
local test_rc="$TEST_DIR/.bashrc"
export SHELL="/bin/bash"
export HOME="$TEST_DIR"
touch "$test_rc"
run add_to_path
grep -q "export PATH=\"\$PATH:$CONTRIBUTOOR_BIN\"" "$test_rc"
[ "$?" -eq 0 ]
}
@test "add_to_path skips if directory already in PATH" {
# Setup test shell environment
local test_rc="$TEST_DIR/.bashrc"
export SHELL="/bin/bash"
export HOME="$TEST_DIR"
touch "$test_rc"
# Add path entry first
echo "export PATH=\"\$PATH:$CONTRIBUTOOR_BIN\"" >> "$test_rc"
# Get initial line count
local initial_lines=$(wc -l < "$test_rc")
run add_to_path
# Verify line count hasn't changed
local final_lines=$(wc -l < "$test_rc")
[ "$initial_lines" -eq "$final_lines" ]
# Verify path only appears once
[ "$(grep -c "export PATH=\"\$PATH:$CONTRIBUTOOR_BIN\"" "$test_rc")" -eq 1 ]
}
@test "setup_installer downloads and verifies checksums" {
mkdir -p "$CONTRIBUTOOR_BIN"
# Set required variables
ARCH="x86_64"
PLATFORM="linux"
INSTALLER_BINARY_NAME="contributoor-installer_${PLATFORM}_${ARCH}"
INSTALLER_URL="https://github.com/ethpandaops/contributoor-installer/releases/latest/download/${INSTALLER_BINARY_NAME}.tar.gz"
# Mock the curl commands
function curl() {
local output_file=""
local url=""
# Parse arguments
while (( "$#" )); do
case "$1" in
-o)
output_file="$2"
shift 2
;;
-*)
shift
;;
*)
url="$1"
shift
;;
esac
done
case "$url" in
*"/checksums.txt")
echo "0123456789abcdef $INSTALLER_BINARY_NAME.tar.gz" > "$output_file"
;;
*)
echo "mock binary" > "$output_file"
;;
esac
return 0
}
# Mock sha256sum to return expected hash
function sha256sum() {
echo "0123456789abcdef $1"
}
# Mock tar extraction
function tar() {
touch "$CONTRIBUTOOR_BIN/contributoor"
touch "$CONTRIBUTOOR_BIN/docker-compose.yml"
return 0
}
export -f curl sha256sum tar
run setup_installer
[ "$status" -eq 0 ]
[ -f "$CONTRIBUTOOR_BIN/contributoor" ]
[ -f "$CONTRIBUTOOR_BIN/docker-compose.yml" ]
}
@test "setup_installer fails on checksum mismatch" {
mkdir -p "$CONTRIBUTOOR_BIN"
# Set required variables
ARCH="x86_64"
PLATFORM="linux"
INSTALLER_BINARY_NAME="contributoor-installer_${PLATFORM}_${ARCH}"
INSTALLER_URL="https://github.com/ethpandaops/contributoor-installer/releases/latest/download/${INSTALLER_BINARY_NAME}.tar.gz"
# Mock curl to return different checksums
function curl() {
local output_file=""
local url=""
# Parse arguments
while (( "$#" )); do
case "$1" in
-o)
output_file="$2"
shift 2
;;
-*)
shift
;;
*)
url="$1"
shift
;;
esac
done
case "$url" in
*"/checksums.txt")
echo "different_hash $INSTALLER_BINARY_NAME.tar.gz" > "$output_file"
;;
*)
echo "mock binary" > "$output_file"
;;
esac
return 0
}
function sha256sum() {
echo "0123456789abcdef $1"
}
export -f curl sha256sum
run setup_installer
[ "$status" -eq 1 ]
echo "$output" | grep -q "Checksum mismatch"
}
@test "setup_docker_contributoor pulls image" {
# Mock docker commands
function docker() {
case "$1" in
"system")
return 0
;;
"pull")
return 0
;;
esac
}
export -f docker
run setup_docker_contributoor
[ "$status" -eq 0 ]
}
@test "setup_binary_contributoor downloads and verifies binary checksum" {
mkdir -p "$CONTRIBUTOOR_BIN"
# Set required variables
ARCH="x86_64"
PLATFORM="linux"
VERSION="1.0.0"
CONTRIBUTOOR_URL="https://github.com/ethpandaops/contributoor/releases/download/v${VERSION}/contributoor_${VERSION}_${PLATFORM}_${ARCH}.tar.gz"
# Mock the curl commands
function curl() {
local output_file=""
local url=""
# Parse arguments
while (( "$#" )); do
case "$1" in
-o)
output_file="$2"
shift 2
;;
-*)
shift
;;
*)
url="$1"
shift
;;
esac
done
case "$url" in
*"checksums.txt")
# Match the exact format expected by the script
cat > "$output_file" << EOF
0123456789abcdef contributoor_1.0.0_linux_x86_64.tar.gz
EOF
;;
*)
echo "mock binary" > "$output_file"
;;
esac
return 0
}
# Mock sha256sum
function sha256sum() {
echo "0123456789abcdef $1"
}
# Mock tar
function tar() {
touch "$CONTRIBUTOOR_BIN/sentry"
return 0
}
export -f curl sha256sum tar
run setup_binary_contributoor
[ "$status" -eq 0 ]
[ -f "$CONTRIBUTOOR_BIN/sentry" ]
}
@test "setup_binary_contributoor fails on missing binary" {
mkdir -p "$CONTRIBUTOOR_BIN"
# Set required variables
ARCH="x86_64"
PLATFORM="linux"
VERSION="1.0.0"
CONTRIBUTOOR_URL="https://github.com/ethpandaops/contributoor/releases/download/v${VERSION}/contributoor_${VERSION}_${PLATFORM}_${ARCH}.tar.gz"
function curl() {
local output_file=""
local url=""
# Parse arguments
while (( "$#" )); do
case "$1" in
-o)
output_file="$2"
shift 2
;;
-*)
shift
;;
*)
url="$1"
shift
;;
esac
done
case "$url" in
*"checksums.txt")
cat > "$output_file" << EOF
0123456789abcdef contributoor_${VERSION}_${PLATFORM}_${ARCH}.tar.gz
EOF
;;
*)
echo "mock binary" > "$output_file"
;;
esac
return 0
}
function sha256sum() {
echo "0123456789abcdef $1"
}
# Mock tar to not create the binary
function tar() {
return 0
}
export -f curl sha256sum tar
run setup_binary_contributoor
[ "$status" -eq 1 ]
echo "$output" | grep -q "Failed to extract contributoor binary"
}
@test "check_docker fails when docker not installed" {
# Mock command to simulate docker not being installed
function command() {
case "$2" in
"docker") return 1 ;;
*) command "$@" ;;
esac
}
export -f command
run check_docker 2>/dev/null
[ "$status" -eq 1 ]
echo "$output" | grep -F -q "**ERROR**"
echo "$output" | grep -q "Docker is not installed. Please install Docker first: https://docs.docker.com/get-docker/"
}
@test "check_docker fails when docker daemon not running" {
# Mock docker info to simulate daemon not running
function docker() {
case "$1" in
"info") return 1 ;;
*) return 0 ;;
esac
}
export -f docker
run check_docker 2>/dev/null
[ "$status" -eq 1 ]
echo "$output" | grep -F -q "**ERROR**"
echo "$output" | grep -q "Docker daemon is not running. Please start Docker and try again."
}
@test "check_docker fails when docker compose not available" {
# Mock docker and command to simulate compose missing
function docker() {
case "$1" in
"compose")
return 1
;;
"info") return 0 ;; # Need docker daemon to appear running
*) return 0 ;;
esac
}
function docker-compose() {
return 1
}
export -f docker docker-compose
run check_docker 2>/dev/null
[ "$status" -eq 1 ]
echo "$output" | grep -F -q "**ERROR**"
echo "$output" | grep -q "Docker Compose is not installed. Please install Docker Compose: https://docs.docker.com/compose/install/"
}
@test "check_docker succeeds when everything available" {
# Mock successful docker environment
function docker() {
case "$1" in
"info") return 0 ;;
"compose") return 0 ;;
*) return 0 ;;
esac
}
export -f docker
run check_docker
[ "$status" -eq 0 ]
[ -z "$output" ]
}
@test "[linux] setup_systemd_contributoor creates service file correctly" {
# Set platform for test
function detect_platform() {
echo "linux"
}
export -f detect_platform
# Mock sudo to capture commands
function sudo() {
case "$1" in
"tee")
# Capture service file content
cat > "$TEST_DIR/contributoor.service"
;;
*) return 0 ;;
esac
}
export -f sudo
run setup_systemd_contributoor
# Check status
[ "$status" -eq 0 ]
# Verify service file was created and has correct content
[ -f "$TEST_DIR/contributoor.service" ]
grep -q "Description=Contributoor Service" "$TEST_DIR/contributoor.service"
grep -q "User=$USER" "$TEST_DIR/contributoor.service"
grep -q "ExecStart=$CONTRIBUTOOR_BIN/sentry" "$TEST_DIR/contributoor.service"
}
@test "[linux] setup_systemd_contributoor removes any existing systemd service before installing" {
# Set platform for test
function detect_platform() {
echo "linux"
}
export -f detect_platform
# Mock sudo and systemctl
function sudo() {
case "$1" in
"systemctl")
case "$2" in
"list-unit-files") echo "contributoor.service enabled" ;;
*) return 0 ;;
esac
;;
*) return 0 ;;
esac
}
export -f sudo
run setup_systemd_contributoor
# Check status
[ "$status" -eq 0 ]
# Verify it detected and handled existing service
echo "$output" | grep -q "Stopped and disabled existing systemd service"
}
@test "[linux] check_systemd validates systemd availability" {
# Set platform for test
function detect_platform() {
echo "linux"
}
export -f detect_platform
# Mock commands to simulate working systemd
function pidof() { echo "1"; }
function systemctl() { return 0; }
export -f pidof systemctl
run check_systemd_or_launchd
[ "$status" -eq 0 ]
}
@test "[linux] check_systemd fails when systemd not available" {
# Set platform for test
function detect_platform() {
echo "linux"
}
export -f detect_platform
# Mock pidof to simulate no systemd
function pidof() { return 1; }
export -f pidof
run check_systemd_or_launchd
[ "$status" -eq 1 ]
echo "$output" | grep -q "Systemd is not available on this system. Please choose a different installation mode."
}
@test "[darwin] check_systemd_or_launchd validates launchd availability" {
# Set platform for test
function detect_platform() {
echo "darwin"
}
export -f detect_platform
# Mock launchctl to simulate working launchd
function command() {
case "$2" in
"launchctl") return 0 ;;
"sudo") return 0 ;;
*) command "$@" ;;
esac
}
export -f command
run check_systemd_or_launchd
[ "$status" -eq 0 ]
}
@test "[darwin] setup_systemd_contributoor removes any existing launchd service before installing" {
# Set platform for test
function detect_platform() {
echo "darwin"
}
export -f detect_platform
# Mock sudo and launchctl
function sudo() {
case "$1" in
"launchctl")
case "$2" in
"list") echo "12345 0 io.ethpandaops.contributoor" ;;
*) return 0 ;;
esac
;;
*) return 0 ;;
esac
}
export -f sudo
run setup_systemd_contributoor
# Check status
[ "$status" -eq 0 ]
# Verify it detected and handled existing service
echo "$output" | grep -q "Stopped and unloaded existing launchd service"
}
@test "[darwin] setup_systemd_contributoor creates launchd service file correctly" {
# Set platform for test
function detect_platform() {
echo "darwin"
}
export -f detect_platform
# Mock sudo to capture commands
function sudo() {
case "$1" in
"tee")
# Capture service file content
cat > "$TEST_DIR/io.ethpandaops.contributoor.plist"
;;
*) return 0 ;;
esac
}
export -f sudo
run setup_systemd_contributoor
# Check status
[ "$status" -eq 0 ]
# Verify service file was created and has correct content
[ -f "$TEST_DIR/io.ethpandaops.contributoor.plist" ]
grep -q "<string>io.ethpandaops.contributoor</string>" "$TEST_DIR/io.ethpandaops.contributoor.plist"
grep -q "<string>$CONTRIBUTOOR_BIN/sentry</string>" "$TEST_DIR/io.ethpandaops.contributoor.plist"
grep -q "<string>$USER</string>" "$TEST_DIR/io.ethpandaops.contributoor.plist"
}