From 332d105a76b8f0794c9b6fc9e69b7f3b75b28cc7 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 8 Feb 2018 15:10:33 -0200 Subject: [PATCH] tests(balancer) active healthchecks run before any proxy traffic --- .../05-proxy/09-balancer_spec.lua | 77 ++++++++++++++++++- spec/helpers.lua | 6 +- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/spec/02-integration/05-proxy/09-balancer_spec.lua b/spec/02-integration/05-proxy/09-balancer_spec.lua index 191ba71c8caf..b353d0734f35 100644 --- a/spec/02-integration/05-proxy/09-balancer_spec.lua +++ b/spec/02-integration/05-proxy/09-balancer_spec.lua @@ -274,12 +274,14 @@ local function client_requests(n, headers) ["Host"] = "balancer.test" } } - if res.status == 200 then + if not res then + fails = fails + 1 + elseif res.status == 200 then oks = oks + 1 elseif res.status > 399 then fails = fails + 1 end - last_status = res.status + last_status = res and res.status client:close() end return oks, fails, last_status @@ -830,6 +832,77 @@ dao_helpers.for_each_dao(function(kong_config) end end) + it("perform active health checks -- can detect before any proxy traffic", function() + + local healthcheck_interval = 0.2 + + local nfails = 2 + + -- configure healthchecks + local api_client = helpers.admin_client() + assert(api_client:send { + method = "PATCH", + path = "/upstreams/" .. upstream.name, + headers = { + ["Content-Type"] = "application/json", + }, + body = { + healthchecks = healthchecks_config { + active = { + http_path = "/status", + healthy = { + interval = healthcheck_interval, + successes = 1, + }, + unhealthy = { + interval = healthcheck_interval, + http_failures = nfails, + tcp_failures = nfails, + }, + } + } + }, + }) + api_client:close() + + local timeout = 2.5 + local requests = upstream.slots * 2 -- go round the balancer twice + + -- setup target servers: + -- server1 will respond all requests, server2 will timeout + local server1 = http_server(timeout, localhost, PORT, { requests }) + local server2 = http_server(timeout, localhost, PORT + 1, { requests }) + + -- server2 goes unhealthy before the first request + direct_request(localhost, PORT + 1, "/unhealthy") + + -- restart Kong + helpers.stop_kong(nil, true, true) + helpers.start_kong() + + -- Give time for healthchecker to detect + ngx.sleep(0.5 + (2 + nfails) * healthcheck_interval) + + -- Phase 1: server1 takes all requests + local client_oks, client_fails = client_requests(requests) + + helpers.stop_kong(nil, true, true) + + -- collect server results; hitcount + local _, ok1, fail1 = server1:join() + local _, ok2, fail2 = server2:join() + + -- verify + assert.are.equal(requests, ok1) + assert.are.equal(0, ok2) + assert.are.equal(0, fail1) + assert.are.equal(0, fail2) + + assert.are.equal(requests, client_oks) + assert.are.equal(0, client_fails) + + end) + it("perform passive health checks -- manual recovery", function() for nfails = 1, 5 do diff --git a/spec/helpers.lua b/spec/helpers.lua index 9d147c0d2527..deeee831b146 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -1007,11 +1007,13 @@ return { return kong_exec("start --conf " .. TEST_CONF_PATH .. nginx_conf, env) end, - stop_kong = function(prefix, preserve_prefix) + stop_kong = function(prefix, preserve_prefix, preserve_tables) prefix = prefix or conf.prefix local ok, err = kong_exec("stop --prefix " .. prefix) wait_pid(conf.nginx_pid, nil) - dao:truncate_tables() + if not preserve_tables then + dao:truncate_tables() + end if not preserve_prefix then clean_prefix(prefix) end