-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathtc_utils.source
executable file
·2588 lines (2345 loc) · 75.7 KB
/
tc_utils.source
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
# vi: ts=4 sw=4 autoindent expandtab :
############################################################################################
## Copyright 2003, 2016 IBM Corp ##
## ##
## Redistribution and use in source and binary forms, with or without modification, ##
## are permitted provided that the following conditions are met: ##
## 1.Redistributions of source code must retain the above copyright notice, ##
## this list of conditions and the following disclaimer. ##
## 2.Redistributions in binary form must reproduce the above copyright notice, this ##
## list of conditions and the following disclaimer in the documentation and/or ##
## other materials provided with the distribution. ##
## ##
## THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS AND ANY EXPRESS ##
## OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF ##
## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ##
## THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ##
## EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF ##
## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ##
## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ##
## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ##
## SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ##
############################################################################################
#
# File : tc_utils.source
#
# Description: Utility functions to be used by shell script testcases.
# Shell script testcases should source this file near the
# beginning of the file. This should be done as follows:
#
# LTPBIN=HOME_OF_AUTOTEST_CLIENT_INSTALL/autotest-client-tests/linux-tools/shared
# source $LTPBIN/tc_utils.source
#
# Author: Robert Paulsen, [email protected]
# Modified: Poornima Nayak [email protected]
#
################################################################################
# global variables
################################################################################
declare -i TST_TOTAL TST_COUNT
export TST_TOTAL
export TST_COUNT=0 # Current test's sequence number
export TCID=${0##*/} # Name of executable containing the testcases.
[ "$TST_TOTAL" ] || TST_TOTAL=1 # Total number of testcases in file
# used to pass info to/from testcases and these utility functions
TCNAME="" # set by tc_register. Used in output
TC_TEMP_PASSWD="" # set by tc_add_user when a temp user is created
TC_TEMP_HOME="" # set by tc_add_user when a temp user is created
TC_TEMP_USER="" # set by tc_add_user when a temp user is created
TC_TEMP_GROUP="" # set by tc_add_group when a temp group is created
stdout="" # testcases can put stdout in this file (set in setup)
stderr="" # testcases can put stderr in this file (set in setup)
TCTMP="" # subdir for use by the testcase (a subdir of TC_MYTMP)
TC_PORT="" # to return a found port number
TC_LOOPDEV="" # to return a free loop device
TC_SLEEPER_PID="" # to return pd of a newly created sleeper process
# track temp users and groups
declare -i tc_u_ndx=0 # count temp users
declare -i tc_g_ndx=0 # count temp groups
TC_TEMP_USERS="" # holds all temp users
TC_TEMP_GROUPS="" # holds all temp groups
# final exit codes. Set by various functions.
declare -i tc_final_result=0 # will be count of failures
declare -i tc_brok_result=0 # 0 and -1 are only allowed values.
declare -i tc_warn_result=0 # 0 and 99 are only allowed values.
declare -i tc_watchdog_result=0 # 0 and 88 are only allowed values.
TC_RUNLOG="fivrunlog.txt" # name of file to list tests as they run
TC_ABAT_LOG_DIR="" # where to put $TC_RUNLOG
# things to kill at end of testcase
TC_SLEEPER_PIDS="" # list of PIDs for sleeper processes
TC_MYTMP="" # if setup creates the temp dir, remember it here
TC_SAVE_NET="/etc/hosts /etc/resolv.conf" # use fully qualified names
TC_ARGS="$@" # script args as they are when this file is sourced
OS="Redhat" # At the moment enabling support for Redhat OS
################################################################################
# internal utility functions
# NOTE: functions named tc_internal_* are intended for use ONLY by other
# functions in this file and should NOT be used directly by testcases.
################################################################################
#
# tc_internal_dump Helper function for FAIL or BROK situations
#
function tc_internal_dump()
{
local -i rc=$1
shift;
tc_info "$@" "rc=$rc"
if [ -s $stdout ] ; then
tc_info "============= stdout follows ============="
cat $stdout
echo ""
tc_info "============== stdout above =============="
cat /dev/null > $stdout
fi
if [ -s $stderr ] ; then
[ $rc -eq 0 ] && rc=255 # show break/fail due to stderr
tc_info "============= stderr follows ============="
cat $stderr
echo ""
tc_info "============== stderr above =============="
cat /dev/null > $stderr
fi
return $rc
}
#
# tc_internal_executes helper function for tc_executes, tc_exec_or_break,
# tc_exec_or_fail
#
# Checks all passed words to be sure they represent executable cmds.
# Echos a string of those that are not executable.
#
function tc_internal_executes()
{
local cmd fails
for cmd in "$@" ; do
type $cmd &>/dev/null || fails="$fails $cmd"
done
echo $fails
}
#
# tc_internal_exists helper function for tc_exists, tc_exist_or_break,
# tc_exist_or_fail
#
# Checks all passed words to be sure they represent executable cmds.
# Echos a string of those that are not executable.
#
function tc_internal_exists()
{
local file fails
for file in "$@" ; do
[ -e "$file" ] || fails="$fails $file"
done
echo $fails
}
#
# tc_internal_cleanup testcase cleanup (reached via trap 0)
#
# input: None.
#
# output: If passfail has registered any failure(s), exits with count of
# failures (which will be in $tc_final_result). If no failure
# occured perhaps there was a BROK or WARN. If so exit with
# appropriate result. Otherwise exit with 0.
#
# Trap for this is set in tc_setup after enoigh infrastructure available to support it.
#
function tc_internal_cleanup()
{
# Be sure watchdog did not time out then kill it. Do this first thing so
# any watchdog timeout is correctly attributed to the currently running test.
local tc_watchdog_pid=$(<$TC_SAVE/tc_watchdog_pid-$$)
[ -d /proc/$tc_watchdog_pid ] || {
tc_watchdog_result=88
tc_fail_if_bad $tc_watchdog_result "watchdog timed out!"
echo "$(date) WATCHDOG TIMEOUT: $TCID" >> $TMPBASE/WATCHDOG.log
}
kill $tc_watchdog_pid &>/dev/null ||
kill -9 $tc_watchdog_pid &>/dev/null
rm -f $TC_SAVE/tc_watchdog_script-$$.sh
rm -f $TC_SAVE/tc_watchdog_pid-$$
# call testcase's cleanup, if any
TCNAME="tc_local_cleanup"
:>$stdout
:>$stderr
tc_local_cleanup
# account for un-executed tests
[ "$TST_COUNT" -lt "$TST_TOTAL" ] && {
let TST_COUNT+=1
echo "Remaining cases broken"
exit 1
}
# restore saved networking files and flush nscd cache, if possible
local f dir file
[ -d "$TC_SAVE" ] &&
for f in $TC_SAVE_NET ; do
if tc_is_busybox cp ; then
dir=${f%/*}
file=${f##*/}
cp $TC_SAVE/$dir/$file $dir/$file
else
cp -ax $TC_SAVE/$f $f
fi
done
type nscd &>/dev/null && nscd -i hosts
# get rid of any residual sleeper processes
local sleeper_pid
for sleeper_pid in $TC_SLEEPER_PIDS ; do
[ -d /proc/$sleeper_pid ] || continue
kill $sleeper_pid &>/dev/null ||
kill -9 $sleeper_pid &>/dev/null
done
# clean up some other outstanding things
tc_cap_log_stop
tc_del_user_or_break
tc_del_group_or_break
[ -d "$TC_MYTMP" ] && rm -rf $TC_MYTMP # supplied by setup so delete it
TST_COUNT=0
rm -rf $TC_SAVE
# remove dir created in tc_check_or_create_dir
rm -rf $REMOVE_DIRS
# record the fact that we ended a testcase
[ "$TC_ABAT_LOG_DIR" ] && echo "$(date) FINISHED: $TCID" >> $TC_ABAT_LOG_DIR/$TC_RUNLOG
tc_info "Finish at $(date)"
tc_logger "FINISHED"
# set proper exit code and be gone
[ $tc_watchdog_result -ne 0 ] && exit $tc_watchdog_result
[ $tc_final_result -ne 0 ] && exit $tc_final_result
[ $tc_brok_result -ne 0 ] && exit $tc_brok_result
[ $tc_warn_result -ne 0 ] && exit $tc_warn_result
exit 0
}
################################################################################
# utility functions for use by testcases
################################################################################
#
# tc_logger Put data into syslog
# Simply passes arguments to logger command
# but first checks that logger command is available.
#
# returns 0 if logged OK, non-zero if problem.
#
# Testcsae status is unaffected.
#
function tc_logger()
{
tc_executes logger || return
local tag="TEST unknown"
local message="no message"
[ "$TCID" ] && tag="TEST $TCID"
[ "$*" ] && message="$*"
logger -t "$tag" "$message"
}
#
# tc_cap_log_start Start recording snapshot of syslog
#
# $1 the name of a file to capture the snapshot. A required argument.
# The testcase will be BROK and aborted if the name of a writable file
# is not passed as first and only argument.
#
# Only one instance of the capture process is allowed. If an instance is
# already running, it will be killed before a new instance is started.
# If the same filename is specified the file will be overwritten.
#
# If the file is not under $TCTMP it is the caller's responsibility to
# eventually delete it.
#
# Use tc_cap_log_stop to stop capturing the snapshot.
#
# Breaks testcase and exits if there is an error.
# Returns true otherwise.
#
function tc_cap_log_start()
{
[ $# -eq 1 ] && touch $1 && rm -f $1 && tc_wait_for_no_file $1
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: Must pass writable file." || exit
local snapshot=$1
tc_cap_log_stop
# "-n 0" is needed to ensure we don't include extraneous log data in snapshot.
# "sync" is needed but I'm not sure why.
tail -n 0 -f /var/log/messages > $snapshot &
local tc_cap_log_pid=$!
echo $tc_cap_log_pid > $TC_SAVE/tc_cap_log_pid
tc_wait_for_pid $tc_cap_log_pid && tc_wait_for_file $snapshot
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: could not tail /var/log/messages" || exit
sync
# if we have the ability, test out capture capability
tc_logger "$FUNCNAME" && {
tc_wait_for_file_text $snapshot "$FUNCNAME" 30
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: cannot log to $snapshot" || {
tc_info "============ $snapshot ============"
cat $snapshot
tc_info "==================================="
exit 1
}
}
return 0
}
#
# tc_cap_log_stop Stop recording snapshot of syslog
#
# No arguments required. Always stops the one allowed snapshot, if any,
# as identified by $TC_SAVE/tc_cap_log_pid.
#
# Exits on error.
#
function tc_cap_log_stop()
{
[ -f $TC_SAVE/tc_cap_log_pid ] || return 0 # cap_log not running
local tc_cap_log_pid=$(<$TC_SAVE/tc_cap_log_pid)
[ "$tc_cap_log_pid" ] && {
kill $tc_cap_log_pid
tc_wait_for_no_pid $tc_cap_log_pid
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: Could not kill tc_cap_log_pid=$tc_cap_log_pid" || exit
}
rm -f $TC_SAVE/tc_cap_log_pid
return 0
}
#
# tc_run_me_only_once. Allow testcase to execute only once per run
#
# Simply invoke this function right after doing tc_setup and if
# the same testcase, with the same options, is in a scenario file
# more than once, it will only be run once, at the first instatnce.
#
# Note that the directory $TMPBASE/tc_run_me_only_once is created by
# the runfiv script. If that directory doesn't exist, this function
# is a no-op.
#
function tc_run_me_only_once()
{
[ -d $TMPBASE/tc_run_me_only_once ] || return 0
local tag=$(echo $0 $TC_ARGS) # drops leading and trailing blanks
tag=${tag//*\//} # drop leading path info
tag=${tag// /_} # no spaces
[ -f $TMPBASE/tc_run_me_only_once/$tag ] && {
tc_info "$tag has already run"
TST_COUNT=$TST_TOTAL
exit 0
}
touch $TMPBASE/tc_run_me_only_once/$tag
tc_info "$tag running for the first and only time"
return 0
}
#
# Convert decimal to hex
#
# $1 a decimal integer
# $2 number of digits in output (optional, "as needed") Will include leading zeros
# $3 X or x for upper or lower case (optional, default upper case)
#
# Invalid argument will BROK and exit testcase.
#
# Output is to stdout. Typical use:
#
# my_hex_number=$(tc_dec2hex $my_decimal_number)
#
function tc_dec2hex()
{
local digits="" # cannot be declared -i as we manipulate it as a string
local case=X
# parse 1st arg (a decimal number)
local -i dec_num=$1
((dec_num+=0)) &>/dev/null
tc_break_if_bad_rc $? "$FUNCNAME: Internal testcase error: first arg must be decimal integer" || exit
# parse 2nd arg (number of output digits)
[ "$2" ] && {
digits=$2
((0<digits)) &>/dev/null
tc_break_if_bad_rc $? "$FUNCNAME: Internal testcase error: $digits not valid number of digits" || exit
digits="0$digits" # force leading zeros
}
# parse 3rd arg (upper or lower case)
[ "$3" ] && case=$3
[ "$case" = "x" -o "$case" = "X" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal testcase error: $case must be x or X" || exit
# print the converted value
printf "%${digits}${case}" $dec_num
}
#
# Convert hex to dec
#
# $1 a hex integer
# $2 number of digits in output (optional, default "as needed") Will include leading zeros
#
# Supply the hex number with or without the leading "0x".
#
# Invalid argument will BROK and exit testcase.
#
# Output is to stdout. Typical use:
#
# my_decimal_number=$(tc_hex2dec $my_hex_number)
#
function tc_hex2dec()
{
local hex_num=$1
local digits="" # cannot be declared -i as we manipulate it as a string
# parse first arg (a hex number)
[ "${hex_num:0:2}" = "0x" -o "${hex_num:0:2}" = "0X" ] || hex_num=0x$1
((hex_num+=0)) &>/dev/null
tc_break_if_bad_rc $? "$FUNCNAME: Internal testcase error: first arg must be hex integer" || exit
# parse 2nd arg (number of output digits)
[ "$2" ] && {
digits=$2
((0<digits)) &>/dev/null
tc_break_if_bad_rc $? "$FUNCNAME: Internal testcase error: $digits not valid number of digits" || exit
digits="0$digits" # force leading zeros
}
# print the converted value
printf "%${digits}d" $hex_num
}
#
# tc_wait_for_file Wait for file (or directory) meet criteria
#
# $1 (required) file or directory to wait for
# $2 (optional) how long to wait, in seconds (defailt 10)
# $3 (optional) one of directory, size, exist (default exist)
#
# By default checks for existence of file/directory. Use of third
# argument can check that the file has non-zero size or must be a
# directory. Note that the arguments are positional so in order to
# provide a test type (exist, size, directory) you must also supply a
# wait time.
#
# Invalid arguments will cause the entire testcase to be aborted via exit.
#
function tc_wait_for_file()
{
[ "$1" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: called w/o file name." || exit
local -i max=10 # default
[ "$2" ] && {
max=$2
((max>0))
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: $2 is not a positive integer." || exit
}
local -i n=$max
local cmd_arg=""
[ "$3" ] && {
[ "$3" = "size" ] && cmd_arg="-s"
[ "$3" = "directory" ] && cmd_arg="-d"
[ "$3" = "exist" ] && cmd_arg="-e"
[ "$cmd_arg" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: $3 is invalid argument." || exit
}
[ "$cmd_arg" ] || cmd_arg="-e" # the default
while [ $n -gt 0 ] ; do
[ $cmd_arg $1 ] && break
((--n)) || break
tc_info "Waiting for file \"$1\" to appear ($n)"
sleep 1
done
((n>0))
}
#
# tc_wait_for_no_file Wait for file (or directory) to go away
#
# $1 (required) file or directory to wait for
# $2 (optional) how long to wait, in seconds (defailt 10)
#
# Invalid arguments will cause the entire testcase to be aborted via exit
#
function tc_wait_for_no_file()
{
[ "$1" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: called w/o file name." || exit
local -i max=10 # default
[ "$2" ] && {
max=$2
((max>0))
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: $2 is not a positive integer." || exit
}
local -i n=$max
while [ -e "$1" ] ; do
((--n)) || break
tc_info "Waiting for file \"$1\" to go away ($n)"
sleep 1
done
((n>0))
}
#
# tc_wait_for_file_text Wait for text to show up in a file
#
# $1 (required) file name
# $2 (required) text to look for
# $3 (optional) number of seconds to attempt (default 10)
#
# Invalid arguments will cause the entire testcase to be aborted via exit
#
function tc_wait_for_file_text()
{
set "$@"
[ $# -eq 2 -o $# -eq 3 ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: Needs 2 arguments." || exit
local -i max=10 # default
[ "$3" ] && {
max=$3
((max>0))
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: $3 is not a positive integer." || exit
}
tc_wait_for_file $1 $max || return
# this deliberately avoids grep so as not to introduce a dependency
local -i n=$max
while ((--n)) ; do
while read line ; do
local mod=${line/$2/}
[ "$mod" = "$line" ] || break 2
done < $1
tc_info "Waiting for file \"$1\" to contain \"$2\" ($n)"
sync
sleep 1
done
((n>0))
}
# tc_wait_for_service Wait for a service to be up
#
# $1 (required) name of service to be up
# $2 (optional) how long to wait, in seconds (default 10)
#
# Returns non-zero on failure.
# Invalid arguments will cause the entire testcase to be aborted via exit
#
function tc_wait_for_service()
{
local WAIT_SERVICE="$1"
[ "$WAIT_SERVICE" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: called w/o service." || exit
local -i n=10 # default
local -i rc=0
[ "$2" ] && {
n=$2
((n>0))
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: $2 is not a positive integer." || exit
}
tc_info "checking if service $WAIT_SERVICE is up"
while (! service $WAIT_SERVICE status ) &>/dev/null; do
tc_info "waiting for service $WAIT_SERVICE to be up"
((--n)) || break
sleep 1
done
((n>0))
}
# tc_wait_for_no_service Wait for a service to be down
#
# $1 (required) name of service to be down
# $2 (optional) how long to wait, in seconds (default 10)
#
# Returns non-zero on failure.
# Invalid arguments will cause the entire testcase to be aborted via exit
#
function tc_wait_for_no_service()
{
local WAIT_SERVICE="$1"
[ "$WAIT_SERVICE" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: called w/o service." || exit
local -i n=10 # default
local -i rc=0
[ "$2" ] && {
n=$2
((n>0))
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: $2 is not a positive integer." || exit
}
tc_info "checking if service $WAIT_SERVICE is down"
##########################################
# check for Ubuntu OS
##########################################
grep -i "ubuntu" /etc/*-release >/dev/null 2>&1
if [ $? -eq 0 ];then # Start of OS check
while ( service $WAIT_SERVICE $WAIT_SERVICE ) &>/dev/null; do
tc_info "waiting for service $WAIT_SERVICE to be down"
((--n)) || break
sleep 1
done
((n>0))
else
while ( systemctl status $WAIT_SERVICE ) &>/dev/null; do
tc_info "waiting for service $WAIT_SERVICE to be down"
((--n)) || break
sleep 1
done
((n>0))
fi
}
# tc_service_start_and_wait Start and Wait for a service to be up
#
# $1 (required) name of service to be up
# $2 (optional) how long to wait, in seconds (default 10)
#
# Returns non-zero on failure.
# Invalid arguments will cause the entire testcase to be aborted via exit
#
function tc_service_start_and_wait()
{
local SERVICE="$1"
local TIMEOUT="$2"
local -i rc=0
[ "$SERVICE" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: called w/o service." || exit
tc_info "starting the service $SERVICE"
##########################################
# check for Ubuntu OS
##########################################
grep -i "ubuntu" /etc/*-release >/dev/null 2>&1
if [ $? -eq 0 ];then # Start of OS check
service $SERVICE start >$stdout 2>$stderr
else
systemctl start $SERVICE >$stdout 2>$stderr
fi
tc_break_if_bad $? "$FUNCNAME: $SERVICE failed to start" || exit
tc_wait_for_service $SERVICE $TIMEOUT
}
# tc_service_restart_and_wait restart and Wait for a service to be restarted
# #
# # $1 (required) name of service to be restarted
# # $2 (optional) how long to wait, in seconds (default 10)
# #
# # Returns non-zero on failure.
# # Invalid arguments will cause the entire testcase to be aborted via exit
# #
#
function tc_service_restart_and_wait()
{
local SERVICE="$1"
local TIMEOUT="$2"
local -i rc=0
[ "$SERVICE" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: called w/o service." || exit
tc_info "restarting the service $SERVICE"
##########################################
# check for Ubuntu OS
##########################################
grep -i "ubuntu" /etc/*-release >/dev/null 2>&1
if [ $? -eq 0 ];then # Start of OS check
service $SERVICE restart >$stdout 2>$stderr
else
systemctl restart $SERVICE >$stdout 2>$stderr
fi
tc_break_if_bad $? "$FUNCNAME: $SERVICE failed to start" || exit
tc_wait_for_service $SERVICE $TIMEOUT
}
function tc_service_status()
{
local SERVICE="$1"
local -i rc=0
[ "$SERVICE" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: called w/o service." || exit
tc_info "checking the status of the service $SERVICE"
##########################################
# check for Ubuntu OS
##########################################
grep -i "ubuntu" /etc/*-release >/dev/null 2>&1
if [ $? -eq 0 ];then # Start of OS check
service $SERVICE status >$stdout 2>$stderr
else
systemctl status $SERVICE >$stdout 2>$stderr
fi
rc=$?
if [ $rc -eq 0 ]
then
tc_info "$SERVICE is active (running)"
else
tc_info "$SERVICE is inactive (dead)"
fi
return $rc
}
# tc_service_stop_and_wait stop and Wait for a service to be stopped
# #
# # $1 (required) name of service to be stopped
# # $2 (optional) how long to wait, in seconds (default 10)
# #
# # Returns non-zero on failure.
# # Invalid arguments will cause the entire testcase to be aborted via exit
# #
#
function tc_service_stop_and_wait()
{
local SERVICE="$1"
local TIMEOUT="$2"
local -i rc=0
local -i n=10 # default
[ "$2" ] && {
n=$2
((n>0))
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: $2 is not a positive integer." || exit
}
[ "$SERVICE" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: called w/o service." || exit
tc_info "stopping the service $SERVICE"
##########################################
# check for Ubuntu OS
##########################################
grep -i "ubuntu" /etc/*-release >/dev/null 2>&1
if [ $? -eq 0 ];then # Start of OS check
service $SERVICE stop >$stdout 2>$stderr
tc_break_if_bad $? "$FUNCNAME: $SERVICE failed to stop" || exit
tc_info "checking if service $SERVICE is stopped"
while ( systemctl $SERVICE status) &>/dev/null; do
tc_info "waiting for service $SERVICE to be stopped"
((--n)) || break
sleep 1
done
((n>0))
else
systemctl stop $SERVICE >$stdout 2>$stderr
tc_break_if_bad $? "$FUNCNAME: $SERVICE failed to stop" || exit
tc_info "checking if service $SERVICE is stopped"
while ( systemctl status $SERVICE ) &>/dev/null; do
tc_info "waiting for service $SERVICE to be stopped"
((--n)) || break
sleep 1
done
((n>0))
fi
}
#
# tc_wait_for_pid Wait for a pid (or list of pids) to appear
#
# $1 (required) pid to wait for. put multiple pids in quotes.
# $2 (optional) how long to wait, in seconds (defailt 10)
#
# Invalid arguments will cause the entire testcase to be aborted via exit
#
function tc_wait_for_pid()
{
local WAIT_PIDS="$1"
[ "$WAIT_PIDS" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: called w/o pid." || exit
local -i max=10 # default
[ "$2" ] && {
max=$2
((max>0))
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: $2 is not a positive integer." || exit
}
local WAIT_PID
for WAIT_PID in $WAIT_PIDS ; do
local -i n=$max
# /proc/pid/maps will be :
# - non-empty for an active process.
# - Doesn't exist for a dead process
# - Empty for a zombie process
# However [ -s ] doesn't work for verifying if the file
# is empty.
while ( ! grep . /proc/$WAIT_PID/maps ) >/dev/null 2>&1 ; do
tc_info "waiting for pid $WAIT_PID to show ($n)"
sleep 1
((--n)) || break
done
((n>0)) || return
done
return 0
}
#
# tc_wait_for_no_pid Wait for a pid (or list of pids) to DISappear
#
# $1 (required) pid to wait for. put multiple pids in quotes.
# $2 (optional) how long to wait, in seconds (defailt 10)
#
# Invalid arguments will cause the entire testcase to be aborted via exit
#
function tc_wait_for_no_pid()
{
local WAIT_PIDS="$1"
[ "$WAIT_PIDS" ]
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: called w/o pid." || exit
local -i max=10 # default
[ "$2" ] && {
max=$2
((max>0))
tc_break_if_bad_rc $? "$FUNCNAME: Internal script error: $2 is not a positive integer." || exit
}
local WAIT_PID
for WAIT_PID in $WAIT_PIDS ; do
local -i n=$max
while ls /proc/$WAIT_PID >/dev/null 2>&1 ; do
tc_info "waiting for pid $WAIT_PID to disappear ($n)"
sleep 1
((--n)) || break
done
((n>0)) || return
done
return 0
}
#
# tc_ignore_warnings Ignore expected stderr warnings
#
# $@ - string to ignore in stderr
#
#
function tc_ignore_warnings()
{
save_stderr="$TCTMP/save_stderr"
diff_stderr="$TCTMP/diff_stderr"
search_string=$@
cp $stderr $save_stderr
cat /dev/null >$stderr
`sed "/$search_string/d" $save_stderr >$stderr`
`diff -B $stderr $save_stderr | grep "^>" |awk -F ">" '{print $2}'>$diff_stderr`
if [ -s $diff_stderr ]; then
echo "### $TCNAME ###"
echo "There are some warnings in stderr which are ignored"
cat $diff_stderr
fi
}
#############################################################################
# array operations
#############################################################################
#
# There are nine functions defined.
#
# Array operations:
# tc_array_is_empty return true if array is empty
# tc_array_size Show (echo) size of the array
# tc_array_dump Show (echo) all elements of an array, one per line
#
# Stack operations (array is manipulated like a stack):
# tc_array_push Push element to top of stack.
# tc_array_top Show (echo) element on top of stack.
# tc_array_pop Remove element from top of stack.
#
# Queue operations (array is manipulated like a queue):
# tc_array_enqueue Enqueue element to FIFO queue (same as tc_array_push).
# tc_array_next Show (echo) oldest element on FIFO queue.
# tc_array_dequeue Remove oldest element on FIFO queue.
#
# These assume all elements are contiguous in the array. No empty slots.
#
# Invalid arguments will cause the entire testcase to be aborted via exit
#
function tc_array_is_empty()
{
local tc_array_name=$1
[ "$tc_array_name" ]
tc_break_if_bad_rc $? "$FUNCNAME: internal script error. Need array name." || exit
! (( $(tc_array_size $tc_array_name) ))
}
function tc_array_size()
{
local tc_array_name=$1
local -i tc_size
[ "$tc_array_name" ]
tc_break_if_bad_rc $? "$FUNCNAME: internal script error. Need array name." || exit
eval tc_size=\${#$tc_array_name[*]}
echo $tc_size
}
function tc_array_dump()
{
local tc_array_name=$1
local -i tc_size
local -i tc_index=0
[ "$tc_array_name" ]
tc_break_if_bad_rc $? "$FUNCNAME: internal script error. Need array name." || exit
eval tc_size=\${#$tc_array_name[*]}
while ((tc_index<tc_size)) ; do
eval echo "\${$tc_array_name[tc_index++]}"
done
echo
}
#
# Push a value on to an array acting as a stack.
# This can also be used as enqueue to FIFO queue.
#
function tc_array_push()
{
local tc_array_name=$1
local tc_value=$2
local -i tc_size
[ "$tc_array_name" -a "$tc_value" ]
tc_break_if_bad_rc $? "$FUNCNAME: internal script error. Need array name and value to push." || exit
eval tc_size=\${#$tc_array_name[*]}
eval $tc_array_name[tc_size]=\"$tc_value\"
# eval $tc_array_name=\("\${$tc_array_name[@]}" $tc_value\) # alternate method -- probably slower for large arrays
}
#
# Show top of stack
#
function tc_array_top()
{
local tc_array_name=$1
local -i tc_size
[ "$tc_array_name" ]
tc_break_if_bad_rc $? "$FUNCNAME: internal script error. Need array name." || exit
eval tc_size=\${#$tc_array_name[*]}
((tc_size)) &&
eval echo \${$tc_array_name[tc_size-1]}
}
#
# Pop a value off of stack.
#
function tc_array_pop()
{
local tc_array_name=$1
[ "$tc_array_name" ]
tc_break_if_bad_rc $? "$FUNCNAME: internal script error. Need array name." || exit
eval unset $tc_array_name[\${#$tc_array_name[@]}-1]
}
#
# Enqueue a value to a FIFO queue. This is really the same as tc_array_push.
#
function tc_array_enqueue() { tc_array_push "$@" ; }
#
# Show oldest value on FIFO queue, i.e. next to be processed.
#
function tc_array_next()
{
local tc_array_name=$1
local -i tc_size
[ "$tc_array_name" ]
tc_break_if_bad_rc $? "$FUNCNAME: internal script error. Need array name." || exit
eval tc_size=\${#$tc_array_name[*]}
((tc_size)) &&
eval echo \${$tc_array_name[0]}
}
#
# Dequeue oldest entry on FIFO queue, i.e. most recently processed.
#
function tc_array_dequeue()
{
local tc_array_name=$1
local -i tc_size
[ "$tc_array_name" ]