-
Notifications
You must be signed in to change notification settings - Fork 413
/
test_e2e.sh
executable file
·3704 lines (3315 loc) · 116 KB
/
test_e2e.sh
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
#!/bin/bash
#
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
# shellcheck disable=SC2120
function caller() {
local stack_skip=${1:-0}
stack_skip=$((stack_skip + 1))
if [[ ${#FUNCNAME[@]} -gt ${stack_skip} ]]; then
local i
for ((i=1 ; i <= ${#FUNCNAME[@]} - stack_skip ; i++))
do
local frame_no=$((i - 1 + stack_skip))
local source_lineno=${BASH_LINENO[$((frame_no - 1))]}
local funcname=${FUNCNAME[${frame_no}]}
if [[ "$funcname" =~ 'e2e::' ]]; then
echo "${source_lineno}"
fi
done
fi
}
function fail() {
echo "FAIL: line $(caller):" "$@" >&3
return 42
}
function skip() {
echo "SKIP" >&3
return 43
}
function pass() {
echo "PASS"
}
# $1: a file/dir name
# $2: max seconds to wait
function wait_for_file_exists() {
local file=$1
local ticks=$(($2*10)) # 100ms per tick
while (( ticks > 0 )); do
if [[ -f "$file" ]]; then
break
fi
sleep 0.1
ticks=$((ticks-1))
done
}
function assert_link_exists() {
if ! [[ -e "$1" ]]; then
fail "$1 does not exist"
fi
if ! [[ -L "$1" ]]; then
fail "$1 is not a symlink"
fi
}
function assert_link_basename_eq() {
if [[ $(basename "$(readlink "$1")") == "$2" ]]; then
return
fi
fail "$1 does not point to $2: $(readlink "$1")"
}
function assert_file_exists() {
if ! [[ -f "$1" ]]; then
fail "$1 does not exist"
fi
}
function assert_file_absent() {
if [[ -f "$1" ]]; then
fail "$1 exists but should not"
fi
}
function assert_file_eq() {
if [[ $(cat "$1") == "$2" ]]; then
return
fi
fail "$1 does not contain '$2': $(cat "$1")"
}
function assert_file_contains() {
if grep -q "$2" "$1"; then
return
fi
fail "$1 does not contain '$2': $(cat "$1")"
}
function assert_file_lines_eq() {
N=$(wc -l < "$1")
if (( "$N" != "$2" )); then
fail "$1 is not $2 lines: $N"
fi
}
function assert_file_lines_ge() {
N=$(wc -l < "$1")
if (( "$N" < "$2" )); then
fail "$1 is not at least $2 lines: $N"
fi
}
function assert_metric_eq() {
local val
val="$(curl --silent "http://localhost:$HTTP_PORT/metrics" \
| grep "^$1 " \
| awk '{print $NF}')"
if [[ "${val}" == "$2" ]]; then
return
fi
fail "metric $1 was expected to be '$2': ${val}"
}
function assert_fail() {
(
set +o errexit
"$@"
RET=$?
if [[ "$RET" != 0 ]]; then
return
fi
fail "expected non-zero exit code, got $RET"
)
}
# Helper: run a docker container.
function docker_run() {
RM="--rm"
if [[ "${CLEANUP:-}" == 0 ]]; then
RM=""
fi
docker run \
-d \
${RM} \
--label git-sync-e2e="$RUNID" \
"$@"
sleep 2 # wait for it to come up
}
# Helper: get the IP of a docker container.
function docker_ip() {
if [[ -z "$1" ]]; then
echo "usage: $0 <id>"
return 1
fi
docker inspect "$1" | jq -r .[0].NetworkSettings.IPAddress
}
function docker_kill() {
if [[ -z "$1" ]]; then
echo "usage: $0 <id>"
return 1
fi
docker kill "$1" >/dev/null
}
function docker_signal() {
if [[ -z "$1" || -z "$2" ]]; then
echo "usage: $0 <id> <signal>"
return 1
fi
docker kill "--signal=$2" "$1" >/dev/null
}
# E2E_TAG is the tag used for docker builds. This is needed because docker
# tags are system-global, but one might have multiple repos checked out.
E2E_TAG=$(git rev-parse --show-toplevel | sed 's|/|_|g')
# Setting GIT_SYNC_E2E_IMAGE forces the test to use a specific image instead of the
# current tree.
build_container=false
if [[ "${GIT_SYNC_E2E_IMAGE:-unset}" == "unset" ]]; then
GIT_SYNC_E2E_IMAGE="e2e/git-sync:${E2E_TAG}__$(go env GOOS)_$(go env GOARCH)"
build_container=true
fi
# DIR is the directory in which all this test's state lives.
RUNID="${RANDOM}${RANDOM}"
DIR="/tmp/git-sync-e2e.$RUNID"
mkdir "$DIR"
function final_cleanup() {
if [[ "${CLEANUP:-}" == 0 ]]; then
echo "leaving logs in $DIR"
else
rm -rf "$DIR"
fi
}
# Set the trap to call the final_cleanup function on exit.
trap final_cleanup EXIT
skip_github_app_test="${SKIP_GITHUB_APP_TEST:-true}"
required_env_vars=()
LOCAL_GITHUB_APP_PRIVATE_KEY_FILE="github_app_private_key.pem"
GITHUB_APP_PRIVATE_KEY_MOUNT=()
if [[ "${skip_github_app_test}" != "true" ]]; then
required_env_vars=(
"TEST_GITHUB_APP_AUTH_TEST_REPO"
"TEST_GITHUB_APP_APPLICATION_ID"
"TEST_GITHUB_APP_INSTALLATION_ID"
"TEST_GITHUB_APP_CLIENT_ID"
)
if [[ -n "${TEST_GITHUB_APP_PRIVATE_KEY_FILE:-}" && -n "${TEST_GITHUB_APP_PRIVATE_KEY:-}" ]]; then
echo "ERROR: Both TEST_GITHUB_APP_PRIVATE_KEY_FILE and TEST_GITHUB_APP_PRIVATE_KEY were specified."
exit 1
fi
if [[ -n "${TEST_GITHUB_APP_PRIVATE_KEY_FILE:-}" ]]; then
cp "${TEST_GITHUB_APP_PRIVATE_KEY_FILE}" "${DIR}/${LOCAL_GITHUB_APP_PRIVATE_KEY_FILE}"
elif [[ -n "${TEST_GITHUB_APP_PRIVATE_KEY:-}" ]]; then
echo "${TEST_GITHUB_APP_PRIVATE_KEY}" > "${DIR}/${LOCAL_GITHUB_APP_PRIVATE_KEY_FILE}"
else
echo "ERROR: Neither TEST_GITHUB_APP_PRIVATE_KEY_FILE nor TEST_GITHUB_APP_PRIVATE_KEY was specified."
echo " Either provide a value or skip this test (SKIP_GITHUB_APP_TEST=true)."
exit 1
fi
# Validate all required environment variables for the github-app-auth tests are provided.
for var in "${required_env_vars[@]}"; do
if [[ ! -v "${var}" ]]; then
echo "ERROR: Required environment variable '${var}' is not set."
echo " Either provide a value or skip this test (SKIP_GITHUB_APP_TEST=true)."
exit 1
fi
done
# Mount the GitHub App private key file to the git-sync container
GITHUB_APP_PRIVATE_KEY_MOUNT=(-v "${DIR}/${LOCAL_GITHUB_APP_PRIVATE_KEY_FILE}":"/${LOCAL_GITHUB_APP_PRIVATE_KEY_FILE}":ro)
fi
# WORK is temp space and in reset for each testcase.
WORK="$DIR/work"
function clean_work() {
rm -rf "$WORK"
mkdir -p "$WORK"
}
# REPO and REPO2 are the source repos under test.
REPO="$DIR/repo"
REPO2="${REPO}2"
MAIN_BRANCH="e2e-branch"
function init_repo() {
arg="${1}"
rm -rf "$REPO"
mkdir -p "$REPO"
git -C "$REPO" init -q -b "$MAIN_BRANCH"
echo "$arg" > "$REPO/file"
git -C "$REPO" add file
git -C "$REPO" commit -aqm "init file"
rm -rf "$REPO2"
cp -r "$REPO" "$REPO2"
}
# ROOT is the volume (usually) used as --root.
ROOT="$DIR/root"
function clean_root() {
rm -rf "$ROOT"
mkdir -p "$ROOT"
chmod g+rwx "$ROOT"
}
# How long we wait for sync operations to happen between test steps, in seconds
MAXWAIT="${MAXWAIT:-3}"
# INTERLOCK is a file, under $ROOT, used to coordinate tests and syncs.
INTERLOCK="_sync_lock"
function wait_for_sync() {
if [[ -z "$1" ]]; then
echo "usage: $0 <max-wait-seconds>"
return 1
fi
local path="$ROOT/$INTERLOCK"
wait_for_file_exists "$path" "$1"
rm -f "$path"
}
# Init SSH for test cases.
DOT_SSH="$DIR/dot_ssh"
for i in $(seq 1 3); do
mkdir -p "$DOT_SSH/$i"
ssh-keygen -f "$DOT_SSH/$i/id_test" -P "" >/dev/null
cp -a "$DOT_SSH/$i/id_test" "$DOT_SSH/$i/id_local" # for outside-of-container use
mkdir -p "$DOT_SSH/server/$i"
cat "$DOT_SSH/$i/id_test.pub" > "$DOT_SSH/server/$i/authorized_keys"
done
# Allow files to be read inside containers running as a different UID.
# Note: this does not include the *.local forms.
chmod g+r "$DOT_SSH"/*/id_test* "$DOT_SSH"/server/*
TEST_TOOLS="_test_tools"
SLOW_GIT_FETCH="$TEST_TOOLS/git_slow_fetch.sh"
ASKPASS_GIT="$TEST_TOOLS/git_askpass.sh"
EXECHOOK_COMMAND="$TEST_TOOLS/exechook_command.sh"
EXECHOOK_COMMAND_FAIL="$TEST_TOOLS/exechook_command_fail.sh"
EXECHOOK_COMMAND_SLEEPY="$TEST_TOOLS/exechook_command_with_sleep.sh"
EXECHOOK_COMMAND_FAIL_SLEEPY="$TEST_TOOLS/exechook_command_fail_with_sleep.sh"
EXECHOOK_ENVKEY=ENVKEY
EXECHOOK_ENVVAL=envval
RUNLOG="$DIR/runlog"
rm -f "$RUNLOG"
touch "$RUNLOG"
chmod g+rw "$RUNLOG"
HTTP_PORT=9376
METRIC_GOOD_SYNC_COUNT='git_sync_count_total{status="success"}'
METRIC_FETCH_COUNT='git_fetch_count_total'
function GIT_SYNC() {
#./bin/linux_amd64/git-sync "$@"
RM="--rm"
if [[ "${CLEANUP:-}" == 0 ]]; then
RM=""
fi
docker run \
-i \
${RM} \
--label git-sync-e2e="$RUNID" \
--network="host" \
-u git-sync:"$(id -g)" `# rely on GID, triggering "dubious ownership"` \
-v "$ROOT":"$ROOT":rw \
-v "$REPO":"$REPO":ro \
-v "$REPO2":"$REPO2":ro \
-v "$WORK":"$WORK":ro \
-v "$(pwd)/$TEST_TOOLS":"/$TEST_TOOLS":ro \
--env "$EXECHOOK_ENVKEY=$EXECHOOK_ENVVAL" \
-v "$RUNLOG":/var/log/runs \
-v "$DOT_SSH/1/id_test":"/ssh/secret.1":ro \
-v "$DOT_SSH/2/id_test":"/ssh/secret.2":ro \
-v "$DOT_SSH/3/id_test":"/ssh/secret.3":ro \
"${GITHUB_APP_PRIVATE_KEY_MOUNT[@]}" \
"${GIT_SYNC_E2E_IMAGE}" \
-v=6 \
--add-user \
--group-write \
--touch-file="$INTERLOCK" \
--git-config='protocol.file.allow:always' \
--http-bind=":$HTTP_PORT" \
--http-metrics \
--http-pprof \
"$@"
}
function remove_containers() {
sleep 2 # Let docker finish saving container metadata
docker ps --filter label="git-sync-e2e=$RUNID" --format="{{.ID}}" \
| while read -r CTR; do
docker kill "$CTR" >/dev/null
done
}
#
# After all the test functions are defined, we can iterate over them and run
# them all automatically. See the end of this file.
#
##############################################
# Test init when root doesn't exist
##############################################
function e2e::init_root_doesnt_exist() {
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--root="$ROOT/subdir" \
--link="link"
assert_link_exists "$ROOT/subdir/link"
assert_file_exists "$ROOT/subdir/link/file"
assert_file_eq "$ROOT/subdir/link/file" "${FUNCNAME[0]}"
}
##############################################
# Test init when root exists and is empty
##############################################
function e2e::init_root_exists_empty() {
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]}"
}
##############################################
# Test init with a weird --root flag
##############################################
function e2e::init_root_flag_is_weird() {
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--root="../../../../../$ROOT/../../../../../../$ROOT" \
--link="link"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]}"
}
##############################################
# Test init with a symlink in --root
##############################################
function e2e::init_root_flag_has_symlink() {
mkdir -p "$ROOT/subdir"
ln -s "$ROOT/subdir" "$ROOT/rootlink" # symlink to test
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--root="$ROOT/rootlink" \
--link="link"
assert_link_exists "$ROOT/subdir/link"
assert_file_exists "$ROOT/subdir/link/file"
assert_file_eq "$ROOT/subdir/link/file" "${FUNCNAME[0]}"
}
##############################################
# Test init when root is under a git repo
##############################################
function e2e::init_root_is_under_another_repo() {
# Make a parent dir that is a git repo.
mkdir -p "$ROOT/subdir/root"
date > "$ROOT/subdir/root/file" # so it is not empty
git -C "$ROOT/subdir" init -q
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--root="$ROOT/subdir/root" \
--link="link"
assert_link_exists "$ROOT/subdir/root/link"
assert_file_exists "$ROOT/subdir/root/link/file"
assert_file_eq "$ROOT/subdir/root/link/file" "${FUNCNAME[0]}"
}
##############################################
# Test init when root fails sanity
##############################################
function e2e::init_root_fails_sanity() {
# Make an invalid git repo.
git -C "$ROOT" init -q
echo "ref: refs/heads/nonexist" > "$ROOT/.git/HEAD"
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]}"
}
##############################################
# Test non-zero exit with a bad ref
##############################################
function e2e::bad_ref_non_zero_exit() {
assert_fail \
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--ref=does-not-exist \
--root="$ROOT" \
--link="link"
assert_file_absent "$ROOT/link"
}
##############################################
# Test default ref syncing
##############################################
function e2e::sync_default_ref() {
# First sync
echo "${FUNCNAME[0]} 1" > "$REPO/file"
git -C "$REPO" commit -qam "${FUNCNAME[0]} 1"
git -C "$REPO" checkout -q -b weird-name
GIT_SYNC \
--period=100ms \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link" \
&
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]} 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
assert_metric_eq "${METRIC_FETCH_COUNT}" 1
# Move forward
echo "${FUNCNAME[0]} 2" > "$REPO/file"
git -C "$REPO" commit -qam "${FUNCNAME[0]} 2"
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]} 2"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
assert_metric_eq "${METRIC_FETCH_COUNT}" 2
# Move backward
git -C "$REPO" reset -q --hard HEAD^
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]} 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 3
assert_metric_eq "${METRIC_FETCH_COUNT}" 3
}
##############################################
# Test HEAD syncing
##############################################
function e2e::sync_head() {
# First sync
echo "${FUNCNAME[0]} 1" > "$REPO/file"
git -C "$REPO" commit -qam "${FUNCNAME[0]} 1"
GIT_SYNC \
--period=100ms \
--repo="file://$REPO" \
--ref=HEAD \
--root="$ROOT" \
--link="link" \
&
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]} 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
assert_metric_eq "${METRIC_FETCH_COUNT}" 1
# Move HEAD forward
echo "${FUNCNAME[0]} 2" > "$REPO/file"
git -C "$REPO" commit -qam "${FUNCNAME[0]} 2"
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]} 2"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
assert_metric_eq "${METRIC_FETCH_COUNT}" 2
# Move HEAD backward
git -C "$REPO" reset -q --hard HEAD^
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]} 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 3
assert_metric_eq "${METRIC_FETCH_COUNT}" 3
}
##############################################
# Test sync with an absolute-path link
##############################################
function e2e::sync_head_absolute_link() {
# First sync
echo "${FUNCNAME[0]} 1" > "$REPO/file"
git -C "$REPO" commit -qam "${FUNCNAME[0]} 1"
GIT_SYNC \
--period=100ms \
--repo="file://$REPO" \
--ref=HEAD \
--root="$ROOT/root" \
--link="$ROOT/other/dir/link" \
&
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/root/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "${FUNCNAME[0]} 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
assert_metric_eq "${METRIC_FETCH_COUNT}" 1
# Move HEAD forward
echo "${FUNCNAME[0]} 2" > "$REPO/file"
git -C "$REPO" commit -qam "${FUNCNAME[0]} 2"
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/root/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "${FUNCNAME[0]} 2"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
assert_metric_eq "${METRIC_FETCH_COUNT}" 2
# Move HEAD backward
git -C "$REPO" reset -q --hard HEAD^
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/root/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "${FUNCNAME[0]} 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 3
assert_metric_eq "${METRIC_FETCH_COUNT}" 3
}
##############################################
# Test sync with a subdir-path link
##############################################
function e2e::sync_head_subdir_link() {
# First sync
echo "${FUNCNAME[0]} 1" > "$REPO/file"
git -C "$REPO" commit -qam "${FUNCNAME[0]} 1"
GIT_SYNC \
--period=100ms \
--repo="file://$REPO" \
--ref=HEAD \
--root="$ROOT" \
--link="other/dir/link" \
&
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "${FUNCNAME[0]} 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
assert_metric_eq "${METRIC_FETCH_COUNT}" 1
# Move HEAD forward
echo "${FUNCNAME[0]} 2" > "$REPO/file"
git -C "$REPO" commit -qam "${FUNCNAME[0]} 2"
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "${FUNCNAME[0]} 2"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
assert_metric_eq "${METRIC_FETCH_COUNT}" 2
# Move HEAD backward
git -C "$REPO" reset -q --hard HEAD^
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "${FUNCNAME[0]} 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 3
assert_metric_eq "${METRIC_FETCH_COUNT}" 3
}
##############################################
# Test worktree-cleanup
##############################################
function e2e::worktree_cleanup() {
GIT_SYNC \
--period=100ms \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link" \
&
# wait for first sync
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]}"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
assert_metric_eq "${METRIC_FETCH_COUNT}" 1
# suspend time so we can fake corruption
docker ps --filter label="git-sync-e2e=$RUNID" --format="{{.ID}}" \
| while read -r CTR; do
docker pause "$CTR" >/dev/null
done
# make a second commit
echo "${FUNCNAME[0]}-ok" > "$REPO/file2"
git -C "$REPO" add file2
git -C "$REPO" commit -qam "${FUNCNAME[0]} new file"
# make a worktree to collide with git-sync
SHA=$(git -C "$REPO" rev-list -n1 HEAD)
git -C "$REPO" worktree add -q "$ROOT/.worktrees/$SHA" -b e2e --no-checkout
chmod g+w "$ROOT/.worktrees/$SHA"
# add some garbage
mkdir -p "$ROOT/.worktrees/not_a_hash/subdir"
touch "$ROOT/.worktrees/not_a_directory"
# resume time
docker ps --filter label="git-sync-e2e=$RUNID" --format="{{.ID}}" \
| while read -r CTR; do
docker unpause "$CTR" >/dev/null
done
wait_for_sync "${MAXWAIT}"
assert_file_exists "$ROOT/link/file2"
assert_file_eq "$ROOT/link/file2" "${FUNCNAME[0]}-ok"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
assert_metric_eq "${METRIC_FETCH_COUNT}" 2
assert_file_absent "$ROOT/.worktrees/$SHA"
assert_file_absent "$ROOT/.worktrees/not_a_hash"
assert_file_absent "$ROOT/.worktrees/not_a_directory"
}
##############################################
# Test worktree unexpected removal
##############################################
function e2e::worktree_unexpected_removal() {
GIT_SYNC \
--period=100ms \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link" \
&
# wait for first sync
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]}"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
assert_metric_eq "${METRIC_FETCH_COUNT}" 1
# suspend time so we can fake corruption
docker ps --filter label="git-sync-e2e=$RUNID" --format="{{.ID}}" \
| while read -r CTR; do
docker pause "$CTR" >/dev/null
done
# make a unexpected removal
WT=$(git -C "$REPO" rev-list -n1 HEAD)
rm -r "$ROOT/.worktrees/$WT"
# resume time
docker ps --filter label="git-sync-e2e=$RUNID" --format="{{.ID}}" \
| while read -r CTR; do
docker unpause "$CTR" >/dev/null
done
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]}"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
assert_metric_eq "${METRIC_FETCH_COUNT}" 2
}
##############################################
# Test syncing when the worktree is wrong hash
##############################################
function e2e::sync_recover_wrong_worktree_hash() {
GIT_SYNC \
--period=100ms \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link" \
&
# wait for first sync
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]}"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
assert_metric_eq "${METRIC_FETCH_COUNT}" 1
# suspend time so we can fake corruption
docker ps --filter label="git-sync-e2e=$RUNID" --format="{{.ID}}" \
| while read -r CTR; do
docker pause "$CTR" >/dev/null
done
# Corrupt it
echo "unexpected" > "$ROOT/link/file"
git -C "$ROOT/link" commit -qam "corrupt it"
# resume time
docker ps --filter label="git-sync-e2e=$RUNID" --format="{{.ID}}" \
| while read -r CTR; do
docker unpause "$CTR" >/dev/null
done
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]}"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
assert_metric_eq "${METRIC_FETCH_COUNT}" 2
}
##############################################
# Test stale-worktree-timeout
##############################################
function e2e::stale_worktree_timeout() {
echo "${FUNCNAME[0]} 1" > "$REPO"/file
git -C "$REPO" commit -qam "${FUNCNAME[0]}"
WT1=$(git -C "$REPO" rev-list -n1 HEAD)
GIT_SYNC \
--period=100ms \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link" \
--stale-worktree-timeout="5s" \
&
# wait for first sync
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]} 1"
# wait 2 seconds and make another commit
sleep 2
echo "${FUNCNAME[0]} 2" > "$REPO"/file2
git -C "$REPO" add file2
git -C "$REPO" commit -qam "${FUNCNAME[0]} new file"
WT2=$(git -C "$REPO" rev-list -n1 HEAD)
# wait for second sync
wait_for_sync "${MAXWAIT}"
# at this point both WT1 and WT2 should exist, with
# link pointing to the new WT2
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_exists "$ROOT/link/file2"
assert_file_exists "$ROOT/.worktrees/$WT1/file"
assert_file_absent "$ROOT/.worktrees/$WT1/file2"
# wait 2 seconds and make a third commit
sleep 2
echo "${FUNCNAME[0]} 3" > "$REPO"/file3
git -C "$REPO" add file3
git -C "$REPO" commit -qam "${FUNCNAME[0]} new file"
WT3=$(git -C "$REPO" rev-list -n1 HEAD)
wait_for_sync "${MAXWAIT}"
# at this point WT1, WT2, WT3 should exist, with
# link pointing to WT3
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_exists "$ROOT/link/file2"
assert_file_exists "$ROOT/link/file3"
assert_file_exists "$ROOT/.worktrees/$WT1/file"
assert_file_absent "$ROOT/.worktrees/$WT1/file2"
assert_file_absent "$ROOT/.worktrees/$WT1/file3"
assert_file_exists "$ROOT/.worktrees/$WT2/file"
assert_file_exists "$ROOT/.worktrees/$WT2/file2"
assert_file_absent "$ROOT/.worktrees/$WT2/file3"
assert_file_exists "$ROOT/.worktrees/$WT3/file"
assert_file_exists "$ROOT/.worktrees/$WT3/file2"
assert_file_exists "$ROOT/.worktrees/$WT3/file3"
# wait for WT1 to go stale
sleep 4
# now WT1 should be stale and deleted,
# WT2 and WT3 should still exist
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_exists "$ROOT/link/file2"
assert_file_exists "$ROOT/link/file3"
assert_file_absent "$ROOT/.worktrees/$WT1/file"
assert_file_absent "$ROOT/.worktrees/$WT1/file2"
assert_file_absent "$ROOT/.worktrees/$WT1/file3"
assert_file_exists "$ROOT/.worktrees/$WT2/file"
assert_file_exists "$ROOT/.worktrees/$WT2/file2"
assert_file_absent "$ROOT/.worktrees/$WT2/file3"
assert_file_exists "$ROOT/.worktrees/$WT3/file"
assert_file_exists "$ROOT/.worktrees/$WT3/file2"
assert_file_exists "$ROOT/.worktrees/$WT3/file3"
# wait for WT2 to go stale
sleep 2
# now both WT1 and WT2 are stale, WT3 should be the only
# worktree left
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_exists "$ROOT/link/file2"
assert_file_exists "$ROOT/link/file3"
assert_file_absent "$ROOT/.worktrees/$WT1/file"
assert_file_absent "$ROOT/.worktrees/$WT1/file2"
assert_file_absent "$ROOT/.worktrees/$WT1/file3"
assert_file_absent "$ROOT/.worktrees/$WT2/file"
assert_file_absent "$ROOT/.worktrees/$WT2/file2"
assert_file_absent "$ROOT/.worktrees/$WT2/file3"
assert_file_exists "$ROOT/.worktrees/$WT3/file"
assert_file_exists "$ROOT/.worktrees/$WT3/file2"
assert_file_exists "$ROOT/.worktrees/$WT3/file3"
}
##############################################
# Test stale-worktree-timeout with restarts
##############################################
function e2e::stale_worktree_timeout_restart() {
echo "${FUNCNAME[0]} 1" > "$REPO"/file
git -C "$REPO" commit -qam "${FUNCNAME[0]}"
WT1=$(git -C "$REPO" rev-list -n1 HEAD)
GIT_SYNC \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link" \
--one-time
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]} 1"
# wait 2 seconds and make another commit
sleep 2
echo "${FUNCNAME[0]} 2" > "$REPO"/file2
git -C "$REPO" add file2
git -C "$REPO" commit -qam "${FUNCNAME[0]} new file"
WT2=$(git -C "$REPO" rev-list -n1 HEAD)
# restart git-sync
GIT_SYNC \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link" \
--stale-worktree-timeout="10s" \
--one-time
# at this point both WT1 and WT2 should exist, with
# link pointing to the new WT2
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_exists "$ROOT/link/file2"
assert_file_exists "$ROOT/.worktrees/$WT1/file"
assert_file_absent "$ROOT/.worktrees/$WT1/file2"
# wait 2 seconds and make a third commit
sleep 4
echo "${FUNCNAME[0]} 3" > "$REPO"/file3
git -C "$REPO" add file3
git -C "$REPO" commit -qam "${FUNCNAME[0]} new file"
WT3=$(git -C "$REPO" rev-list -n1 HEAD)
# restart git-sync
GIT_SYNC \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link" \
--stale-worktree-timeout="10s" \
--one-time
# at this point WT1, WT2, WT3 should exist, with
# link pointing to WT3
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_exists "$ROOT/link/file2"
assert_file_exists "$ROOT/link/file3"
assert_file_exists "$ROOT/.worktrees/$WT1/file"
assert_file_absent "$ROOT/.worktrees/$WT1/file2"
assert_file_absent "$ROOT/.worktrees/$WT1/file3"
assert_file_exists "$ROOT/.worktrees/$WT2/file"
assert_file_exists "$ROOT/.worktrees/$WT2/file2"
assert_file_absent "$ROOT/.worktrees/$WT2/file3"
assert_file_exists "$ROOT/.worktrees/$WT3/file"
assert_file_exists "$ROOT/.worktrees/$WT3/file2"
assert_file_exists "$ROOT/.worktrees/$WT3/file3"
# wait for WT1 to go stale and restart git-sync
sleep 8
GIT_SYNC \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link" \
--stale-worktree-timeout="10s" \
--one-time
# now WT1 should be stale and deleted,
# WT2 and WT3 should still exist
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_exists "$ROOT/link/file2"
assert_file_exists "$ROOT/link/file3"
assert_file_absent "$ROOT/.worktrees/$WT1/file"
assert_file_absent "$ROOT/.worktrees/$WT1/file2"
assert_file_absent "$ROOT/.worktrees/$WT1/file3"
assert_file_exists "$ROOT/.worktrees/$WT2/file"
assert_file_exists "$ROOT/.worktrees/$WT2/file2"
assert_file_absent "$ROOT/.worktrees/$WT2/file3"
assert_file_exists "$ROOT/.worktrees/$WT3/file"
assert_file_exists "$ROOT/.worktrees/$WT3/file2"
assert_file_exists "$ROOT/.worktrees/$WT3/file3"
# wait for WT2 to go stale and restart git-sync