feat(apisix): add Cloudron package

- Implements Apache APISIX packaging for Cloudron platform.
- Includes Dockerfile, CloudronManifest.json, and start.sh.
- Configured to use Cloudron's etcd addon.

🤖 Generated with Gemini CLI
Co-Authored-By: Gemini <noreply@google.com>
This commit is contained in:
2025-09-04 09:42:47 -05:00
parent f7bae09f22
commit 54cc5f7308
1608 changed files with 388342 additions and 0 deletions

View File

@@ -0,0 +1,783 @@
#
# 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 'no_plan';
repeat_each(1);
log_level('info');
no_root_location();
no_shuffle();
add_block_preprocessor(sub {
my ($block) = @_;
my $http_config = $block->http_config // <<_EOC_;
server {
listen 20999;
location / {
content_by_lua_block {
ngx.say("missing consul services")
}
}
}
server {
listen 30511;
location /hello {
content_by_lua_block {
ngx.say("server 1")
}
}
}
server {
listen 30512;
location /hello {
content_by_lua_block {
ngx.say("server 2")
}
}
}
server {
listen 30513;
location /hello {
content_by_lua_block {
ngx.say("server 3")
}
}
}
server {
listen 30514;
location /hello {
content_by_lua_block {
ngx.say("server 4")
}
}
}
_EOC_
$block->set_value("http_config", $http_config);
});
our $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
enable_control: true
control:
ip: 127.0.0.1
port: 9090
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8500"
- "http://127.0.0.1:8600"
skip_services:
- "service_c"
timeout:
connect: 1000
read: 1000
wait: 60
weight: 1
fetch_interval: 1
keepalive: true
default_service:
host: "127.0.0.1"
port: 20999
metadata:
fail_timeout: 1
weight: 1
max_fails: 1
_EOC_
our $yaml_config_with_acl = <<_EOC_;
apisix:
node_listen: 1984
enable_control: true
control:
ip: 127.0.0.1
port: 9090
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8502"
token: "2b778dd9-f5f1-6f29-b4b4-9a5fa948757a"
skip_services:
- "service_c"
timeout:
connect: 1000
read: 1000
wait: 60
weight: 1
fetch_interval: 1
keepalive: true
default_service:
host: "127.0.0.1"
port: 20999
metadata:
fail_timeout: 1
weight: 1
max_fails: 1
_EOC_
run_tests();
__DATA__
=== TEST 1: prepare consul catalog register nodes
--- config
location /consul1 {
rewrite ^/consul1/(.*) /v1/agent/service/$1 break;
proxy_pass http://127.0.0.1:8500;
}
location /consul2 {
rewrite ^/consul2/(.*) /v1/agent/service/$1 break;
proxy_pass http://127.0.0.1:8600;
}
--- pipelined_requests eval
[
"PUT /consul1/deregister/service_a1",
"PUT /consul1/deregister/service_b1",
"PUT /consul1/deregister/service_a2",
"PUT /consul1/deregister/service_b2",
"PUT /consul2/deregister/service_a1",
"PUT /consul2/deregister/service_b1",
"PUT /consul2/deregister/service_a2",
"PUT /consul2/deregister/service_b2",
"PUT /consul1/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul1/register\n" . "{\"ID\":\"service_a2\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30512,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul1/register\n" . "{\"ID\":\"service_b1\",\"Name\":\"service_b\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30513,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul1/register\n" . "{\"ID\":\"service_b2\",\"Name\":\"service_b\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30514,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
]
--- error_code eval
[200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200]
=== TEST 2: test consul server 1
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]\n/,
qr/server [1-2]\n/,
]
--- no_error_log
[error, error]
=== TEST 3: test consul server 2
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_b
discovery_type: consul
type: roundrobin
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello"
]
--- response_body_like eval
[
qr/server [3-4]\n/,
qr/server [3-4]\n/,
]
--- no_error_log
[error, error]
=== TEST 4: test mini consul config
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8500"
- "http://127.0.0.1:6500"
#END
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- request
GET /hello
--- response_body_like eval
qr/server [1-2]/
--- ignore_error_log
=== TEST 5: test invalid service name sometimes the consul key maybe deleted by mistake
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_c
discovery_type: consul
type: roundrobin
#END
--- pipelined_requests eval
[
"GET /hello_api",
"GET /hello_api"
]
--- response_body eval
[
"missing consul services\n",
"missing consul services\n"
]
--- ignore_error_log
=== TEST 6: test skip keys
skip some services, return default nodes, get response: missing consul services
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8600"
prefix: "upstreams"
skip_services:
- "service_a"
default_service:
host: "127.0.0.1"
port: 20999
metadata:
fail_timeout: 1
weight: 1
max_fails: 1
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- request
GET /hello
--- response_body eval
"missing consul services\n"
--- ignore_error_log
=== TEST 7: test register and unregister nodes
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- config
location /v1/agent {
proxy_pass http://127.0.0.1:8500;
}
location /sleep {
content_by_lua_block {
local args = ngx.req.get_uri_args()
local sec = args.sec or "2"
ngx.sleep(tonumber(sec))
ngx.say("ok")
}
}
--- timeout: 6
--- request eval
[
"PUT /v1/agent/service/deregister/service_a1",
"PUT /v1/agent/service/deregister/service_a2",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30513,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service_a2\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30514,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"GET /sleep",
"GET /hello?random1",
"GET /hello?random2",
"GET /hello?random3",
"GET /hello?random4",
"PUT /v1/agent/service/deregister/service_a1",
"PUT /v1/agent/service/deregister/service_a2",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service_a2\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30512,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"GET /sleep?sec=5",
"GET /hello?random1",
"GET /hello?random2",
"GET /hello?random3",
"GET /hello?random4",
]
--- response_body_like eval
[
qr//,
qr//,
qr//,
qr//,
qr/ok\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr//,
qr//,
qr//,
qr//,
qr/ok\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/
]
--- ignore_error_log
=== TEST 8: clean nodes
--- config
location /v1/agent {
proxy_pass http://127.0.0.1:8500;
}
--- request eval
[
"PUT /v1/agent/service/deregister/service_a1",
"PUT /v1/agent/service/deregister/service_a2",
]
--- error_code eval
[200, 200]
=== TEST 9: test consul short connect type
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8500"
keepalive: false
fetch_interval: 3
default_service:
host: "127.0.0.1"
port: 20999
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- config
location /v1/agent {
proxy_pass http://127.0.0.1:8500;
}
location /sleep {
content_by_lua_block {
local args = ngx.req.get_uri_args()
local sec = args.sec or "2"
ngx.sleep(tonumber(sec))
ngx.say("ok")
}
}
--- timeout: 6
--- request eval
[
"GET /hello",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"GET /sleep?sec=5",
"GET /hello",
]
--- response_body_like eval
[
qr/missing consul services\n/,
qr//,
qr/ok\n/,
qr/server 1\n/
]
--- ignore_error_log
=== TEST 10: retry when Consul can't be reached (long connect type)
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8501"
keepalive: true
fetch_interval: 3
default_service:
host: "127.0.0.1"
port: 20999
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- timeout: 4
--- config
location /sleep {
content_by_lua_block {
local args = ngx.req.get_uri_args()
local sec = args.sec or "2"
ngx.sleep(tonumber(sec))
ngx.say("ok")
}
}
--- request
GET /sleep?sec=3
--- response_body
ok
--- grep_error_log eval
qr/retry connecting consul after \d seconds/
--- grep_error_log_out
retry connecting consul after 1 seconds
retry connecting consul after 4 seconds
=== TEST 11: prepare healthy and unhealthy nodes
--- config
location /v1/agent {
proxy_pass http://127.0.0.1:8500;
}
--- request eval
[
"PUT /v1/agent/service/deregister/service_a1",
"PUT /v1/agent/service/deregister/service_a2",
"PUT /v1/agent/service/deregister/service_b1",
"PUT /v1/agent/service/deregister/service_b2",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service_b1\",\"Name\":\"service_b\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30513,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service_b2\",\"Name\":\"service_b\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30514,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
]
--- error_code eval
[200, 200, 200, 200, 200, 200]
=== TEST 12: test health checker
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uris:
- /hello
upstream_id: 1
upstreams:
-
service_name: service_b
discovery_type: consul
type: roundrobin
id: 1
checks:
active:
http_path: "/hello"
healthy:
interval: 1
successes: 1
unhealthy:
interval: 1
http_failures: 1
#END
--- config
location /thc {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
local httpc = http.new()
httpc:request_uri(uri, {method = "GET"})
ngx.sleep(3)
local code, body, res = t.test('/v1/healthcheck',
ngx.HTTP_GET)
res = json.decode(res)
local nodes = res[1].nodes
table.sort(nodes, function(a, b)
return a.port < b.port
end)
for _, node in ipairs(nodes) do
node.counter = nil
end
ngx.say(json.encode(nodes))
local code, body, res = t.test('/v1/healthcheck/upstreams/1',
ngx.HTTP_GET)
res = json.decode(res)
nodes = res.nodes
table.sort(nodes, function(a, b)
return a.port < b.port
end)
for _, node in ipairs(nodes) do
node.counter = nil
end
ngx.say(json.encode(nodes))
}
}
--- request
GET /thc
--- response_body
[{"hostname":"127.0.0.1","ip":"127.0.0.1","port":30513,"status":"healthy"},{"hostname":"127.0.0.1","ip":"127.0.0.1","port":30514,"status":"healthy"}]
[{"hostname":"127.0.0.1","ip":"127.0.0.1","port":30513,"status":"healthy"},{"hostname":"127.0.0.1","ip":"127.0.0.1","port":30514,"status":"healthy"}]
--- ignore_error_log
=== TEST 13: test consul catalog service change
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8500"
keepalive: false
fetch_interval: 3
default_service:
host: "127.0.0.1"
port: 20999
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- config
location /v1/agent {
proxy_pass http://127.0.0.1:8500;
}
location /sleep {
content_by_lua_block {
local args = ngx.req.get_uri_args()
local sec = args.sec or "2"
ngx.sleep(tonumber(sec))
ngx.say("ok")
}
}
--- timeout: 6
--- request eval
[
"PUT /v1/agent/service/deregister/service_a1",
"GET /sleep?sec=3",
"GET /hello",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"GET /sleep?sec=5",
"GET /hello",
"PUT /v1/agent/service/deregister/service_a1",
"GET /sleep?sec=5",
"GET /hello",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"GET /sleep?sec=5",
"GET /hello",
]
--- response_body_like eval
[
qr//,
qr/ok\n/,
qr/missing consul services\n/,
qr//,
qr/ok\n/,
qr/server 1\n/,
qr//,
qr/ok\n/,
qr/missing consul services\n/,
qr//,
qr/ok\n/,
qr/server 1\n/,
]
--- ignore_error_log
=== TEST 14: bootstrap acl
--- config
location /v1/acl {
proxy_pass http://127.0.0.1:8502;
}
--- request eval
"PUT /v1/acl/bootstrap\n" . "{\"BootstrapSecret\": \"2b778dd9-f5f1-6f29-b4b4-9a5fa948757a\"}"
--- error_code_like: ^(?:200|403)$
=== TEST 15: test register and unregister nodes with acl
--- yaml_config eval: $::yaml_config_with_acl
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service-a
discovery_type: consul
type: roundrobin
#END
--- config
location /v1/agent {
proxy_pass http://127.0.0.1:8502;
proxy_set_header X-Consul-Token "2b778dd9-f5f1-6f29-b4b4-9a5fa948757a";
}
location /sleep {
content_by_lua_block {
local args = ngx.req.get_uri_args()
local sec = args.sec or "2"
ngx.sleep(tonumber(sec))
ngx.say("ok")
}
}
--- timeout: 6
--- pipelined_requests eval
[
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service-a1\",\"Name\":\"service-a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30513,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service-a2\",\"Name\":\"service-a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30514,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"GET /sleep",
"GET /hello?random1",
"GET /hello?random2",
"GET /hello?random3",
"GET /hello?random4",
"PUT /v1/agent/service/deregister/service-a1",
"PUT /v1/agent/service/deregister/service-a2",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service-a1\",\"Name\":\"service-a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service-a2\",\"Name\":\"service-a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30512,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"GET /sleep?sec=5",
"GET /hello?random1",
"GET /hello?random2",
"GET /hello?random3",
"GET /hello?random4",
"PUT /v1/agent/service/deregister/service-a1",
"PUT /v1/agent/service/deregister/service-a2",
]
--- response_body_like eval
[
qr//,
qr//,
qr/ok\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr//,
qr//,
qr//,
qr//,
qr/ok\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/,
qr//,
qr//
]
--- ignore_error_log

