3 # useful functions that are somewhat general. these are not needed for
4 # the basic setup of the test environment, but they are used by other
5 # test and tool functions and also by specific tests.
7 # Author: Chris Koeritz
9 # prints out a timestamp with the current date and time up to seconds.
10 function date_string()
12 date +"%Y_%b_%e_%H%M_%S" | sed -e 's/ //g'
15 # prints out the timestamp in a somewhat readable way.
16 function readable_date_string()
22 # (donated by the feisty meow scripts at http://feistymeow.org)
25 [[ "$(declare -p $1)" =~ "declare -a" ]]
33 # displays the value of a variable in bash friendly format.
38 local varname="$1"; shift
39 if [ -z "$varname" ]; then
43 if is_alias "$varname"; then
44 #echo found $varname is alias
45 local tmpfile="$(mktemp $TMP/aliasout.XXXXXX)"
46 alias $varname | sed -e 's/.*=//' >$tmpfile
47 echo "alias $varname=$(cat $tmpfile)"
49 elif [ -z "${!varname}" ]; then
50 echo "$varname undefined"
52 if is_array "$varname"; then
53 #echo found $varname is array var
55 eval temparray="(\${$varname[@]})"
56 echo "$varname=(${temparray[@]})"
57 #hmmm: would be nice to print above with elements enclosed in quotes, so that we can properly
58 # see ones that have spaces in them.
60 #echo found $varname is simple
61 echo "$varname=${!varname}"
69 # given a file name and a phrase to look for, this replaces all instances of
70 # it with a piece of replacement text. note that slashes are okay in the two
71 # text pieces, but we are using pound signs as the regular expression
72 # separator; phrases including the octothorpe (#) will cause syntax errors.
73 function replace_phrase_in_file()
75 local file="$1"; shift
76 local phrase="$1"; shift
77 local replacement="$1"; shift
78 if [ -z "$file" -o -z "$phrase" ]; then
79 echo "replace_phrase_in_file: needs a filename, a phrase to replace, and the"
80 echo "text to replace that phrase with."
83 sed -i -e "s%$phrase%$replacement%g" "$file"
86 # prints an error message (from parameters) and exits if the previous command failed.
87 function check_if_failed()
95 # takes a first parameter that is the name for a combined error and output log,
96 # and then runs all the other parameters as a command.
97 function logged_command()
99 local my_output="$1"; shift
100 # echo "logged_command args: $(printf -- "[%s] " "${@}")"
101 eval "${@}" >>"$my_output" 2>&1
103 if [ $retval == 0 ]; then
104 # good so far, but check for more subtle ways of failing; if there is
105 # an occurrence of our fail message in the output, that also indicates
106 # the command did not succeed.
107 grep "\[FAILURE\]" $my_output
108 # we do not want to see that phrase in the log.
110 return 0 # fine exit, can ignore log.
113 if [[ ! "$my_output" =~ .*fuse_output.* ]]; then
114 # this was a failure, so we need to see the log.
115 # fuse errors currently don't count since they are multifarious.
121 # runs an arbitrary command. if the command fails, then the output from it is
122 # displayed and an error code is returned. otherwise the output is discarded.
123 function run_any_command()
125 local my_output="$(mktemp $TEST_TEMP/test_logs/out_run_any_cmd_$(date_string).XXXXXX)"
126 logged_command "$my_output" "${@}"
128 # make the external version of the log file available. if we're multiplexing users,
129 # this will be clobbered constantly, which is why we used unique names above.
130 \cp -f "$my_output" "$TESTKIT_OUTPUT_FILE"
131 # then add the logging results to our huge mongo log of all actions.
132 echo >> "$CONGLOMERATED_TESTKIT_OUTPUT"
133 echo "$(readable_date_string) log from: $my_output" >> "$CONGLOMERATED_TESTKIT_OUTPUT"
134 echo "=======" >> "$CONGLOMERATED_TESTKIT_OUTPUT"
135 cat "$my_output" >> "$CONGLOMERATED_TESTKIT_OUTPUT"
136 echo "=======" >> "$CONGLOMERATED_TESTKIT_OUTPUT"
137 # and now remove the tiny individual log file so we don't litter.
143 # returns 0 if there should be no problems using fuse, or non-zero if this platform
144 # does not currently support fuse.
145 function fuse_supported()
148 local platform="$(uname -a | tr A-Z a-z)"
149 if [[ $platform =~ .*darwin.* ]]; then retval=1; fi
150 if [[ $platform =~ .*cygwin.* ]]; then retval=1; fi
151 if [[ $platform =~ .*ming.* ]]; then retval=1; fi
155 # returns 0 if there should be no problems creating links in the file system,
156 # or non-zero if this platform does not support symbolic links.
157 function links_supported()
160 local platform="$(uname -a | tr A-Z a-z)"
161 if [[ $platform =~ .*cygwin.* ]]; then retval=1; fi
162 if [[ $platform =~ .*ming.* ]]; then retval=1; fi
166 # Create a test directory (in the first parameter) with $2 subdirectories,
167 # each with $3 subdirs, each with $4 files.
168 fan_out_directories()
170 local dir_name="$1"; shift
171 local top_count=$1; shift
172 local mid_count=$1; shift
173 local file_count=$1; shift
175 for (( di=0 ; di<$top_count ; di++ )); do
176 mkdir "$dir_name"/sub$di
177 for (( dj=0 ; dj<$mid_count ; dj++ )); do
178 mkdir "$dir_name"/sub$di/sub$dj
179 for (( dk=0 ; dk<$file_count ; dk++ )); do
180 echo "file $di$dj$dk" > "$dir_name"/sub$di/sub$dj/file$di$dj$dk
187 # copied from open source codebase at: http://feistymeow.org with permission of author (chris koeritz),
188 # assigned apache ASL license for this usage.
189 # locates a process given a search pattern to match in the process list.
190 # supports a single command line flag style parameter of "-u USERNAME";
191 # if the -u flag is found, a username is expected afterwards, and only the
192 # processes of that user are considered.
194 local -a patterns=("${@}")
196 #echo patterns list is: "${patterns[@]}"
200 if [ "${patterns[0]}" == "-u" ]; then
201 user_flag="-u ${patterns[1]}"
202 #echo "found a -u parm and user=${patterns[1]}"
203 # void the two elements with that user flag so we don't use them as patterns.
204 unset patterns[0] patterns[1]
210 local PID_DUMP="$(mktemp "$TMP/zz_pidlist.XXXXXX")"
213 if [ "$OS" == "Windows_NT" ]; then
214 # gets cygwin's (god awful) ps to show windoze processes also.
215 local EXTRA_DOZER_FLAGS="-W"
216 # pattern to use for peeling off the process numbers.
217 local pid_finder_pattern='s/ *\([0-9][0-9]*\) *.*$/\1/p'
220 # flags which clean up the output on unixes, which apparently cygwin
221 # doesn't count as. their crappy specialized ps doesn't support this.
222 local EXTRA_UNIX_FLAGS="-o pid,args"
223 # pattern to use for peeling off the process numbers.
224 local pid_finder_pattern='s/^[[:space:]]*\([0-9][0-9]*\).*$/\1/p'
227 /bin/ps $EXTRA_DOZER_FLAGS $EXTRA_UNIX_FLAGS $user_flag | tail -n +2 >$PID_DUMP
229 #echo got all this stuff in the pid dump file:
233 # search for the pattern the user wants to find, and just pluck the process
234 # ids out of the results.
236 for i in "${patterns[@]}"; do
237 PIDS_SOUGHT+=($(cat $PID_DUMP \
239 | sed -n -e "$pid_finder_pattern"))
242 #echo pids sought list became:
243 #echo "${PIDS_SOUGHT[@]}"
246 if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
247 local PIDS_SOUGHT2=$(printf -- '%s\n' ${PIDS_SOUGHT[@]} | sort | uniq)
249 PIDS_SOUGHT=${PIDS_SOUGHT2[*]}
250 echo ${PIDS_SOUGHT[*]}
258 # tests the supposed fuse mount that is passed in as the first parameter.
259 function test_fuse_mount()
261 local mount_point="$1"; shift
262 local trunc_mount="$(basename "$(dirname $mount_point)").$(basename "$mount_point")"
264 checkMount="$(mount)"
265 #echo "checkmount is: '$checkMount'"
266 #echo "mount point seeking is: '$trunc_mount'"
268 if [[ "$checkMount" =~ .*$trunc_mount.* ]]; then
269 #echo found the mount in the list
272 if [ $retval -ne 0 ]; then
273 echo "Finding mount point '$trunc_mount' failed."
276 ls -l "$mount_point" &>/dev/null
282 # also borrowed from feisty meow scripts... by consent of author (chris koeritz).
284 # is this the Mac OS X operating system?
287 if [ ! -z "$(echo $OSTYPE | grep -i darwin)" ]; then
294 # switches from a /X/path form to an X:/ form. this also processes cygwin paths.
295 function unix_to_dos_path() {
296 # we usually remove dos slashes in favor of forward slashes.
297 # if [ ! -z "$SERIOUS_SLASH_TREATMENT" ]; then
298 # # unless this flag is set, in which case we force dos slashes.
299 # echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/' | sed -e 's/\//\\/g'
301 echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/'
305 # switches from an X:/ form to an /X/path form.
306 function dos_to_unix_path() {
307 # we always remove dos slashes in favor of forward slashes.
308 echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/'