forked from apache/apisix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(deployment): implement health check for conf_server (apache#7378)
Signed-off-by: spacewander <spacewanderlzx@gmail.com>
- Loading branch information
1 parent
6157037
commit a8a692d
Showing
3 changed files
with
283 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
use t::APISIX; | ||
|
||
my $nginx_binary = $ENV{'TEST_NGINX_BINARY'} || 'nginx'; | ||
my $version = eval { `$nginx_binary -V 2>&1` }; | ||
|
||
if ($version =~ m/\/1.17.8/) { | ||
plan(skip_all => "require OpenResty 1.19+"); | ||
} else { | ||
plan('no_plan'); | ||
} | ||
|
||
add_block_preprocessor(sub { | ||
my ($block) = @_; | ||
|
||
if (!$block->request) { | ||
$block->set_value("request", "GET /t"); | ||
} | ||
|
||
if ((!defined $block->error_log) && (!defined $block->no_error_log)) { | ||
$block->set_value("no_error_log", "[error]"); | ||
} | ||
|
||
}); | ||
|
||
Test::Nginx::Socket::set_http_config_filter(sub { | ||
my $config = shift; | ||
my $snippet = `./t/bin/gen_snippet.lua conf_server`; | ||
$config .= $snippet; | ||
return $config; | ||
}); | ||
|
||
run_tests(); | ||
|
||
__DATA__ | ||
=== TEST 1: health check, ensure unhealthy endpoint is skipped | ||
--- http_config | ||
server { | ||
listen 12345; | ||
location / { | ||
access_by_lua_block { | ||
if package.loaded.start_to_fail then | ||
ngx.exit(502) | ||
end | ||
} | ||
proxy_pass http://127.0.0.1:2379; | ||
} | ||
} | ||
--- extra_yaml_config | ||
deployment: | ||
role: traditional | ||
role_traditional: | ||
config_provider: etcd | ||
etcd: | ||
prefix: "/apisix" | ||
host: | ||
- http://127.0.0.1:2379 | ||
- http://localhost:12345 | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local etcd = require("apisix.core.etcd") | ||
package.loaded.start_to_fail = true | ||
for i = 1, 7 do | ||
assert(etcd.set("/apisix/test", "foo")) | ||
end | ||
package.loaded.start_to_fail = nil | ||
ngx.say('OK') | ||
} | ||
} | ||
--- response_body | ||
OK | ||
--- error_log | ||
report failure, endpoint: localhost:12345 | ||
endpoint localhost:12345 is unhealthy, skipped | ||
=== TEST 2: health check, all endpoints are unhealthy | ||
--- http_config | ||
server { | ||
listen 12345; | ||
location / { | ||
access_by_lua_block { | ||
if package.loaded.start_to_fail then | ||
ngx.exit(502) | ||
end | ||
} | ||
proxy_pass http://127.0.0.1:2379; | ||
} | ||
} | ||
--- extra_yaml_config | ||
deployment: | ||
role: traditional | ||
role_traditional: | ||
config_provider: etcd | ||
etcd: | ||
prefix: "/apisix" | ||
host: | ||
- http://localhost:12345 | ||
- http://127.0.0.1:12345 | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local etcd = require("apisix.core.etcd") | ||
package.loaded.start_to_fail = true | ||
for i = 1, 6 do | ||
etcd.set("/apisix/test", "foo") | ||
end | ||
package.loaded.start_to_fail = nil | ||
local _, err = etcd.set("/apisix/test", "foo") | ||
ngx.say(err) | ||
} | ||
} | ||
--- response_body | ||
invalid response code: 503 | ||
--- error_log | ||
endpoint localhost:12345 is unhealthy, skipped | ||
endpoint 127.0.0.1:12345 is unhealthy, skipped | ||
=== TEST 3: health check, all endpoints recover from unhealthy | ||
--- http_config | ||
server { | ||
listen 12345; | ||
location / { | ||
access_by_lua_block { | ||
if package.loaded.start_to_fail then | ||
ngx.exit(502) | ||
end | ||
} | ||
proxy_pass http://127.0.0.1:2379; | ||
} | ||
} | ||
--- extra_yaml_config | ||
deployment: | ||
role: traditional | ||
role_traditional: | ||
config_provider: etcd | ||
etcd: | ||
health_check_timeout: 1 | ||
prefix: "/apisix" | ||
host: | ||
- http://localhost:12345 | ||
- http://127.0.0.1:12345 | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local etcd = require("apisix.core.etcd") | ||
package.loaded.start_to_fail = true | ||
for i = 1, 6 do | ||
etcd.set("/apisix/test", "foo") | ||
end | ||
package.loaded.start_to_fail = nil | ||
ngx.sleep(1.2) | ||
local res, err = etcd.set("/apisix/test", "foo") | ||
ngx.say(err or res.body.node.value) | ||
} | ||
} | ||
--- response_body | ||
foo | ||
--- error_log | ||
endpoint localhost:12345 is unhealthy, skipped | ||
endpoint 127.0.0.1:12345 is unhealthy, skipped |