-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuat.sh
349 lines (321 loc) · 8.55 KB
/
uat.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
set -e
testid=`date +%s`
remote_base_template="://gsg-uat/$testid"
start() {
echo ">>>>>>>> $1"
}
finish() {
what=$1
if [[ "$what" == "" ]]
then
what="done"
fi
echo "<<<<<<<< $what"
echo
}
same() {
if diff $1 $2 &>/dev/null
then
echo OK: $1 and $2 does not differ
else
echo FATAL: $1 and $2 are different
exit 1
fi
}
assertValue() {
if [[ "$3" == "remote" ]]
then
case $mode in
gs)
if gsutil ls $remote_base/$1 &>/dev/null
then
content="$(gsutil cat $remote_base/$1 2>/dev/null)"
if [[ "$content" == "$2" ]]
then
echo OK: $1 exists with correct content remotely.
else
echo FATAL: required file $1 does not have correct content remotely.
exit 1
fi
else
echo FATAL: required file $1 does not exists remotely.
exit 1
fi
;;
s3)
if aws s3 cp $remote_base/$1 .temp &>/dev/null
then
content="$(cat .temp)"
if [[ "$content" == "$2" ]]
then
echo OK: $1 exists with correct content remotely.
else
echo FATAL: required file $1 does not have correct content remotely.
exit 1
fi
else
echo FATAL: required file $1 does not exists remotely.
exit 1
fi
;;
*)
exit 1
;;
esac
else
if ls $1 &>/dev/null
then
content=$(cat $1)
if [[ "$content" == "$2" ]]
then
echo OK: $1 exists with correct content locally.
else
echo FATAL: required file $1 does not have correct content locally.
exit 1
fi
else
echo FATAL: required file $1 does not exists locally.
exit 1
fi
fi
}
assert() {
if [[ "$2" == "remote" ]]
then
case $mode in
gs)
if gsutil ls $remote_base/$1 &>/dev/null
then
content="$(gsutil cat $remote_base/$1 2>/dev/null)"
if [[ "$content" == "$testid" ]]
then
echo OK: $1 exists with correct content remotely.
else
echo FATAL: required file $1 does not have correct content remotely.
exit 1
fi
else
echo FATAL: required file $1 does not exists remotely.
exit 1
fi
;;
s3)
if aws s3 cp $remote_base/$1 .temp &>/dev/null
then
content="$(cat .temp)"
if [[ "$content" == "$testid" ]]
then
echo OK: $1 exists with correct content remotely.
else
echo FATAL: required file $1 does not have correct content remotely.
exit 1
fi
else
echo FATAL: required file $1 does not exists remotely.
exit 1
fi
;;
*)
exit 1
;;
esac
else
if ls $1 &>/dev/null
then
content=$(cat $1)
if [[ "$content" == "$testid" ]]
then
echo OK: $1 exists with correct content locally.
else
echo FATAL: required file $1 does not have correct content locally.
exit 1
fi
else
echo FATAL: required file $1 does not exists locally.
exit 1
fi
fi
}
assert_not() {
if [[ "$2" == "remote" ]]
then
case $mode in
gs)
if gsutil ls $remote_base/$1 &>/dev/null
then
echo FATAL: required file $1 does exists remotely.
exit 1
else
echo OK: required file $1 does not exists remotely.
fi
;;
s3)
if aws s3 cp $remote_base/$1 .temp &>/dev/null
then
echo FATAL: required file $1 does exists remotely.
exit 1
else
echo OK: required file $1 does not exists remotely.
fi
;;
*)
exit 1
;;
esac
else
if ls $1 &>/dev/null
then
echo FATAL: required file $1 does exists locally.
exit 1
else
echo OK: required file $1 does not exists locally.
fi
fi
}
remote_copy() {
local r=""
case $mode in
gs)
if [[ "$1" == "true" ]]
then
r=" -r"
fi
echo "gsutil cp $r"
;;
s3)
if [[ "$1" == "true" ]]
then
r=" --recursive"
fi
echo "aws s3 cp $r"
;;
*)
exit 1
;;
esac
}
prepare_file() {
value=$testid
if [[ "$2" != "" ]]
then
value=$2
fi
echo "$value" > $1
}
do_test() {
mode=$1
remote_base="$1$remote_base_template"
start "prepare test ground for mode: $mode"
rm -rf uat_temp || true
testbase="uat_temp"
mkdir $testbase
finish
start "entering $testbase"
pushd $testbase
finish
start "test upload"
start "test upload single file"
prepare_file to_upload
../gsg cp to_upload $remote_base/to_upload
assert to_upload remote
finish
ftu="folder_to_upload"
start "test upload a folder"
mkdir -p $ftu/a/b/c
prepare_file $ftu/a/1.txt 1_txt
prepare_file $ftu/a/2.txt 2_txt
prepare_file $ftu/a/b/c/3.txt 3_txt
../gsg cp -r $ftu $remote_base/$ftu
assertValue $ftu/a/1.txt 1_txt remote
assertValue $ftu/a/2.txt 2_txt remote
assertValue $ftu/a/b/c/3.txt 3_txt remote
finish
start "test download"
start "test download single file"
prepare_file to_download
$(remote_copy) to_download $remote_base/to_download
rm to_download
../gsg cp $remote_base/to_download to_download
assert to_download
finish
ftd="folder_to_download"
start "test download a folder"
mkdir -p $ftd/a/b/c
prepare_file $ftd/a/1.txt 1_txt
prepare_file $ftd/a/2.txt 2_txt
prepare_file $ftd/a/b/c/3.txt 3_txt
$(remote_copy true) $ftd $remote_base/$ftd
rm -rf $ftd
../gsg cp -r $remote_base/$ftd $ftd
assertValue $ftd/a/1.txt 1_txt
assertValue $ftd/a/2.txt 2_txt
assertValue $ftd/a/b/c/3.txt 3_txt
finish
ftm="folder_to_move"
start "test moving a folder"
mkdir -p $ftm/a/b/c
prepare_file $ftm/a/1.txt
prepare_file $ftm/a/2.txt
prepare_file $ftm/a/b/c/3.txt
$(remote_copy true) $ftm $remote_base/$ftm
rm -rf $ftm
../gsg mv -r $remote_base/$ftm $ftm
assert $ftm/a/1.txt
assert $ftm/a/2.txt
assert $ftm/a/b/c/3.txt
assert_not $ftm/a/1.txt remote
assert_not $ftm/a/2.txt remote
assert_not $ftm/a/b/c/3.txt remote
finish
start "testing rsync"
ftr="folder_to_rsync"
start "test rsync a local folder to remote"
mkdir -p $ftr/a/b/c
prepare_file $ftr/a/1.txt
prepare_file $ftr/a/2.txt
prepare_file $ftr/a/b/c/3.txt
../gsg rsync -r $ftr $remote_base/$ftr
assert $ftr/a/1.txt remote
assert $ftr/a/2.txt remote
assert $ftr/a/b/c/3.txt remote
echo "whocares" > $ftr/a/1.txt
../gsg rsync -r $ftr $remote_base/$ftr
$(remote_copy) $remote_base/$ftr/a/1.txt $ftr/a/1_remote.txt
same $ftr/a/1.txt $ftr/a/1_remote.txt
finish
start "test rsync a remote folder to local"
prepare_file $ftr/a/1.txt
$(remote_copy) $ftr/a/1.txt $remote_base/$ftr/a/1.txt
rm -rf $ftr
../gsg rsync -r $remote_base/$ftr $ftr
assert $ftr/a/1.txt
assert $ftr/a/2.txt
assert $ftr/a/b/c/3.txt
finish
start "test rsync with -d and non-existing src"
../gsg rsync -d whocares $remote_base/$ftr
assert_not $ftr/a/1.txt remote
assert_not $ftr/a/2.txt remote
assert_not $ftr/a/b/c/3.txt remote
finish
start "leaving $testbase"
popd
finish
start "cleanup test ground"
rm -rf uat_temp || true
case $mode in
gs)
gsutil ls gs://gsg-uat | xargs -I {} gsutil -m rm -r {} || true
;;
s3)
aws s3 rm s3://gsg-uat --recursive || true
;;
esac
finish
finish "everything OK with $mode !"
}
start "building gsg binary"
go build
finish
do_test gs
do_test s3