aboutsummaryrefslogtreecommitdiff
path: root/tests/SharedInputs/server_wrapper.sh
blob: 84b377babf4caf8ec0105323356889394df80b60 (plain)
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
#!/bin/bash
# This script wraps a call to lnt runtest with a local server
# instance.  It is intended for testing full runtest invocations
# that need a real server instance to work. Starts a server at
# `http://localhost:9089`.
# ./server_wrapper <location of server files> <port> <command>
# Example:
# ./server_wrapper /tmp 9089 lnt runtest nt --submit "http://localhost:9089/db_default/submitRun" --cc /bin/clang --sandbox /tmp/sandbox

# First launch the server.

PROGRAM="$(basename $0)"

usage() {
    echo "usage: $PROGRAM <location of server files> <runtest type> <submit-through-url> <portnr> [arguments for lnt runtest]"
    echo "e.g:   $PROGRAM /tmp/ nt yes --cc /bin/clang --sandbox /tmp/sandbox"
}

error() {
    echo "error: $PROGRAM: $*" >&2
    usage >&2
    exit 1
}

main() {
    [ $# -lt 2 ] &&
        error "not enough arguments"

    local fail_on_error=0
    if [ "$1" = "--fail-on-error" ]; then
        fail_on_error=1
        shift
    fi

    local server_instance=$1
    local port_number=$2
    shift 2

    lnt runserver ${server_instance} --hostname localhost --port ${port_number} >& ${server_instance}/server_wrapper_runserver.log &
    local pid=$!

    # In the polling code below, absence of 'curl' would cause an infinite loop
    # instead of "command not found" error, so check for environment
    # misconfiguration explicitly.
    if ! curl --version > /dev/null; then
        echo 1>&2 "Command 'curl' not found."
        exit 1
    fi
    # Poll the server until it is up and running
    while ! curl http://localhost:${port_number}/ping -m1 -o/dev/null -s ; do
        # Maybe server is totally dead.
        kill -0 ${pid} 2> /dev/null || { echo "Server exit detected"; cat ${server_instance}/server_wrapper_runserver.log; break; }
        # If not sleep and keep trying.
        sleep 0.1
    done

    # Execute command.
    eval "$@"
    local rc=$?

    kill -15 ${pid}
    local kill_rc=$?
    [ ${kill_rc} -ne 0 ] &&
        error "wha happen??  ${kill_rc}"

    wait ${pid}

    if [ "$fail_on_error" = "1" ]; then
        if egrep "ERROR|FATAL|raise" "${server_instance}/server_wrapper_runserver.log"; then
            echo 1>&2 "Logfile indicates problems: ${server_instance}/server_wrapper_runserver.log"
            exit 1
        fi
    fi

    exit ${rc}
}

main "$@"