aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/test.sh
blob: 1662eed54105caed70309839be7a7d0b46d0264d (plain) (blame)
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
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bash

# Copyright (c) 2021 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "linux-status" project.
# For details, see https://github.com/egor-tensin/linux-status.
# Distributed under the MIT License.

set -o errexit -o nounset -o pipefail

script_dir="$( dirname -- "${BASH_SOURCE[0]}" )"
script_dir="$( cd -- "$script_dir" && pwd )"
readonly script_dir
script_name="$( basename -- "${BASH_SOURCE[0]}" )"
readonly script_name

readonly server_port=18101
server_pid=
curl_header_file=
curl_output_file=

dump() {
    local msg
    for msg; do
        echo "$script_name: $msg"
    done
}

run_server() {
    dump "Starting up server..."
    "$script_dir/../server.py" --port "$server_port" &
    server_pid="$!"
    dump "Its PID is $server_pid"
    sleep 5
}

kill_server() {
    [ -z "$server_pid" ] && return
    dump "Killing server with PID $server_pid..."
    kill "$server_pid"
    dump "Waiting for it to terminate..."
    wait "$server_pid" || true
    dump "Done"
}

create_files() {
    curl_header_file="$( mktemp )"
    curl_output_file="$( mktemp )"
    dump "curl header file: $curl_header_file"
    dump "curl output file: $curl_output_file"
}

cleanup_files() {
    dump "Cleaning up curl files..."
    [ -n "$curl_header_file" ] && rm -f -- "$curl_header_file"
    [ -n "$curl_output_file" ] && rm -f -- "$curl_output_file"
}

prepare() {
    run_server
    create_files
}

cleanup() {
    kill_server || true
    cleanup_files || true
}

run_curl() {
    if [ "$#" -ne 1 ]; then
        echo "usage: ${FUNCNAME[0]} URL" >&2
        return 1
    fi
    local url="$1"
    curl \
        --silent --show-error \
        --dump-header "$curl_header_file" \
        --output "$curl_output_file" \
        --connect-timeout 3 \
        -- "http://localhost:$server_port$url" || true
}

curl_check_status() {
    if [ "$#" -ne 1 ]; then
        echo "usage: ${FUNCNAME[0]} HTTP_STATUS" >&2
        return 1
    fi

    local expected="$1"
    expected="HTTP/1.0 $expected"$'\r'
    local actual
    actual="$( head -n 1 -- "$curl_header_file" )"

    [ "$expected" == "$actual" ] && return 0

    dump "Actual HTTP response: $actual" >&2
    dump "Expected:             $expected" >&2

    dump 'HTTP headers:' >&2
    cat -- "$curl_header_file" >&2
    dump 'HTTP response:' >&2
    cat -- "$curl_output_file" >&2
    return 1
}

curl_check_keyword() {
    local keyword
    for keyword; do
        if ! grep --fixed-strings --quiet -- "$keyword" "$curl_output_file"; then
            dump "The following pattern hasn't been found:"
            dump "$keyword"
        fi
    done
}

run_curl_test() {
    if [ "$#" -lt 1 ]; then
        echo "usage: ${FUNCNAME[0]} URL [KEYWORD...]" >&2
        return 1
    fi

    local url="$1"
    shift
    dump "Running test for URL: $url"

    run_curl "$url"
    curl_check_status '200 OK'

    local keyword
    for keyword; do
        curl_check_keyword "$keyword"
    done
}

run_curl_tests() {
    # / and /index.html are identical:
    run_curl_test '/'           '<link rel="stylesheet" href="css/bootstrap.min.css">' 'var status_refresh_interval_seconds'
    run_curl_test '/index.html' '<link rel="stylesheet" href="css/bootstrap.min.css">' 'var status_refresh_interval_seconds'

    # /status returns a JSON with a number of fields:
    run_curl_test '/status' '"hostname":' '"system":' '"user":'

    # /top is a `top` output:
    run_curl_test '/top' 'load average:'
}

cgi_check_header() {
    local expected='Content-Type: text/html; charset=utf-8'
    local actual
    actual="$( head -n 1 -- "$curl_output_file" )"

    [ "$expected" == "$actual" ] && return 0

    dump "Actual CGI header: $actual" >&2
    dump "Expected:          $expected" >&2

    diff <( echo "$actual" ) <( echo "$expected" ) | cat -te
    return 1
}

run_cgi_test() {
    if [ "$#" -lt 1 ]; then
        echo "usage: ${FUNCNAME[0]} WHAT [KEYWORD...]" >&2
        return 1
    fi

    local what="$1"
    shift

    local query_string="what=$what"
    dump "Running CGI test for query string: $query_string"

    QUERY_STRING="$query_string" "$script_dir/../app.py" > "$curl_output_file"

    cgi_check_header

    local keyword
    for keyword; do
        curl_check_keyword "$keyword"
    done
}

run_cgi_tests() {
    # Check that app.py still works as a CGI script.

    # /status returns a JSON with a number of fields:
    run_cgi_test 'status' '"hostname":' '"system":' '"user":'
    # /top is a `top` output:
    run_cgi_test 'top' 'load average:'
}

main() {
    trap cleanup EXIT
    prepare
    run_curl_tests
    run_cgi_tests
}

main "$@"