diff --git a/lib/resty/healthcheck.lua b/lib/resty/healthcheck.lua index 8b3bd47b..53b38588 100644 --- a/lib/resty/healthcheck.lua +++ b/lib/resty/healthcheck.lua @@ -24,6 +24,7 @@ -- @author Hisham Muhammad, Thijs Schreijer -- @license Apache 2.0 +local ngx = ngx local ERR = ngx.ERR local WARN = ngx.WARN local DEBUG = ngx.DEBUG @@ -38,6 +39,17 @@ local resty_lock = require ("resty.lock") local re_find = ngx.re.find local bit = require("bit") local ngx_now = ngx.now +local error = error +local print = print +local require = require +local string = string +local assert = assert +local tonumber = tonumber +local type = type +local math = math +local next = next +local pairs = pairs +local table = table -- constants local EVENT_SOURCE_PREFIX = "lua-resty-healthcheck" @@ -503,8 +515,8 @@ local function incr_counter(self, health_report, ip, port, hostname, limit, ctr_ local ctr = ctr_get(multictr, ctr_type) self:log(WARN, health_report, " ", COUNTER_NAMES[ctr_type], - " increment (", ctr, "/", limit, ") for '", hostname or "", - "(", ip, ":", port, ")'") + " increment (", ctr, "/", limit, ") for '", hostname or "", + "(", ip, ":", port, ")'") local new_multictr if ctr_type == CTR_SUCCESS then @@ -1011,7 +1023,7 @@ end -- Log a message specific to this checker -- @param level standard ngx log level constant function checker:log(level, ...) - ngx_log(level, worker_color(self.LOG_PREFIX), ...) + return ngx_log(level, worker_color(self.LOG_PREFIX), ...) end diff --git a/lib/resty/healthcheck/utils.lua b/lib/resty/healthcheck/utils.lua index b95731e7..8faa6c80 100644 --- a/lib/resty/healthcheck/utils.lua +++ b/lib/resty/healthcheck/utils.lua @@ -6,6 +6,9 @@ -- @author Hisham Muhammad, Thijs Schreijer -- @license Apache 2.0 +local assert = assert +local type = type + local timer_at = ngx.timer.at local _M = {} diff --git a/t/00-new.t b/t/00-new.t index d8d11c6b..a3945b72 100644 --- a/t/00-new.t +++ b/t/00-new.t @@ -34,6 +34,8 @@ GET /t --- error_log please configure + + === TEST 2: new() requires 'name' --- http_config eval: $::HttpConfig --- config @@ -55,6 +57,8 @@ GET /t --- error_log required option 'name' is missing + + === TEST 3: new() requires 'shm_name' --- http_config eval: $::HttpConfig --- config @@ -76,6 +80,8 @@ GET /t --- error_log required option 'shm_name' is missing + + === TEST 4: new() fails with invalid shm --- http_config eval: $::HttpConfig --- config @@ -98,6 +104,8 @@ GET /t --- error_log no shm found by name + + === TEST 5: new() initializes with default config --- http_config eval: $::HttpConfig --- config @@ -119,6 +127,8 @@ GET /t --- error_log Healthchecker started! + + === TEST 6: new() only accepts http or tcp types --- http_config eval: $::HttpConfig --- config @@ -154,6 +164,8 @@ true true false + + === TEST 7: new() deals with bad inputs --- http_config eval: $::HttpConfig --- config diff --git a/t/01-start-stop.t b/t/01-start-stop.t index a3e189e3..e460562c 100644 --- a/t/01-start-stop.t +++ b/t/01-start-stop.t @@ -54,6 +54,8 @@ true --- no_error_log [error] + + === TEST 2: start() cannot start a second time using active health checks --- http_config eval: $::HttpConfig --- config @@ -87,6 +89,8 @@ cannot start, 2 (of 2) timers are still running --- no_error_log [error] + + === TEST 3: start() is a no-op if active intervals are 0 --- http_config eval: $::HttpConfig --- config @@ -128,6 +132,8 @@ true --- no_error_log [error] + + === TEST 4: stop() stops health checks --- http_config eval: $::HttpConfig --- config @@ -167,6 +173,8 @@ true [error] checking + + === TEST 5: start() restarts health checks --- http_config eval: $::HttpConfig --- config diff --git a/t/04-report_success.t b/t/04-report_success.t index 91b36a10..628ad2d4 100644 --- a/t/04-report_success.t +++ b/t/04-report_success.t @@ -17,8 +17,6 @@ run_tests(); __DATA__ - - === TEST 1: report_success() recovers HTTP active + passive --- http_config eval qq{ @@ -96,6 +94,7 @@ healthy SUCCESS increment (3/3) for '(127.0.0.1:2116)' event: target status '(127.0.0.1:2118)' from 'false' to 'true' + === TEST 2: report_success() recovers TCP active = passive --- http_config eval qq{ @@ -172,6 +171,8 @@ healthy SUCCESS increment (2/3) for '(127.0.0.1:2116)' healthy SUCCESS increment (3/3) for '(127.0.0.1:2116)' event: target status '(127.0.0.1:2118)' from 'false' to 'true' + + === TEST 3: report_success() is a nop when active.healthy.sucesses == 0 --- http_config eval qq{ diff --git a/t/05-report_failure.t b/t/05-report_failure.t index 9b95c395..714e7248 100644 --- a/t/05-report_failure.t +++ b/t/05-report_failure.t @@ -17,8 +17,6 @@ run_tests(); __DATA__ - - === TEST 1: report_failure() fails HTTP active + passive --- http_config eval qq{ @@ -96,6 +94,7 @@ unhealthy HTTP increment (3/3) for '(127.0.0.1:2113)' event: target status '(127.0.0.1:2113)' from 'true' to 'false' + === TEST 2: report_failure() fails TCP active + passive --- http_config eval qq{ @@ -171,6 +170,7 @@ unhealthy TCP increment (2/2) for '(127.0.0.1:2113)' event: target status '(127.0.0.1:2113)' from 'true' to 'false' + === TEST 3: report_failure() is a nop when failure counters == 0 --- http_config eval qq{ diff --git a/t/06-report_http_status.t b/t/06-report_http_status.t index f70bc0a0..013d70dc 100644 --- a/t/06-report_http_status.t +++ b/t/06-report_http_status.t @@ -17,8 +17,6 @@ run_tests(); __DATA__ - - === TEST 1: report_http_status() failures active + passive --- http_config eval qq{ @@ -178,6 +176,7 @@ healthy SUCCESS increment (4/4) for '(127.0.0.1:2113)' event: target status '(127.0.0.1:2113)' from 'false' to 'true' + === TEST 3: report_http_status() with success is a nop when passive.healthy.successes == 0 --- http_config eval qq{ @@ -245,6 +244,7 @@ healthy SUCCESS increment event: target status '127.0.0.1 (127.0.0.1:2119)' from 'false' to 'true' + === TEST 4: report_http_status() with success is a nop when active.healthy.successes == 0 --- http_config eval qq{ @@ -312,6 +312,7 @@ healthy SUCCESS increment event: target status '127.0.0.1 (127.0.0.1:2119)' from 'false' to 'true' + === TEST 5: report_http_status() with failure is a nop when passive.unhealthy.http_failures == 0 --- http_config eval qq{ @@ -379,7 +380,8 @@ unhealthy HTTP increment event: target status '127.0.0.1 (127.0.0.1:2119)' from 'true' to 'false' -=== TEST 4: report_http_status() with success is a nop when active.unhealthy.http_failures == 0 + +=== TEST 6: report_http_status() with success is a nop when active.unhealthy.http_failures == 0 --- http_config eval qq{ $::HttpConfig diff --git a/t/07-report_tcp_failure.t b/t/07-report_tcp_failure.t index f0d8b6b4..5baa983f 100644 --- a/t/07-report_tcp_failure.t +++ b/t/07-report_tcp_failure.t @@ -17,8 +17,6 @@ run_tests(); __DATA__ - - === TEST 1: report_tcp_failure() active + passive --- http_config eval qq{ @@ -96,6 +94,7 @@ unhealthy TCP increment (3/3) for '(127.0.0.1:2113)' event: target status '(127.0.0.1:2113)' from 'true' to 'false' + === TEST 2: report_tcp_failure() for active is a nop when active.unhealthy.tcp_failures == 0 --- http_config eval qq{ diff --git a/t/08-report_timeout.t b/t/08-report_timeout.t index 7f4ef911..b8658e4f 100644 --- a/t/08-report_timeout.t +++ b/t/08-report_timeout.t @@ -17,8 +17,6 @@ run_tests(); __DATA__ - - === TEST 1: report_timeout() active + passive --- http_config eval qq{ @@ -94,6 +92,7 @@ unhealthy TIMEOUT increment (2/2) for '(127.0.0.1:2113)' event: target status '(127.0.0.1:2113)' from 'true' to 'false' + === TEST 2: report_timeout() for active is a nop when active.unhealthy.timeouts == 0 --- http_config eval qq{ diff --git a/t/09-active_probes.t b/t/09-active_probes.t index fa1bdce8..9f1e83f0 100644 --- a/t/09-active_probes.t +++ b/t/09-active_probes.t @@ -17,8 +17,6 @@ run_tests(); __DATA__ - - === TEST 1: active probes, http node failing --- http_config eval qq{ @@ -127,6 +125,8 @@ healthy SUCCESS increment (3/3) for '(127.0.0.1:2114)' event: target status '(127.0.0.1:2114)' from 'false' to 'true' checking unhealthy targets: nothing to do + + === TEST 3: active probes, custom http status (regression test for pre-filled defaults) --- http_config eval qq{ @@ -183,6 +183,7 @@ unhealthy HTTP increment (3/3) for '(127.0.0.1:2114)' event: target status '(127.0.0.1:2114)' from 'true' to 'false' + === TEST 4: active probes, custom http status, node failing --- http_config eval qq{ @@ -295,6 +296,7 @@ event: target status 'example.com(127.0.0.1:2114)' from 'false' to 'true' checking unhealthy targets: nothing to do + === TEST 6: active probes, tcp node failing --- http_config eval qq{ @@ -395,4 +397,3 @@ healthy SUCCESS increment (2/3) for '(127.0.0.1:2114)' healthy SUCCESS increment (3/3) for '(127.0.0.1:2114)' event: target status '(127.0.0.1:2114)' from 'false' to 'true' checking unhealthy targets: nothing to do - diff --git a/t/10-garbagecollect.t b/t/10-garbagecollect.t index e38ec55a..210cfcac 100644 --- a/t/10-garbagecollect.t +++ b/t/10-garbagecollect.t @@ -17,8 +17,6 @@ run_tests(); __DATA__ - - === TEST 1: garbage collect the checker object --- http_config eval qq{ diff --git a/t/11-clear.t b/t/11-clear.t index 70f1407f..3473f9b3 100644 --- a/t/11-clear.t +++ b/t/11-clear.t @@ -72,6 +72,8 @@ initial target list (9 targets) initial target list (10 targets) initial target list (11 targets) + + === TEST 2: clear() clears the list, other checkers get notified and clear too --- http_config eval: $::HttpConfig --- config @@ -115,6 +117,8 @@ checking unhealthy targets: nothing to do --- no_error_log checking unhealthy targets: #10 + + === TEST 3: clear() resets counters --- http_config eval qq{ diff --git a/t/12-set_target_status.t b/t/12-set_target_status.t index ff69adc0..fe8aa10a 100644 --- a/t/12-set_target_status.t +++ b/t/12-set_target_status.t @@ -47,6 +47,9 @@ GET /t true false true + + + === TEST 2: set_target_status() restores node after passive check disables it --- http_config eval qq{ @@ -86,6 +89,9 @@ GET /t true false true + + + === TEST 3: set_target_status() resets the failure counters --- http_config eval qq{ @@ -129,7 +135,10 @@ GET /t true true false -=== TEST 3: set_target_status() resets the success counters + + + +=== TEST 4: set_target_status() resets the success counters --- http_config eval qq{ $::HttpConfig diff --git a/t/13-integration.t b/t/13-integration.t index bc3549f3..43b08b1c 100644 --- a/t/13-integration.t +++ b/t/13-integration.t @@ -17,8 +17,6 @@ run_tests(); __DATA__ - - === TEST 1: ensure counters work properly --- http_config eval qq{ diff --git a/t/14-tls_active_probes.t b/t/14-tls_active_probes.t index 51854928..e5957a29 100644 --- a/t/14-tls_active_probes.t +++ b/t/14-tls_active_probes.t @@ -6,24 +6,28 @@ workers(1); plan tests => blocks() * 2; my $pwd = cwd(); +my $ca_certs = '/etc/ssl/certs/ca-certificates.crt'; +if (!-e $ca_certs) { + # for centos or redhat + $ca_certs = '/etc/ssl/certs/ca-bundle.trust.crt'; +} our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;"; lua_shared_dict test_shm 8m; lua_shared_dict my_worker_events 8m; + + lua_ssl_trusted_certificate $ca_certs; }; run_tests(); __DATA__ - - === TEST 1: active probes, valid https --- http_config eval: $::HttpConfig --- config location = /t { - lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt; lua_ssl_verify_depth 2; content_by_lua_block { local we = require "resty.worker.events" @@ -59,11 +63,12 @@ true --- timeout 15 + + === TEST 2: active probes, invalid cert --- http_config eval: $::HttpConfig --- config location = /t { - lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt; lua_ssl_verify_depth 2; content_by_lua_block { local we = require "resty.worker.events" @@ -99,11 +104,12 @@ false --- timeout 15 + + === TEST 3: active probes, accept invalid cert when disabling check --- http_config eval: $::HttpConfig --- config location = /t { - lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt; lua_ssl_verify_depth 2; content_by_lua_block { local we = require "resty.worker.events"