REGTESTS: healthcheckmail: Update the test to be functionnal again

This reg-test is broken since a while. It was simplified to be
functionnal. Now, it only test email alerts.

(cherry picked from commit 52912579eea3680952c7dd9e80c8ea5c298eeca3)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 6ad2f99a7f03652fc86837c37e3dce81b822b162)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 00fc0b6f9db548bb9121dccd3a29a6a48875f12f)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/reg-tests/mailers/healthcheckmail.lua b/reg-tests/mailers/healthcheckmail.lua
index 50b561c..4cb0e9d 100644
--- a/reg-tests/mailers/healthcheckmail.lua
+++ b/reg-tests/mailers/healthcheckmail.lua
@@ -4,46 +4,6 @@
 local mailconnectionsmade = 0
 local healthcheckcounter = 0
 
-core.register_action("bug", { "http-res" }, function(txn)
-	data = txn:get_priv()
-	if not data then
-		data = 0
-	end
-	data = data + 1
-	print(string.format("set to %d", data))
-	txn.http:res_set_status(200 + data)
-	txn:set_priv(data)
-end)
-
-core.register_service("luahttpservice", "http", function(applet)
-	local response = "?"
-	local responsestatus = 200
-       if applet.path == "/setport" then
-		vtc_port1 = applet.headers["vtcport1"][0]
-		response = "OK"
-	end
-	if applet.path == "/svr_healthcheck" then
-		healthcheckcounter = healthcheckcounter + 1
-		if healthcheckcounter < 2 or healthcheckcounter > 6 then
-			responsestatus = 403
-		end
-	end
-
-	applet:set_status(responsestatus)
-       if applet.path == "/checkMailCounters" then
-		response = "MailCounters"
-		applet:add_header("mailsreceived", mailsreceived)
-		applet:add_header("mailconnectionsmade", mailconnectionsmade)
-	end
-	applet:start_response()
-	applet:send(response)
-end)
-
-core.register_service("fakeserv", "http", function(applet)
-	applet:set_status(200)
-	applet:start_response()
-end)
-
 function RecieveAndCheck(applet, expect)
 	data = applet:getline()
 	if data:sub(1,expect:len()) ~= expect then
@@ -60,20 +20,24 @@
 	applet:send("220 Welcome\r\n")
 	local data
 
-	if RecieveAndCheck(applet, "EHLO") == false then
-		return
+	if RecieveAndCheck(applet, "HELO") == false then
+	   applet:set_var("txn.result", "ERROR (step: HELO)")
+	   return
 	end
 	applet:send("250 OK\r\n")
 	if RecieveAndCheck(applet, "MAIL FROM:") == false then
-		return
+	   applet:set_var("txn.result", "ERROR (step: MAIL FROM)")
+	   return
 	end
 	applet:send("250 OK\r\n")
 	if RecieveAndCheck(applet, "RCPT TO:") == false then
-		return
+	   applet:set_var("txn.result", "ERROR (step: RCPT TO)")
+	   return
 	end
 	applet:send("250 OK\r\n")
 	if RecieveAndCheck(applet, "DATA") == false then
-		return
+	   applet:set_var("txn.result", "ERROR (step: DATA)")
+	   return
 	end
 	applet:send("354 OK\r\n")
 	core.Info("#### Send your mailbody")
@@ -83,7 +47,7 @@
 		data = applet:getline() -- BODY CONTENT
 		--core.Info(data)
 		if data:sub(1, 9) == "Subject: " then
-			subject = data
+		   subject = data
 		end
 		if (data == "\r\n") then
 			data = applet:getline() -- BODY CONTENT
@@ -97,9 +61,10 @@
 	applet:send("250 OK\r\n")
 
 	if RecieveAndCheck(applet, "QUIT") == false then
-		return
+	   applet:set_var("txn.result", "ERROR (step: QUIT)")
+	   return
 	end
 	applet:send("221 Mail queued for delivery to /dev/null \r\n")
 	core.Info("Mail queued for delivery to /dev/null subject: "..subject)
