varnishtest "Health-checks: some http-check tests"
feature ignore_unknown_macro
#REQUIRE_VERSION=2.2
#REGTEST_TYPE=slow
# This script tests HTTP health-checks.

server s1 {
    rxreq
    expect req.method == OPTIONS
    expect req.url == /
    expect req.proto == HTTP/1.0
    txresp
} -start

server s2 {
    rxreq
    expect req.method == GET
    expect req.url == /status
    expect req.proto == HTTP/1.1
    txresp
} -start

server s3 {
    rxreq
    expect req.method == GET
    expect req.url == /status
    expect req.proto == HTTP/1.1
    txresp
} -start

server s4 {
    rxreq
    expect req.method == GET
    expect req.url == /req1
    expect req.proto == HTTP/1.1
    expect req.http.x-test == "server=srv"
    expect req.http.x-haproxy-server-state ~ "UP.+name=be4/srv"
    expect req.bodylen == 0
    txresp

    accept
    rxreq
    expect req.method == GET
    expect req.url == /req2
    expect req.proto == HTTP/1.1
    expect req.http.x-test == "server="
    expect req.http.x-haproxy-server-state ~ "UP.+name=be4/srv"
    expect req.http.content-length == 17
    expect req.bodylen == 17
    expect req.body == "health-check body"
    txresp

    accept
    rxreq
    expect req.method == GET
    expect req.url == /req3
    expect req.proto == HTTP/1.0
    expect req.http.x-test == <undef>
    expect req.http.x-haproxy-server-state ~ "UP.+name=be4/srv"
    expect req.bodylen == 0
    txresp

    accept
    rxreq
    expect req.method == GET
    expect req.url == /
    expect req.proto == HTTP/1.0
    expect req.http.x-test == <undef>
    expect req.http.x-haproxy-server-state ~ "UP.+name=be4/srv"
    expect req.bodylen == 23
    expect req.body == "health-check on be4-srv"
    txresp

} -start

syslog S1 -level notice {
    recv
    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be[0-9]/srv succeeded.*code: 200"
    recv
    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be[0-9]/srv succeeded.*code: 200"
    recv
    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be[0-9]/srv succeeded.*code: 200"
    recv
    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be[0-9]/srv succeeded.*code: 200"
    recv
    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be[0-9]/srv succeeded.*code: 200"
    recv
    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be[0-9]/srv succeeded.*code: 200"
} -start

haproxy h1 -conf {
    defaults
        mode http
        timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
        timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
        timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
        option log-health-checks

    backend be1
        log ${S1_addr}:${S1_port} len 2048 local0
        option httpchk
        server srv ${s1_addr}:${s1_port} check inter 100ms rise 1 fall 1

    backend be2
        log ${S1_addr}:${S1_port} len 2048 local0
        option httpchk GET /status HTTP/1.1
        server srv ${s2_addr}:${s2_port} check inter 100ms rise 1 fall 1

    backend be3
        log ${S1_addr}:${S1_port} len 2048 local0
        option httpchk
        http-check send meth GET uri /status ver HTTP/1.1
        server srv ${s3_addr}:${s3_port} check inter 100ms rise 1 fall 1

    backend be4
        mode tcp
        log ${S1_addr}:${S1_port} len 2048 local0
        option httpchk
        http-check send-state
        http-check connect addr ${s4_addr}:${s4_port}
        http-check set-var(check.server) "str(srv)"
        http-check set-var(check.path) "str(/req1)"
        http-check send meth GET uri-lf "%[var(check.path)]" ver HTTP/1.1 hdr x-test "server=%[var(check.server)]"
        http-check expect status 200
        http-check connect addr ${s4_addr} port ${s4_port}
        http-check unset-var(check.server)
        http-check set-var(check.path) "str(/req2)"
        http-check send meth GET uri-lf "%[var(check.path)]" ver HTTP/1.1 hdr x-test "server=%[var(check.server)]" body "health-check body"
        http-check expect rstatus "^2[0-9]{2}"
        http-check connect addr ${s4_addr} port ${s4_port}
        http-check set-var(check.path) "str(/req3)"
        http-check send meth GET uri-lf "%[var(check.path)]"
        http-check expect rstatus "^2[0-9]{2}"
        http-check connect addr ${s4_addr} port ${s4_port}
        http-check unset-var(check.path)
        http-check send meth GET uri-lf "%[var(check.path)]" body-lf "health-check on %[be_name]-%[srv_name]"
        ## implicit expect rule
        server srv ${s1_addr}:${s1_port} check inter 100ms rise 1 fall 1

    backend be5
        log ${S1_addr}:${S1_port} len 2048 local0
        option httpchk
        server srv ${h1_li1_addr}:${h1_li1_port} proto h2 check inter 100ms rise 1 fall 1

    backend be6
        log ${S1_addr}:${S1_port} len 2048 local0
        option httpchk GET /status HTTP/1.1
        server srv ${h1_li1_addr}:${h1_li1_port} check check-proto h2 inter 100ms rise 1 fall 1

    listen li1
        mode http
        bind "fd@${li1}" proto h2
        http-request return status 200

} -start

syslog S1 -wait
