Merge commit 'v1.1.7'
[unicorn.git] / t / test-lib.sh
blob7b4a36581ea7fbb55f93c8a7dd6d4f1ea2532e1f
1 #!/bin/sh
2 # Copyright (c) 2009 Rainbows! hackers
3 # Copyright (c) 2010 Unicorn hackers
4 . ./my-tap-lib.sh
6 set +u
8 # sometimes we rely on http_proxy to avoid wasting bandwidth with Isolate
9 # and multiple Ruby versions
10 NO_PROXY=${UNICORN_TEST_ADDR-127.0.0.1}
11 export NO_PROXY
13 set -e
14 RUBY="${RUBY-ruby}"
15 RUBY_VERSION=${RUBY_VERSION-$($RUBY -e 'puts RUBY_VERSION')}
16 RUBY_ENGINE=${RUBY_ENGINE-$($RUBY -e 'puts((RUBY_ENGINE rescue "ruby"))')}
17 t_pfx=$PWD/trash/$T-$RUBY_ENGINE-$RUBY_VERSION
18 set -u
20 PATH=$PWD/bin:$PATH
21 export PATH
23 test -x $PWD/bin/unused_listen || die "must be run in 't' directory"
25 wait_for_pid () {
26 path="$1"
27 nr=30
28 while ! test -s "$path" && test $nr -gt 0
30 nr=$(($nr - 1))
31 sleep 1
32 done
35 # given a list of variable names, create temporary files and assign
36 # the pathnames to those variables
37 rtmpfiles () {
38 for id in "$@"
40 name=$id
41 _tmp=$t_pfx.$id
42 eval "$id=$_tmp"
44 case $name in
45 *fifo)
46 rm -f $_tmp
47 mkfifo $_tmp
48 T_RM_LIST="$T_RM_LIST $_tmp"
50 *socket)
51 rm -f $_tmp
52 T_RM_LIST="$T_RM_LIST $_tmp"
55 > $_tmp
56 T_OK_RM_LIST="$T_OK_RM_LIST $_tmp"
58 esac
59 done
62 dbgcat () {
63 id=$1
64 eval '_file=$'$id
65 echo "==> $id <=="
66 sed -e "s/^/$id:/" < $_file
69 check_stderr () {
70 set +u
71 _r_err=${1-${r_err}}
72 set -u
73 if grep -v $T $_r_err | grep -i Error
74 then
75 die "Errors found in $_r_err"
76 elif grep SIGKILL $_r_err
77 then
78 die "SIGKILL found in $_r_err"
82 # unicorn_setup
83 unicorn_setup () {
84 eval $(unused_listen)
85 port=$(expr $listen : '[^:]*:\([0-9]\+\)')
86 host=$(expr $listen : '\([^:]*\):[0-9]\+')
88 rtmpfiles unicorn_config pid r_err r_out fifo tmp ok
89 cat > $unicorn_config <<EOF
90 listen "$listen"
91 pid "$pid"
92 stderr_path "$r_err"
93 stdout_path "$r_out"
94 EOF
97 unicorn_wait_start () {
98 # no need to play tricks with FIFOs since we got "ready_pipe" now
99 unicorn_pid=$(cat $pid)
102 rsha1 () {
103 _cmd="$(which sha1sum 2>/dev/null || :)"
104 test -n "$_cmd" || _cmd="$(which openssl 2>/dev/null || :) sha1"
105 test "$_cmd" != " sha1" || _cmd="$(which gsha1sum 2>/dev/null || :)"
107 # last resort, see comments in sha1sum.rb for reasoning
108 test -n "$_cmd" || _cmd=sha1sum.rb
109 expr "$($_cmd)" : '\([a-f0-9]\{40\}\)'