varnishtest "Add/Delete server via cli with check support"

feature cmd "$HAPROXY_PROGRAM -cc 'version_atleast(2.5-dev3)'"
feature cmd "$HAPROXY_PROGRAM -cc 'feature(OPENSSL)'"
feature ignore_unknown_macro

barrier b1 cond 2 -cyclic
barrier b2 cond 2 -cyclic

server s1 {
	rxreq
	txresp
} -start

server s2 {
} -start

# used for agent checks
server s3 {
	recv 5
	send "ready up\n"
	barrier b2 sync
} -start

syslog S1 -level notice {
	recv
	expect ~ ".*Server be1/s1 is UP/READY \\(leaving forced maintenance\\)."
	recv
	expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be1/s1 succeeded.+reason: Layer7 check passed, code: 200, check duration: [[:digit:]]+ms.+status: 1/1 UP"

	barrier b1 sync

	recv
	expect ~ ".*Server be1/s2 is UP/READY \\(leaving forced maintenance\\)."
	recv
	expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be1/s2 failed.+reason: Layer7 timeout, check duration: [[:digit:]]+ms.+status: 0/1 DOWN"

	barrier b1 sync

	recv
	expect ~ ".*Server be1/s2 was DOWN and now enters maintenance."

	recv
	expect ~ ".*Server be1/s3 is UP/READY \\(leaving forced maintenance\\)."

	recv
	expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Agent check for server be1/s3 succeeded.+reason: Layer7 check passed, code: 0, info: \"via agent : up\", check duration: [[:digit:]]+ms.+status: 1/1 UP"

	barrier b1 sync
	barrier b2 sync

	recv
	expect ~ ".*Server be1/s4 is UP/READY \\(leaving forced maintenance\\)."
	recv
	expect ~ "Health check for server be1/s4 failed"

	barrier b1 sync

	recv
	expect ~ ".*Server be1/s5 is UP/READY \\(leaving forced maintenance\\)."
	recv
	expect ~ "Health check for server be1/s5 succeeded."
} -start

haproxy h1 -conf {
	global
		stats socket "${tmpdir}/h1/stats" level admin

	defaults
		timeout client 30s
		timeout server 30s
		timeout connect 30s

	backend be1
		option log-health-checks
		option httpchk GET /
		log ${S1_addr}:${S1_port} daemon

	frontend fe-proxy
		mode http
		bind "fd@${hapsrv}" accept-proxy
		http-request return status 200
} -start

# check on a functional server
haproxy h1 -cli {
	send "add server be1/s1 ${s1_addr}:${s1_port} check inter 200ms rise 1 fall 1"
	expect ~ "New server registered."

	send "enable server be1/s1"
	expect ~ ".*"
	send "enable health be1/s1"
	expect ~ ".*"

	barrier b1 sync

	send "disable server be1/s1"
	expect ~ ".*"

	send "del server be1/s1"
	expect ~ "Server deleted."
}

server s2 -break

# check on a disabled server
haproxy h1 -cli {
	send "add server be1/s2 ${s2_addr}:${s2_port} check inter 200ms rise 1 fall 1"
	expect ~ "New server registered."

	send "enable server be1/s2"
	expect ~ ".*"
	send "enable health be1/s2"
	expect ~ ".*"

	barrier b1 sync

	send "disable server be1/s2"
	expect ~ ".*"

	send "del server be1/s2"
	expect ~ "Server deleted."
}

# agent check
haproxy h1 -cli {
	send "add server be1/s3 ${s1_addr}:${s1_port} agent-check agent-addr ${s3_addr} agent-port ${s3_port} agent-send 'hello' agent-inter 200ms rise 1 fall 1"
	expect ~ "New server registered."

	send "enable agent be1/s3"
	expect ~ ".*"

	barrier b1 sync

	send "disable agent be1/s3; disable server be1/s3"
	expect ~ ".*"

	send "del server be1/s3"
	expect ~ "Server deleted."
}

# check PROXY protocol interaction with checks
haproxy h1 -cli {
	# no explicit check-send-proxy
	# The health check should failed.
	send "add server be1/s4 ${h1_hapsrv_addr}:${h1_hapsrv_port} send-proxy check rise 1 fall 1"
	expect ~ "New server registered."

	send "enable server be1/s4"
	expect ~ ".*"
	send "enable health be1/s4"
	expect ~ ".*"

	barrier b1 sync

	# explicit check-send-proxy : health check should succeeded
	send "add server be1/s5 ${h1_hapsrv_addr}:${h1_hapsrv_port} send-proxy check rise 1 fall 1 check-send-proxy"
	expect ~ "New server registered."

	send "enable server be1/s5"
	expect ~ ".*"
	send "enable health be1/s5"
	expect ~ ".*"
}

syslog S1 -wait
