-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-autorsync.bash
executable file
·222 lines (182 loc) · 6.73 KB
/
test-autorsync.bash
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
#!/usr/bin/env bash
set -euo pipefail
DEBUG=1
ARGS=()
while (( $# > 0 ))
do
case "$1" in
--autorsync=*|autorsync=*) ##ToDo move this to test-autorsync.bash
AUTORSYNC="${1#*=}"
;;
*)
ARGS+=("$1")
esac
shift
done
source "$(realpath $(dirname "${BASH_SOURCE[0]}"))/testing.bash" "${ARGS[@]}"
AUTORSYNC=${AUTORSYNC:=$SRCDIR/autorsync.bash}
## PREREQUISITES
## 1. ssh must be configured to be able `ssh localhost` without typing passwords
## 2.
start_autorsync(){
assert var_is_unset_or_empty autorsync_pid
autorsync "$@" & autorsync_pid=$!
sleep 0.2 && assert kill -0 $autorsync_pid 2>&-
trap_append "pstree $autorsync_pid; kill_sure $autorsync_pid; unset autorsync_pid" exit
#echoinfo "AutoRSync started with PID $autorsync_pid"
}
autorsync(){
"$AUTORSYNC" "$@" ||
{
## If autorsync was terminated with SIGTERM do not report an error
declare -i status=$? SIGTERM=15
(($status==SIGTERM)) && status=0
return $status
}
}
#############################################
## Test 1
## Initial sync
#############################################
declare_test initial_sync "Initial sync"
function initial_sync {
mkdir tx_local rx_remote
echo "Hello World" >tx_local/hello_world
autorsync --initial-tx-only tx_local localhost:$PWD/rx_remote ## SRC ends without slash '/'
assert_file_contents_are_same tx_local/hello_world rx_remote/tx_local/hello_world
rm -rf rx_remote/*
autorsync --initial-tx-only tx_local/ localhost:$PWD/rx_remote
assert_file_contents_are_same tx_local/hello_world rx_remote/hello_world
}
readonly -f initial_sync
#############################################
## Test 2
## Live changes on local side
#############################################
declare_test live_changes "Live changes on local side should be synchronized to remote side."
function live_changes {
mkdir -p test1a test1b
#clean_up(){ rm -rf $PWD/test1a $PWD/test1b; } ## PWD will be removed
assert_diff(){
assert diff -q test1a/"$1" test1b/"$1"
}
echo "Hello World" >test1a/hello_world
start_autorsync test1a/ localhost:$PWD/test1b
sleep 1.2 ## wait for inital sync
## Test live changes a->b
touch test1a/touched
echo "I Love World" >test1a/i_love_world
sleep 1.1
assert_diff touched
assert_diff i_love_world
# ## Test live changes b->a
# touch test1b/touched-b
# echo "I Love World B" >test1a/i_love_world-b
# sleep 1.1
# debug tree test1a test1b
# assert_diff touched-b
# assert_diff i_love_world-b
assert diff -q test1a/hello_world test1b/hello_world
}
readonly -f live_changes
#############################################
## Test 3
## Synchronize to existing directory
#############################################
declare_test sync_to_existing "Synchronize files to existing directory."
function sync_to_existing {
skip
mkdir -p tx_local rx_remote/tx_local
echo "Hello World" >tx_local/hello_world
start_autorsync --rx --no-initial-tx tx_local localhost:$PWD/rx_remote/
sleep 1.2 ## Wait for possible initial sync, but it should not
echoinfo "Test hello_world was not initially synced to destination"
assert ! test -e rx_remote/hello_world
## Test SRC synced to DST
touch tx_local/touched
echo "I Love World" >tx_local/i_love_world
sleep 2.1
debug tree tx_local rx_remote
assert ! test -e rx_remote/touched
assert diff -q tx_local/touched rx_remote/tx_local/touched
pkill -P $autorsync_pid ## https://stackoverflow.com/questions/2618403/how-to-kill-all-subprocesses-of-shell#17615178
assert diff -q tx_local/hello_world rx_remote/hello_world
debug jobs -l
# wait
}
readonly sync_to_existing
#############################################
## Test 4
## Synchronize to existing directory
## If SRC ends with / sync files in it, else sync SRC itself.
#############################################
declare_test initial_sync_with_slash "Synchronize files to existing directory."
function initial_sync_with_slash {
mkdir -p tx_local rx_remote
echo "Hello World" >tx_local/hello_world
start_autorsync tx_local localhost:$PWD/rx_remote
sleep 1.2
echoinfo "Test hello_world was initially synced to destination"
debug tree tx_local rx_remote
assert test -e rx_remote/tx_local/hello_world
pkill -P $autorsync_pid ## https://stackoverflow.com/questions/2618403/how-to-kill-all-subprocesses-of-shell#17615178
assert diff -q tx_local/hello_world rx_remote/tx_local/hello_world
#echodbg ----- jobs; debug jobs -l
#echodbg ----- pgrep; debug pgrep -P $$
#echodbg ----- pstree; debug pstree $$
}
readonly -f initial_sync_with_slash
#############################################
## Test 5. TODO
## Synchronize to existing directory. Live sync
## If SRC ends with / sync files in it, else sync SRC itself.
#############################################
declare_test live_sync_with_slash "Live synchronize files to existing directory."
function live_sync_with_slash {
mkdir -p tx_local rx_remote
start_autorsync --no-initial-tx tx_local localhost:$PWD/rx_remote
sleep 1.1
echo "Hello World" >tx_local/hello_world
sleep 1.1
debug tree tx_local rx_remote
assert diff -q tx_local/hello_world rx_remote/tx_local/hello_world
pkill -P $autorsync_pid ## https://stackoverflow.com/questions/2618403/how-to-kill-all-subprocesses-of-shell#17615178
}
readonly -f live_sync_with_slash
#############################################
## Test 6.
## RSync SRC/ and SRC - with or without trailing slash
## If SRC ends with / sync contained files, else sync SRC directory itself.
#############################################
declare_test rsync_with_slash "Live synchronize files to existing directory."
function rsync_with_slash {
mkdir -p tx_local rx_remote
echo "Hello World" >tx_local/hello_world
rsync -a tx_local rx_remote
debug tree tx_local rx_remote
assert diff -q tx_local/hello_world rx_remote/tx_local/hello_world
rm -rf rx_remote/*
rsync -a tx_local/ rx_remote
debug tree tx_local rx_remote
assert diff -q tx_local/hello_world rx_remote/hello_world
}
readonly -f rsync_with_slash
#############################################
## Test 7
## Stop process
#############################################
declare_test stop_process "Stop process"
function stop_process {
SKIP ## Look at last line of function
mkdir tx_local rx_remote
echo "Hello World" >tx_local/hello_world
autorsync tx_local/ localhost:$PWD/rx_remote & ars_pid=$!
sleep 0.2 && assert kill -0 $ars_pid ## are you alive?
sleep 1
kill $ars_pid 2>&- ## Kills only parent process but not its childs. FIXME
}
readonly -f stop_process
#############################################
## Execute Testcases
#############################################
Testing