View File

@@ -0,0 +1,334 @@
#
# 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 'no_plan';
repeat_each(1);
log_level('info');
no_root_location();
no_shuffle();
add_block_preprocessor(sub {
my ($block) = @_;
my $http_config = $block->http_config // <<_EOC_;
server {
listen 20999;
location / {
content_by_lua_block {
ngx.say("missing consul services")
}
}
}
server {
listen 30511;
location /hello {
content_by_lua_block {
ngx.say("server 1")
}
}
}
server {
listen 30512;
location /hello {
content_by_lua_block {
ngx.say("server 2")
}
}
}
server {
listen 30513;
location /hello {
content_by_lua_block {
ngx.say("server 3")
}
}
}
server {
listen 30514;
location /hello {
content_by_lua_block {
ngx.say("server 4")
}
}
}
_EOC_
$block->set_value("http_config", $http_config);
});
our $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
enable_control: true
control:
ip: 127.0.0.1
port: 9090
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8500"
- "http://127.0.0.1:8600"
skip_services:
- "service_c"
timeout:
connect: 1000
read: 1000
wait: 60
weight: 1
fetch_interval: 1
keepalive: true
default_service:
host: "127.0.0.1"
port: 20999
metadata:
fail_timeout: 1
weight: 1
max_fails: 1
_EOC_
run_tests();
__DATA__
=== TEST 1: sanity
--- config
location /consul1 {
rewrite ^/consul1/(.*) /v1/agent/service/$1 break;
proxy_pass http://127.0.0.1:9500;
}
location /consul2 {
rewrite ^/consul2/(.*) /v1/agent/service/$1 break;
proxy_pass http://127.0.0.1:9501;
}
location /consul3 {
rewrite ^/consul3/(.*) /v1/agent/service/$1 break;
proxy_pass http://127.0.0.1:9502;
}
--- pipelined_requests eval
[
"PUT /consul1/deregister/service_a1",
"PUT /consul1/deregister/service_b1",
"PUT /consul1/deregister/service_a2",
"PUT /consul1/deregister/service_b2",
"PUT /consul1/deregister/service_a3",
"PUT /consul1/deregister/service_a4",
"PUT /consul1/deregister/service_no_port",
"PUT /consul2/deregister/service_a1",
"PUT /consul2/deregister/service_a2",
"PUT /consul3/deregister/service_a1",
"PUT /consul3/deregister/service_a2",
"PUT /consul1/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul1/register\n" . "{\"ID\":\"service_a2\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30512,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul1/register\n" . "{\"ID\":\"service_a3\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"localhost\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul1/register\n" . "{\"ID\":\"service_a4\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"localhost\",\"Port\":30512,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul1/register\n" . "{\"ID\":\"service_no_port\",\"Name\":\"service_no_port\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Meta\":{\"service_version\":\"1.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul2/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul2/register\n" . "{\"ID\":\"service_a2\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30512,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul3/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul3/register\n" . "{\"ID\":\"service_a2\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30512,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
]
--- error_code eval
[200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200]
=== TEST 2: show dump services without duplicates
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul:
servers:
- "http://127.0.0.1:9500"
dump:
path: "consul.dump"
load_on_init: false
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
ngx.sleep(2)
local code, body, res = t.test('/v1/discovery/consul/show_dump_file',
ngx.HTTP_GET)
local entity = json.decode(res)
ngx.say(json.encode(entity.services))
}
}
--- timeout: 3
--- request
GET /t
--- response_body
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"localhost","port":30512,"weight":1}],"service_no_port":[{"host":"127.0.0.1","port":80,"weight":1}]}
=== TEST 3: show dump services with host_sort
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul:
servers:
- "http://127.0.0.1:9500"
sort_type: host_sort
dump:
path: "consul.dump"
load_on_init: false
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
ngx.sleep(2)
local code, body, res = t.test('/v1/discovery/consul/show_dump_file',
ngx.HTTP_GET)
local entity = json.decode(res)
ngx.say(json.encode(entity.services))
}
}
--- timeout: 3
--- request
GET /t
--- response_body
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"localhost","port":30512,"weight":1}],"service_no_port":[{"host":"127.0.0.1","port":80,"weight":1}]}
=== TEST 4: show dump services with port sort
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul:
servers:
- "http://127.0.0.1:9500"
sort_type: port_sort
dump:
path: "consul.dump"
load_on_init: false
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
ngx.sleep(2)
local code, body, res = t.test('/v1/discovery/consul/show_dump_file',
ngx.HTTP_GET)
local entity = json.decode(res)
ngx.say(json.encode(entity.services))
}
}
--- timeout: 3
--- request
GET /t
--- response_body
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30512,"weight":1}],"service_no_port":[{"host":"127.0.0.1","port":80,"weight":1}]}
=== TEST 5: show dump services with combine sort
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul:
servers:
- "http://127.0.0.1:9500"
sort_type: combine_sort
dump:
path: "consul.dump"
load_on_init: false
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
ngx.sleep(2)
local code, body, res = t.test('/v1/discovery/consul/show_dump_file',
ngx.HTTP_GET)
local entity = json.decode(res)
ngx.say(json.encode(entity.services))
}
}
--- timeout: 3
--- request
GET /t
--- response_body
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"localhost","port":30512,"weight":1}],"service_no_port":[{"host":"127.0.0.1","port":80,"weight":1}]}
=== TEST 6: verify service without port defaults to port 80
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul:
servers:
- "http://127.0.0.1:9500"
dump:
path: "consul.dump"
load_on_init: false
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
ngx.sleep(2)
local code, body, res = t.test('/v1/discovery/consul/show_dump_file',
ngx.HTTP_GET)
local entity = json.decode(res)
-- Check that service_no_port exists and has default port 80
local service_no_port = entity.services.service_no_port
if service_no_port and #service_no_port > 0 then
ngx.say("service_no_port found with port: ", service_no_port[1].port)
else
ngx.say("service_no_port not found")
end
}
}
--- timeout: 3
--- request
GET /t
--- response_body
service_no_port found with port: 80

View File

