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
|
#!/bin/bash
IN_STOP=0
IN_FUCKED=0
banner(){
echo "======================================"
echo "$@"
echo "======================================"
}
banner "starting tests"
quilt push -a
make -C src
LOG=debian/httpd.log
if [ -z "$DHTTPD" ]; then
DHTTPD=./src/dhttpd
fi
runit() {
echo "Starting web server with these arguments: -d $@"
# we can't let it fork
$DHTTPD -d "$@" 2>&1
echo "Exited with error code: $?"
echo
}
runit_under_log(){
runit "$@" | tee -a $LOG >/dev/null
}
start_him(){
runit_under_log "$@" &
WEBSERVER_PID=$!
echo "webserver started, PID is $WEBSERVER_PID"
}
start_him_with_port(){
PORT="$1"
shift
start_him -p "$PORT" "$@"
}
start_web_server(){
stop_web_server
start_him_with_port 55555 "$@"
}
cleanup(){
if [ -n "$DHTTPD_TEST_NOCLEANUP" ]; then
return
fi
rm -f $LOG
}
fucked(){
if [ "$IN_FUCKED" = "1" ]; then
# just to be safe
return
fi
IN_FUCKED=1
echo "$@"
stop_web_server
echo "here's the log:"
cat $LOG
banner "tests failed"
cleanup
exit 1
}
list_children_of(){
pstree -p "$1" | sed -r 's/[^()]*\(([0-9]+)\)/\1\n/g' | sed -e '/^$/ d' -e "/^$1\$/ d"
}
get_name_of_pid(){
ps -p "$1" -o cmd=
}
find_dhttpd_child(){
for i in $(list_children_of $1); do
if get_name_of_pid "$i" | grep -q dhttpd; then
echo "$i"
return
fi
done
fucked "couldn't find dhttpd child of $1"
return
}
stop_web_server(){
if [ "$IN_STOP" = "1" ]; then
# this calls fucked and fucked calls this
return
fi
IN_STOP=1
if [ -z "$WEBSERVER_PID" ]; then
IN_STOP=0
return
fi
if ! kill -0 $WEBSERVER_PID; then
fucked "It is no longer running"
return
fi
# TODO: should the dhttpd child just be found before everything happens, and set WEBSERVER_PID to that? seems like it would be better.
KILLME=$(find_dhttpd_child $WEBSERVER_PID)
if [ -n "$KILLME" ]; then
kill $KILLME
fi
wait $WEBSERVER_PID
IN_STOP=0
}
is_running(){
if [ -z "$WEBSERVER_PID" ]; then
fucked "it's been stopped"
return
fi
kill -0 $WEBSERVER_PID
return $?
}
test_is_running(){
echo "testing if it's running.."
if ! is_running; then
fucked "it died"
return
fi
echo "good"
}
test_is_listening(){
echo "testing if it's listening.."
if ! netstat -tpln 2>/dev/null | grep 0.0.0.0:$PORT | grep -q dhttpd; then
fucked "netstat doesn't see it"
return
fi
echo "good"
}
all_good(){
banner "tests passed"
cleanup
exit 0
}
test_is_accepting(){
echo "testing if it's accepting and responding to connections.."
if ! echo -ne "GET / HTTP/1.1\r\nHostname: foobar\r\n\r\n" | nc 127.0.0.1 $PORT >/dev/null; then
fucked "nc didn't return true"
return
fi
echo "good"
}
test_is_ok(){
echo "testing if it's returning 200.."
if ! echo -ne "GET / HTTP/1.1\r\nHostname: foobar\r\n\r\n" | nc 127.0.0.1 $PORT | head -1 | grep -qE "^HTTP/[0-9.]+ 200 OK"; then
fucked "didn't return a 200 OK response"
return
fi
echo good
}
nap(){
echo "taking a nap.."
sleep 3
echo "back"
}
test_is_running_after_nap() {
nap
test_is_running
}
build_test_tree() {
}
start_web_server -r ./debian/testdir
test_is_running
test_is_running_after_nap
test_is_listening
test_is_accepting
test_is_ok
stop_web_server
all_good
|