Skip to content

Commit 100ee17

Browse files
authored
improve logging and add tests (#47)
Previous logging was always sent to stdout in functions, this prevented some functions like snapList from logging the commands sent as these functions printed return values used by other functions. To fix this, all user facing logging has been switched to stderr which is not picked up by calling functions. This commit also adds tests for the --status/-s and --help/-h options which includes testing of the sortLogs function.
1 parent 19f8d2f commit 100ee17

File tree

3 files changed

+149
-86
lines changed

3 files changed

+149
-86
lines changed

test/find.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ set -eu
77
(set -o pipefail 2> /dev/null) && set -o pipefail
88

99
_fakeFIND() {
10-
printf "find %s\n" "$*"
10+
path="$1"
11+
printf "%s/autorep-test1.log\n" "$path"
12+
printf "%s/autorep-test2.log\n" "$path"
13+
printf "%s/autorep-test3.log\n" "$path"
1114
return 0
1215
}
1316

14-
_fakeZFS "$@"
17+
_fakeFIND "$@"

test/test.sh

Lines changed: 123 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ _testZFSReplicate() {
4646
# shellcheck source=/dev/null
4747
. ../zfs-replicate.sh
4848
printf "_testZFSReplicate/loadConfigWithoutError\n"
49-
line=$(loadConfig)
50-
_fail "$line" "null" ## we expect no output here
49+
lines=$(loadConfig 2>&1)
50+
_fail "$lines" "null" ## we expect no output here
5151
)
5252

5353
## test loadConfig with missing values
@@ -58,37 +58,66 @@ _testZFSReplicate() {
5858
# shellcheck source=/dev/null
5959
. ../zfs-replicate.sh
6060
printf "_testZFSReplicate/loadConfigWithError\n"
61-
! line=$(loadConfig) && true ## prevent tests from exiting
62-
_fail "$line" "missing required setting REPLICATE_SETS"
61+
! lines=$(loadConfig 2>&1) && true ## prevent tests from exiting
62+
_fail "$lines" "missing required setting REPLICATE_SETS"
6363
)
6464

6565
## test config override of script defaults
6666
(
67-
## likely default values at script load time
68-
FIND="/usr/bin/find"
69-
ZFS="/sbin/zfs"
70-
SSH="/usr/sbin/ssh"
67+
## generic default values
68+
FIND="fakeFIND"
69+
ZFS="fakeZFS"
70+
SSH="fakeSSH"
7171
REPLICATE_SETS="fakeSource:fakeDest"
7272
# shellcheck source=/dev/null
7373
. ../zfs-replicate.sh
7474
printf "_testZFSReplicate/loadConfigOverrideDefaults\n"
75-
_fail "/usr/sbin/ssh %HOST% /sbin/zfs receive -vFd" "$DEST_PIPE_WITH_HOST"
76-
_fail "/sbin/zfs receive -vFd" "$DEST_PIPE_WITHOUT_HOST"
75+
_fail "fakeSSH %HOST% /sbin/zfs receive -vFd" "$DEST_PIPE_WITH_HOST"
76+
_fail "fakeZFS receive -vFd" "$DEST_PIPE_WITHOUT_HOST"
7777
## generate config
7878
config="$(mktemp)"
7979
printf "ZFS=\"myZFS\"\n" >> "$config"
8080
## set SSH via environment
8181
SSH="mySSH"
82-
loadConfig "$config" && rm -f "$config"
82+
loadConfig "$config" 2>&1 && rm -f "$config"
8383
## values should match config and environment
8484
_fail "mySSH %HOST% myZFS receive -vFd" "$DEST_PIPE_WITH_HOST"
8585
_fail "myZFS receive -vFd" "$DEST_PIPE_WITHOUT_HOST"
8686
)
8787

88+
## test loadConfig with options
89+
(
90+
FIND="${SCRIPT_PATH}/find.sh"
91+
ZFS="fakeZFS"
92+
SSH="fakeSSH"
93+
LOG_BASE="$(mktemp -d)"
94+
# shellcheck source=/dev/null
95+
. ../zfs-replicate.sh
96+
## test --help and -h
97+
printf "_testZFSReplicate/loadConfigWithHelp\n"
98+
! lines=$(loadConfig "--help" 2>&1) && true ## prevent tests from exiting
99+
_fail "$lines" "Usage: test.sh"
100+
! lines=$(loadConfig "-h" 2>&1) && true ## prevent tests from exiting
101+
_fail "$lines" "Usage: test.sh"
102+
## test --status and -s
103+
printf "_testZFSReplicate/loadConfigWithStatus\n"
104+
## generate fake log files with staggered creation time
105+
for idx in $(seq 1 3); do
106+
printf "testing log %d\n" "$idx" > "${LOG_BASE}/autorep-test${idx}.log" && sleep 1
107+
done
108+
## check status command
109+
! lines=$(loadConfig "--status" 2>&1) && true ## prevent tests from exiting
110+
_fail "$lines" "testing log 3"
111+
! lines=$(loadConfig "-s" 2>&1) && true ## prevent tests from exiting
112+
_fail "$lines" "testing log 3"
113+
## cleanup
114+
rm -rvf "${LOG_BASE}"
115+
)
116+
88117
## test snapCreate with different set combinations
89118
(
90119
## configure test parameters
91-
FIND="${SCRIPT_PATH}/find.sh"
120+
FIND="fakeFIND"
92121
ZFS="${SCRIPT_PATH}/zfs.sh"
93122
SSH="${SCRIPT_PATH}/ssh.sh"
94123
HOST_CHECK="${ECHO} %HOST%"
@@ -101,7 +130,7 @@ _testZFSReplicate() {
101130
. ../zfs-replicate.sh && loadConfig
102131
printf "_testZFSReplicate/snapCreateWithoutErrors\n"
103132
idx=0
104-
snapCreate | while IFS= read -r line; do
133+
snapCreate 2>&1 | while IFS= read -r line; do
105134
match=""
106135
printf "%d %s\n" "$idx" "$line"
107136
case $idx in
@@ -114,129 +143,159 @@ _testZFSReplicate() {
114143
3)
115144
match="cmd=${ZFS} list -H -o name dstPool0/dstFS0"
116145
;;
146+
5)
147+
match="cmd=${ZFS} list -Hr -o name -s creation -t snapshot -d 1 srcPool0/srcFS0"
148+
;;
117149
6)
150+
match="cmd=${ZFS} list -Hr -o name -s creation -t snapshot dstPool0/dstFS0"
151+
;;
152+
8)
118153
match="cmd=${ZFS} destroy srcPool0/srcFS0@autorep-test1"
119154
;;
120-
7)
155+
9)
121156
match="cmd=${ZFS} snapshot srcPool0/srcFS0@autorep-"
122157
;;
123-
8)
158+
10)
124159
match="creating lockfile ${TMPDIR}/.replicate.send.lock"
125160
;;
126-
9)
161+
11)
127162
match="cmd=${ZFS} send -Rs -I srcPool0/srcFS0@autorep-test3 srcPool0/srcFS0@autorep-${TAG} |"
128163
match="$match ${DEST_PIPE_WITHOUT_HOST} dstPool0/dstFS0"
129164
;;
130-
10)
165+
12)
131166
match="receive -vFd dstPool0/dstFS0"
132167
;;
133-
11)
168+
13)
134169
match="deleting lockfile ${TMPDIR}/.replicate.send.lock"
135170
;;
136-
12)
171+
14)
137172
match="cmd=${ECHO} dstHost1"
138173
;;
139-
13)
174+
15)
140175
match="cmd=${ZFS} list -H -o name srcPool1/srcFS1/subFS1"
141176
;;
142-
15)
177+
17)
143178
match="cmd=${SSH} dstHost1 ${ZFS} list -H -o name dstPool1/dstFS1"
144179
;;
145-
18)
180+
19)
181+
match="cmd=${ZFS} list -Hr -o name -s creation -t snapshot -d 1 srcPool1/srcFS1/subFS1"
182+
;;
183+
20)
184+
match="cmd=${SSH} dstHost1 ${ZFS} list -Hr -o name -s creation -t snapshot dstPool1/dstFS1"
185+
;;
186+
22)
146187
match="cmd=${ZFS} destroy srcPool1/srcFS1/subFS1@autorep-test1"
147188
;;
148-
19)
189+
23)
149190
match="cmd=${ZFS} snapshot srcPool1/srcFS1/subFS1@autorep-${TAG}"
150191
;;
151-
20)
192+
24)
152193
match="creating lockfile ${TMPDIR}/.replicate.send.lock"
153194
;;
154-
21)
195+
25)
155196
match="cmd=${ZFS} send -Rs -I srcPool1/srcFS1/subFS1@autorep-test3 srcPool1/srcFS1/subFS1@autorep-${TAG} |"
156197
match="$match ${SSH} dstHost1 ${ZFS} receive -vFd dstPool1/dstFS1"
157198
;;
158-
23)
199+
27)
159200
match="deleting lockfile ${TMPDIR}/.replicate.send.lock"
160201
;;
161-
24)
202+
28)
162203
match="cmd=${ECHO} dstHost2"
163204
;;
164-
25)
205+
29)
165206
match="cmd=${ZFS} list -H -o name srcPool2/srcFS2"
166207
;;
167-
27)
208+
31)
168209
match="cmd=${SSH} dstHost2 ${ZFS} list -H -o name dstPool2/dstFS2"
169210
;;
170-
30)
211+
33)
212+
match="cmd=${ZFS} list -Hr -o name -s creation -t snapshot -d 1 srcPool2/srcFS2"
213+
;;
214+
34)
215+
match="cmd=${SSH} dstHost2 ${ZFS} list -Hr -o name -s creation -t snapshot dstPool2/dstFS2"
216+
;;
217+
36)
171218
match="cmd=${ZFS} destroy srcPool2/srcFS2@autorep-test1"
172219
;;
173-
31)
220+
37)
174221
match="cmd=${ZFS} snapshot srcPool2/srcFS2@autorep-${TAG}"
175222
;;
176-
32)
223+
38)
177224
match="creating lockfile ${TMPDIR}/.replicate.send.lock"
178225
;;
179-
33)
226+
39)
180227
match="cmd=${ZFS} send -Rs -I srcPool2/srcFS2@autorep-test3 srcPool2/srcFS2@autorep-${TAG} |"
181228
match="$match ${SSH} dstHost2 ${ZFS} receive -vFd dstPool2/dstFS2"
182229
;;
183-
35)
230+
41)
184231
match="deleting lockfile ${TMPDIR}/.replicate.send.lock"
185232
;;
186-
36)
233+
42)
187234
match="cmd=${ECHO} srcHost3"
188235
;;
189-
37)
236+
43)
190237
match=" cmd=${SSH} srcHost3 ${ZFS} list -H -o name srcPool3/srcFS3"
191238
;;
192-
39)
239+
45)
193240
match="cmd=${ZFS} list -H -o name dstPool3/dstFS3"
194241
;;
195-
42)
242+
47)
243+
match="cmd=${SSH} srcHost3 ${ZFS} list -Hr -o name -s creation -t snapshot -d 1 srcPool3/srcFS3"
244+
;;
245+
48)
246+
match="cmd=${ZFS} list -Hr -o name -s creation -t snapshot dstPool3/dstFS3"
247+
;;
248+
50)
196249
match="cmd=${SSH} srcHost3 ${ZFS} destroy srcPool3/srcFS3@autorep-test1"
197250
;;
198-
43)
251+
51)
199252
match="cmd=${SSH} srcHost3 ${ZFS} snapshot srcPool3/srcFS3@autorep-${TAG}"
200253
;;
201-
44)
254+
52)
202255
match="creating lockfile ${TMPDIR}/.replicate.send.lock"
203256
;;
204-
45)
257+
53)
205258
match="cmd=${SSH} srcHost3 ${ZFS} send -Rs -I srcPool3/srcFS3@autorep-test3 srcPool3/srcFS3@autorep-${TAG} |"
206259
match="$match ${ZFS} receive -vFd dstPool3/dstFS3"
207260
;;
208-
47)
261+
55)
209262
match="deleting lockfile ${TMPDIR}/.replicate.send.lock"
210263
;;
211-
48)
264+
56)
212265
match="cmd=${ECHO} srcHost4"
213266
;;
214-
49)
267+
57)
215268
match="cmd=${ECHO} dstHost4"
216269
;;
217-
50)
270+
58)
218271
match="cmd=${SSH} srcHost4 ${ZFS} list -H -o name srcPool4/srcFS4"
219272
;;
220-
52)
273+
60)
221274
match="cmd=${SSH} dstHost4 ${ZFS} list -H -o name dstPool4/dstFS4"
222275
;;
223-
55)
276+
62)
277+
match="cmd=${SSH} srcHost4 ${ZFS} list -Hr -o name -s creation -t snapshot -d 1 srcPool4/srcFS4"
278+
;;
279+
63)
280+
match="cmd=${SSH} dstHost4 ${ZFS} list -Hr -o name -s creation -t snapshot dstPool4/dstFS4"
281+
;;
282+
65)
224283
match="cmd=${SSH} srcHost4 ${ZFS} destroy srcPool4/srcFS4@autorep-test1"
225284
;;
226-
56)
285+
66)
227286
match="cmd=${SSH} srcHost4 ${ZFS} snapshot srcPool4/srcFS4@autorep-${TAG}"
228287
;;
229-
57)
288+
67)
230289
match="creating lockfile ${TMPDIR}/.replicate.send.lock"
231290
;;
232-
58)
291+
68)
233292
match="cmd=${SSH} srcHost4 ${ZFS} send -Rs -I srcPool4/srcFS4@autorep-test3 srcPool4/srcFS4@autorep-${TAG} |"
234293
match="$match ${SSH} dstHost4 ${ZFS} receive -vFd dstPool4/dstFS4"
235294
;;
236-
60)
295+
70)
237296
match="deleting lockfile ${TMPDIR}/.replicate.send.lock"
238297
;;
239-
61)
298+
71)
240299
match="deleting lockfile ${TMPDIR}/.replicate.snapshot.lock"
241300
;;
242301
esac
@@ -248,7 +307,7 @@ _testZFSReplicate() {
248307
## test snapCreate with host check errors
249308
(
250309
## configure test parameters
251-
FIND="${SCRIPT_PATH}/find.sh"
310+
FIND="fakeFIND"
252311
ZFS="${SCRIPT_PATH}/zfs.sh"
253312
SSH="${SCRIPT_PATH}/ssh.sh"
254313
HOST_CHECK="false"
@@ -261,16 +320,13 @@ _testZFSReplicate() {
261320
. ../zfs-replicate.sh && loadConfig
262321
printf "_testZFSReplicate/snapCreateWithHostCheckErrors\n"
263322
idx=0
264-
snapCreate | while IFS= read -r line; do
323+
snapCreate 2>&1 | while IFS= read -r line; do
265324
match=""
266325
printf "%d %s\n" "$idx" "$line"
267326
case $idx in
268327
0)
269328
match="creating lockfile ${TMPDIR}/.replicate.snapshot.lock"
270329
;;
271-
13)
272-
match="source or destination host check failed"
273-
;;
274330
15)
275331
match="source or destination host check failed"
276332
;;
@@ -280,7 +336,10 @@ _testZFSReplicate() {
280336
19)
281337
match="source or destination host check failed"
282338
;;
283-
20)
339+
21)
340+
match="source or destination host check failed"
341+
;;
342+
22)
284343
match="deleting lockfile ${TMPDIR}/.replicate.snapshot.lock"
285344
;;
286345
esac
@@ -292,7 +351,7 @@ _testZFSReplicate() {
292351
## test snapCreate with dataset check errors
293352
(
294353
## configure test parameters
295-
FIND="${SCRIPT_PATH}/find.sh"
354+
FIND="fakeFIND"
296355
ZFS="${SCRIPT_PATH}/zfs.sh"
297356
SSH="${SCRIPT_PATH}/ssh.sh"
298357
HOST_CHECK="${ECHO} %HOST%"
@@ -303,7 +362,7 @@ _testZFSReplicate() {
303362
. ../zfs-replicate.sh && loadConfig
304363
printf "_testZFSReplicate/snapCreateWithDatasetCheckErrors\n"
305364
idx=0
306-
snapCreate | while IFS= read -r line; do
365+
snapCreate 2>&1 | while IFS= read -r line; do
307366
match=""
308367
printf "%d %s\n" "$idx" "$line"
309368
case $idx in
@@ -362,9 +421,9 @@ _testZFSReplicate() {
362421
# shellcheck source=/dev/null
363422
. ../zfs-replicate.sh && loadConfig
364423
printf "_testZFSReplicate/exitCleanSuccess\n"
365-
line=$(exitClean 0 "test message")
424+
lines=$(exitClean 0 "test message" 2>&1)
366425
match="success total sets 0 skipped 0: test message" ## counts are modified in snapCreate
367-
_fail "$line" "$match"
426+
_fail "$lines" "$match"
368427
)
369428

370429
## test exitClean code=99 with error message
@@ -377,9 +436,9 @@ _testZFSReplicate() {
377436
# shellcheck source=/dev/null
378437
. ../zfs-replicate.sh && loadConfig
379438
printf "_testZFSReplicate/exitCleanError\n"
380-
! line=$(exitClean 99 "error message") && true ## prevent tests from exiting
439+
! lines=$(exitClean 99 "error message" 2>&1) && true ## prevent tests from exiting
381440
match="operation exited unexpectedly: code=99 msg=error message"
382-
_fail "$line" "$match"
441+
_fail "$lines" "$match"
383442
)
384443

385444
## yay, tests completed!

0 commit comments

Comments
 (0)