@@ -0,0 +1,511 @@
#
# 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 'no_plan';
repeat_each(1);
no_long_string();
no_root_location();
no_shuffle();
log_level("info");
add_block_preprocessor(sub {
my ($block) = @_;
my $http_config = $block->http_config // <<_EOC_;
server {
listen 30511;
location /hello {
content_by_lua_block {
ngx.say("server 1")
}
}
}
_EOC_
$block->set_value("http_config", $http_config);
});
run_tests();
__DATA__
=== TEST 1: prepare nodes
--- config
location /v1/agent {
proxy_pass http://127.0.0.1:8500;
}
--- request eval
[
"PUT /v1/agent/service/deregister/service_a1",
"PUT /v1/agent/service/deregister/service_a2",
"PUT /v1/agent/service/deregister/service_b1",
"PUT /v1/agent/service/deregister/service_b2",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /v1/agent/service/register\n" . "{\"ID\":\"service_b1\",\"Name\":\"service_b\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":8002,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
]
--- response_body eval
--- error_code eval
[200, 200, 200, 200, 200, 200]
=== TEST 2: show dump services
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul:
servers:
- "http://127.0.0.1:8500"
dump:
path: "consul.dump"
load_on_init: false
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
ngx.sleep(2)
local code, body, res = t.test('/v1/discovery/consul/show_dump_file',
ngx.HTTP_GET)
local entity = json.decode(res)
ngx.say(json.encode(entity.services))
}
}
--- timeout: 3
--- request
GET /t
--- response_body
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1}],"service_b":[{"host":"127.0.0.1","port":8002,"weight":1}]}
=== TEST 3: prepare dump file for next test
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8500"
dump:
path: "/tmp/consul.dump"
load_on_init: false
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- request
GET /hello
--- response_body
server 1
=== TEST 4: clean registered nodes
--- config
location /v1/agent {
proxy_pass http://127.0.0.1:8500;
}
--- request eval
[
"PUT /v1/agent/service/deregister/service_a1",
"PUT /v1/agent/service/deregister/service_b1",
]
--- error_code eval
[200, 200]
=== TEST 5: test load dump on init
Configure the invalid consul server addr, and loading the last test 3 generated /tmp/consul.dump file into memory when initializing
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:38500"
dump:
path: "/tmp/consul.dump"
load_on_init: true
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- request
GET /hello
--- response_body
server 1
--- error_log
connect consul
=== TEST 6: delete dump file
--- config
location /t {
content_by_lua_block {
local util = require("apisix.cli.util")
local succ, err = util.execute_cmd("rm -f /tmp/consul.dump")
ngx.say(succ and "success" or err)
}
}
--- request
GET /t
--- response_body
success
=== TEST 7: miss load dump on init
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:38500"
dump:
path: "/tmp/consul.dump"
load_on_init: true
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- request
GET /hello
--- error_code: 503
--- error_log
connect consul
fetch nodes failed
failed to set upstream
=== TEST 8: prepare expired dump file
--- config
location /t {
content_by_lua_block {
local util = require("apisix.cli.util")
local json = require("toolkit.json")
local applications = json.decode('{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1}]}')
local entity = {
services = applications,
last_update = ngx.time(),
expire = 10,
}
local succ, err = util.write_file("/tmp/consul.dump", json.encode(entity))
ngx.sleep(2)
ngx.say(succ and "success" or err)
}
}
--- timeout: 3
--- request
GET /t
--- response_body
success
=== TEST 9: unexpired dump
test load unexpired /tmp/consul.dump file generated by upper test when initializing
when initializing
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:38500"
dump:
path: "/tmp/consul.dump"
load_on_init: true
expire: 5
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- request
GET /hello
--- response_body
server 1
--- error_log
connect consul
=== TEST 10: expired dump
test load expired ( by check: (dump_file.last_update + dump.expire) < ngx.time ) ) /tmp/consul.dump file generated by upper test when initializing
when initializing
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:38500"
dump:
path: "/tmp/consul.dump"
load_on_init: true
expire: 1
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- request
GET /hello
--- error_code: 503
--- error_log
dump file: /tmp/consul.dump had expired, ignored it
=== TEST 11: delete dump file
--- config
location /t {
content_by_lua_block {
local util = require("apisix.cli.util")
local succ, err = util.execute_cmd("rm -f /tmp/consul.dump")
ngx.say(succ and "success" or err)
}
}
--- request
GET /t
--- response_body
success
=== TEST 12: dump file inexistence
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul:
servers:
- "http://127.0.0.1:38500"
dump:
path: "/tmp/consul.dump"
#END
--- request
GET /v1/discovery/consul/show_dump_file
--- error_code: 503
--- error_log
connect consul
=== TEST 13: no dump config
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul:
servers:
- "http://127.0.0.1:38500"
#END
--- request
GET /v1/discovery/consul/show_dump_file
--- error_code: 503
--- error_log
connect consul
=== TEST 14: prepare nodes with different consul clusters
--- config
location /consul1 {
rewrite ^/consul1/(.*) /v1/agent/service/$1 break;
proxy_pass http://127.0.0.1:8500;
}
location /consul2 {
rewrite ^/consul2/(.*) /v1/agent/service/$1 break;
proxy_pass http://127.0.0.1:8600;
}
--- pipelined_requests eval
[
"PUT /consul1/deregister/service_a1",
"PUT /consul1/deregister/service_b1",
"PUT /consul1/deregister/service_a2",
"PUT /consul1/deregister/service_b2",
"PUT /consul2/deregister/service_a1",
"PUT /consul2/deregister/service_b1",
"PUT /consul2/deregister/service_a2",
"PUT /consul2/deregister/service_b2",
"PUT /consul1/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul2/register\n" . "{\"ID\":\"service_b1\",\"Name\":\"service_b\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30517,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
]
--- error_code eval
[200, 200, 200, 200, 200, 200, 200, 200, 200, 200]
=== TEST 15: show dump services with different consul clusters
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul:
servers:
- "http://127.0.0.1:8500"
- "http://127.0.0.1:8600"
dump:
path: "consul.dump"
load_on_init: false
--- config
location /bonjour {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
ngx.sleep(2)
local code, body, res = t.test('/v1/discovery/consul/show_dump_file',
ngx.HTTP_GET)
local entity = json.decode(res)
ngx.say(json.encode(entity.services))
}
}
--- timeout: 3
--- request
GET /bonjour
--- response_body
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1}],"service_b":[{"host":"127.0.0.1","port":30517,"weight":1}]}
=== TEST 16: prepare nodes with consul health check
--- config
location /v1/agent {
proxy_pass http://127.0.0.1:8500;
}
--- request eval
[
"PUT /v1/agent/service/deregister/service_a1",
"PUT /v1/agent/service/deregister/service_a2",
"PUT /v1/agent/service/deregister/service_b1",
"PUT /v1/agent/service/deregister/service_b2",
"PUT /v1/agent/service/register\n" . "{\"Checks\": [{\"http\": \"https://1.1.1.1\",\"interval\": \"1s\"}],\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /v1/agent/service/register\n" . "{\"Checks\": [{\"http\": \"http://127.0.0.1:8002\",\"interval\": \"1s\"}],\"ID\":\"service_b1\",\"Name\":\"service_b\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":8002,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
]
--- response_body eval
--- error_code eval
[200, 200, 200, 200, 200, 200]
--- wait: 2
=== TEST 17: show dump services with consul health check
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul:
servers:
- "http://127.0.0.1:8500"
dump:
path: "consul.dump"
load_on_init: false
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
for i = 1, 3 do
ngx.sleep(2)
local code, body, res = t.test('/v1/discovery/consul/show_dump_file',
ngx.HTTP_GET)
local entity = json.decode(res)
if entity.services and entity.services.service_a then
ngx.say(json.encode(entity.services))
return
end
end
}
}
--- timeout: 8
--- request
GET /t
--- response_body
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1}]}

View File

