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:
@@ -0,0 +1,159 @@
|
||||
#
|
||||
# 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_shuffle();
|
||||
no_root_location();
|
||||
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!$block->error_log && !$block->no_error_log) {
|
||||
$block->set_value("no_error_log", "[error]\n[alert]");
|
||||
}
|
||||
});
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: blacklist
|
||||
--- 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,
|
||||
[[{
|
||||
"nodes": {
|
||||
"127.0.0.1:1995": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"ip-restriction": {
|
||||
"blacklist": [
|
||||
"127.0.0.0/24"
|
||||
]
|
||||
}
|
||||
},
|
||||
"upstream_id": "1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: hit
|
||||
--- stream_request eval
|
||||
mmm
|
||||
--- error_log
|
||||
Connection reset by peer
|
||||
|
||||
|
||||
|
||||
=== TEST 3: whitelist
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"ip-restriction": {
|
||||
"whitelist": [
|
||||
"127.0.0.0/24"
|
||||
]
|
||||
}
|
||||
},
|
||||
"upstream_id": "1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 4: hit
|
||||
--- stream_request eval
|
||||
mmm
|
||||
--- stream_response
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 5: validate schema
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"ip-restriction": {
|
||||
}
|
||||
},
|
||||
"upstream_id": "1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.print(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- error_code: 400
|
||||
--- response_body
|
||||
{"error_msg":"failed to check the configuration of stream plugin [ip-restriction]: value should match only one schema, but matches none"}
|
@@ -0,0 +1,336 @@
|
||||
#
|
||||
# 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_shuffle();
|
||||
no_root_location();
|
||||
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!$block->error_log && !$block->no_error_log) {
|
||||
$block->set_value("no_error_log", "[error]\n[alert]");
|
||||
}
|
||||
|
||||
my $config = $block->config // <<_EOC_;
|
||||
location /hit {
|
||||
content_by_lua_block {
|
||||
local sock = ngx.socket.tcp()
|
||||
local ok, err = sock:connect("127.0.0.1", 1985)
|
||||
if not ok then
|
||||
ngx.log(ngx.ERR, "failed to connect: ", err)
|
||||
return ngx.exit(503)
|
||||
end
|
||||
|
||||
local bytes, err = sock:send("mmm")
|
||||
if not bytes then
|
||||
ngx.log(ngx.ERR, "send stream request error: ", err)
|
||||
return ngx.exit(503)
|
||||
end
|
||||
local data, err = sock:receive("*a")
|
||||
if not data then
|
||||
sock:close()
|
||||
return ngx.exit(503)
|
||||
end
|
||||
ngx.print(data)
|
||||
}
|
||||
}
|
||||
|
||||
location /test_concurrency {
|
||||
content_by_lua_block {
|
||||
local reqs = {}
|
||||
for i = 1, 5 do
|
||||
reqs[i] = { "/hit" }
|
||||
end
|
||||
local resps = { ngx.location.capture_multi(reqs) }
|
||||
for i, resp in ipairs(resps) do
|
||||
ngx.say(resp.status)
|
||||
end
|
||||
}
|
||||
}
|
||||
_EOC_
|
||||
|
||||
$block->set_value("config", $config);
|
||||
|
||||
my $stream_upstream_code = $block->stream_upstream_code // <<_EOC_;
|
||||
local sock = ngx.req.socket()
|
||||
local data = sock:receive("1")
|
||||
ngx.sleep(0.2)
|
||||
ngx.say("hello world")
|
||||
_EOC_
|
||||
$block->set_value("stream_upstream_code", $stream_upstream_code);
|
||||
});
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route
|
||||
--- 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,
|
||||
[[{
|
||||
"nodes": {
|
||||
"127.0.0.1:1995": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"limit-conn": {
|
||||
"conn": 100,
|
||||
"burst": 50,
|
||||
"default_conn_delay": 0.1,
|
||||
"key": "remote_addr"
|
||||
}
|
||||
},
|
||||
"upstream_id": "1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: not exceeding the burst
|
||||
--- request
|
||||
GET /test_concurrency
|
||||
--- response_body
|
||||
200
|
||||
200
|
||||
200
|
||||
200
|
||||
200
|
||||
--- stream_enable
|
||||
|
||||
|
||||
|
||||
=== TEST 3: update route
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"limit-conn": {
|
||||
"conn": 2,
|
||||
"burst": 1,
|
||||
"default_conn_delay": 0.1,
|
||||
"key": "remote_addr"
|
||||
}
|
||||
},
|
||||
"upstream_id": "1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 4: exceeding the burst
|
||||
--- request
|
||||
GET /test_concurrency
|
||||
--- response_body
|
||||
200
|
||||
200
|
||||
200
|
||||
503
|
||||
503
|
||||
--- error_log
|
||||
Connection reset by peer
|
||||
--- stream_enable
|
||||
|
||||
|
||||
|
||||
=== TEST 5: var combination
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"limit-conn": {
|
||||
"conn": 2,
|
||||
"burst": 1,
|
||||
"default_conn_delay": 0.1,
|
||||
"key": "$remote_addr $server_addr",
|
||||
"key_type": "var_combination"
|
||||
}
|
||||
},
|
||||
"upstream_id": "1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 6: exceeding the burst
|
||||
--- request
|
||||
GET /test_concurrency
|
||||
--- response_body
|
||||
200
|
||||
200
|
||||
200
|
||||
503
|
||||
503
|
||||
--- error_log
|
||||
Connection reset by peer
|
||||
--- stream_enable
|
||||
|
||||
|
||||
|
||||
=== TEST 7: var combination (not exceed the burst)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"limit-conn": {
|
||||
"conn": 2,
|
||||
"burst": 1,
|
||||
"default_conn_delay": 0.1,
|
||||
"key": "$remote_port $server_addr",
|
||||
"key_type": "var_combination"
|
||||
}
|
||||
},
|
||||
"upstream_id": "1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 8: hit
|
||||
--- request
|
||||
GET /test_concurrency
|
||||
--- response_body
|
||||
200
|
||||
200
|
||||
200
|
||||
200
|
||||
200
|
||||
--- stream_enable
|
||||
|
||||
|
||||
|
||||
=== TEST 9: bypass empty key
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"limit-conn": {
|
||||
"conn": 2,
|
||||
"burst": 1,
|
||||
"default_conn_delay": 0.1,
|
||||
"key": "$proxy_protocol_addr $proxy_protocol_port",
|
||||
"key_type": "var_combination"
|
||||
}
|
||||
},
|
||||
"upstream_id": "1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 10: hit
|
||||
--- request
|
||||
GET /test_concurrency
|
||||
--- response_body
|
||||
200
|
||||
200
|
||||
200
|
||||
503
|
||||
503
|
||||
--- error_log
|
||||
The value of the configured key is empty, use client IP instead
|
||||
--- stream_enable
|
@@ -0,0 +1,134 @@
|
||||
#
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
use t::APISIX;
|
||||
|
||||
my $nginx_binary = $ENV{'TEST_NGINX_BINARY'} || 'nginx';
|
||||
my $version = eval { `$nginx_binary -V 2>&1` };
|
||||
|
||||
if ($version !~ m/\/apisix-nginx-module/) {
|
||||
plan(skip_all => "apisix-nginx-module not installed");
|
||||
} else {
|
||||
plan('no_plan');
|
||||
}
|
||||
|
||||
$ENV{TEST_NGINX_REDIS_PORT} ||= 1985;
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!$block->extra_yaml_config) {
|
||||
my $extra_yaml_config = <<_EOC_;
|
||||
xrpc:
|
||||
protocols:
|
||||
- name: redis
|
||||
_EOC_
|
||||
$block->set_value("extra_yaml_config", $extra_yaml_config);
|
||||
}
|
||||
|
||||
|
||||
if (!defined $block->request) {
|
||||
$block->set_value("request", "GET /t");
|
||||
}
|
||||
|
||||
$block;
|
||||
});
|
||||
|
||||
worker_connections(1024);
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: create a stream router with limit-conn
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"limit-conn": {
|
||||
"conn": 2,
|
||||
"burst": 1,
|
||||
"default_conn_delay": 0.1,
|
||||
"key": "$remote_port $server_addr",
|
||||
"key_type": "var_combination"
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"type": "none",
|
||||
"nodes": {
|
||||
"127.0.0.1:6379": 1
|
||||
}
|
||||
},
|
||||
"protocol": {
|
||||
"name": "redis"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: access the redis via proxy
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local redis = require "resty.redis"
|
||||
local red = redis:new()
|
||||
|
||||
local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local res, err = red:hmset("animals", "dog", "bark", "cat", "meow")
|
||||
if not res then
|
||||
ngx.say("failed to set animals: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("hmset animals: ", res)
|
||||
|
||||
local res, err = red:hmget("animals", "dog", "cat")
|
||||
if not res then
|
||||
ngx.say("failed to get animals: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("hmget animals: ", res)
|
||||
|
||||
ok, err = red:close()
|
||||
if not ok then
|
||||
ngx.say("failed to close: ", err)
|
||||
return
|
||||
end
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
hmset animals: OK
|
||||
hmget animals: barkmeow
|
||||
--- no_error_log
|
||||
attempt to perform arithmetic on field 'request_time'
|
||||
--- stream_conf_enable
|
@@ -0,0 +1,395 @@
|
||||
#
|
||||
# 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(2);
|
||||
no_long_string();
|
||||
no_shuffle();
|
||||
no_root_location();
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"remote_addr": "127.0.0.1",
|
||||
"server_port": 1985,
|
||||
"plugins": {
|
||||
"mqtt-proxy": {
|
||||
"protocol_name": "MQTT",
|
||||
"protocol_level": 4
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"type": "chash",
|
||||
"key": "mqtt_client_id",
|
||||
"nodes": [
|
||||
{
|
||||
"host": "127.0.0.1",
|
||||
"port": 1995,
|
||||
"weight": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: invalid header
|
||||
--- stream_request eval
|
||||
mmm
|
||||
--- error_log
|
||||
Received unexpected MQTT packet type+flags
|
||||
|
||||
|
||||
|
||||
=== TEST 3: hit route
|
||||
--- stream_request eval
|
||||
"\x10\x0f\x00\x04\x4d\x51\x54\x54\x04\x02\x00\x3c\x00\x03\x66\x6f\x6f"
|
||||
--- stream_response
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 4: set route (wrong server port)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"remote_addr": "127.0.0.1",
|
||||
"server_port": 2000,
|
||||
"plugins": {
|
||||
"mqtt-proxy": {
|
||||
"protocol_name": "MQTT",
|
||||
"protocol_level": 4,
|
||||
"upstream": {
|
||||
"host": "127.0.0.1",
|
||||
"port": 1995
|
||||
}
|
||||
}
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 5: failed to match route
|
||||
--- stream_request eval
|
||||
"\x10\x0f"
|
||||
--- stream_response
|
||||
receive stream response error: connection reset by peer
|
||||
--- error_log
|
||||
receive stream response error: connection reset by peer
|
||||
--- error_log
|
||||
match(): not hit any route
|
||||
|
||||
|
||||
|
||||
=== TEST 6: set route with host
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"remote_addr": "127.0.0.1",
|
||||
"server_port": 1985,
|
||||
"plugins": {
|
||||
"mqtt-proxy": {
|
||||
"protocol_name": "MQTT",
|
||||
"protocol_level": 4
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"type": "chash",
|
||||
"key": "mqtt_client_id",
|
||||
"nodes": [
|
||||
{
|
||||
"host": "localhost",
|
||||
"port": 1995,
|
||||
"weight": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 7: hit route
|
||||
--- stream_request eval
|
||||
"\x10\x0f\x00\x04\x4d\x51\x54\x54\x04\x02\x00\x3c\x00\x03\x66\x6f\x6f"
|
||||
--- stream_response
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 8: set route with upstream
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"remote_addr": "127.0.0.1",
|
||||
"server_port": 1985,
|
||||
"plugins": {
|
||||
"mqtt-proxy": {
|
||||
"protocol_name": "MQTT",
|
||||
"protocol_level": 4
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"type": "roundrobin",
|
||||
"nodes": [{
|
||||
"host": "127.0.0.1",
|
||||
"port": 1995,
|
||||
"weight": 1
|
||||
}]
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 9: hit route
|
||||
--- stream_request eval
|
||||
"\x10\x0f\x00\x04\x4d\x51\x54\x54\x04\x02\x00\x3c\x00\x03\x66\x6f\x6f"
|
||||
--- stream_response
|
||||
hello world
|
||||
--- grep_error_log eval
|
||||
qr/mqtt client id: \w+/
|
||||
--- grep_error_log_out
|
||||
mqtt client id: foo
|
||||
|
||||
|
||||
|
||||
=== TEST 10: hit route with empty client id
|
||||
--- stream_request eval
|
||||
"\x10\x0c\x00\x04\x4d\x51\x54\x54\x04\x02\x00\x3c\x00\x00"
|
||||
--- stream_response
|
||||
hello world
|
||||
--- grep_error_log eval
|
||||
qr/mqtt client id: \w+/
|
||||
--- grep_error_log_out
|
||||
|
||||
|
||||
|
||||
=== TEST 11: MQTT 5
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"remote_addr": "127.0.0.1",
|
||||
"server_port": 1985,
|
||||
"plugins": {
|
||||
"mqtt-proxy": {
|
||||
"protocol_name": "MQTT",
|
||||
"protocol_level": 5
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"type": "roundrobin",
|
||||
"nodes": [{
|
||||
"host": "127.0.0.1",
|
||||
"port": 1995,
|
||||
"weight": 1
|
||||
}]
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 12: hit route with empty property
|
||||
--- stream_request eval
|
||||
"\x10\x0d\x00\x04\x4d\x51\x54\x54\x05\x02\x00\x3c\x00\x00\x00"
|
||||
--- stream_response
|
||||
hello world
|
||||
--- grep_error_log eval
|
||||
qr/mqtt client id: \w+/
|
||||
--- grep_error_log_out
|
||||
|
||||
|
||||
|
||||
=== TEST 13: hit route with property
|
||||
--- stream_request eval
|
||||
"\x10\x1b\x00\x04\x4d\x51\x54\x54\x05\x02\x00\x3c\x05\x11\x00\x00\x0e\x10\x00\x09\x63\x6c\x69\x6e\x74\x2d\x31\x31\x31"
|
||||
--- stream_response
|
||||
hello world
|
||||
--- grep_error_log eval
|
||||
qr/mqtt client id: \S+/
|
||||
--- grep_error_log_out
|
||||
mqtt client id: clint-111
|
||||
|
||||
|
||||
|
||||
=== TEST 14: balance with mqtt_client_id
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"remote_addr": "127.0.0.1",
|
||||
"server_port": 1985,
|
||||
"plugins": {
|
||||
"mqtt-proxy": {
|
||||
"protocol_name": "MQTT",
|
||||
"protocol_level": 5
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"type": "chash",
|
||||
"key": "mqtt_client_id",
|
||||
"nodes": [
|
||||
{
|
||||
"host": "0.0.0.0",
|
||||
"port": 1995,
|
||||
"weight": 1
|
||||
},
|
||||
{
|
||||
"host": "127.0.0.1",
|
||||
"port": 1995,
|
||||
"weight": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 15: hit route with empty id
|
||||
--- stream_request eval
|
||||
"\x10\x0d\x00\x04\x4d\x51\x54\x54\x05\x02\x00\x3c\x00\x00\x00"
|
||||
--- stream_response
|
||||
hello world
|
||||
--- grep_error_log eval
|
||||
qr/(mqtt client id: \w+|proxy request to \S+)/
|
||||
--- grep_error_log_out
|
||||
proxy request to 127.0.0.1:1995
|
||||
|
||||
|
||||
|
||||
=== TEST 16: hit route with different client id, part 1
|
||||
--- stream_request eval
|
||||
"\x10\x0e\x00\x04\x4d\x51\x54\x54\x05\x02\x00\x3c\x00\x00\x01\x66"
|
||||
--- stream_response
|
||||
hello world
|
||||
--- grep_error_log eval
|
||||
qr/(mqtt client id: \w+|proxy request to \S+)/
|
||||
--- grep_error_log_out
|
||||
mqtt client id: f
|
||||
proxy request to 0.0.0.0:1995
|
||||
|
||||
|
||||
|
||||
=== TEST 17: hit route with different client id, part 2
|
||||
--- stream_request eval
|
||||
"\x10\x0e\x00\x04\x4d\x51\x54\x54\x05\x02\x00\x3c\x00\x00\x01\x67"
|
||||
--- stream_response
|
||||
hello world
|
||||
--- grep_error_log eval
|
||||
qr/(mqtt client id: \w+|proxy request to \S+)/
|
||||
--- grep_error_log_out
|
||||
mqtt client id: g
|
||||
proxy request to 127.0.0.1:1995
|
@@ -0,0 +1,184 @@
|
||||
#
|
||||
# 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_shuffle();
|
||||
no_root_location();
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: set route with invalid host
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"remote_addr": "127.0.0.1",
|
||||
"server_port": 1985,
|
||||
"plugins": {
|
||||
"mqtt-proxy": {
|
||||
"protocol_name": "MQTT",
|
||||
"protocol_level": 4
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"type": "chash",
|
||||
"key": "mqtt_client_id",
|
||||
"nodes": [
|
||||
{
|
||||
"host": "loc",
|
||||
"port": 1995,
|
||||
"weight": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: hit route
|
||||
--- stream_request eval
|
||||
"\x10\x0f\x00\x04\x4d\x51\x54\x54\x04\x02\x00\x3c\x00\x03\x66\x6f\x6f"
|
||||
--- error_log
|
||||
failed to parse domain: loc, error:
|
||||
--- timeout: 10
|
||||
|
||||
|
||||
|
||||
=== TEST 3: set upstream(id: 1)
|
||||
--- 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,
|
||||
[[{
|
||||
"type": "chash",
|
||||
"key": "mqtt_client_id",
|
||||
"nodes": [
|
||||
{
|
||||
"host": "0.0.0.0",
|
||||
"port": 1995,
|
||||
"weight": 1
|
||||
},
|
||||
{
|
||||
"host": "127.0.0.1",
|
||||
"port": 1995,
|
||||
"weight": 1
|
||||
}
|
||||
]
|
||||
}]]
|
||||
)
|
||||
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 4: balance with mqtt_client_id
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"remote_addr": "127.0.0.1",
|
||||
"server_port": 1985,
|
||||
"plugins": {
|
||||
"mqtt-proxy": {
|
||||
"protocol_name": "MQTT",
|
||||
"protocol_level": 5
|
||||
}
|
||||
},
|
||||
"upstream_id": 1
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 5: hit route with empty id
|
||||
--- stream_request eval
|
||||
"\x10\x0d\x00\x04\x4d\x51\x54\x54\x05\x02\x00\x3c\x00\x00\x00"
|
||||
--- stream_response
|
||||
hello world
|
||||
--- grep_error_log eval
|
||||
qr/(mqtt client id: \w+|proxy request to \S+)/
|
||||
--- grep_error_log_out
|
||||
proxy request to 127.0.0.1:1995
|
||||
|
||||
|
||||
|
||||
=== TEST 6: hit route with different client id, part 1
|
||||
--- stream_request eval
|
||||
"\x10\x0e\x00\x04\x4d\x51\x54\x54\x05\x02\x00\x3c\x00\x00\x01\x66"
|
||||
--- stream_response
|
||||
hello world
|
||||
--- grep_error_log eval
|
||||
qr/(mqtt client id: \w+|proxy request to \S+)/
|
||||
--- grep_error_log_out
|
||||
mqtt client id: f
|
||||
proxy request to 0.0.0.0:1995
|
||||
|
||||
|
||||
|
||||
=== TEST 7: hit route with different client id, part 2
|
||||
--- stream_request eval
|
||||
"\x10\x0e\x00\x04\x4d\x51\x54\x54\x05\x02\x00\x3c\x00\x00\x01\x67"
|
||||
--- stream_response
|
||||
hello world
|
||||
--- grep_error_log eval
|
||||
qr/(mqtt client id: \w+|proxy request to \S+)/
|
||||
--- grep_error_log_out
|
||||
mqtt client id: g
|
||||
proxy request to 127.0.0.1:1995
|
@@ -0,0 +1,78 @@
|
||||
#
|
||||
# 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';
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
$block->set_value("no_error_log", "[error]");
|
||||
|
||||
$block;
|
||||
});
|
||||
|
||||
no_long_string();
|
||||
no_shuffle();
|
||||
no_root_location();
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: ensure all plugins have exposed their name
|
||||
--- stream_enable
|
||||
--- stream_server_config
|
||||
content_by_lua_block {
|
||||
local lfs = require("lfs")
|
||||
for file_name in lfs.dir(ngx.config.prefix() .. "/../../apisix/stream/plugins/") do
|
||||
if string.match(file_name, ".lua$") then
|
||||
local expected = file_name:sub(1, #file_name - 4)
|
||||
local plugin = require("apisix.stream.plugins." .. expected)
|
||||
if plugin.name ~= expected then
|
||||
ngx.say("expected ", expected, " got ", plugin.name)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
ngx.say('ok')
|
||||
}
|
||||
--- stream_response
|
||||
ok
|
||||
|
||||
|
||||
|
||||
=== TEST 2: ensure all plugins have unique priority
|
||||
--- stream_enable
|
||||
--- stream_server_config
|
||||
content_by_lua_block {
|
||||
local lfs = require("lfs")
|
||||
local pri_name = {}
|
||||
for file_name in lfs.dir(ngx.config.prefix() .. "/../../apisix/stream/plugins/") do
|
||||
if string.match(file_name, ".lua$") then
|
||||
local name = file_name:sub(1, #file_name - 4)
|
||||
local plugin = require("apisix.stream.plugins." .. name)
|
||||
if pri_name[plugin.priority] then
|
||||
ngx.say(name, " has same priority with ", pri_name[plugin.priority])
|
||||
return
|
||||
end
|
||||
pri_name[plugin.priority] = plugin.name
|
||||
end
|
||||
end
|
||||
ngx.say('ok')
|
||||
}
|
||||
--- stream_response
|
||||
ok
|
@@ -0,0 +1,158 @@
|
||||
#
|
||||
# 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 {
|
||||
if ($ENV{TEST_NGINX_CHECK_LEAK}) {
|
||||
$SkipReason = "unavailable for the hup tests";
|
||||
|
||||
} else {
|
||||
$ENV{TEST_NGINX_USE_HUP} = 1;
|
||||
undef $ENV{TEST_NGINX_USE_STAP};
|
||||
}
|
||||
}
|
||||
|
||||
use t::APISIX;
|
||||
|
||||
my $nginx_binary = $ENV{'TEST_NGINX_BINARY'} || 'nginx';
|
||||
my $version = eval { `$nginx_binary -V 2>&1` };
|
||||
|
||||
if ($version !~ m/\/apisix-nginx-module/) {
|
||||
plan(skip_all => "apisix-nginx-module not installed");
|
||||
} else {
|
||||
plan('no_plan');
|
||||
}
|
||||
|
||||
repeat_each(1);
|
||||
no_long_string();
|
||||
no_shuffle();
|
||||
no_root_location();
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
my $extra_yaml_config = <<_EOC_;
|
||||
stream_plugins:
|
||||
- mqtt-proxy
|
||||
- prometheus
|
||||
_EOC_
|
||||
|
||||
$block->set_value("extra_yaml_config", $extra_yaml_config);
|
||||
|
||||
if (!defined $block->request) {
|
||||
$block->set_value("request", "GET /t");
|
||||
}
|
||||
});
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: pre-create public API route
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local data = {
|
||||
{
|
||||
url = "/apisix/admin/routes/metrics",
|
||||
data = [[{
|
||||
"plugins": {
|
||||
"public-api": {}
|
||||
},
|
||||
"uri": "/apisix/prometheus/metrics"
|
||||
}]]
|
||||
},
|
||||
{
|
||||
url = "/apisix/admin/stream_routes/mqtt",
|
||||
data = [[{
|
||||
"plugins": {
|
||||
"mqtt-proxy": {
|
||||
"protocol_name": "MQTT",
|
||||
"protocol_level": 4
|
||||
},
|
||||
"prometheus": {}
|
||||
},
|
||||
"upstream": {
|
||||
"type": "roundrobin",
|
||||
"nodes": [{
|
||||
"host": "127.0.0.1",
|
||||
"port": 1995,
|
||||
"weight": 1
|
||||
}]
|
||||
}
|
||||
}]]
|
||||
}
|
||||
}
|
||||
|
||||
local t = require("lib.test_admin").test
|
||||
|
||||
for _, data in ipairs(data) do
|
||||
local code, body = t(data.url, ngx.HTTP_PUT, data.data)
|
||||
if code > 300 then
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
end
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
|
||||
|
||||
|
||||
=== TEST 2: hit
|
||||
--- stream_request eval
|
||||
"\x10\x0f\x00\x04\x4d\x51\x54\x54\x04\x02\x00\x3c\x00\x03\x66\x6f\x6f"
|
||||
--- stream_response
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 3: fetch the prometheus metric data
|
||||
--- request
|
||||
GET /apisix/prometheus/metrics
|
||||
--- response_body eval
|
||||
qr/apisix_stream_connection_total\{route="mqtt"\} 1/
|
||||
|
||||
|
||||
|
||||
=== TEST 4: hit, error
|
||||
--- stream_request eval
|
||||
mmm
|
||||
--- error_log
|
||||
Received unexpected MQTT packet type+flags
|
||||
|
||||
|
||||
|
||||
=== TEST 5: fetch the prometheus metric data
|
||||
--- request
|
||||
GET /apisix/prometheus/metrics
|
||||
--- response_body eval
|
||||
qr/apisix_stream_connection_total\{route="mqtt"\} 2/
|
||||
|
||||
|
||||
|
||||
=== TEST 6: contains metrics from stub_status
|
||||
--- request
|
||||
GET /apisix/prometheus/metrics
|
||||
--- response_body eval
|
||||
qr/apisix_nginx_http_current_connections\{state="active"\} 1/
|
||||
|
||||
|
||||
|
||||
=== TEST 7: contains basic metrics
|
||||
--- request
|
||||
GET /apisix/prometheus/metrics
|
||||
--- response_body eval
|
||||
qr/apisix_node_info\{hostname="[^"]+"\}/
|
416
CloudronPackages/APISIX/apisix-source/t/stream-plugin/syslog.t
Normal file
416
CloudronPackages/APISIX/apisix-source/t/stream-plugin/syslog.t
Normal file
@@ -0,0 +1,416 @@
|
||||
#
|
||||
# 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_shuffle();
|
||||
no_root_location();
|
||||
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!$block->error_log && !$block->no_error_log) {
|
||||
$block->set_value("no_error_log", "[error]\n[alert]");
|
||||
}
|
||||
|
||||
if (!defined $block->extra_stream_config) {
|
||||
my $stream_config = <<_EOC_;
|
||||
server {
|
||||
listen 8125 udp;
|
||||
content_by_lua_block {
|
||||
require("lib.mock_layer4").dogstatsd()
|
||||
}
|
||||
}
|
||||
_EOC_
|
||||
$block->set_value("extra_stream_config", $stream_config);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: custom log format not set
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
-- ensure the format is not set
|
||||
t('/apisix/admin/plugin_metadata/syslog',
|
||||
ngx.HTTP_DELETE
|
||||
)
|
||||
|
||||
local code, body = t('/apisix/admin/upstreams/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"nodes": {
|
||||
"127.0.0.1:1995": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"syslog": {
|
||||
"host" : "127.0.0.1",
|
||||
"port" : 8125,
|
||||
"sock_type": "udp",
|
||||
"batch_max_size": 1,
|
||||
"flush_limit":1
|
||||
}
|
||||
},
|
||||
"upstream_id": "1"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 2: hit
|
||||
--- stream_request eval
|
||||
mmm
|
||||
--- stream_response
|
||||
hello world
|
||||
--- error_log
|
||||
syslog's log_format is not set
|
||||
|
||||
|
||||
|
||||
=== TEST 3: set custom log format
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/plugin_metadata/syslog',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"log_format": {
|
||||
"client_ip": "$remote_addr"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 4: hit
|
||||
--- stream_request eval
|
||||
mmm
|
||||
--- stream_response
|
||||
hello world
|
||||
--- wait: 0.5
|
||||
--- error_log eval
|
||||
qr/message received:.*\"client_ip\\"\:\\"127.0.0.1\\"/
|
||||
|
||||
|
||||
|
||||
=== TEST 5: flush manually
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local plugin = require("apisix.stream.plugins.syslog")
|
||||
local logger_socket = require("resty.logger.socket")
|
||||
local logger, err = logger_socket:new({
|
||||
host = "127.0.0.1",
|
||||
port = 5044,
|
||||
flush_limit = 100,
|
||||
})
|
||||
|
||||
local bytes, err = logger:log("abc")
|
||||
if err then
|
||||
ngx.log(ngx.ERR, err)
|
||||
end
|
||||
|
||||
local bytes, err = logger:log("efg")
|
||||
if err then
|
||||
ngx.log(ngx.ERR, err)
|
||||
end
|
||||
|
||||
local ok, err = plugin.flush_syslog(logger)
|
||||
if not ok then
|
||||
ngx.say("failed to flush syslog: ", err)
|
||||
return
|
||||
end
|
||||
ngx.say("done")
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
done
|
||||
|
||||
|
||||
|
||||
=== TEST 6: small flush_limit, instant flush
|
||||
--- stream_conf_enable
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"syslog": {
|
||||
"host" : "127.0.0.1",
|
||||
"port" : 5044,
|
||||
"flush_limit" : 1,
|
||||
"inactive_timeout": 1
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1995": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
|
||||
-- wait etcd sync
|
||||
ngx.sleep(0.5)
|
||||
|
||||
local sock = ngx.socket.tcp()
|
||||
local ok, err = sock:connect("127.0.0.1", 1985)
|
||||
if not ok then
|
||||
ngx.say("failed to connect: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
assert(sock:send("mmm"))
|
||||
local data = assert(sock:receive("*a"))
|
||||
ngx.print(data)
|
||||
|
||||
-- wait flush log
|
||||
ngx.sleep(2.5)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
hello world
|
||||
--- timeout: 5
|
||||
--- error_log
|
||||
try to lock with key stream/route#1
|
||||
unlock with key stream/route#1
|
||||
|
||||
|
||||
|
||||
=== TEST 7: check plugin configuration updating
|
||||
--- stream_conf_enable
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body1 = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"syslog": {
|
||||
"host" : "127.0.0.1",
|
||||
"port" : 5044,
|
||||
"batch_max_size": 1
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1995": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say("fail")
|
||||
return
|
||||
end
|
||||
|
||||
local sock = ngx.socket.tcp()
|
||||
local ok, err = sock:connect("127.0.0.1", 1985)
|
||||
if not ok then
|
||||
ngx.status = code
|
||||
ngx.say("fail")
|
||||
return
|
||||
end
|
||||
|
||||
assert(sock:send("mmm"))
|
||||
local body2 = assert(sock:receive("*a"))
|
||||
|
||||
local code, body3 = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"syslog": {
|
||||
"host" : "127.0.0.1",
|
||||
"port" : 5045,
|
||||
"batch_max_size": 1
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1995": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say("fail")
|
||||
return
|
||||
end
|
||||
|
||||
local sock = ngx.socket.tcp()
|
||||
local ok, err = sock:connect("127.0.0.1", 1985)
|
||||
if not ok then
|
||||
ngx.status = code
|
||||
ngx.say("fail")
|
||||
return
|
||||
end
|
||||
|
||||
assert(sock:send("mmm"))
|
||||
local body4 = assert(sock:receive("*a"))
|
||||
|
||||
ngx.print(body1)
|
||||
ngx.print(body2)
|
||||
ngx.print(body3)
|
||||
ngx.print(body4)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- wait: 0.5
|
||||
--- response_body
|
||||
passedhello world
|
||||
passedhello world
|
||||
--- grep_error_log eval
|
||||
qr/sending a batch logs to 127.0.0.1:(\d+)/
|
||||
--- grep_error_log_out
|
||||
sending a batch logs to 127.0.0.1:5044
|
||||
sending a batch logs to 127.0.0.1:5045
|
||||
|
||||
|
||||
|
||||
=== TEST 8: log format in plugin
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/stream_routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"plugins": {
|
||||
"syslog": {
|
||||
"batch_max_size": 1,
|
||||
"flush_limit": 1,
|
||||
"log_format": {
|
||||
"vip": "$remote_addr"
|
||||
},
|
||||
"host" : "127.0.0.1",
|
||||
"port" : 5050
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1995": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/hello"
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
ngx.say(body)
|
||||
return
|
||||
end
|
||||
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
|
||||
|
||||
|
||||
=== TEST 9: access
|
||||
--- stream_extra_init_by_lua
|
||||
local syslog = require("apisix.plugins.syslog.init")
|
||||
local json = require("apisix.core.json")
|
||||
local log = require("apisix.core.log")
|
||||
local old_f = syslog.push_entry
|
||||
syslog.push_entry = function(conf, ctx, entry)
|
||||
assert(entry.vip == "127.0.0.1")
|
||||
log.info("push_entry is called with data: ", json.encode(entry))
|
||||
return old_f(conf, ctx, entry)
|
||||
end
|
||||
--- stream_request
|
||||
mmm
|
||||
--- stream_response
|
||||
hello world
|
||||
--- wait: 0.5
|
||||
--- no_error_log
|
||||
[error]
|
||||
--- error_log
|
||||
push_entry is called with data
|
Reference in New Issue
Block a user