-	mailsreceived = mailsreceived + 1
+	applet:set_var("txn.result", "SUCCESS")
 end)
diff --git a/reg-tests/mailers/healthcheckmail.vtc b/reg-tests/mailers/healthcheckmail.vtc
index ce3335f..e6b668e 100644
--- a/reg-tests/mailers/healthcheckmail.vtc
+++ b/reg-tests/mailers/healthcheckmail.vtc
@@ -1,76 +1,59 @@
-varnishtest "Lua: txn:get_priv() scope"
+varnishtest "Check health-check email alerts"
 #REQUIRE_OPTIONS=LUA
-#REQUIRE_VERSION=1.6
-#REGTEST_TYPE=broken
 
 feature ignore_unknown_macro
 
-server s1 {
-    rxreq
-    txresp
+syslog S1 -level notice {
+    recv
+    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be1/srv1 failed.+reason: Socket error.+info: \"Connection reset by peer\".+check duration: [[:digit:]]+ms.+status: 0/1 DOWN."
+    recv info
+    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Result=SUCCESS Bytes=[[:digit:]]+"
 } -start
 
 haproxy h1 -conf {
     global
         lua-load ${testdir}/healthcheckmail.lua
-defaults
-    frontend femail
+
+    defaults
+        timeout client 1s
+        timeout server 1s
+        timeout connect 1s
+
+    listen lisrv
         mode tcp
-        bind "fd@${femail}"
-        tcp-request content use-service lua.mailservice
+        bind "fd@${lisrv}"
+        tcp-request connection reject
 
-    frontend luahttpservice
-        mode http
-        bind "fd@${luahttpservice}"
-        http-request use-service lua.luahttpservice
+    listen lismtp
+        mode tcp
+        bind "fd@${lismtp}"
+        log ${S1_addr}:${S1_port} daemon
+        log-format "Result=%[var(txn.result)] Bytes=%B"
+        tcp-request content use-service lua.mailservice
 
     frontend fe1
         mode http
         bind "fd@${fe1}"
-        default_backend b1
+        default_backend be1
 
-        http-response lua.bug
-
-    backend b1
+    backend be1
         mode http
-        option httpchk /svr_healthcheck
+        log ${S1_addr}:${S1_port} daemon
+        option httpchk
         option log-health-checks
 
+        default-server inter 200ms downinter 100ms rise 1 fall 1
+
         email-alert mailers mymailers
         email-alert level info
         email-alert from from@domain.tld
         email-alert to to@domain.tld
 
-        server broken 127.0.0.1:65535 check
-        server srv_lua ${h1_luahttpservice_addr}:${h1_luahttpservice_port} check inter 500
-        server srv1 ${s1_addr}:${s1_port} check inter 500
+        server srv1 ${h1_lisrv_addr}:${h1_lisrv_port} check
 
     mailers mymailers
-#      timeout mail 20s
-#      timeout mail 200ms
-      mailer smtp1 ${h1_femail_addr}:${h1_femail_port}
+        mailer smtp1 ${h1_lismtp_addr}:${h1_lismtp_port}
 
 } -start
 
-# configure port for lua to call feluaservice
-client c1 -connect ${h1_luahttpservice_sock} {
-    timeout 2
-    txreq -url "/setport" -hdr "vtcport1: ${h1_femail_port}"
-    rxresp
-    expect resp.status == 200
-    expect resp.body == "OK"
-} -run
-
-delay 2
-server s2 -repeat 5 -start
-delay 5
-
-client c2 -connect ${h1_luahttpservice_sock} {
-    timeout 2
-    txreq -url "/checkMailCounters"
-    rxresp
-    expect resp.status == 200
-    expect resp.body == "MailCounters"
-    expect resp.http.mailsreceived == 16
-    expect resp.http.mailconnectionsmade == 16
-} -run
+syslog S1 -wait