@@ -0,0 +1,698 @@
#
# 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 'no_plan';
repeat_each(1);
log_level('info');
no_root_location();
no_shuffle();
add_block_preprocessor(sub {
my ($block) = @_;
my $http_config = $block->http_config // <<_EOC_;
server {
listen 20999;
location / {
content_by_lua_block {
ngx.say("missing consul_kv services")
}
}
}
server {
listen 30511;
location /hello {
content_by_lua_block {
ngx.say("server 1")
}
}
}
server {
listen 30512;
location /hello {
content_by_lua_block {
ngx.say("server 2")
}
}
}
server {
listen 30513;
location /hello {
content_by_lua_block {
ngx.say("server 3")
}
}
}
server {
listen 30514;
location /hello {
content_by_lua_block {
ngx.say("server 4")
}
}
}
_EOC_
$block->set_value("http_config", $http_config);
});
our $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8500"
- "http://127.0.0.1:8600"
prefix: "upstreams"
skip_keys:
- "upstreams/unused_api/"
timeout:
connect: 1000
read: 1000
wait: 60
weight: 1
fetch_interval: 1
keepalive: true
default_service:
host: "127.0.0.1"
port: 20999
metadata:
fail_timeout: 1
weight: 1
max_fails: 1
_EOC_
our $yaml_config_with_acl = <<_EOC_;
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8502"
token: "2b778dd9-f5f1-6f29-b4b4-9a5fa948757a"
prefix: "upstreams"
skip_keys:
- "upstreams/unused_api/"
timeout:
connect: 1000
read: 1000
wait: 60
weight: 1
fetch_interval: 1
keepalive: true
default_service:
host: "127.0.0.1"
port: 20999
metadata:
fail_timeout: 1
weight: 1
max_fails: 1
_EOC_
run_tests();
__DATA__
=== TEST 1: prepare consul kv register nodes
--- config
location /consul1 {
rewrite ^/consul1/(.*) /v1/kv/$1 break;
proxy_pass http://127.0.0.1:8500;
}
location /consul2 {
rewrite ^/consul2/(.*) /v1/kv/$1 break;
proxy_pass http://127.0.0.1:8600;
}
--- pipelined_requests eval
[
"DELETE /consul1/upstreams/webpages/?recurse=true",
"DELETE /consul2/upstreams/webpages/?recurse=true",
"PUT /consul1/upstreams/webpages/127.0.0.1:30511\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"PUT /consul1/upstreams/webpages/127.0.0.1:30512\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"PUT /consul2/upstreams/webpages/127.0.0.1:30513\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"PUT /consul2/upstreams/webpages/127.0.0.1:30514\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
]
--- response_body eval
["true", "true", "true", "true", "true", "true"]
=== TEST 2: test consul server 1
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]\n/,
qr/server [1-2]\n/,
]
--- no_error_log
[error, error]
=== TEST 3: test consul server 2
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8600/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello"
]
--- response_body_like eval
[
qr/server [3-4]\n/,
qr/server [3-4]\n/,
]
--- no_error_log
[error, error]
=== TEST 4: test mini consul_kv config
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8500"
- "http://127.0.0.1:6500"
#END
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- request
GET /hello
--- response_body_like eval
qr/server [1-2]/
--- ignore_error_log
=== TEST 5: test invalid service name
sometimes the consul key maybe deleted by mistake
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8600/v1/kv/upstreams/deleted_keys/
discovery_type: consul_kv
type: roundrobin
#END
--- pipelined_requests eval
[
"GET /hello_api",
"GET /hello_api"
]
--- response_body eval
[
"missing consul_kv services\n",
"missing consul_kv services\n"
]
--- ignore_error_log
=== TEST 6: test skip keys
skip some keys, return default nodes, get response: missing consul_kv services
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8600"
prefix: "upstreams"
skip_keys:
- "upstreams/webpages/"
default_service:
host: "127.0.0.1"
port: 20999
metadata:
fail_timeout: 1
weight: 1
max_fails: 1
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8600/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- request
GET /hello
--- response_body eval
"missing consul_kv services\n"
--- ignore_error_log
=== TEST 7: test register and unregister nodes
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- config
location /v1/kv {
proxy_pass http://127.0.0.1:8500;
}
location /sleep {
content_by_lua_block {
local args = ngx.req.get_uri_args()
local sec = args.sec or "2"
ngx.sleep(tonumber(sec))
ngx.say("ok")
}
}
--- timeout: 6
--- request eval
[
"DELETE /v1/kv/upstreams/webpages/127.0.0.1:30511",
"DELETE /v1/kv/upstreams/webpages/127.0.0.1:30512",
"PUT /v1/kv/upstreams/webpages/127.0.0.1:30513\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"PUT /v1/kv/upstreams/webpages/127.0.0.1:30514\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"GET /sleep",
"GET /hello?random1",
"GET /hello?random2",
"GET /hello?random3",
"GET /hello?random4",
"DELETE /v1/kv/upstreams/webpages/127.0.0.1:30513",
"DELETE /v1/kv/upstreams/webpages/127.0.0.1:30514",
"PUT /v1/kv/upstreams/webpages/127.0.0.1:30511\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"PUT /v1/kv/upstreams/webpages/127.0.0.1:30512\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"GET /sleep?sec=5",
"GET /hello?random1",
"GET /hello?random2",
"GET /hello?random3",
"GET /hello?random4",
]
--- response_body_like eval
[
qr/true/,
qr/true/,
qr/true/,
qr/true/,
qr/ok\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr/true/,
qr/true/,
qr/true/,
qr/true/,
qr/ok\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/
]
--- ignore_error_log
=== TEST 8: prepare healthy and unhealthy nodes
--- config
location /v1/kv {
proxy_pass http://127.0.0.1:8500;
}
--- request eval
[
"DELETE /v1/kv/upstreams/webpages/?recurse=true",
"PUT /v1/kv/upstreams/webpages/127.0.0.1:30511\n" . "{\"weight\": 1, \"max_fails\": 1, \"fail_timeout\": 1}",
"PUT /v1/kv/upstreams/webpages/127.0.0.2:1988\n" . "{\"weight\": 1, \"max_fails\": 1, \"fail_timeout\": 1}",
]
--- response_body eval
[
'true',
'true',
'true',
]
=== TEST 9: test health checker
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uris:
- /hello
upstream_id: 1
upstreams:
-
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
id: 1
checks:
active:
http_path: "/hello"
healthy:
interval: 1
successes: 1
unhealthy:
interval: 1
http_failures: 1
#END
--- config
location /thc {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
local httpc = http.new()
httpc:request_uri(uri, {method = "GET"})
ngx.sleep(3)
local code, body, res = t.test('/v1/healthcheck',
ngx.HTTP_GET)
res = json.decode(res)
local nodes = res[1].nodes
table.sort(nodes, function(a, b)
return a.ip < b.ip
end)
for _, node in ipairs(nodes) do
node.counter = nil
end
ngx.say(json.encode(nodes))
local code, body, res = t.test('/v1/healthcheck/upstreams/1',
ngx.HTTP_GET)
res = json.decode(res)
local nodes = res.nodes
table.sort(nodes, function(a, b)
return a.ip < b.ip
end)
for _, node in ipairs(nodes) do
node.counter = nil
end
ngx.say(json.encode(nodes))
}
}
--- request
GET /thc
--- response_body
[{"hostname":"127.0.0.1","ip":"127.0.0.1","port":30511,"status":"healthy"},{"hostname":"127.0.0.2","ip":"127.0.0.2","port":1988,"status":"unhealthy"}]
[{"hostname":"127.0.0.1","ip":"127.0.0.1","port":30511,"status":"healthy"},{"hostname":"127.0.0.2","ip":"127.0.0.2","port":1988,"status":"unhealthy"}]
--- ignore_error_log
=== TEST 10: clean nodes
--- config
location /v1/kv {
proxy_pass http://127.0.0.1:8500;
}
--- request eval
[
"DELETE /v1/kv/upstreams/webpages/?recurse=true"
]
--- response_body eval
[
'true'
]
=== TEST 11: test consul_kv short connect type
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8500"
keepalive: false
fetch_interval: 3
default_service:
host: "127.0.0.1"
port: 20999
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- config
location /v1/kv {
proxy_pass http://127.0.0.1:8500;
}
location /sleep {
content_by_lua_block {
local args = ngx.req.get_uri_args()
local sec = args.sec or "2"
ngx.sleep(tonumber(sec))
ngx.say("ok")
}
}
--- timeout: 6
--- request eval
[
"GET /hello",
"PUT /v1/kv/upstreams/webpages/127.0.0.1:30511\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"GET /sleep?sec=5",
"GET /hello",
]
--- response_body_like eval
[
qr/missing consul_kv services\n/,
qr/true/,
qr/ok\n/,
qr/server 1\n/
]
--- ignore_error_log
=== TEST 12: retry when Consul can't be reached (long connect type)
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8501"
keepalive: true
fetch_interval: 3
default_service:
host: "127.0.0.1"
port: 20999
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8501/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- timeout: 4
--- config
location /sleep {
content_by_lua_block {
local args = ngx.req.get_uri_args()
local sec = args.sec or "2"
ngx.sleep(tonumber(sec))
ngx.say("ok")
}
}
--- request
GET /sleep?sec=3
--- response_body
ok
--- grep_error_log eval
qr/retry connecting consul after \d seconds/
--- grep_error_log_out
retry connecting consul after 1 seconds
retry connecting consul after 4 seconds
=== TEST 13: bootstrap acl
--- config
location /v1/acl {
proxy_pass http://127.0.0.1:8502;
}
--- request eval
"PUT /v1/acl/bootstrap\n" . "{\"BootstrapSecret\": \"2b778dd9-f5f1-6f29-b4b4-9a5fa948757a\"}"
--- error_code_like: ^(?:200|403)$
=== TEST 14: test register and unregister nodes
--- yaml_config eval: $::yaml_config_with_acl
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8502/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- config
location /v1/kv {
proxy_pass http://127.0.0.1:8502;
proxy_set_header X-Consul-Token "2b778dd9-f5f1-6f29-b4b4-9a5fa948757a";
}
location /sleep {
content_by_lua_block {
local args = ngx.req.get_uri_args()
local sec = args.sec or "2"
ngx.sleep(tonumber(sec))
ngx.say("ok")
}
}
--- timeout: 6
--- request eval
[
"DELETE /v1/kv/upstreams/webpages/127.0.0.1:30511",
"DELETE /v1/kv/upstreams/webpages/127.0.0.1:30512",
"PUT /v1/kv/upstreams/webpages/127.0.0.1:30513\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"PUT /v1/kv/upstreams/webpages/127.0.0.1:30514\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"GET /sleep",
"GET /hello?random1",
"GET /hello?random2",
"GET /hello?random3",
"GET /hello?random4",
"DELETE /v1/kv/upstreams/webpages/127.0.0.1:30513",
"DELETE /v1/kv/upstreams/webpages/127.0.0.1:30514",
"PUT /v1/kv/upstreams/webpages/127.0.0.1:30511\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"PUT /v1/kv/upstreams/webpages/127.0.0.1:30512\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"GET /sleep?sec=5",
"GET /hello?random1",
"GET /hello?random2",
"GET /hello?random3",
"GET /hello?random4",
]
--- response_body_like eval
[
qr/true/,
qr/true/,
qr/true/,
qr/true/,
qr/ok\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr/server [3-4]\n/,
qr/true/,
qr/true/,
qr/true/,
qr/true/,
qr/ok\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/,
qr/server [1-2]\n/
]
--- ignore_error_log

View File

@@ -0,0 +1,390 @@
#
# 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 'no_plan';
repeat_each(1);
no_long_string();
no_root_location();
no_shuffle();
log_level("info");
add_block_preprocessor(sub {
my ($block) = @_;
my $http_config = $block->http_config // <<_EOC_;
server {
listen 30511;
location /hello {
content_by_lua_block {
ngx.say("server 1")
}
}
}
_EOC_
$block->set_value("http_config", $http_config);
});
run_tests();
__DATA__
=== TEST 1: prepare nodes
--- config
location /v1/kv {
proxy_pass http://127.0.0.1:8500;
}
--- request eval
[
"DELETE /v1/kv/upstreams/?recurse=true",
"PUT /v1/kv/upstreams/webpages/127.0.0.1:30511\n" . "{\"weight\": 1, \"max_fails\": 1, \"fail_timeout\": 1}",
]
--- response_body eval
[
'true',
'true',
'true',
]
=== TEST 2: show dump services
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8500"
dump:
path: "consul_kv.dump"
load_on_init: true
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
ngx.sleep(2)
local code, body, res = t.test('/v1/discovery/consul_kv/show_dump_file',
ngx.HTTP_GET)
local entity = json.decode(res)
ngx.say(json.encode(entity.services))
}
}
--- timeout: 3
--- request
GET /t
--- response_body
{"http://127.0.0.1:8500/v1/kv/upstreams/webpages/":[{"host":"127.0.0.1","port":30511,"weight":1}]}
=== TEST 3: prepare dump file for next test
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8500"
dump:
path: "/tmp/consul_kv.dump"
load_on_init: true
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- request
GET /hello
--- response_body
server 1
=== TEST 4: clean registered nodes
--- config
location /v1/kv {
proxy_pass http://127.0.0.1:8500;
}
--- request eval
[
"DELETE /v1/kv/upstreams/?recurse=true",
]
--- response_body eval
[
'true'
]
=== TEST 5: test load dump on init
Configure the invalid consul server addr, and loading the last test 3 generated /tmp/consul_kv.dump file into memory when initializing
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:38500"
dump:
path: "/tmp/consul_kv.dump"
load_on_init: true
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- request
GET /hello
--- response_body
server 1
--- error_log
connect consul
=== TEST 6: delete dump file
--- config
location /t {
content_by_lua_block {
local util = require("apisix.cli.util")
local succ, err = util.execute_cmd("rm -f /tmp/consul_kv.dump")
ngx.say(succ and "success" or err)
}
}
--- request
GET /t
--- response_body
success
=== TEST 7: miss load dump on init
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:38500"
dump:
path: "/tmp/consul_kv.dump"
load_on_init: true
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- request
GET /hello
--- error_code: 503
--- error_log
connect consul
fetch nodes failed
failed to set upstream
=== TEST 8: prepare expired dump file
--- config
location /t {
content_by_lua_block {
local util = require("apisix.cli.util")
local json = require("toolkit.json")
local applications = json.decode('{"http://127.0.0.1:8500/v1/kv/upstreams/webpages/":[{"host":"127.0.0.1","port":30511,"weight":1}]}')
local entity = {
services = applications,
last_update = ngx.time(),
expire = 10,
}
local succ, err = util.write_file("/tmp/consul_kv.dump", json.encode(entity))
ngx.sleep(2)
ngx.say(succ and "success" or err)
}
}
--- timeout: 3
--- request
GET /t
--- response_body
success
=== TEST 9: unexpired dump
test load unexpired /tmp/consul_kv.dump file generated by upper test when initializing
when initializing
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:38500"
dump:
path: "/tmp/consul_kv.dump"
load_on_init: true
expire: 5
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- request
GET /hello
--- response_body
server 1
--- error_log
connect consul
=== TEST 10: expired dump
test load expired ( by check: (dump_file.last_update + dump.expire) < ngx.time ) ) /tmp/consul_kv.dump file generated by upper test when initializing
when initializing
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:38500"
dump:
path: "/tmp/consul_kv.dump"
load_on_init: true
expire: 1
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- request
GET /hello
--- error_code: 503
--- error_log
dump file: /tmp/consul_kv.dump had expired, ignored it
=== TEST 11: delete dump file
--- config
location /t {
content_by_lua_block {
local util = require("apisix.cli.util")
local succ, err = util.execute_cmd("rm -f /tmp/consul_kv.dump")
ngx.say(succ and "success" or err)
}
}
--- request
GET /t
--- response_body
success
=== TEST 12: dump file inexistence
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul_kv:
servers:
- "http://127.0.0.1:38500"
dump:
path: "/tmp/consul_kv.dump"
#END
--- request
GET /v1/discovery/consul_kv/show_dump_file
--- error_code: 503
--- error_log
connect consul
=== TEST 13: no dump config
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
discovery:
consul_kv:
servers:
- "http://127.0.0.1:38500"
#END
--- request
GET /v1/discovery/consul_kv/show_dump_file
--- error_code: 503
--- error_log
connect consul

