From a20719957396e11abba921a248b021fde6c1098e Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Fri, 12 Jul 2019 13:20:44 +0800 Subject: [PATCH 1/5] chore(reindex) reformat the test suite. --- t/00-new.t | 12 +++++ t/01-start-stop.t | 8 ++++ t/04-report_success.t | 5 ++- t/05-report_failure.t | 4 +- t/06-report_http_status.t | 8 ++-- t/07-report_tcp_failure.t | 3 +- t/08-report_timeout.t | 3 +- t/09-active_probes.t | 7 +-- t/10-garbagecollect.t | 2 - t/11-clear.t | 4 ++ t/12-set_target_status.t | 11 ++++- t/13-integration.t | 2 - t/14-tls_active_probes.t | 6 ++- t/util/reindex | 92 +++++++++++++++++++++++++++------------ 14 files changed, 119 insertions(+), 48 deletions(-) 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..ebeb1946 100644 --- a/t/14-tls_active_probes.t +++ b/t/14-tls_active_probes.t @@ -17,8 +17,6 @@ run_tests(); __DATA__ - - === TEST 1: active probes, valid https --- http_config eval: $::HttpConfig --- config @@ -59,6 +57,8 @@ true --- timeout 15 + + === TEST 2: active probes, invalid cert --- http_config eval: $::HttpConfig --- config @@ -99,6 +99,8 @@ false --- timeout 15 + + === TEST 3: active probes, accept invalid cert when disabling check --- http_config eval: $::HttpConfig --- config diff --git a/t/util/reindex b/t/util/reindex index 77ae5484..d0a09276 100755 --- a/t/util/reindex +++ b/t/util/reindex @@ -1,27 +1,65 @@ -#!/usr/bin/env lua - -if not arg[1] then - io.stderr:write("Usage: "..arg[0].." t/*.t\n") - os.exit(1) -end - -for _, name in ipairs(arg) do - local i = 1 - local fd = io.open(name, "r") - if fd then - local new = name.."~" - local out = io.open(new, "w") - for line in fd:lines() do - local test, n, desc = line:match("^(===%s*TEST%s*)(%d+)(.*)$") - if test then - out:write(test .. tostring(i) .. desc .. "\n") - i = i + 1 - else - out:write(line .. "\n") - end - end - out:close() - fd:close() - os.execute("mv " .. new .. " " .. name) - end -end +#!/usr/bin/env perl + +#: reindexes .t files for Test::Base based test files +#: Copyright (c) Yichun Zhang + +use strict; +use warnings; + +#use File::Copy; +use Getopt::Std; + +my %opts; +getopts('hb:', \%opts); +if ($opts{h} or ! @ARGV) { + die "Usage: reindex [-b 0] t/*.t\n"; +} + +my $init = $opts{b}; +$init = 1 if not defined $init; + +my @files = map glob, @ARGV; +for my $file (@files) { + next if -d $file or $file !~ /\.t_?$/; + reindex($file); +} + +sub reindex { + my $file = $_[0]; + open my $in, $file or + die "Can't open $file for reading: $!"; + my @lines; + my $counter = $init; + my $changed; + while (<$in>) { + s/\r$//; + my $num; + s/ ^ === \s+ TEST \s+ (\d+)/$num=$1; "=== TEST " . $counter++/xie; + next if !defined $num; + if ($num != $counter-1) { + $changed++; + } + } continue { + push @lines, $_; + } + close $in; + + my $text = join '', @lines; + $text =~ s/\n\n+\z/\n/sm; + $text =~ s/(?x) \n+ === \s+ TEST/\n\n\n\n=== TEST/ixsg; + $text =~ s/__(DATA|END)__\n+=== TEST/__${1}__\n\n=== TEST/; + #$text =~ s/\n+$/\n\n/s; + if (! $changed and $text eq join '', @lines) { + warn "reindex: $file:\tskipped.\n"; + return; + } + #File::Copy::copy( $file, "$file.bak" ); + open my $out, "> $file" or + die "Can't open $file for writing: $!"; + binmode $out; + print $out $text; + close $out; + + warn "reindex: $file:\tdone.\n"; +} + From 09d94e9235dc16fdb1ce92850939c29d22cdacba Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Fri, 12 Jul 2019 15:44:51 +0800 Subject: [PATCH 2/5] test(active_probes) use ca-bundle.trust.crt if fails to find ca-certificates.crt. --- t/14-tls_active_probes.t | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/t/14-tls_active_probes.t b/t/14-tls_active_probes.t index ebeb1946..8dddc94c 100644 --- a/t/14-tls_active_probes.t +++ b/t/14-tls_active_probes.t @@ -6,11 +6,18 @@ 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(); @@ -21,7 +28,6 @@ __DATA__ --- 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" @@ -63,7 +69,6 @@ true --- 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" @@ -105,7 +110,6 @@ false --- 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" From d130d7a9d6f494a806de4037d0dd2c8930c3c167 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Fri, 12 Jul 2019 21:01:06 +0800 Subject: [PATCH 3/5] optimize(healthchecks): cache local variables. --- lib/resty/healthcheck.lua | 18 +++++++++++++++--- lib/resty/healthcheck/utils.lua | 3 +++ 2 files changed, 18 insertions(+), 3 deletions(-) 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 = {} From 272d4ba03bf494e6b4ba821a09078b2497324995 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Fri, 12 Jul 2019 21:07:47 +0800 Subject: [PATCH 4/5] test(tls_active_probes): removed useless double quotes. --- t/14-tls_active_probes.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/14-tls_active_probes.t b/t/14-tls_active_probes.t index 8dddc94c..e5957a29 100644 --- a/t/14-tls_active_probes.t +++ b/t/14-tls_active_probes.t @@ -17,7 +17,7 @@ our $HttpConfig = qq{ lua_shared_dict test_shm 8m; lua_shared_dict my_worker_events 8m; - lua_ssl_trusted_certificate "$ca_certs"; + lua_ssl_trusted_certificate $ca_certs; }; run_tests(); From 94a53db6cf3a1039e0be42c1f07fb98874b8a317 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Wed, 17 Jul 2019 08:04:04 +0800 Subject: [PATCH 5/5] change(utils): used original `reindex` version. --- t/util/reindex | 92 +++++++++++++++----------------------------------- 1 file changed, 27 insertions(+), 65 deletions(-) diff --git a/t/util/reindex b/t/util/reindex index d0a09276..77ae5484 100755 --- a/t/util/reindex +++ b/t/util/reindex @@ -1,65 +1,27 @@ -#!/usr/bin/env perl - -#: reindexes .t files for Test::Base based test files -#: Copyright (c) Yichun Zhang - -use strict; -use warnings; - -#use File::Copy; -use Getopt::Std; - -my %opts; -getopts('hb:', \%opts); -if ($opts{h} or ! @ARGV) { - die "Usage: reindex [-b 0] t/*.t\n"; -} - -my $init = $opts{b}; -$init = 1 if not defined $init; - -my @files = map glob, @ARGV; -for my $file (@files) { - next if -d $file or $file !~ /\.t_?$/; - reindex($file); -} - -sub reindex { - my $file = $_[0]; - open my $in, $file or - die "Can't open $file for reading: $!"; - my @lines; - my $counter = $init; - my $changed; - while (<$in>) { - s/\r$//; - my $num; - s/ ^ === \s+ TEST \s+ (\d+)/$num=$1; "=== TEST " . $counter++/xie; - next if !defined $num; - if ($num != $counter-1) { - $changed++; - } - } continue { - push @lines, $_; - } - close $in; - - my $text = join '', @lines; - $text =~ s/\n\n+\z/\n/sm; - $text =~ s/(?x) \n+ === \s+ TEST/\n\n\n\n=== TEST/ixsg; - $text =~ s/__(DATA|END)__\n+=== TEST/__${1}__\n\n=== TEST/; - #$text =~ s/\n+$/\n\n/s; - if (! $changed and $text eq join '', @lines) { - warn "reindex: $file:\tskipped.\n"; - return; - } - #File::Copy::copy( $file, "$file.bak" ); - open my $out, "> $file" or - die "Can't open $file for writing: $!"; - binmode $out; - print $out $text; - close $out; - - warn "reindex: $file:\tdone.\n"; -} - +#!/usr/bin/env lua + +if not arg[1] then + io.stderr:write("Usage: "..arg[0].." t/*.t\n") + os.exit(1) +end + +for _, name in ipairs(arg) do + local i = 1 + local fd = io.open(name, "r") + if fd then + local new = name.."~" + local out = io.open(new, "w") + for line in fd:lines() do + local test, n, desc = line:match("^(===%s*TEST%s*)(%d+)(.*)$") + if test then + out:write(test .. tostring(i) .. desc .. "\n") + i = i + 1 + else + out:write(line .. "\n") + end + end + out:close() + fd:close() + os.execute("mv " .. new .. " " .. name) + end +end