-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.bats
More file actions
1655 lines (1313 loc) · 56.1 KB
/
Copy pathtest.bats
File metadata and controls
1655 lines (1313 loc) · 56.1 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
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bats
export SYSTEM_NAME="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
case "$ARCH" in
x86_64) ARCH="amd64" ;;
aarch64) ARCH="arm64" ;;
arm64) ARCH="arm64" ;;
esac
export DOCKER_ORCHESTRATE="${BATS_TEST_DIRNAME}/build/${SYSTEM_NAME}/docker-orchestrate-${ARCH}"
flunk() {
{
if [[ "$#" -eq 0 ]]; then
cat -
else
echo "$*"
fi
}
return 1
}
assert_equal() {
if [[ "$1" != "$2" ]]; then
{
echo "expected: $1"
echo "actual: $2"
} | flunk
fi
}
assert_exit_status() {
exit_status="$1"
if [[ "$status" -ne "$exit_status" ]]; then
{
echo "expected exit status: $exit_status"
echo "actual exit status: $status"
} | flunk
flunk
fi
}
assert_failure() {
if [[ "$status" -eq 0 ]]; then
flunk "expected failed exit status"
elif [[ "$#" -gt 0 ]]; then
assert_output "$1"
fi
}
assert_success() {
if [[ "$status" -ne 0 ]]; then
flunk "command failed with exit status $status"
elif [[ "$#" -gt 0 ]]; then
assert_output "$1"
fi
}
assert_output() {
local expected
if [[ $# -eq 0 ]]; then
expected="$(cat -)"
else
expected="$1"
fi
assert_equal "$expected" "$output"
}
assert_output_contains() {
local input="$output"
local expected="$1"
local count="${2:-1}"
local found=0
until [ "${input/$expected/}" = "$input" ]; do
input="${input/$expected/}"
found=$((found + 1))
done
assert_equal "$count" "$found"
}
assert_output_not_exists() {
[[ -z "$output" ]] || flunk "expected no output, found some"
}
# reset_image_prune removes any containers and images left behind by the
# image-prune fixture so those tests start from a clean slate and do not leak
# images into one another.
reset_image_prune() {
docker rm -f $(docker ps -aq --filter "label=com.docker.compose.project=bats-image-prune") >/dev/null 2>&1 || true
docker image rm -f $(docker images -aq --filter "label=com.docker.compose.project=bats-image-prune") >/dev/null 2>&1 || true
}
setup_file() {
docker pull nginx:latest
}
setup() {
rm -f /tmp/orch-*
}
teardown() {
for project in $(docker compose ls -a -q 2>/dev/null | grep "^bats-"); do
docker compose -p "$project" down --remove-orphans --volumes --timeout 5 2>/dev/null || true
done
rm -f /tmp/orch-*
}
@test "default" {
run true
echo "output: $output"
echo "status: $status"
assert_success
}
@test "pre-stop host command detached continues running after exit" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/detached-pre-stop"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-pre-stop web
echo "output: $output"
echo "status: $status"
assert_success
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-pre-stop --force web
echo "output: $output"
echo "status: $status"
assert_success
# started marker should exist (command was launched)
[ -f /tmp/orch-detached-pre-stop-started ]
# completed marker should NOT exist yet (still running in background)
[ ! -f /tmp/orch-detached-pre-stop-completed ]
# poll for the detached command to finish (up to 25s)
for i in $(seq 1 25); do
[ -f /tmp/orch-detached-pre-stop-completed ] && break
sleep 1
done
# completed marker should now exist (process ran to completion independently)
[ -f /tmp/orch-detached-pre-stop-completed ]
}
@test "post-stop host command detached continues running after exit" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/detached-post-stop"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-post-stop web
echo "output: $output"
echo "status: $status"
assert_success
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-post-stop --force web
echo "output: $output"
echo "status: $status"
assert_success
# started marker should exist (command was launched)
[ -f /tmp/orch-detached-post-stop-started ]
# completed marker should NOT exist yet (still running in background)
[ ! -f /tmp/orch-detached-post-stop-completed ]
# poll for the detached command to finish (up to 25s)
for i in $(seq 1 25); do
[ -f /tmp/orch-detached-post-stop-completed ] && break
sleep 1
done
# completed marker should now exist (process ran to completion independently)
[ -f /tmp/orch-detached-post-stop-completed ]
}
@test "both pre-stop and post-stop detached commands continue running after exit" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/detached-both"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-both web
echo "output: $output"
echo "status: $status"
assert_success
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-both --force web
echo "output: $output"
echo "status: $status"
assert_success
# both started markers should exist
[ -f /tmp/orch-detached-both-pre-started ]
[ -f /tmp/orch-detached-both-post-started ]
# neither completed marker should exist yet
[ ! -f /tmp/orch-detached-both-pre-completed ]
[ ! -f /tmp/orch-detached-both-post-completed ]
# poll for both detached commands to finish (up to 25s)
for i in $(seq 1 25); do
[ -f /tmp/orch-detached-both-pre-completed ] && [ -f /tmp/orch-detached-both-post-completed ] && break
sleep 1
done
# both completed markers should now exist
[ -f /tmp/orch-detached-both-pre-completed ]
[ -f /tmp/orch-detached-both-post-completed ]
}
@test "non-detached commands complete before exit" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/non-detached-baseline"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-sync web
echo "output: $output"
echo "status: $status"
assert_success
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-sync --force web
echo "output: $output"
echo "status: $status"
assert_success
# both started AND completed markers should exist immediately
# because synchronous commands block until done
[ -f /tmp/orch-sync-pre-started ]
[ -f /tmp/orch-sync-pre-completed ]
[ -f /tmp/orch-sync-post-started ]
[ -f /tmp/orch-sync-post-completed ]
}
@test "shebang sh executes correctly" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/shebang-sh"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shebang-sh web
echo "output: $output"
echo "status: $status"
assert_success
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shebang-sh --force web
echo "output: $output"
echo "status: $status"
assert_success
[ -f /tmp/orch-shebang-sh-completed ]
}
@test "shebang bash executes correctly" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/shebang-bash"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shebang-bash web
echo "output: $output"
echo "status: $status"
assert_success
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shebang-bash --force web
echo "output: $output"
echo "status: $status"
assert_success
# marker is only created if BASH_VERSION is set, proving bash ran
[ -f /tmp/orch-shebang-bash-completed ]
}
@test "shebang dash executes correctly" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/shebang-dash"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shebang-dash web
echo "output: $output"
echo "status: $status"
assert_success
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shebang-dash --force web
echo "output: $output"
echo "status: $status"
assert_success
[ -f /tmp/orch-shebang-dash-completed ]
}
@test "shebang python3 executes correctly" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/shebang-python3"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shebang-python3 web
echo "output: $output"
echo "status: $status"
assert_success
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shebang-python3 --force web
echo "output: $output"
echo "status: $status"
assert_success
# marker is created via python3 pathlib, proving python3 ran
[ -f /tmp/orch-shebang-python3-completed ]
}
@test "default shebang uses sh" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/shebang-default"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shebang-default web
echo "output: $output"
echo "status: $status"
assert_success
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shebang-default --force web
echo "output: $output"
echo "status: $status"
assert_success
# no shebang in script, should default to #!/bin/sh
[ -f /tmp/orch-shebang-default-completed ]
}
@test "SHELL directive /bin/bash -c is detected for container scripts" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/shell-directive"
# build the custom image with SHELL ["/bin/bash", "-c"]
docker compose -p bats-shell-directive build
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shell-directive web
echo "output: $output"
echo "status: $status"
assert_success
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-shell-directive --force web
echo "output: $output"
echo "status: $status"
assert_success
# verify host command ran
[ -f /tmp/orch-shell-directive-host-completed ]
# check that the container script ran at all
run docker run --rm -v bats-shell-directive_shell-test:/data nginx:latest cat /data/executed
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "script-ran" "$output"
# check that bash was used (image SHELL directive detected)
# the script uses [[ ]] which only works in bash, not dash/sh
run docker run --rm -v bats-shell-directive_shell-test:/data nginx:latest cat /data/result
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "bash-ok" "$output"
}
@test "exited containers are cleaned up before deploy" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/exited-container-cleanup"
# Initial deploy: 3 running containers via docker-orchestrate
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-exited-cleanup web
echo "output: $output"
echo "status: $status"
assert_success
run docker ps --filter "label=com.docker.compose.project=bats-exited-cleanup" --filter "status=running" -q
echo "initial running containers: $output"
assert_equal "3" "$(echo "$output" | wc -l | tr -d ' ')"
# Get container IDs for manipulation
container_to_stop=$(docker ps --filter "label=com.docker.compose.project=bats-exited-cleanup" --filter "status=running" -q | sed -n '1p')
container_to_kill=$(docker ps --filter "label=com.docker.compose.project=bats-exited-cleanup" --filter "status=running" -q | sed -n '2p')
# Stop one container gracefully (exit code 0)
docker stop "$container_to_stop"
# Kill another container with SIGKILL (non-zero exit code)
docker kill --signal=KILL "$container_to_kill"
# Verify intermediate state: 1 running, 2 exited
run docker ps --filter "label=com.docker.compose.project=bats-exited-cleanup" --filter "status=running" -q
echo "running after stop/kill: $output"
assert_equal "1" "$(echo "$output" | wc -l | tr -d ' ')"
run docker ps -a --filter "label=com.docker.compose.project=bats-exited-cleanup" --filter "status=exited" -q
echo "exited after stop/kill: $output"
assert_equal "2" "$(echo "$output" | wc -l | tr -d ' ')"
# Verify exit codes: one should be 0, one should be non-zero
exited_container_0=$(docker ps -a --filter "label=com.docker.compose.project=bats-exited-cleanup" --filter "status=exited" -q | sed -n '1p')
exited_container_1=$(docker ps -a --filter "label=com.docker.compose.project=bats-exited-cleanup" --filter "status=exited" -q | sed -n '2p')
exit_code_0=$(docker inspect --format '{{.State.ExitCode}}' "$exited_container_0")
exit_code_1=$(docker inspect --format '{{.State.ExitCode}}' "$exited_container_1")
echo "exit codes: $exit_code_0, $exit_code_1"
# One should be 0 and one should be non-zero (137 for SIGKILL)
if [[ "$exit_code_0" -eq 0 && "$exit_code_1" -ne 0 ]] || [[ "$exit_code_0" -ne 0 && "$exit_code_1" -eq 0 ]]; then
echo "exit code verification passed: one is 0, one is non-zero"
else
flunk "expected one exit code 0 and one non-zero, got: $exit_code_0 and $exit_code_1"
fi
# Redeploy with --replicas 3: should clean up exited containers and reach 3 running
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-exited-cleanup --replicas 3 web
echo "output: $output"
echo "status: $status"
assert_success
# Verify final state: 3 running, 0 exited
run docker ps --filter "label=com.docker.compose.project=bats-exited-cleanup" --filter "status=running" -q
echo "final running: $output"
assert_equal "3" "$(echo "$output" | wc -l | tr -d ' ')"
run docker ps -a --filter "label=com.docker.compose.project=bats-exited-cleanup" --filter "status=exited" -q
echo "final exited: $output"
assert_equal "" "$output"
}
@test "compose spec pre_stop hooks execute inside container" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/pre-stop-hooks"
export BATS_HOST_TEST_VAR="interpolated-ok"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-pre-stop-hooks web
echo "output: $output"
echo "status: $status"
assert_success
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-pre-stop-hooks --force web
echo "output: $output"
echo "status: $status"
assert_success
# Verify host commands ran
[ -f /tmp/orch-pre-stop-hooks-host-completed ]
[ -f /tmp/orch-pre-stop-hooks-post-completed ]
# Verify x-pre-stop-command ran inside container
run docker run --rm -v bats-pre-stop-hooks_hook-test:/data nginx:latest cat /data/pre-stop-cmd
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "script-ran" "$output"
# Verify first pre_stop hook ran
run docker run --rm -v bats-pre-stop-hooks_hook-test:/data nginx:latest cat /data/hook1
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "hook1-ran" "$output"
# Verify second pre_stop hook ran
run docker run --rm -v bats-pre-stop-hooks_hook-test:/data nginx:latest cat /data/hook2
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "hook2-ran" "$output"
# Verify third hook with direct environment value
run docker run --rm -v bats-pre-stop-hooks_hook-test:/data nginx:latest cat /data/hook3
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "env-ok" "$output"
# Verify fourth hook with compose-level ${VAR} interpolation from host env
run docker run --rm -v bats-pre-stop-hooks_hook-test:/data nginx:latest cat /data/hook4
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "interpolated-ok" "$output"
}
@test "compose spec post_start hooks execute inside container during scale-up" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/post-start-hooks"
export BATS_HOST_TEST_VAR="interpolated-ok"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-post-start-hooks web
echo "output: $output"
echo "status: $status"
assert_success
# Verify first post_start hook ran
run docker run --rm -v bats-post-start-hooks_hook-test:/data nginx:latest cat /data/hook1
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "hook1-ran" "$output"
# Verify second post_start hook ran
run docker run --rm -v bats-post-start-hooks_hook-test:/data nginx:latest cat /data/hook2
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "hook2-ran" "$output"
# Verify third hook with direct environment value
run docker run --rm -v bats-post-start-hooks_hook-test:/data nginx:latest cat /data/hook3
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "env-ok" "$output"
# Verify fourth hook with compose-level ${VAR} interpolation from host env
run docker run --rm -v bats-post-start-hooks_hook-test:/data nginx:latest cat /data/hook4
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "interpolated-ok" "$output"
}
@test "multiple compose files via repeated --file flag" {
run "$DOCKER_ORCHESTRATE" deploy \
--file "${BATS_TEST_DIRNAME}/tests/fixtures/multiple-files/docker-compose.yaml" \
--file "${BATS_TEST_DIRNAME}/tests/fixtures/multiple-files/docker-compose.override.yaml" \
--project-name bats-multi-file web
echo "output: $output"
echo "status: $status"
assert_success
# Verify the override was applied by checking the env var on the running container
container_id=$(docker ps --filter "label=com.docker.compose.project=bats-multi-file" --filter "status=running" -q | head -1)
run docker exec "$container_id" printenv OVERRIDE_APPLIED
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "true" "$output"
}
@test "env file via repeated --env-file flag" {
run "$DOCKER_ORCHESTRATE" deploy \
--file "${BATS_TEST_DIRNAME}/tests/fixtures/env-file/docker-compose.yaml" \
--env-file "${BATS_TEST_DIRNAME}/tests/fixtures/env-file/custom.env" \
--project-name bats-env-file web
echo "output: $output"
echo "status: $status"
assert_success
# Verify the env file was applied by checking the env var on the running container
container_id=$(docker ps --filter "label=com.docker.compose.project=bats-env-file" --filter "status=running" -q | head -1)
run docker exec "$container_id" printenv FROM_ENV_FILE
echo "output: $output"
echo "status: $status"
assert_success
assert_equal "env-file-works" "$output"
}
@test "pull policy from compose spec is respected" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/pull-policy"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-pull-policy web
echo "output: $output"
echo "status: $status"
assert_success
# Verify container is running
run docker ps --filter "label=com.docker.compose.project=bats-pull-policy" --filter "status=running" -q
echo "running containers: $output"
assert_equal "1" "$(echo "$output" | wc -l | tr -d ' ')"
}
@test "pull policy CLI flag overrides compose spec" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/pull-policy"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-pull-policy --pull missing web
echo "output: $output"
echo "status: $status"
assert_success
# Verify container is running
run docker ps --filter "label=com.docker.compose.project=bats-pull-policy" --filter "status=running" -q
echo "running containers: $output"
assert_equal "1" "$(echo "$output" | wc -l | tr -d ' ')"
}
@test "invalid pull policy is rejected" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/pull-policy"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-pull-policy --pull invalid web
echo "output: $output"
echo "status: $status"
assert_failure
assert_output_contains "invalid --pull value"
}
@test "pull policy if_not_present maps to missing" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/pull-policy-if-not-present"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-pull-policy-ifnp web
echo "output: $output"
echo "status: $status"
assert_success
# Verify container is running
run docker ps --filter "label=com.docker.compose.project=bats-pull-policy-ifnp" --filter "status=running" -q
echo "running containers: $output"
assert_equal "1" "$(echo "$output" | wc -l | tr -d ' ')"
}
@test "pull policy build from compose spec builds and deploys" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/pull-policy-build"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-pull-policy-build web
echo "output: $output"
echo "status: $status"
assert_success
# Verify container is running
run docker ps --filter "label=com.docker.compose.project=bats-pull-policy-build" --filter "status=running" -q
echo "running containers: $output"
assert_equal "1" "$(echo "$output" | wc -l | tr -d ' ')"
}
@test "build CLI flag builds and deploys" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/pull-policy-build"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-pull-policy-build --build --pull never web
echo "output: $output"
echo "status: $status"
assert_success
# Verify container is running
run docker ps --filter "label=com.docker.compose.project=bats-pull-policy-build" --filter "status=running" -q
echo "running containers: $output"
assert_equal "1" "$(echo "$output" | wc -l | tr -d ' ')"
}
@test "image prune removes leftover images from previous builds" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/image-prune"
reset_image_prune
export TAG=v1
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-image-prune --build --force web
echo "output: $output"
echo "status: $status"
assert_success
export TAG=v2
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-image-prune --build --force web
echo "output: $output"
echo "status: $status"
assert_success
# The previous deploy left bats-image-prune-web:v1 behind, still project-labeled
run docker images bats-image-prune-web:v1 -q
echo "v1 before prune: $output"
[[ -n "$output" ]] || flunk "expected the previous image to be present before prune"
run "$DOCKER_ORCHESTRATE" image prune --project-name bats-image-prune
echo "output: $output"
echo "status: $status"
assert_success
# The leftover image is gone
run docker images bats-image-prune-web:v1 -q
echo "v1 after prune: $output"
assert_equal "" "$output"
# The current image is kept
run docker images bats-image-prune-web:v2 -q
echo "v2 after prune: $output"
[[ -n "$output" ]] || flunk "expected the current image to be kept"
reset_image_prune
}
@test "image prune --dry-run reports without removing" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/image-prune"
reset_image_prune
export TAG=v1
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-image-prune --build --force web
assert_success
export TAG=v2
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-image-prune --build --force web
assert_success
run "$DOCKER_ORCHESTRATE" image prune --project-name bats-image-prune --dry-run
echo "output: $output"
echo "status: $status"
assert_success
assert_output_contains "Would remove image"
# Dry-run did not remove the leftover
run docker images bats-image-prune-web:v1 -q
echo "v1 after dry-run: $output"
[[ -n "$output" ]] || flunk "expected dry-run to leave the leftover image in place"
reset_image_prune
}
@test "deploy --prune-images prunes old images after a rebuild" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/image-prune"
reset_image_prune
export TAG=v1
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-image-prune --build --force web
assert_success
export TAG=v2
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-image-prune --build --force --prune-images web
echo "output: $output"
echo "status: $status"
assert_success
# The deploy pruned the previous image
run docker images bats-image-prune-web:v1 -q
echo "v1 after prune-images deploy: $output"
assert_equal "" "$output"
# The current image is kept
run docker images bats-image-prune-web:v2 -q
[[ -n "$output" ]] || flunk "expected the current image to be kept"
reset_image_prune
}
@test "x-healthcheck-wait delays before treating container as healthy" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/healthcheck-wait"
# Record start time
start_time=$(date +%s)
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-healthcheck-wait web
echo "output: $output"
echo "status: $status"
assert_success
# Record end time
end_time=$(date +%s)
elapsed=$((end_time - start_time))
# The deploy should take at least 3 seconds due to x-healthcheck-wait
# (we check >= 2 to allow for timing variance)
if [[ "$elapsed" -lt 2 ]]; then
flunk "expected deploy to take at least 2s due to x-healthcheck-wait, took ${elapsed}s"
fi
# Verify the container is running
run docker ps --filter "label=com.docker.compose.project=bats-healthcheck-wait" --filter "status=running" -q
echo "running containers: $output"
assert_equal "1" "$(echo "$output" | wc -l | tr -d ' ')"
}
@test "x-wait-after-healthy delays after container becomes healthy" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/wait-after-healthy"
# Record start time
start_time=$(date +%s)
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-wait-after-healthy web
echo "output: $output"
echo "status: $status"
assert_success
# Record end time
end_time=$(date +%s)
elapsed=$((end_time - start_time))
# The deploy should take at least 3 seconds due to x-wait-after-healthy
# (we check >= 2 to allow for timing variance)
if [[ "$elapsed" -lt 2 ]]; then
flunk "expected deploy to take at least 2s due to x-wait-after-healthy, took ${elapsed}s"
fi
# Verify the container is running
run docker ps --filter "label=com.docker.compose.project=bats-wait-after-healthy" --filter "status=running" -q
echo "running containers: $output"
assert_equal "1" "$(echo "$output" | wc -l | tr -d ' ')"
}
@test "unhealthy containers are replaced first during rolling update" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/unhealthy-priority"
# Initial deploy with 3 replicas
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-unhealthy-priority --force web
echo "output: $output"
echo "status: $status"
assert_success
# Wait for all containers to become healthy
sleep 6
# Verify all 3 containers are healthy
healthy_count=0
for id in $(docker ps -q --filter "label=com.docker.compose.project=bats-unhealthy-priority" --filter "status=running"); do
health=$(docker inspect --format '{{.State.Health.Status}}' "$id")
if [[ "$health" == "healthy" ]]; then
healthy_count=$((healthy_count + 1))
fi
done
assert_equal "3" "$healthy_count"
# Pick one container and make it unhealthy
unhealthy_id=$(docker ps -q --filter "label=com.docker.compose.project=bats-unhealthy-priority" --filter "status=running" | head -1)
docker exec "$unhealthy_id" rm /tmp/healthy
# Wait for Docker to mark it unhealthy
sleep 6
# Verify it's actually unhealthy
health=$(docker inspect --format '{{.State.Health.Status}}' "$unhealthy_id")
assert_equal "unhealthy" "$health"
# Redeploy (rolling update)
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-unhealthy-priority --force web
echo "output: $output"
echo "status: $status"
assert_success
# Verify the output contains the prioritization message
assert_output_contains "Prioritizing 1 unhealthy container(s) for replacement"
# The unhealthy container should have been replaced (no longer running)
run docker ps -q --filter "id=$unhealthy_id" --filter "status=running"
assert_equal "" "$output"
# Verify we still have 3 running containers
count=$(docker ps -q --filter "label=com.docker.compose.project=bats-unhealthy-priority" --filter "status=running" | wc -l | tr -d ' ')
assert_equal "3" "$count"
}
@test "one-shot service (restart: no) runs to completion" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/one-shot-success"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-one-shot-success migrate
echo "output: $output"
echo "status: $status"
assert_success
assert_output_contains "One-shot service migrate completed successfully"
}
@test "one-shot service (restart: no) exit 1 aborts deployment" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/one-shot-failure"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-one-shot-failure migrate
echo "output: $output"
echo "status: $status"
assert_failure
}
@test "one-shot service leaves no running containers" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/one-shot-success"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-one-shot-success migrate
echo "output: $output"
echo "status: $status"
assert_success
# No containers should be running (--rm removes them)
run docker ps --filter "label=com.docker.compose.project=bats-one-shot-success" --filter "status=running" -q
echo "running containers: $output"
assert_equal "" "$output"
}
@test "one-shot pre-deploy runs migrate before web in project deploy" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/one-shot-pre-deploy"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-one-shot-pre-deploy
echo "output: $output"
echo "status: $status"
assert_success
# One-shot migrate should have completed
assert_output_contains "One-shot service migrate completed successfully"
# Web should be running
run docker ps --filter "label=com.docker.compose.project=bats-one-shot-pre-deploy" --filter "label=com.docker.compose.service=web" --filter "status=running" -q
echo "web running containers: $output"
assert_equal "1" "$(echo "$output" | wc -l | tr -d ' ')"
# Migrate should have no running containers (--rm removed it)
run docker ps --filter "label=com.docker.compose.project=bats-one-shot-pre-deploy" --filter "label=com.docker.compose.service=migrate" --filter "status=running" -q
echo "migrate running containers: $output"
assert_equal "" "$output"
}
@test "one-shot post-deploy runs warm-cache after web in project deploy" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/one-shot-post-deploy"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-one-shot-post-deploy
echo "output: $output"
echo "status: $status"
assert_success
# warm-cache one-shot should have completed (check before other run commands overwrite $output)
assert_output_contains "One-shot service warm-cache completed successfully"
# Web should be running
run docker ps --filter "label=com.docker.compose.project=bats-one-shot-post-deploy" --filter "label=com.docker.compose.service=web" --filter "status=running" -q
echo "web running containers: $output"
assert_equal "1" "$(echo "$output" | wc -l | tr -d ' ')"
# warm-cache should have no running containers
run docker ps --filter "label=com.docker.compose.project=bats-one-shot-post-deploy" --filter "label=com.docker.compose.service=warm-cache" --filter "status=running" -q
echo "warm-cache running containers: $output"
assert_equal "" "$output"
}
@test "one-shot failure aborts deployment of subsequent services" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/one-shot-failure-aborts"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-one-shot-failure-aborts
echo "output: $output"
echo "status: $status"
assert_failure
# Web should NOT be running (deployment aborted before reaching it)
run docker ps --filter "label=com.docker.compose.project=bats-one-shot-failure-aborts" --filter "label=com.docker.compose.service=web" --filter "status=running" -q
echo "web running containers: $output"
assert_equal "" "$output"
}
@test "one-shot without depends_on runs before web without depends_on in project deploy" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/one-shot-priority"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-one-shot-priority
echo "output: $output"
echo "status: $status"
assert_success
# One-shot migrate should have completed
assert_output_contains "One-shot service migrate completed successfully"
# Verify ordering: one-shot message should appear before web deployment message
deploy_output="$output"
migrate_line=$(echo "$deploy_output" | grep -n "Running one-shot service migrate" | head -1 | cut -d: -f1)
web_line=$(echo "$deploy_output" | grep -n "Deploying service web" | head -1 | cut -d: -f1)
if [[ "$migrate_line" -ge "$web_line" ]]; then
flunk "expected one-shot migrate (line $migrate_line) to run before web deploy (line $web_line)"
fi
# Web should be running
run docker ps --filter "label=com.docker.compose.project=bats-one-shot-priority" --filter "label=com.docker.compose.service=web" --filter "status=running" -q
echo "web running containers: $output"
assert_equal "1" "$(echo "$output" | wc -l | tr -d ' ')"
}
@test "one-shot service ignores --replicas flag" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/one-shot-success"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-one-shot-success --replicas 3 migrate
echo "output: $output"
echo "status: $status"
assert_success
# One-shot should complete successfully regardless of --replicas
assert_output_contains "One-shot service migrate completed successfully"
# No containers should be running (--rm removes them, one-shot ignores replicas)
run docker ps --filter "label=com.docker.compose.project=bats-one-shot-success" --filter "status=running" -q
echo "running containers: $output"
assert_equal "" "$output"
}
# =====================================================
# Per-service deploy host command tests
# =====================================================
@test "per-service pre-deploy host command runs before deploy" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/pre-deploy-host-command"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-pre-deploy-cmd web
echo "output: $output"
echo "status: $status"
assert_success
# pre-deploy marker should exist
[ -f /tmp/orch-pre-deploy-started ]
}
@test "per-service post-deploy host command runs after successful deploy" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/post-deploy-host-command"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-post-deploy-cmd web
echo "output: $output"
echo "status: $status"
assert_success
# post-deploy marker should exist
[ -f /tmp/orch-post-deploy-completed ]
}
@test "both per-service pre and post deploy host commands run in correct order" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/deploy-host-command-both"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-deploy-both web
echo "output: $output"
echo "status: $status"
assert_success
# both markers should exist
[ -f /tmp/orch-deploy-both-pre-time ]
[ -f /tmp/orch-deploy-both-post-time ]
pre_time=$(cat /tmp/orch-deploy-both-pre-time)
post_time=$(cat /tmp/orch-deploy-both-post-time)
if [[ "$pre_time" -gt "$post_time" ]]; then
flunk "expected pre-deploy (${pre_time}) to run before post-deploy (${post_time})"
fi
}
# =====================================================
# Project-level deploy host command tests
# =====================================================
@test "project-level pre-deploy host command runs before deploy" {
cd "${BATS_TEST_DIRNAME}/tests/fixtures/project-pre-deploy-host-command"
run "$DOCKER_ORCHESTRATE" deploy --project-name bats-project-pre-deploy
echo "output: $output"
echo "status: $status"