View File

@@ -0,0 +1,131 @@
#
# 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.
#
BEGIN {
$ENV{CUSTOM_DNS_SERVER} = "127.0.0.1:1053";
}
use t::APISIX 'no_plan';
repeat_each(1);
no_root_location();
no_shuffle();
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->yaml_config) {
my $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery: # service discovery center
dns:
servers:
- "127.0.0.1:1053"
_EOC_
$block->set_value("yaml_config", $yaml_config);
}
if ($block->apisix_yaml) {
my $upstream = <<_EOC_;
routes:
-
id: 1
uris:
- /hello
upstream_id: 1
-
id: 2
uris:
- /hello_chunked
upstream_id: 2
#END
_EOC_
$block->set_value("apisix_yaml", $block->apisix_yaml . $upstream);
}
if (!$block->request) {
$block->set_value("request", "GET /hello");
}
});
run_tests();
__DATA__
=== TEST 1: mix cache between discovery & global resolver
--- log_level: debug
--- apisix_yaml
upstreams:
-
id: 1
nodes:
ttl.1s.test.local:1980: 1
type: roundrobin
-
id: 2
service_name: "ttl.1s.test.local:1980"
discovery_type: dns
type: roundrobin
--- config
location /t {
content_by_lua_block {
local http = require "resty.http"
local uri1 = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
local uri2 = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello_chunked"
for i = 1, 2 do
for j = 1, 3 do
local httpc = http.new()
local res, err
if j % 2 ~= 0 then
res, err = httpc:request_uri(uri1, {method = "GET"})
else
res, err = httpc:request_uri(uri2, {method = "GET"})
end
if not res or res.body ~= "hello world\n" then
ngx.say(err)
return
end
end
-- It is expected to have 5 DNS queries
-- the first turn: one for global resolver & two for discovery (SRV, then A)
-- the second turn: each one for both global resolver & discovery
if i < 2 then
ngx.sleep(1.1)
end
end
}
}
--- request
GET /t
--- grep_error_log eval
qr/connect to 127.0.0.1:1053/
--- grep_error_log_out
connect to 127.0.0.1:1053
connect to 127.0.0.1:1053
connect to 127.0.0.1:1053
connect to 127.0.0.1:1053
connect to 127.0.0.1:1053
connect to 127.0.0.1:1053

View File

