-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
505 lines (480 loc) · 18.1 KB
/
Copy pathtest.sh
File metadata and controls
505 lines (480 loc) · 18.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
#!/bin/bash
#--------------------------------------------------------------------
#Author: Aaron Anthony Valoroso
#Date: January 2nd, 2019
#License: GNU GENERAL PUBLIC LICENSE
#Email: valoroso99@gmail.com
#--------------------------------------------------------------------
# General Explanation:
# The following file is for testing monday and it should be pretty self explanatory. There
# - are two functions; one is cleanup and the other is for when tests fail. Next, I setup
# - the variables for the script, setup the directory to do the testing in, and then process
# - the incoming parameters. There are to major testing suites and they are the pull and push.
# - Each one can run independently and cleanup. I'm not going to go through each test itself
# - becauce I think the name above each test should explana what it is doing. Lastly, the
# - testing directory is deleted and act like nothing every happened.
#--------------------------------------------------------------------
# This function is ran when a test fails. This goes for all the tests in this
# - file and makes sure that everything that gets outputed is formated correctly.
test_failure() {
if [ "$(uname -s)" == "Darwin" ]; then
sed -i '' 's/^/ /' the_output.txt
elif [ "$(uname -s)" == "Linux" ]; then
sed -i 's/^/\t/' the_output.txt
fi
cat the_output.txt
echo -e "\t----------------------------"
curr_dir=$(pwd)
echo -e "\tCurrent Path: $curr_dir"
echo -e "\t----------------------------"
echo -e "\tCurrent Directory Contents: "
ls 1> the_output.txt
if [ "$(uname -s)" == "Darwin" ]; then
sed -i '' 's/^/ /' the_output.txt
elif [ "$(uname -s)" == "Linux" ]; then
sed -i 's/^/\t/' the_output.txt
fi
cat the_output.txt
echo -e "\t----------------------------"
rm the_output.txt
}
#--------------------------------------------------------------------
# This function is used to clean up wherever the script is at. This function can be
# - ran when a command fails or when the user does a control c and etc.
clenaup () {
cd $HOME
if [ -d $HOME/Transfer ]; then
rm -rf $HOME/Transfer
fi
if [ -d $HOME/Monday_Testing ]; then
rm -rf $HOME/Monday_Testing
fi
ssh $username@$ip_address -T << EOF
rm -rf \$HOME/Transfer/*
cd $storage_location
if [ -d monday_testing ]; then
rm -rf monday_testing
fi
if [ -d monday_testing_1 ]; then
rm -rf monday_testing_1
fi
if [ -d monday_testing_2 ]; then
rm -rf monday_testing_2
fi
if [ -d test-1 ]; then
rm -rf test-1
fi
if [ -d test-2 ]; then
rm -rf test-2
fi
if [ -f the_test_file.txt ]; then
rm the_test_file.txt
fi
exit
EOF
return
}
trap cleanup 1 2 3 6
#--------------------------------------------------------------------
# This function is used to pull the information from the .locations file in order
# - setup the ssh calls. How this function works is by locating the file line number
# - of the head name of the ip_address and username. Then grab the next two lines and
# - pull the credentials that we will need. Once you grab the line, cut everything before
# - the equal sign and save.
credentials () {
if [ -f $HOME/.monday/.locations ]; then
line=$(grep -n "$1" $HOME/.monday/.locations | cut -d : -f 1)
if [ ! -z "$line" ] || [ "$line" != "" ]; then
line=$((line+1))
username2=$(sed -n $line'p' $HOME/.monday/.locations | awk -F'=' '{print $2}')
if [ -z "$username2" ] || [ "$username2" == "" ]; then
echo "There was an issue with getting the username that you requested..."
return
else
username=$username2
fi
line=$((line+1))
ip_address2=$(sed -n $line'p' $HOME/.monday/.locations | awk -F'=' '{print $2}')
if [ -z "$ip_address2" ] || [ "$ip_address2" == "" ]; then
echo "There was an issue with getting the password that you requested..."
return
else
ip_address=$ip_address2
fi
fi
fi
}
#--------------------------------------------------------------------
ip_address=""
username=""
storage_location="Documents/storage"
pull_switch=0
push_switch=0
current_directory=$(pwd)
error_status=0
remote_dest=DEFAULT
# Get the credentails from the file.
credentials "DEFAULT"
#--------------------------------------------------------------------
# Setup all of the moveable files for testing.
mkdir Monday_Testing
cd Monday_Testing
# Check the incoming arguments for independent testing capabilities, different
# - storage locations, and then lastly different servers to store too.
argument=$1
while [ $# -gt 0 ]; do
if [ "$1" == "-push" ]; then
push_switch=1
elif [ "$1" == "-pull" ]; then
pull_switch=1
elif [ "$1" == "-storage" ]; then
shift
store=$1
if [ "${store:0:1}" == '/' ]; then store="${store:1}"; fi
if [ "${store: -1}" == '/' ]; then store="${store::-1}"; fi
storage_location=$store
elif [ "$1" == "-remote" ]; then
shift
remote_dest=$(echo $1 | awk '{print toupper($0)}')
credentials $remote_dest
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
cat $HOME/.monday/.usage | more
return
fi
shift
done
if [ $push_switch -eq 0 ] && [ $pull_switch -eq 0 ]; then
push_switch=1
pull_switch=1
fi
if [ $push_switch -eq 1 ]; then
echo "------------------------------"
echo "Testing - Push.sh"
echo "------------------"
#---------------------------------------------------------------------------------
echo "Testing adding a just a file."
touch monday_test.txt
push monday_test.txt -storage $storage_location -remote $remote_dest 1> test_output.txt
results=$(cat test_output.txt | tail -2 | head -1)
rm test_output.txt
ssh $username@$ip_address -T 1> the_output.txt << EOF
outcome=\$(find \$HOME -type f -name monday_test.txt | wc -l)
if [ \$outcome -eq 1 ]; then
ls
cd \$HOME/$storage_location
rm monday_test.txt
echo "0"
elif [ \$outcome -gt 1 ]; then
echo "1"
fi
EOF
outcome=$(cat the_output.txt)
rm the_output.txt
if [ "$results" == 'Finished.' ] || [ "$outcome" == '0' ]; then
echo "PASSED"
else
echo "FAILED"
echo -e "\n\t----------------------------"
push monday_test.txt -storage $storage_location -remote $remote_dest -error 1> the_output.txt
test_failure
fi
rm monday_test.txt
echo "------------------"
#---------------------------------------------------------------------------------
echo "Testing adding a directory w/ files."
mkdir monday_testing
push monday_testing -storage $storage_location -remote $remote_dest 1> test_output.txt
results=$(cat test_output.txt | tail -2 | head -1)
rm test_output.txt
ssh $username@$ip_address -T 1> the_output.txt << EOF
outcome=\$(find \$HOME -type d -name monday_testing | wc -l)
if [ \$outcome -eq 1 ]; then
cd \$HOME/$storage_location
rm -rf monday_testing
echo "0"
elif [ \$outcome -gt 1 ]; then
echo "1"
fi
EOF
outcome=$(cat the_output.txt)
rm the_output.txt
if [ "$results" == "Finished." ] || [ "$outcome" == "0" ]; then
echo "PASSED"
else
echo "FAILED"
echo -e "\n\t----------------------------"
push monday_testing -storage $storage_location -remote $remote_dest -error 1> the_output.txt
test_failure
fi
rm -rf monday_testing
echo "------------------"
#---------------------------------------------------------------------------------
echo "Testing replacing a directory w/ an added file."
mkdir monday_testing
touch monday_testing/example_2222.txt
push monday_testing -storage $storage_location -remote $remote_dest 1> test_output.txt
results=$(cat test_output.txt | tail -2 | head -1)
rm test_output.txt
ssh $username@$ip_address -T 1> the_output.txt << EOF
outcome=\$(find \$HOME -type f -name example_2222.txt | wc -l)
if [ \$outcome -eq 1 ]; then
cd \$HOME/$storage_location
rm -rf monday_testing
echo "0"
elif [ \$outcome -gt 1 ]; then
echo "1"
fi
EOF
outcome=$(cat the_output.txt)
rm the_output.txt
if [ "$results" == 'Finished.' ] || [ "$outcome" == '0' ]; then
echo "PASSED"
else
echo "FAILED"
echo -e "\n\t----------------------------"
push monday_testing -storage $storage_location -remote $remote_dest -error 1> the_output.txt
test_failure
fi
echo "------------------"
#---------------------------------------------------------------------------------
echo "Testing adding two folders at once."
mkdir test-1 test-2
push test-1 test2 -storage $storage_location -remote $remote_dest 1> test_output.txt
results=$(cat test_output.txt | tail -2 | head -1)
rm test_output.txt
ssh -T $username@$ip_address 1> the_output.txt << EOF
outcome=\$(find \$HOME -type d -name test-1 | wc -l)
if [ \$outcome -eq 1 ]; then
cd \$HOME/$storage_location
rm -rf test-1
outcome=\$(find \$HOME -type d -name test-2 | wc -l)
if [ \$outcome -gt 1 ]; then
echo "1"
else
rm -rf test-2
echo "0"
fi
elif [ \$outcome -gt 1 ]; then
echo "1"
fi
EOF
outcome=$(cat the_output.txt)
rm the_output.txt
if [ "$results" == 'Finished.' ] || [ "$outcome" == '0' ]; then
echo "PASSED"
else
echo "FAILED"
echo -e "\n\t----------------------------"
push test-1 test2 -storage $storage_location -remote $remote_dest -error 1> the_output.txt
test_failure
fi
rm -rf test-1 test-2
echo "------------------"
#---------------------------------------------------------------------------------
echo "Testing adding a folder that has the same name as a file."
mkdir example_1.txt
push example_1.txt -storage $storage_location -remote $remote_dest 1> test_output.txt
results=$(cat test_output.txt | tail -2 | head -1)
rm test_output.txt
ssh -T $username@$ip_address 1> the_output.txt << EOF
cd \$HOME/$storage_location
# This is for the next test.
mkdir blank_1
touch blank_1/something.txt
mkdir blank_2
touch blank_2/something.txt
if [ -d "example_1.txt" ]; then
rm -rf example_1.txt
echo "0"
else
echo 1
fi
EOF
outcome=$(cat the_output.txt)
rm the_output.txt
if [ "$results" == 'Finished.' ] || [ "$outcome" == '0' ]; then
echo "PASSED"
else
echo "FAILED"
echo -e "\n\t----------------------------"
push example_1.txt -storage $storage_location -remote $remote_dest -error 1> the_output.txt
test_failure
fi
rm -rf example_1.txt
echo "------------------"
#---------------------------------------------------------------------------------
echo "Testing updating the correct file."
mkdir blank_1
touch blank_1/something.txt
echo "This has been updated." > blank_1/something.txt
cd blank_1
push something.txt -storage $storage_location -remote $remote_dest 1> ../test_output.txt
cd ..
results=$(cat test_output.txt | tail -2 | head -1)
rm test_output.txt
ssh -T $username@$ip_address 1> the_output.txt << EOF
cd \$HOME/$storage_location
temp=\$(cat blank_1/something.txt)
if [ "\$temp" == "This has been updated." ]; then
rm -rf blank_1 blank_2
echo "0"
else
rm -rf blank_1 blank_2
echo 1
fi
EOF
outcome=$(cat the_output.txt)
rm the_output.txt
if [ "$results" == 'Finished.' ] || [ "$outcome" == '0' ]; then
echo "PASSED"
else
echo "FAILED"
echo -e "\n\t----------------------------"
cd blank_1
push blank_1/something.txt -storage $storage_location -remote $remote_dest -error 1> ../the_output.txt
cd ..
test_failure
fi
rm -rf blank_1
echo "------------------"
#---------------------------------------------------------------------------------
echo "Cleaning Up the Push tests."
ssh $username@$ip_address -T << EOF
cd \$HOME/$storage_location
rm -rf monday_testing
EOF
rm -rf $current_directory/Monday_Testing/*
#---------------------------------------------------------------------------------
echo "------------------------------"
if [ $pull_switch -eq 1 ]; then
echo ""
fi
fi
if [ $pull_switch -eq 1 ]; then
ssh $username@$ip_address -T << EOF
cd \$HOME/$storage_location
mkdir monday_testing_1
touch monday_testing_1/something_1.txt
touch monday_testing_1/something_2.txt
mkdir monday_testing_2
touch the_test_file.txt
EOF
echo "------------------------------"
echo "Testing - Pull.sh"
echo "------------------"
#---------------------------------------------------------------------------------
echo "Pulling a directory with files."
pull monday_testing_1 -storage $storage_location -remote $remote_dest 1> the_output.txt
outcome=$(cat the_output.txt | tail -2 | head -1)
rm the_output.txt
directory=$(ls)
if [ "$directory" == "monday_testing_1" ] && [ "$outcome" == 'Finished.' ]; then
echo "PASSED"
else
echo "FAILED"
pull monday_testing_1 -storage $storage_location -remote $remote_dest -error 1> the_output.txt
test_failure
fi
rm -rf monday_testing_1
echo "------------------"
#---------------------------------------------------------------------------------
echo "Pulling an empty directory."
pull monday_testing_2 -storage $storage_location -remote $remote_dest 1> the_output.txt
outcome=$(cat the_output.txt | tail -2 | head -1)
rm the_output.txt
directory=$(ls)
if [ "$directory" == "monday_testing_2" ] && [ "$outcome" == 'Finished.' ]; then
echo "PASSED"
else
echo "FAILED"
echo -e "\n\t----------------------------"
pull monday_testing_2 -storage $storage_location -remote $remote_dest -error 1> the_output.txt
test_failure
fi
rm -rf monday_testing_2
echo "------------------"
#---------------------------------------------------------------------------------
echo "Pulling a file."
pull the_test_file.txt -storage $storage_location -remote $remote_dest 1> the_output.txt
outcome=$(cat the_output.txt | tail -2 | head -1)
rm the_output.txt
directory=$(ls)
if [ "$directory" == "the_test_file.txt" ] && [ "$outcome" == 'Finished.' ]; then
echo "PASSED"
else
echo "FAILED"
echo -e "\n\t----------------------------"
pull the_test_file.txt -storage $storage_location -remote $remote_dest -error 1> the_output.txt
test_failure
fi
echo "------------------"
#---------------------------------------------------------------------------------
echo "Pulling a file thats already on the system."
first_stamp=$(stat --printf=%y the_test_file.txt | cut -d. -f1)
sleep 5
ssh $username@$ip_address -T << EOF
echo "This is something extra" 1> \$HOME/$storage_location/the_test_file.txt
EOF
pull the_test_file.txt -storage $storage_location -remote $remote_dest 1> the_output.txt
outcome=$(cat the_output.txt | tail -2 | head -1)
rm the_output.txt
second_stamp=$(stat --printf=%y the_test_file.txt | cut -d. -f1)
if [ "$first_stamp" == "$second_stamp" ] || [ "$outcome" != 'Finished.' ]; then
echo "FAILED"
echo -e "\t----------------------------"
pull the_test_file.txt -storage $storage_location -remote $remote_dest -error 1> the_output.txt
test_failure
else
echo "PASSED"
fi
rm the_test_file.txt
echo "------------------"
#---------------------------------------------------------------------------------
echo "Pulling a file that is on the server twice."
ssh $username@$ip_address -T << EOF
cd \$HOME/$storage_location
touch monday_testing_2/the_test_file.txt
EOF
pull the_test_file.txt -storage $storage_location -remote $remote_dest 1> the_output.txt
outcome=$(cat the_output.txt | tail -1)
rm the_output.txt
if [ "$outcome" == 'Exiting...' ]; then
echo "PASSED"
else
echo "FAILED"
echo -e "\t----------------------------"
pull the_test_file.txt -storage $storage_location -remote $remote_dest -error 1> output.txt
test_failure
fi
echo "------------------"
#---------------------------------------------------------------------------------
echo "Pulling two folders."
pull monday_testing_1 monday_testing_2 -test -storage $storage_location -remote $remote_dest 1> the_output.txt
outcome=$(cat the_output.txt | tail -2 | head -1)
rm the_output.txt
directory_contents=$(ls | wc -l)
if [ "$directory_contents" == "2" ] && [ "$outcome" == "Finished." ]; then
echo "PASSED"
else
echo "FAILED"
echo -e "\t----------------------------"
pull monday_testing_1 monday_testing_2 -storage $storage_location -remote $remote_dest -error 1> the_output.txt
test_failure
fi
rm -rf monday_testing_1
rm -rf monday_testing_2
echo "------------------"
#---------------------------------------------------------------------------------
echo "Cleaning Up the Pull tests."
ssh $username@$ip_address -T << EOF
cd \$HOME/$storage_location
rm -rf monday_testing_1
rm -rf monday_testing_2
rm the_test_file.txt
EOF
#---------------------------------------------------------------------------------
echo "------------------------------"
fi
cd ..
rm -rf Monday_Testing
return