bringing in testkit tools
[feisty_meow.git] / testkit / library / runner_functions.sh
1 #!/bin/bash
2
3 # assorted useful ways of running executables.
4 #
5 # Author: Chris Koeritz
6
7 ##############
8
9 # similar to timed_grid, but for arbitrary commands, and fits in with the timing
10 # calculators.
11 function timed_command()
12 {
13   echo "[started timer $(readable_date_string)]"
14   $(\which time) -p -o "$TESTKIT_TIMING_FILE" $*
15   local retval=$?
16   echo "[stopped timer $(readable_date_string)]"
17   return $retval
18 }
19
20 ##############
21
22 # uses the timing file to determine how long the last activity took in
23 # seconds and then prints out the value.
24 calculateTimeTaken()
25 {
26   head -n 1 $TESTKIT_TIMING_FILE | awk '{print $2}'
27 }
28
29 # calculates the bandwidth for a transfer.  this takes the elapsed time as
30 # the first parameter and the size transferred as second parameter.
31 calculateBandwidth()
32 {
33   local real_time="$1"; shift
34   local size="$1"; shift
35   # drop down to kilobytes rather than bytes.
36   size=$(echo $size / 1024 | $(\which bc) -l)
37
38 #echo "time=$real_time size=$size"
39
40   local total_sec="$(echo "$real_time" | awk -Fm '{print $1}'| awk -F. '{print $1}' )"
41   local bandwidth=""
42   if [[ "$total_sec" =~ .*exited.* ]]; then
43     echo "FAILURE: Test run failed in some way; setting total seconds to very large number."
44     total_sec="99999999"
45   fi
46   if [ $total_sec -eq 0 ]; then
47     # fake it when we get a math issue where something took less than a second.
48     total_sec=1
49   fi
50   bandwidth="$(echo "scale=3; $size / $total_sec" | $(\which bc) -l)"
51   echo "$bandwidth"
52 }
53
54 # a wrapper for calculateBandwidth that prints out a nicer form of the
55 # bandwidth.  it requires the same parameters as calculateBandwidth.
56 showBandwidth()
57 {
58   echo "  Bandwidth $(calculateBandwidth $*) kbps"
59 }
60
61 ##############
62
63 # connects to a host as a particular user and executes a command there.
64 function run_command_remotely()
65 {
66   if [ $# -lt 3 ]; then
67     echo This function connects to a remote host to run a command.  It requires
68     echo at least three parameters: the host to connect to, the user name on
69     echo that host which supports passwordless logins, and the command to run.
70     echo The command to run is the third through Nth parameters.
71   fi
72   host="$1"; shift
73   username="$1"; shift
74   # run our expecter to feed commands in, and the last one needs to be exit so we
75   # return to the original host.
76   OUTFILE="$(mktemp $TMP/ssh_run.XXXXXX)"
77   expect $TESTKIT_ROOT/library/ssh_expecter.tcl "$username" "" "$host" "${@}" >"$OUTFILE"
78   reval=$?
79   # make sure we didn't experience a failure on the other side.
80   grep "YO_FAILURE" $OUTFILE &>/dev/null
81   if [ $? -eq 0 ]; then
82     echo Detected failure running command via ssh.
83     ((retval++))
84   fi
85
86 #debugging
87 echo ========== output from command ============
88 cat "$OUTFILE"
89 echo ===========================================
90
91   rm "$OUTFILE"
92   return $retval
93 }
94
95 #testing
96 #run_command_remotely serene fred "ls /"
97
98 ##############
99