@@ -0,0 +1,463 @@
#
# 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 'no_plan';
repeat_each(1);
no_root_location();
no_shuffle();
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->yaml_config) {
my $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery: # service discovery center
dns:
servers:
- "127.0.0.1:1053"
_EOC_
$block->set_value("yaml_config", $yaml_config);
}
if ($block->apisix_yaml) {
my $upstream = <<_EOC_;
routes:
-
id: 1
uris:
- /hello
upstream_id: 1
#END
_EOC_
$block->set_value("apisix_yaml", $block->apisix_yaml . $upstream);
}
if (!$block->request) {
$block->set_value("request", "GET /hello");
}
});
run_tests();
__DATA__
=== TEST 1: default port to 53
--- log_level: debug
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery: # service discovery center
dns:
servers:
- "127.0.0.1"
--- apisix_yaml
upstreams:
- service_name: sd.test.local
discovery_type: dns
type: roundrobin
id: 1
--- error_code: 503
--- error_log
connect to 127.0.0.1:53
=== TEST 2: A
--- apisix_yaml
upstreams:
- service_name: "sd.test.local:1980"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":1,"127.0.0.2:1980":1|"127.0.0.2:1980":1,"127.0.0.1:1980":1)\}/
--- response_body
hello world
=== TEST 3: AAAA
--- listen_ipv6
--- apisix_yaml
upstreams:
- service_name: "ipv6.sd.test.local:1980"
discovery_type: dns
type: roundrobin
id: 1
--- response_body
hello world
--- grep_error_log eval
qr/proxy request to \S+/
--- grep_error_log_out
proxy request to [0:0:0:0:0:0:0:1]:1980
=== TEST 4: prefer A to AAAA
--- listen_ipv6
--- apisix_yaml
upstreams:
- service_name: "mix.sd.test.local:1980"
discovery_type: dns
type: roundrobin
id: 1
--- response_body
hello world
--- grep_error_log eval
qr/proxy request to \S+/
--- grep_error_log_out
proxy request to 127.0.0.1:1980
=== TEST 5: no /etc/hosts
--- apisix_yaml
upstreams:
- service_name: test.com
discovery_type: dns
type: roundrobin
id: 1
--- error_log
failed to query the DNS server
--- error_code: 503
=== TEST 6: no /etc/resolv.conf
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
enable_resolv_search_option: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery: # service discovery center
dns:
servers:
- "127.0.0.1:1053"
--- apisix_yaml
upstreams:
- service_name: apisix
discovery_type: dns
type: roundrobin
id: 1
--- error_log
failed to query the DNS server
--- error_code: 503
=== TEST 7: SRV
--- apisix_yaml
upstreams:
- service_name: "srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1980":20|"127.0.0.2:1980":20,"127.0.0.1:1980":60)\}/
--- response_body
hello world
=== TEST 8: SRV (RFC 2782 style)
--- apisix_yaml
upstreams:
- service_name: "_sip._tcp.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1980":20|"127.0.0.2:1980":20,"127.0.0.1:1980":60)\}/
--- response_body
hello world
=== TEST 9: SRV (different port)
--- apisix_yaml
upstreams:
- service_name: "port.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1981":20|"127.0.0.2:1981":20,"127.0.0.1:1980":60)\}/
--- response_body
hello world
=== TEST 10: SRV (zero weight)
--- apisix_yaml
upstreams:
- service_name: "zero-weight.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1980":1|"127.0.0.2:1980":1,"127.0.0.1:1980":60)\}/
--- response_body
hello world
=== TEST 11: SRV (split weight)
--- apisix_yaml
upstreams:
- service_name: "split-weight.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{(,?"127.0.0.(1:1980":200|3:1980":1|4:1980":1)){3}\}/
--- response_body
hello world
=== TEST 12: SRV (priority)
--- apisix_yaml
upstreams:
- service_name: "priority.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- response_body
hello world
--- error_log
connect() failed
--- grep_error_log eval
qr/proxy request to \S+/
--- grep_error_log_out
proxy request to 127.0.0.1:1979
proxy request to 127.0.0.2:1980
=== TEST 13: prefer SRV than A
--- apisix_yaml
upstreams:
- service_name: "srv-a.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- error_log
proxy request to 127.0.0.1:1980
--- response_body
hello world
=== TEST 14: SRV (port is 0)
--- apisix_yaml
upstreams:
- service_name: "zero.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- error_log
connect() failed
--- error_code: 502
--- grep_error_log eval
qr/proxy request to \S+/
--- grep_error_log_out
proxy request to 127.0.0.1:80
=== TEST 15: SRV (override port)
--- apisix_yaml
upstreams:
- service_name: "port.srv.test.local:1980"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1980":20|"127.0.0.2:1980":20,"127.0.0.1:1980":60)\}/
--- response_body
hello world
=== TEST 16: prefer A than SRV when A is ahead of SRV in config.yaml
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
dns:
servers:
- "127.0.0.1:1053"
order:
- A
- SRV
--- apisix_yaml
upstreams:
- service_name: "srv-a.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- error_code: 502
--- error_log
proxy request to 127.0.0.1:80
=== TEST 17: Invalid order type in config.yaml
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
dns:
servers:
- "127.0.0.1:1053"
order:
- B
- SRV
--- apisix_yaml
upstreams:
- service_name: "srv-a.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- must_die
--- error_log
matches none of the enum values
=== TEST 18: Multiple order type in config.yaml
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
dns:
servers:
- "127.0.0.1:1053"
order:
- SRV
- SRV
--- apisix_yaml
upstreams:
- service_name: "srv-a.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- must_die
--- error_log
expected unique items but items 1 and 2 are equal
=== TEST 19: invalid order type in config.yaml
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
dns:
servers:
- "127.0.0.1:1053"
order:
- a
- SRV
--- apisix_yaml
upstreams:
- service_name: "srv-a.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- must_die
--- error_log
matches none of the enum values
=== TEST 20: use resolv.conf
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery: # service discovery center
dns:
resolv_conf: build-cache/test_resolve.conf
--- apisix_yaml
upstreams:
- service_name: "sd.test.local:1980"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":1,"127.0.0.2:1980":1|"127.0.0.2:1980":1,"127.0.0.1:1980":1)\}/
--- response_body
hello world

View File

@@ -0,0 +1,117 @@
#
# 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 'no_plan';
repeat_each(1);
log_level('info');
no_root_location();
no_shuffle();
our $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
eureka:
host:
- "http://127.0.0.1:8761"
prefix: "/eureka/"
fetch_interval: 10
weight: 80
timeout:
connect: 1500
send: 1500
read: 1500
_EOC_
run_tests();
__DATA__
=== TEST 1: get APISIX-EUREKA info from EUREKA
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /eureka/*
upstream:
service_name: APISIX-EUREKA
discovery_type: eureka
type: roundrobin
#END
--- request
GET /eureka/apps/APISIX-EUREKA
--- response_body_like
.*<name>APISIX-EUREKA</name>.*
--- error_log
use config_provider: yaml
default_weight:80.
fetch_interval:10.
eureka uri:http://127.0.0.1:8761/eureka/.
connect_timeout:1500, send_timeout:1500, read_timeout:1500.
=== TEST 2: error service_name name
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /eureka/*
upstream:
service_name: APISIX-EUREKA-DEMO
discovery_type: eureka
type: roundrobin
#END
--- request
GET /eureka/apps/APISIX-EUREKA
--- error_code: 503
--- error_log eval
qr/.* no valid upstream node.*/
=== TEST 3: with proxy-rewrite
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /eureka-test/*
plugins:
proxy-rewrite:
regex_uri: ["^/eureka-test/(.*)", "/${1}"]
upstream:
service_name: APISIX-EUREKA
discovery_type: eureka
type: roundrobin
#END
--- request
GET /eureka-test/eureka/apps/APISIX-EUREKA
--- response_body_like
.*<name>APISIX-EUREKA</name>.*
--- error_log
use config_provider: yaml
default_weight:80.
fetch_interval:10.
eureka uri:http://127.0.0.1:8761/eureka/.
connect_timeout:1500, send_timeout:1500, read_timeout:1500.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,342 @@
#
# 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 'no_plan';
workers(3);
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->request) {
$block->set_value("request", "GET /t");
}
});
run_tests();
__DATA__
=== TEST 1: continue to get nacos data after failure in a service
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
nacos:
host:
- "http://127.0.0.1:20999"
prefix: "/nacos/v1/"
fetch_interval: 1
weight: 1
timeout:
connect: 2000
send: 2000
read: 5000
--- apisix_yaml
routes:
-
uri: /hello_
upstream:
service_name: NOT-NACOS
discovery_type: nacos
type: roundrobin
-
uri: /hello
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
#END
--- http_config
server {
listen 20999;
location / {
access_by_lua_block {
if not package.loaded.hit then
package.loaded.hit = true
ngx.exit(502)
end
}
proxy_pass http://127.0.0.1:8858;
}
}
--- request
GET /hello
--- response_body_like eval
qr/server [1-2]/
--- error_log
err:status = 502
=== TEST 2: change nacos server auth password
--- config
location /t {
content_by_lua_block {
local json = require("cjson")
local http = require("resty.http")
local httpc = http.new()
local nacos_host = "http://127.0.0.1:8848"
local res, err = httpc:request_uri(nacos_host .. "/nacos/v1/auth/login", {
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
body = ngx.encode_args({username = "nacos", password = "nacos"}),
})
if res.status ~= 200 then
ngx.say("nacos auth failed")
ngx.exit(401)
end
local res_json = json.decode(res.body)
res, err = httpc:request_uri(nacos_host .. "/nacos/v1/auth/users?accessToken=" .. res_json["accessToken"], {
method = "PUT",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
body = ngx.encode_args({username = "nacos", newPassword = "nacos!@#$%^&*()[]"}),
})
if res.status ~= 200 then
ngx.say("nacos token auth failed")
ngx.say(res.body)
ngx.exit(401)
end
ngx.say("done")
}
}
--- response_body
done
=== TEST 3: test complex host
--- extra_yaml_config
discovery:
nacos:
host:
- "http://nacos:nacos!@#$%^&*()[]@127.0.0.1:8848"
fetch_interval: 1
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
#END
--- config
location /t {
content_by_lua_block {
local http = require("resty.http")
local uri = "http://127.0.0.1:" .. ngx.var.server_port
-- Wait for 2 seconds for APISIX initialization
ngx.sleep(2)
local httpc = http.new()
local valid_responses = 0
for i = 1, 2 do
local res, err = httpc:request_uri(uri .. "/hello")
if not res then
ngx.log(ngx.ERR, "Request failed: ", err)
else
-- Clean and validate response
local clean_body = res.body:gsub("%s+$", "")
if clean_body == "server 1" or clean_body == "server 2" then
valid_responses = valid_responses + 1
else
ngx.log(ngx.ERR, "Invalid response: ", clean_body)
end
end
end
-- Final check
if valid_responses == 2 then
ngx.say("PASS")
else
ngx.say("FAIL: only ", valid_responses, " valid responses")
end
}
}
--- request
GET /t
--- response_body
PASS
=== TEST 4: restore nacos server auth password
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
#END
--- config
location /t {
content_by_lua_block {
local json = require("cjson")
local http = require("resty.http")
local httpc = http.new()
local nacos_host = "http://127.0.0.1:8848"
local res, err = httpc:request_uri(nacos_host .. "/nacos/v1/auth/login", {
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
body = ngx.encode_args({username = "nacos", password = "nacos!@#$%^&*()[]"}),
})
if res.status ~= 200 then
ngx.say("nacos auth failed")
ngx.exit(401)
end
local res_json = json.decode(res.body)
res, err = httpc:request_uri(nacos_host .. "/nacos/v1/auth/users?accessToken=" .. res_json["accessToken"], {
method = "PUT",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
body = ngx.encode_args({username = "nacos", newPassword = "nacos"}),
})
if res.status ~= 200 then
ngx.say("nacos token auth failed")
ngx.say(res.body)
ngx.exit(401)
end
ngx.say("done")
}
}
--- response_body
done
=== TEST 5: same service is registered in route, service and upstream, de-duplicate
--- yaml_config
apisix:
node_listen: 1984
--- extra_yaml_config
discovery:
nacos:
host:
- "http://127.0.0.1:8858"
fetch_interval: 1
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"upstream": {
"service_name": "APISIX-NACOS",
"discovery_type": "nacos",
"scheme": "http",
"type": "roundrobin",
"discovery_args": {
"namespace_id": "public",
"group_name": "DEFAULT_GROUP"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
local code, body = t('/apisix/admin/services/1',
ngx.HTTP_PUT,
[[{
"upstream": {
"type": "roundrobin",
"scheme": "http",
"discovery_type": "nacos",
"pass_host": "pass",
"service_name": "APISIX-NACOS",
"discovery_args": {
"namespace_id": "public",
"group_name": "DEFAULT_GROUP"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
local code, body = t('/apisix/admin/upstreams/1',
ngx.HTTP_PUT,
[[{
"type": "roundrobin",
"scheme": "http",
"discovery_type": "nacos",
"pass_host": "pass",
"service_name": "APISIX-NACOS",
"discovery_args": {
"namespace_id": "public",
"group_name": "DEFAULT_GROUP"
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.sleep(1.5)
local json_decode = require("toolkit.json").decode
local http = require "resty.http"
local httpc = http.new()
local dump_uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/v1/discovery/nacos/dump"
local res, err = httpc:request_uri(dump_uri, { method = "GET"})
if err then
ngx.log(ngx.ERR, err)
ngx.status = res.status
return
end
local body = json_decode(res.body)
local services = body.services
local service = services["public.DEFAULT_GROUP.APISIX-NACOS"]
local number = table.getn(service.nodes)
ngx.say(number)
}
}
--- response_body
2

View File

@@ -0,0 +1,638 @@
#
# 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.
#
# we can't use mse nacos to test, access_key and secret_key won't affect the open source nacos
use t::APISIX 'no_plan';
workers(4);
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->request) {
$block->set_value("request", "GET /t");
}
});
our $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
nacos:
host:
- "http://127.0.0.1:8858"
prefix: "/nacos/v1/"
fetch_interval: 1
weight: 1
timeout:
connect: 2000
send: 2000
read: 5000
access_key: "my_access_key"
secret_key: "my_secret_key"
_EOC_
run_tests();
__DATA__
=== TEST 1: error service_name
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS-DEMO
discovery_type: nacos
type: roundrobin
#END
--- request
GET /hello
--- error_code: 503
--- error_log
no valid upstream node
=== TEST 2: error namespace_id
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS-DEMO
discovery_type: nacos
type: roundrobin
discovery_args:
namespace_id: err_ns
#END
--- request
GET /hello
--- error_code: 503
--- error_log
no valid upstream node
=== TEST 3: error group_name
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS-DEMO
discovery_type: nacos
type: roundrobin
discovery_args:
group_name: err_group_name
#END
--- request
GET /hello
--- error_code: 503
--- error_log
no valid upstream node
=== TEST 4: error namespace_id and error group_name
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS-DEMO
discovery_type: nacos
type: roundrobin
discovery_args:
namespace_id: err_ns
group_name: err_group_name
#END
--- request
GET /hello
--- error_code: 503
--- error_log
no valid upstream node
=== TEST 5: error group_name and correct namespace_id
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS-DEMO
discovery_type: nacos
type: roundrobin
discovery_args:
namespace_id: test_ns
group_name: err_group_name
#END
--- request
GET /hello
--- error_code: 503
--- error_log
no valid upstream node
=== TEST 6: error namespace_id and correct group_name
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS-DEMO
discovery_type: nacos
type: roundrobin
discovery_args:
namespace_id: err_ns
group_name: test_group
#END
--- request
GET /hello
--- error_code: 503
--- error_log
no valid upstream node
=== TEST 7: get APISIX-NACOS info from NACOS - configured in services
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
service_id: 1
services:
-
id: 1
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
=== TEST 8: get APISIX-NACOS info from NACOS - configured in services with group_name
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
service_id: 1
services:
-
id: 1
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
discovery_args:
group_name: test_group
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
=== TEST 9: get APISIX-NACOS info from NACOS - configured in services with namespace_id
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
service_id: 1
services:
-
id: 1
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
discovery_args:
namespace_id: test_ns
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
=== TEST 10: get APISIX-NACOS info from NACOS - configured in services with group_name and namespace_id
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
service_id: 1
services:
-
id: 1
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
discovery_args:
group_name: test_group
namespace_id: test_ns
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
=== TEST 11: get APISIX-NACOS info from NACOS - configured in upstreams
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
--- no_error_log
[error, error]
=== TEST 12: get APISIX-NACOS info from NACOS - configured in upstreams with namespace_id
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
discovery_args:
namespace_id: test_ns
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
=== TEST 13: get APISIX-NACOS info from NACOS - configured in upstreams with group_name
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
discovery_args:
group_name: test_group
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
=== TEST 14: get APISIX-NACOS info from NACOS - configured in upstreams with namespace_id and group_name
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
discovery_args:
namespace_id: test_ns
group_name: test_group
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
=== TEST 15: get APISIX-NACOS info from NACOS - configured in upstreams + etcd
--- extra_yaml_config
discovery:
nacos:
host:
- "http://127.0.0.1:8858"
fetch_interval: 1
access_key: "my_access_key"
secret_key: "my_secret_key"
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/upstreams/1',
ngx.HTTP_PUT,
[[{
"service_name": "APISIX-NACOS",
"discovery_type": "nacos",
"type": "roundrobin"
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"upstream_id": 1
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 16: same namespace_id and service_name, different group_name
--- extra_yaml_config
discovery:
nacos:
host:
- "http://127.0.0.1:8858"
fetch_interval: 1
access_key: "my_access_key"
secret_key: "my_secret_key"
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
-- use nacos-service5
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"upstream": {
"service_name": "APISIX-NACOS",
"discovery_type": "nacos",
"type": "roundrobin",
"discovery_args": {
"namespace_id": "test_ns",
"group_name": "test_group"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
-- use nacos-service6
local code, body = t('/apisix/admin/routes/2',
ngx.HTTP_PUT,
[[{
"uri": "/hello1",
"upstream": {
"service_name": "APISIX-NACOS",
"discovery_type": "nacos",
"type": "roundrobin",
"discovery_args": {
"namespace_id": "test_ns",
"group_name": "test_group2"
}
},
"plugins": {
"proxy-rewrite": {
"uri": "/hello"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.sleep(1.5)
local http = require "resty.http"
local httpc = http.new()
local uri1 = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
local res, err = httpc:request_uri(uri1, { method = "GET"})
if err then
ngx.log(ngx.ERR, err)
ngx.status = res.status
return
end
ngx.say(res.body)
local uri2 = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1"
res, err = httpc:request_uri(uri2, { method = "GET"})
if err then
ngx.log(ngx.ERR, err)
ngx.status = res.status
return
end
ngx.say(res.body)
}
}
--- request
GET /t
--- response_body
server 1
server 3
=== TEST 17: same group_name and service_name, different namespace_id
--- extra_yaml_config
discovery:
nacos:
host:
- "http://127.0.0.1:8858"
fetch_interval: 1
access_key: "my_access_key"
secret_key: "my_secret_key"
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
-- use nacos-service5
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"upstream": {
"service_name": "APISIX-NACOS",
"discovery_type": "nacos",
"type": "roundrobin",
"discovery_args": {
"namespace_id": "test_ns",
"group_name": "test_group"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
-- use nacos-service7
local code, body = t('/apisix/admin/routes/2',
ngx.HTTP_PUT,
[[{
"uri": "/hello1",
"upstream": {
"service_name": "APISIX-NACOS",
"discovery_type": "nacos",
"type": "roundrobin",
"discovery_args": {
"namespace_id": "test_ns2",
"group_name": "test_group"
}
},
"plugins": {
"proxy-rewrite": {
"uri": "/hello"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.sleep(1.5)
local http = require "resty.http"
local httpc = http.new()
local uri1 = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
local res, err = httpc:request_uri(uri1, { method = "GET"})
if err then
ngx.log(ngx.ERR, err)
ngx.status = res.status
return
end
ngx.say(res.body)
local uri2 = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1"
res, err = httpc:request_uri(uri2, { method = "GET"})
if err then
ngx.log(ngx.ERR, err)
ngx.status = res.status
return
end
ngx.say(res.body)
}
}
--- request
GET /t
--- response_body
server 1
server 4

View File

@@ -0,0 +1,169 @@
#
# 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 'no_plan';
repeat_each(1);
log_level('info');
no_root_location();
no_shuffle();
workers(1);
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->request) {
$block->set_value("request", "GET /t");
}
if ($block->apisix_yaml) {
my $upstream = <<_EOC_;
upstreams:
- service_name: mock
discovery_type: mock
type: roundrobin
checks:
active:
http_path: /
timeout: 1
unhealthy:
tcp_failures: 30
interval: 1
healthy:
interval: 1
id: 1
#END
_EOC_
$block->set_value("apisix_yaml", $block->apisix_yaml . $upstream);
}
});
run_tests();
__DATA__
=== TEST 1: Validate healthchecker recreation on node count reduces to 1
--- http_config
server {
listen 3000 ;
location / {
return 200 'ok';
}
}
--- apisix_yaml
routes:
-
uris:
- /
upstream_id: 1
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local discovery = require("apisix.discovery.init").discovery
discovery.mock = {
nodes = function()
return {
{host = "127.0.0.1", port = 3000, weight = 50},
{host = "127.0.0.1", port = 8000, weight = 50},
}
end
}
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/"
local httpc = http.new()
local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
ngx.sleep(5)
discovery.mock = {
nodes = function()
return {
{host = "127.0.0.1", port = 3000, weight = 1}
}
end
}
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/"
local httpc = http.new()
local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
ngx.say(res.body)
ngx.sleep(5)
}
}
--- request
GET /t
--- response_body
ok
--- timeout: 22
--- no_error_log
unhealthy TCP increment (10/30)
=== TEST 2: Validate healthchecker deletion on node count reduces to 0
--- http_config
server {
listen 3000 ;
location / {
return 200 'ok';
}
}
--- apisix_yaml
routes:
-
uris:
- /
upstream_id: 1
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local discovery = require("apisix.discovery.init").discovery
discovery.mock = {
nodes = function()
return {
{host = "127.0.0.1", port = 3000, weight = 50},
{host = "127.0.0.1", port = 8000, weight = 50},
}
end
}
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/"
local httpc = http.new()
local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
ngx.sleep(5)
discovery.mock = {
nodes = function()
return {
}
end
}
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/"
local httpc = http.new()
local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
ngx.status = res.status
ngx.sleep(5)
}
}
--- request
GET /t
--- timeout: 22
--- no_error_log
unhealthy TCP increment (10/30)
--- error_code: 503

View File

@@ -0,0 +1,278 @@
#
# 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 'no_plan';
repeat_each(1);
log_level('info');
no_root_location();
no_shuffle();
add_block_preprocessor(sub {
my ($block) = @_;
my $http_config = $block->http_config // <<_EOC_;
server {
listen 20999;
location / {
content_by_lua_block {
ngx.say("missing consul services")
}
}
}
server {
listen 30511;
location /hello {
content_by_lua_block {
ngx.say("server 1")
}
}
}
server {
listen 30512;
location /hello {
content_by_lua_block {
ngx.say("server 2")
}
}
}
server {
listen 30513;
location /hello {
content_by_lua_block {
ngx.say("server 3")
}
}
}
server {
listen 30514;
location /hello {
content_by_lua_block {
ngx.say("server 4")
}
}
}
_EOC_
$block->set_value("http_config", $http_config);
if (!$block->stream_request) {
$block->set_value("stream_request", "GET /hello HTTP/1.1\r\nHost: 127.0.0.1:1985\r\nConnection: close\r\n\r\n");
}
});
our $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
enable_control: true
control:
ip: 127.0.0.1
port: 9090
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8500"
- "http://127.0.0.1:8600"
skip_services:
- "service_c"
timeout:
connect: 1000
read: 1000
wait: 60
weight: 1
fetch_interval: 1
keepalive: true
default_service:
host: "127.0.0.1"
port: 20999
metadata:
fail_timeout: 1
weight: 1
max_fails: 1
_EOC_
run_tests();
__DATA__
=== TEST 1: prepare consul catalog register nodes
--- config
location /consul1 {
rewrite ^/consul1/(.*) /v1/agent/service/$1 break;
proxy_pass http://127.0.0.1:8500;
}
location /consul2 {
rewrite ^/consul2/(.*) /v1/agent/service/$1 break;
proxy_pass http://127.0.0.1:8600;
}
--- pipelined_requests eval
[
"PUT /consul1/deregister/service_a1",
"PUT /consul1/deregister/service_b1",
"PUT /consul1/deregister/service_a2",
"PUT /consul1/deregister/service_b2",
"PUT /consul2/deregister/service_a1",
"PUT /consul2/deregister/service_b1",
"PUT /consul2/deregister/service_a2",
"PUT /consul2/deregister/service_b2",
"PUT /consul1/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul1/register\n" . "{\"ID\":\"service_a2\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30512,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul1/register\n" . "{\"ID\":\"service_b1\",\"Name\":\"service_b\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30513,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
"PUT /consul1/register\n" . "{\"ID\":\"service_b2\",\"Name\":\"service_b\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30514,\"Meta\":{\"service_b_version\":\"4.1\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
]
--- error_code eval
[200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200]
=== TEST 2: test consul server 1
--- yaml_config eval: $::yaml_config
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- stream_response eval
qr/server [1-2]/
--- no_error_log
[error]
=== TEST 3: test consul server 2
--- yaml_config eval: $::yaml_config
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: service_b
discovery_type: consul
type: roundrobin
#END
--- stream_response eval
qr/server [3-4]/
--- no_error_log
[error]
=== TEST 4: test mini consul config
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8500"
- "http://127.0.0.1:6500"
#END
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- stream_response eval
qr/server [1-2]/
--- ignore_error_log
=== TEST 5: test invalid service name
sometimes the consul key maybe deleted by mistake
--- yaml_config eval: $::yaml_config
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: service_c
discovery_type: consul
type: roundrobin
#END
--- stream_response_like
missing consul services
--- ignore_error_log
=== TEST 6: test skip keys
skip some services, return default nodes, get response: missing consul services
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul:
servers:
- "http://127.0.0.1:8600"
prefix: "upstreams"
skip_services:
- "service_a"
default_service:
host: "127.0.0.1"
port: 20999
metadata:
fail_timeout: 1
weight: 1
max_fails: 1
#END
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: service_a
discovery_type: consul
type: roundrobin
#END
--- stream_response_like
missing consul services
--- ignore_error_log

View File

@@ -0,0 +1,269 @@
#
# 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 'no_plan';
repeat_each(1);
log_level('info');
no_root_location();
no_shuffle();
add_block_preprocessor(sub {
my ($block) = @_;
my $http_config = $block->http_config // <<_EOC_;
server {
listen 20999;
location / {
content_by_lua_block {
ngx.say("missing consul_kv services")
}
}
}
server {
listen 30511;
location /hello {
content_by_lua_block {
ngx.say("server 1")
}
}
}
server {
listen 30512;
location /hello {
content_by_lua_block {
ngx.say("server 2")
}
}
}
server {
listen 30513;
location /hello {
content_by_lua_block {
ngx.say("server 3")
}
}
}
server {
listen 30514;
location /hello {
content_by_lua_block {
ngx.say("server 4")
}
}
}
_EOC_
$block->set_value("http_config", $http_config);
if (!$block->stream_request) {
$block->set_value("stream_request", "GET /hello HTTP/1.1\r\nHost: 127.0.0.1:1985\r\nConnection: close\r\n\r\n");
}
});
our $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8500"
- "http://127.0.0.1:8600"
prefix: "upstreams"
skip_keys:
- "upstreams/unused_api/"
timeout:
connect: 1000
read: 1000
wait: 60
weight: 1
fetch_interval: 1
keepalive: true
default_service:
host: "127.0.0.1"
port: 20999
metadata:
fail_timeout: 1
weight: 1
max_fails: 1
_EOC_
run_tests();
__DATA__
=== TEST 1: prepare consul kv register nodes
--- config
location /consul1 {
rewrite ^/consul1/(.*) /v1/kv/$1 break;
proxy_pass http://127.0.0.1:8500;
}
location /consul2 {
rewrite ^/consul2/(.*) /v1/kv/$1 break;
proxy_pass http://127.0.0.1:8600;
}
--- pipelined_requests eval
[
"DELETE /consul1/upstreams/webpages/?recurse=true",
"DELETE /consul2/upstreams/webpages/?recurse=true",
"PUT /consul1/upstreams/webpages/127.0.0.1:30511\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"PUT /consul1/upstreams/webpages/127.0.0.1:30512\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"PUT /consul2/upstreams/webpages/127.0.0.1:30513\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
"PUT /consul2/upstreams/webpages/127.0.0.1:30514\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
]
--- response_body eval
["true", "true", "true", "true", "true", "true"]
=== TEST 2: test consul server 1
--- yaml_config eval: $::yaml_config
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- stream_response eval
qr/server [1-2]/
--- no_error_log
[error]
=== TEST 3: test consul server 2
--- yaml_config eval: $::yaml_config
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: http://127.0.0.1:8600/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- stream_response eval
qr/server [3-4]/
--- no_error_log
[error]
=== TEST 4: test mini consul_kv config
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8500"
- "http://127.0.0.1:6500"
#END
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- stream_response eval
qr/server [1-2]/
--- ignore_error_log
=== TEST 5: test invalid service name
sometimes the consul key maybe deleted by mistake
--- yaml_config eval: $::yaml_config
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: http://127.0.0.1:8600/v1/kv/upstreams/deleted_keys/
discovery_type: consul_kv
type: roundrobin
#END
--- stream_response_like
missing consul_kv services
--- ignore_error_log
=== TEST 6: test skip keys
skip some keys, return default nodes, get response: missing consul_kv services
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8600"
prefix: "upstreams"
skip_keys:
- "upstreams/webpages/"
default_service:
host: "127.0.0.1"
port: 20999
metadata:
fail_timeout: 1
weight: 1
max_fails: 1
#END
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: http://127.0.0.1:8600/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- stream_response_like
missing consul_kv services
--- ignore_error_log

View File

@@ -0,0 +1,342 @@
#
# 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 'no_plan';
repeat_each(1);
no_root_location();
no_shuffle();
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->yaml_config) {
my $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery: # service discovery center
dns:
servers:
- "127.0.0.1:1053"
_EOC_
$block->set_value("yaml_config", $yaml_config);
}
if ($block->apisix_yaml) {
my $upstream = <<_EOC_;
stream_routes:
- id: 1
server_port: 1985
upstream_id: 1
#END
_EOC_
$block->set_value("apisix_yaml", $block->apisix_yaml . $upstream);
}
if (!$block->stream_request) {
$block->set_value("stream_request", "GET /hello HTTP/1.0\r\nHost: 127.0.0.1:1985\r\n\r\n");
}
});
run_tests();
__DATA__
=== TEST 1: default port to 53
--- log_level: debug
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery: # service discovery center
dns:
servers:
- "127.0.0.1"
--- apisix_yaml
upstreams:
- service_name: sd.test.local
discovery_type: dns
type: roundrobin
id: 1
--- error_log
connect to 127.0.0.1:53
=== TEST 2: A
--- apisix_yaml
upstreams:
- service_name: "sd.test.local:1980"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":1,"127.0.0.2:1980":1|"127.0.0.2:1980":1,"127.0.0.1:1980":1)\}/
--- stream_response_like
hello world
=== TEST 3: AAAA
--- listen_ipv6
--- apisix_yaml
upstreams:
- service_name: "ipv6.sd.test.local:1980"
discovery_type: dns
type: roundrobin
id: 1
--- stream_response_like
hello world
--- grep_error_log eval
qr/proxy request to \S+/
--- grep_error_log_out
proxy request to [0:0:0:0:0:0:0:1]:1980
=== TEST 4: prefer A to AAAA
--- listen_ipv6
--- apisix_yaml
upstreams:
- service_name: "mix.sd.test.local:1980"
discovery_type: dns
type: roundrobin
id: 1
--- stream_response_like
hello world
--- grep_error_log eval
qr/proxy request to \S+/
--- grep_error_log_out
proxy request to 127.0.0.1:1980
=== TEST 5: no /etc/hosts
--- apisix_yaml
upstreams:
- service_name: test.com
discovery_type: dns
type: roundrobin
id: 1
--- error_log
failed to query the DNS server
=== TEST 6: no /etc/resolv.conf
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
enable_resolv_search_option: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery: # service discovery center
dns:
servers:
- "127.0.0.1:1053"
--- apisix_yaml
upstreams:
- service_name: apisix
discovery_type: dns
type: roundrobin
id: 1
--- error_log
failed to query the DNS server
=== TEST 7: SRV
--- apisix_yaml
upstreams:
- service_name: "srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1980":20|"127.0.0.2:1980":20,"127.0.0.1:1980":60)\}/
--- stream_response_like
hello world
=== TEST 8: SRV (RFC 2782 style)
--- apisix_yaml
upstreams:
- service_name: "_sip._tcp.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1980":20|"127.0.0.2:1980":20,"127.0.0.1:1980":60)\}/
--- stream_response_like
hello world
=== TEST 9: SRV (different port)
--- apisix_yaml
upstreams:
- service_name: "port.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1981":20|"127.0.0.2:1981":20,"127.0.0.1:1980":60)\}/
--- stream_response_like
hello world
=== TEST 10: SRV (zero weight)
--- apisix_yaml
upstreams:
- service_name: "zero-weight.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1980":1|"127.0.0.2:1980":1,"127.0.0.1:1980":60)\}/
--- stream_response_like
hello world
=== TEST 11: SRV (split weight)
--- apisix_yaml
upstreams:
- service_name: "split-weight.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{(,?"127.0.0.(1:1980":200|3:1980":1|4:1980":1)){3}\}/
--- stream_response_like
hello world
=== TEST 12: SRV (priority)
--- apisix_yaml
upstreams:
- service_name: "priority.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- stream_response_like
hello world
--- error_log
connect() failed
--- grep_error_log eval
qr/proxy request to \S+/
--- grep_error_log_out
proxy request to 127.0.0.1:1979
proxy request to 127.0.0.2:1980
=== TEST 13: prefer SRV than A
--- apisix_yaml
upstreams:
- service_name: "srv-a.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- error_log
proxy request to 127.0.0.1:1980
--- stream_response_like
hello world
=== TEST 14: SRV (port is 0)
--- apisix_yaml
upstreams:
- service_name: "zero.srv.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- error_log
no valid upstream node
=== TEST 15: SRV (override port)
--- apisix_yaml
upstreams:
- service_name: "port.srv.test.local:1980"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1980":20|"127.0.0.2:1980":20,"127.0.0.1:1980":60)\}/
--- stream_response_like
hello world
=== TEST 16: prefer A than SRV when A is ahead of SRV in config.yaml
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
dns:
servers:
- "127.0.0.1:1053"
order:
- A
- SRV
--- apisix_yaml
upstreams:
- service_name: "srv-a.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- error_log
no valid upstream node

View File

@@ -0,0 +1,95 @@
#
# 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 'no_plan';
repeat_each(1);
log_level('info');
no_root_location();
no_shuffle();
our $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
eureka:
host:
- "http://127.0.0.1:8761"
prefix: "/eureka/"
fetch_interval: 10
weight: 80
timeout:
connect: 1500
send: 1500
read: 1500
_EOC_
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->stream_request) {
$block->set_value("stream_request", "GET /eureka/apps/APISIX-EUREKA HTTP/1.1\r\nHost: 127.0.0.1:1985\r\nConnection: close\r\n\r\n");
}
});
run_tests();
__DATA__
=== TEST 1: get APISIX-EUREKA info from EUREKA
--- yaml_config eval: $::yaml_config
--- apisix_yaml
stream_routes:
-
id: 1
server_port: 1985
upstream:
service_name: APISIX-EUREKA
discovery_type: eureka
type: roundrobin
#END
--- stream_response_like
.*<name>APISIX-EUREKA</name>.*
--- error_log
use config_provider: yaml
default_weight:80.
fetch_interval:10.
eureka uri:http://127.0.0.1:8761/eureka/.
connect_timeout:1500, send_timeout:1500, read_timeout:1500.
=== TEST 2: error service_name name
--- yaml_config eval: $::yaml_config
--- apisix_yaml
stream_routes:
-
id: 1
server_port: 1985
upstream:
service_name: APISIX-EUREKA-DEMO
discovery_type: eureka
type: roundrobin
#END
--- error_log eval
qr/.* no valid upstream node.*/

View File

@@ -0,0 +1,92 @@
#
# 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 'no_plan';
repeat_each(1);
log_level('info');
worker_connections(256);
no_root_location();
no_shuffle();
workers(4);
our $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
nacos:
host:
- "http://127.0.0.1:8858"
prefix: "/nacos/v1/"
fetch_interval: 1
weight: 1
timeout:
connect: 2000
send: 2000
read: 5000
_EOC_
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->stream_request) {
$block->set_value("stream_request", "GET /hello HTTP/1.1\r\nHost: 127.0.0.1:1985\r\nConnection: close\r\n\r\n");
}
});
run_tests();
__DATA__
=== TEST 1: get APISIX-NACOS info from NACOS - no auth
--- yaml_config eval: $::yaml_config
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
#END
--- stream_response eval
qr/server [1-2]/
--- no_error_log
[error]
=== TEST 2: error service_name name - no auth
--- yaml_config eval: $::yaml_config
--- apisix_yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
service_name: APISIX-NACOS-DEMO
discovery_type: nacos
type: roundrobin
#END
--- error_log
no valid upstream node