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,287 @@
--
-- 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.
--
local core = require("apisix.core")
local sdk = require("apisix.stream.xrpc.sdk")
local xrpc_socket = require("resty.apisix.stream.xrpc.socket")
local bit = require("bit")
local lshift = bit.lshift
local ffi = require("ffi")
local ffi_str = ffi.string
local ipairs = ipairs
local math_random = math.random
local OK = ngx.OK
local DECLINED = ngx.DECLINED
local DONE = ngx.DONE
local str_byte = string.byte
core.ctx.register_var("rpc_len", function(ctx)
return ctx.len
end)
local _M = {}
local router_version
local router
-- pingpong protocol is designed to use in the test of xRPC.
-- It contains two part: a fixed-length header & a body.
-- Header format:
-- "pp" (magic number) + 1 bytes req type + 2 bytes stream id + 1 reserved bytes
-- + 4 bytes body length + optional 4 bytes service name
local HDR_LEN = 10
local TYPE_HEARTBEAT = 1
local TYPE_UNARY = 2
local TYPE_STREAM = 3
local TYPE_UNARY_DYN_UP = 4
function _M.init_worker()
core.log.info("call pingpong's init_worker")
end
function _M.init_downstream(session)
-- create the downstream
local sk = xrpc_socket.downstream.socket()
sk:settimeout(1000) -- the short timeout is just for test
return sk
end
local function read_data(sk, len, body)
local f = body and sk.drain or sk.read
local p, err = f(sk, len)
if not p then
if err ~= "closed" then
core.log.error("failed to read: ", err)
end
return nil
end
return p
end
local function to_int32(p, idx)
return lshift(p[idx], 24) + lshift(p[idx + 1], 16) + lshift(p[idx + 2], 8) + p[idx + 3]
end
function _M.from_downstream(session, downstream)
-- read a request from downstream
-- return status and the new ctx
core.log.info("call pingpong's from_downstream")
local p = read_data(downstream, HDR_LEN, false)
if p == nil then
return DECLINED
end
local p_b = str_byte("p")
if p[0] ~= p_b or p[1] ~= p_b then
core.log.error("invalid magic number: ", ffi_str(p, 2))
return DECLINED
end
local typ = p[2]
if typ == TYPE_HEARTBEAT then
core.log.info("send heartbeat")
-- need to reset read buf as we won't forward it
downstream:reset_read_buf()
downstream:send(ffi_str(p, HDR_LEN))
return DONE
end
local stream_id = p[3] * 256 + p[4]
local ctx = sdk.get_req_ctx(session, stream_id)
local body_len = to_int32(p, 6)
core.log.info("read body len: ", body_len)
if typ == TYPE_UNARY_DYN_UP then
local p = read_data(downstream, 4, false)
if p == nil then
return DECLINED
end
local len = 4
for i = 0, 3 do
if p[i] == 0 then
len = i
break
end
end
local service = ffi_str(p, len)
core.log.info("get service [", service, "]")
ctx.service = service
local changed, raw_router, version = sdk.get_router(session, router_version)
if changed then
router_version = version
router = {}
for _, r in ipairs(raw_router) do
local conf = r.protocol.conf
if conf and conf.service then
router[conf.service] = r
end
end
end
local conf = router[ctx.service]
if conf then
local err = sdk.set_upstream(session, conf)
if err then
core.log.error("failed to set upstream: ", err)
return DECLINED
end
end
end
local p = read_data(downstream, body_len, true)
if p == nil then
return DECLINED
end
ctx.is_unary = typ == TYPE_UNARY or typ == TYPE_UNARY_DYN_UP
ctx.is_stream = typ == TYPE_STREAM
ctx.id = stream_id
ctx.len = HDR_LEN + body_len
if typ == TYPE_UNARY_DYN_UP then
ctx.len = ctx.len + 4
end
return OK, ctx
end
function _M.connect_upstream(session, ctx)
-- connect the upstream with upstream_conf
-- also do some handshake jobs
-- return status and the new upstream
core.log.info("call pingpong's connect_upstream")
local conf = session.upstream_conf
local nodes = conf.nodes
if #nodes == 0 then
core.log.error("failed to connect: no nodes")
return DECLINED
end
local node = nodes[math_random(#nodes)]
core.log.info("connect to ", node.host, ":", node.port)
local sk = sdk.connect_upstream(node, conf)
if not sk then
return DECLINED
end
return OK, sk
end
function _M.disconnect_upstream(session, upstream)
-- disconnect upstream created by connect_upstream
sdk.disconnect_upstream(upstream, session.upstream_conf)
end
function _M.to_upstream(session, ctx, downstream, upstream)
-- send the request read from downstream to the upstream
-- return whether the request is sent
core.log.info("call pingpong's to_upstream")
local ok, err = upstream:move(downstream)
if not ok then
core.log.error("failed to send to upstream: ", err)
return DECLINED
end
if ctx.is_unary then
local p = read_data(upstream, ctx.len, false)
if p == nil then
return DECLINED
end
local ok, err = downstream:move(upstream)
if not ok then
core.log.error("failed to handle upstream: ", err)
return DECLINED
end
return DONE
end
return OK
end
function _M.from_upstream(session, downstream, upstream)
local p = read_data(upstream, HDR_LEN, false)
if p == nil then
return DECLINED
end
local p_b = str_byte("p")
if p[0] ~= p_b or p[1] ~= p_b then
core.log.error("invalid magic number: ", ffi_str(p, 2))
return DECLINED
end
local typ = p[2]
if typ == TYPE_HEARTBEAT then
core.log.info("send heartbeat")
-- need to reset read buf as we won't forward it
upstream:reset_read_buf()
upstream:send(ffi_str(p, HDR_LEN))
return DONE
end
local stream_id = p[3] * 256 + p[4]
local ctx = sdk.get_req_ctx(session, stream_id)
local body_len = to_int32(p, 6)
if ctx.len then
if body_len ~= ctx.len - HDR_LEN then
core.log.error("upstream body len mismatch, expected: ", ctx.len - HDR_LEN,
", actual: ", body_len)
return DECLINED
end
end
local p = read_data(upstream, body_len, true)
if p == nil then
return DECLINED
end
local ok, err = downstream:move(upstream)
if not ok then
core.log.error("failed to handle upstream: ", err)
return DECLINED
end
return DONE, ctx
end
function _M.log(session, ctx)
core.log.info("call pingpong's log, ctx unfinished: ", ctx.unfinished == true)
end
return _M

View File

@@ -0,0 +1,52 @@
--
-- 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.
--
local core = require("apisix.core")
local schema = {
type = "object",
properties = {
service = {
type = "string"
},
faults = {
type = "array",
minItems = 1,
items = {
type = "object",
properties = {
header_type = { type = "string" },
delay = {
type = "number",
description = "additional delay in seconds",
}
},
required = {"header_type"}
},
},
},
}
local _M = {}
function _M.check_schema(conf)
return core.schema.check(schema, conf)
end
return _M

View File

@@ -0,0 +1,168 @@
#
# 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');
}
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->extra_yaml_config) {
my $extra_yaml_config = <<_EOC_;
xrpc:
protocols:
- name: dubbo
_EOC_
$block->set_value("extra_yaml_config", $extra_yaml_config);
}
my $config = $block->config // <<_EOC_;
location /t {
content_by_lua_block {
ngx.req.read_body()
local sock = ngx.socket.tcp()
sock:settimeout(1000)
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(ngx.req.get_body_data())
if not bytes then
ngx.log(ngx.ERR, "send stream request error: ", err)
return ngx.exit(503)
end
while true do
local data, err = sock:receiveany(4096)
if not data then
sock:close()
break
end
ngx.print(data)
end
}
}
_EOC_
$block->set_value("config", $config);
if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]\nRPC is not finished");
}
$block;
});
worker_connections(1024);
run_tests;
__DATA__
=== TEST 1: init
--- 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,
{
protocol = {
name = "dubbo"
},
upstream = {
nodes = {
["127.0.0.1:20880"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 2: use dubbo_backend_provider server. request=org.apache.dubbo.backend.DemoService,service_version:1.0.1#hello,response=dubbo success & 200
--- request eval
"GET /t
\xda\xbb\xc2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xef\x05\x32\x2e\x30\x2e\x32\x30\x24\x6f\x72\x67\x2e\x61\x70\x61\x63\x68\x65\x2e\x64\x75\x62\x62\x6f\x2e\x62\x61\x63\x6b\x65\x6e\x64\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x05\x31\x2e\x30\x2e\x30\x05\x68\x65\x6c\x6c\x6f\x0f\x4c\x6a\x61\x76\x61\x2f\x75\x74\x69\x6c\x2f\x4d\x61\x70\x3b\x48\x04\x6e\x61\x6d\x65\x08\x7a\x68\x61\x6e\x67\x73\x61\x6e\x5a\x48\x04\x70\x61\x74\x68\x30\x24\x6f\x72\x67\x2e\x61\x70\x61\x63\x68\x65\x2e\x64\x75\x62\x62\x6f\x2e\x62\x61\x63\x6b\x65\x6e\x64\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x12\x72\x65\x6d\x6f\x74\x65\x2e\x61\x70\x70\x6c\x69\x63\x61\x74\x69\x6f\x6e\x0b\x73\x70\x2d\x63\x6f\x6e\x73\x75\x6d\x65\x72\x09\x69\x6e\x74\x65\x72\x66\x61\x63\x65\x30\x24\x6f\x72\x67\x2e\x61\x70\x61\x63\x68\x65\x2e\x64\x75\x62\x62\x6f\x2e\x62\x61\x63\x6b\x65\x6e\x64\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x07\x76\x65\x72\x73\x69\x6f\x6e\x05\x31\x2e\x30\x2e\x30\x07\x74\x69\x6d\x65\x6f\x75\x74\x04\x31\x30\x30\x30\x5a"
--- response_body eval
"\xda\xbb\x02\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x30\x94\x48\x04\x62\x6f\x64\x79\x0e\x64\x75\x62\x62\x6f\x20\x73\x75\x63\x63\x65\x73\x73\x0a\x06\x73\x74\x61\x74\x75\x73\x03\x32\x30\x30\x5a\x48\x05\x64\x75\x62\x62\x6f\x05\x32\x2e\x30\x2e\x32\x5a"
--- stream_conf_enable
--- log_level: debug
--- no_error_log
=== TEST 3: heart beat. request=\xe2|11..,response=\x22|00...
--- request eval
"GET /t
\xda\xbb\xe2\x00\x00\x00\x00\x00\x00\x00\x00\x34\x00\x00\x00\x01\x4e"
--- response_body eval
"\xda\xbb\x22\x14\x00\x00\x00\x00\x00\x00\x00\x34\x00\x00\x00\x01\x4e"
--- stream_conf_enable
--- log_level: debug
--- no_error_log
=== TEST 4: no response. Different from test2 \x82=10000010, the second bit=0 of the third byte means no need to return
--- request eval
"GET /t
\xda\xbb\x82\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xef\x05\x32\x2e\x30\x2e\x32\x30\x24\x6f\x72\x67\x2e\x61\x70\x61\x63\x68\x65\x2e\x64\x75\x62\x62\x6f\x2e\x62\x61\x63\x6b\x65\x6e\x64\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x05\x31\x2e\x30\x2e\x30\x05\x68\x65\x6c\x6c\x6f\x0f\x4c\x6a\x61\x76\x61\x2f\x75\x74\x69\x6c\x2f\x4d\x61\x70\x3b\x48\x04\x6e\x61\x6d\x65\x08\x7a\x68\x61\x6e\x67\x73\x61\x6e\x5a\x48\x04\x70\x61\x74\x68\x30\x24\x6f\x72\x67\x2e\x61\x70\x61\x63\x68\x65\x2e\x64\x75\x62\x62\x6f\x2e\x62\x61\x63\x6b\x65\x6e\x64\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x12\x72\x65\x6d\x6f\x74\x65\x2e\x61\x70\x70\x6c\x69\x63\x61\x74\x69\x6f\x6e\x0b\x73\x70\x2d\x63\x6f\x6e\x73\x75\x6d\x65\x72\x09\x69\x6e\x74\x65\x72\x66\x61\x63\x65\x30\x24\x6f\x72\x67\x2e\x61\x70\x61\x63\x68\x65\x2e\x64\x75\x62\x62\x6f\x2e\x62\x61\x63\x6b\x65\x6e\x64\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x07\x76\x65\x72\x73\x69\x6f\x6e\x05\x31\x2e\x30\x2e\x30\x07\x74\x69\x6d\x65\x6f\x75\x74\x04\x31\x30\x30\x30\x5a"
--- response_body eval
""
--- stream_conf_enable
--- log_level: debug
--- no_error_log
=== TEST 5: failed response. request=org.apache.dubbo.backend.DemoService,service_version:1.0.1#fail,response=503
--- request eval
"GET /t
\xda\xbb\xc2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xee\x05\x32\x2e\x30\x2e\x32\x30\x24\x6f\x72\x67\x2e\x61\x70\x61\x63\x68\x65\x2e\x64\x75\x62\x62\x6f\x2e\x62\x61\x63\x6b\x65\x6e\x64\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x05\x31\x2e\x30\x2e\x30\x04\x66\x61\x69\x6c\x0f\x4c\x6a\x61\x76\x61\x2f\x75\x74\x69\x6c\x2f\x4d\x61\x70\x3b\x48\x04\x6e\x61\x6d\x65\x08\x7a\x68\x61\x6e\x67\x73\x61\x6e\x5a\x48\x04\x70\x61\x74\x68\x30\x24\x6f\x72\x67\x2e\x61\x70\x61\x63\x68\x65\x2e\x64\x75\x62\x62\x6f\x2e\x62\x61\x63\x6b\x65\x6e\x64\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x12\x72\x65\x6d\x6f\x74\x65\x2e\x61\x70\x70\x6c\x69\x63\x61\x74\x69\x6f\x6e\x0b\x73\x70\x2d\x63\x6f\x6e\x73\x75\x6d\x65\x72\x09\x69\x6e\x74\x65\x72\x66\x61\x63\x65\x30\x24\x6f\x72\x67\x2e\x61\x70\x61\x63\x68\x65\x2e\x64\x75\x62\x62\x6f\x2e\x62\x61\x63\x6b\x65\x6e\x64\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x07\x76\x65\x72\x73\x69\x6f\x6e\x05\x31\x2e\x30\x2e\x30\x07\x74\x69\x6d\x65\x6f\x75\x74\x04\x31\x30\x30\x30\x5a"
--- response_body eval
"\xda\xbb\x02\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x2d\x94\x48\x04\x62\x6f\x64\x79\x0b\x64\x75\x62\x62\x6f\x20\x66\x61\x69\x6c\x0a\x06\x73\x74\x61\x74\x75\x73\x03\x35\x30\x33\x5a\x48\x05\x64\x75\x62\x62\x6f\x05\x32\x2e\x30\x2e\x32\x5a"
--- stream_conf_enable
--- log_level: debug
--- no_error_log
=== TEST 6: invalid magic(dabc<>dabb) for heart beat.
--- request eval
"GET /t
\xda\xbc\xe2\x00\x00\x00\x00\x00\x00\x00\x00\x34\x00\x00\x00\x01\x4e"
--- error_log
unknown magic number
--- stream_conf_enable

View File

@@ -0,0 +1,781 @@
#
# 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');
}
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->extra_yaml_config) {
my $extra_yaml_config = <<_EOC_;
xrpc:
protocols:
- name: pingpong
- name: redis
_EOC_
$block->set_value("extra_yaml_config", $extra_yaml_config);
}
my $config = $block->config // <<_EOC_;
location /t {
content_by_lua_block {
ngx.req.read_body()
local sock = ngx.socket.tcp()
sock:settimeout(1000)
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(ngx.req.get_body_data())
if not bytes then
ngx.log(ngx.ERR, "send stream request error: ", err)
return ngx.exit(503)
end
while true do
local data, err = sock:receiveany(4096)
if not data then
sock:close()
break
end
ngx.print(data)
end
}
}
_EOC_
$block->set_value("config", $config);
my $stream_upstream_code = $block->stream_upstream_code // <<_EOC_;
local sock = ngx.req.socket(true)
sock:settimeout(10)
while true do
local data = sock:receiveany(4096)
if not data then
return
end
sock:send(data)
end
_EOC_
$block->set_value("stream_upstream_code", $stream_upstream_code);
if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]\nRPC is not finished");
}
$block;
});
run_tests;
__DATA__
=== TEST 1: init
--- 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,
{
protocol = {
name = "pingpong"
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 2: too short
--- stream_request
mmm
--- error_log
call pingpong's init_worker
failed to read: timeout
=== TEST 3: reply directly
--- request eval
"POST /t
pp\x01\x00\x00\x00\x00\x00\x00\x00"
--- response_body eval
"pp\x01\x00\x00\x00\x00\x00\x00\x00"
--- stream_conf_enable
=== TEST 4: unary
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC" x 3
--- response_body eval
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC" x 3
--- log_level: debug
--- no_error_log
stream lua tcp socket set keepalive
--- stream_conf_enable
=== TEST 5: unary & heartbeat
--- request eval
"POST /t
" .
"pp\x01\x00\x00\x00\x00\x00\x00\x00" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- response_body eval
"pp\x01\x00\x00\x00\x00\x00\x00\x00" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- stream_conf_enable
=== TEST 6: can't connect to 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,
{
protocol = {
name = "pingpong"
},
upstream = {
nodes = {
["127.0.0.1:1979"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 7: hit
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC" x 3
--- error_log
failed to connect: connection refused
--- stream_conf_enable
=== TEST 8: use short timeout to check upstream's bad response
--- 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,
{
protocol = {
name = "pingpong"
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
timeout = {
connect = 0.01,
send = 0.009,
read = 0.008,
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 9: bad response
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC" x 1
--- stream_conf_enable
--- stream_upstream_code
local sock = ngx.req.socket(true)
sock:settimeout(10)
while true do
local data = sock:receiveany(4096)
if not data then
return
end
sock:send(data:sub(5))
end
--- error_log
failed to read: timeout
stream lua tcp socket connect timeout: 10
lua tcp socket send timeout: 9
stream lua tcp socket read timeout: 8
--- log_level: debug
=== TEST 10: reset
--- 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,
{
protocol = {
name = "pingpong"
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 11: client stream, N:N
--- request eval
"POST /t
" .
"pp\x03\x00\x01\x00\x00\x00\x00\x03ABC" .
"pp\x03\x00\x02\x00\x00\x00\x00\x04ABCD"
--- stream_conf_enable
--- stream_upstream_code
local sock = ngx.req.socket(true)
sock:settimeout(10)
local data1 = sock:receive(13)
if not data1 then
return
end
local data2 = sock:receive(14)
if not data2 then
return
end
assert(sock:send(data2))
assert(sock:send(data1))
--- response_body eval
"pp\x03\x00\x02\x00\x00\x00\x00\x04ABCD" .
"pp\x03\x00\x01\x00\x00\x00\x00\x03ABC"
=== TEST 12: client stream, bad response
--- request eval
"POST /t
" .
"pp\x03\x00\x01\x00\x00\x00\x00\x03ABC" .
"pp\x03\x00\x02\x00\x00\x00\x00\x04ABCD"
--- stream_conf_enable
--- stream_upstream_code
local sock = ngx.req.socket(true)
sock:settimeout(10)
local data1 = sock:receive(13)
if not data1 then
return
end
local data2 = sock:receive(14)
if not data2 then
return
end
assert(sock:send(data2))
assert(sock:send(data1:sub(11)))
--- response_body eval
"pp\x03\x00\x02\x00\x00\x00\x00\x04ABCD"
--- error_log
RPC is not finished
call pingpong's log, ctx unfinished: true
=== TEST 13: server stream, heartbeat
--- request eval
"POST /t
" .
"pp\x03\x00\x01\x00\x00\x00\x00\x03ABC"
--- stream_conf_enable
--- stream_upstream_code
local sock = ngx.req.socket(true)
sock:settimeout(10)
local data1 = sock:receive(13)
if not data1 then
return
end
local hb = "pp\x01\x00\x00\x00\x00\x00\x00\x00"
assert(sock:send(hb))
local data2 = sock:receive(10)
if not data2 then
return
end
assert(data2 == hb)
assert(sock:send(data1))
--- response_body eval
"pp\x03\x00\x01\x00\x00\x00\x00\x03ABC"
=== TEST 14: server stream
--- request eval
"POST /t
" .
"pp\x03\x00\x01\x00\x00\x00\x00\x01A"
--- stream_conf_enable
--- stream_upstream_code
local sock = ngx.req.socket(true)
sock:settimeout(10)
local data1 = sock:receive(11)
if not data1 then
return
end
assert(sock:send("pp\x03\x00\x03\x00\x00\x00\x00\x03ABC"))
assert(sock:send("pp\x03\x00\x02\x00\x00\x00\x00\x02AB"))
assert(sock:send(data1))
--- response_body eval
"pp\x03\x00\x03\x00\x00\x00\x00\x03ABC" .
"pp\x03\x00\x02\x00\x00\x00\x00\x02AB" .
"pp\x03\x00\x01\x00\x00\x00\x00\x01A"
--- grep_error_log eval
qr/call pingpong's log, ctx unfinished: \w+/
--- grep_error_log_out
call pingpong's log, ctx unfinished: false
call pingpong's log, ctx unfinished: false
call pingpong's log, ctx unfinished: false
=== TEST 15: superior & subordinate
--- 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,
{
protocol = {
name = "pingpong"
},
upstream = {
nodes = {
["127.0.0.3:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
local code, body = t('/apisix/admin/stream_routes/2',
ngx.HTTP_PUT,
{
protocol = {
superior_id = 1,
conf = {
service = "a"
},
name = "pingpong"
},
upstream = {
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/3',
ngx.HTTP_PUT,
{
protocol = {
superior_id = 1,
conf = {
service = "b"
},
name = "pingpong"
},
upstream = {
nodes = {
["127.0.0.2:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
-- routes below should not be used to matched
local code, body = t('/apisix/admin/stream_routes/4',
ngx.HTTP_PUT,
{
protocol = {
superior_id = 10000,
conf = {
service = "b"
},
name = "pingpong"
},
upstream = {
nodes = {
["127.0.0.2:1979"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
local code, body = t('/apisix/admin/stream_routes/5',
ngx.HTTP_PUT,
{
protocol = {
name = "redis"
},
upstream = {
nodes = {
["127.0.0.1:6379"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 16: hit
--- request eval
"POST /t
" .
"pp\x04\x00\x00\x00\x00\x00\x00\x03a\x00\x00\x00ABC" .
"pp\x04\x00\x00\x00\x00\x00\x00\x04b\x00\x00\x00ABCD" .
"pp\x04\x00\x00\x00\x00\x00\x00\x03a\x00\x00\x00ABC"
--- response_body eval
"pp\x04\x00\x00\x00\x00\x00\x00\x03a\x00\x00\x00ABC" .
"pp\x04\x00\x00\x00\x00\x00\x00\x04b\x00\x00\x00ABCD" .
"pp\x04\x00\x00\x00\x00\x00\x00\x03a\x00\x00\x00ABC"
--- grep_error_log eval
qr/connect to \S+ while prereading client data/
--- grep_error_log_out
connect to 127.0.0.1:1995 while prereading client data
connect to 127.0.0.2:1995 while prereading client data
--- stream_conf_enable
=== TEST 17: hit (fallback to superior if not found)
--- request eval
"POST /t
" .
"pp\x04\x00\x00\x00\x00\x00\x00\x03abcdABC" .
"pp\x04\x00\x00\x00\x00\x00\x00\x04a\x00\x00\x00ABCD" .
"pp\x04\x00\x00\x00\x00\x00\x00\x03abcdABC"
--- response_body eval
"pp\x04\x00\x00\x00\x00\x00\x00\x03abcdABC" .
"pp\x04\x00\x00\x00\x00\x00\x00\x04a\x00\x00\x00ABCD" .
"pp\x04\x00\x00\x00\x00\x00\x00\x03abcdABC"
--- grep_error_log eval
qr/connect to \S+ while prereading client data/
--- grep_error_log_out
connect to 127.0.0.3:1995 while prereading client data
connect to 127.0.0.1:1995 while prereading client data
--- stream_conf_enable
=== TEST 18: cache router by version
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local sock = ngx.socket.tcp()
sock:settimeout(1000)
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
assert(sock:send("pp\x04\x00\x00\x00\x00\x00\x00\x03a\x00\x00\x00ABC"))
ngx.sleep(0.1)
local code, body = t('/apisix/admin/stream_routes/2',
ngx.HTTP_PUT,
{
protocol = {
superior_id = 1,
conf = {
service = "c"
},
name = "pingpong"
},
upstream = {
nodes = {
["127.0.0.4:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
ngx.sleep(0.1)
local s = "pp\x04\x00\x00\x00\x00\x00\x00\x04a\x00\x00\x00ABCD"
assert(sock:send(s .. "pp\x04\x00\x00\x00\x00\x00\x00\x03c\x00\x00\x00ABC"))
while true do
local data, err = sock:receiveany(4096)
if not data then
sock:close()
break
end
ngx.print(data)
end
}
}
--- request
GET /t
--- response_body eval
"pp\x04\x00\x00\x00\x00\x00\x00\x03a\x00\x00\x00ABC" .
"pp\x04\x00\x00\x00\x00\x00\x00\x04a\x00\x00\x00ABCD" .
"pp\x04\x00\x00\x00\x00\x00\x00\x03c\x00\x00\x00ABC"
--- grep_error_log eval
qr/connect to \S+ while prereading client data/
--- grep_error_log_out
connect to 127.0.0.1:1995 while prereading client data
connect to 127.0.0.3:1995 while prereading client data
connect to 127.0.0.4:1995 while prereading client data
--- stream_conf_enable
=== TEST 19: use upstream_id
--- 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.3:1995"] = 1
},
type = "roundrobin"
}
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
local code, body = t('/apisix/admin/stream_routes/2',
ngx.HTTP_PUT,
{
protocol = {
superior_id = 1,
conf = {
service = "a"
},
name = "pingpong"
},
upstream_id = 1
}
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 20: hit
--- request eval
"POST /t
" .
"pp\x04\x00\x00\x00\x00\x00\x00\x03a\x00\x00\x00ABC"
--- response_body eval
"pp\x04\x00\x00\x00\x00\x00\x00\x03a\x00\x00\x00ABC"
--- grep_error_log eval
qr/connect to \S+ while prereading client data/
--- grep_error_log_out
connect to 127.0.0.3:1995 while prereading client data
--- stream_conf_enable
=== TEST 21: cache router by version, with upstream_id
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local sock = ngx.socket.tcp()
sock:settimeout(1000)
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
assert(sock:send("pp\x04\x00\x00\x00\x00\x00\x00\x03a\x00\x00\x00ABC"))
ngx.sleep(0.1)
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
ngx.sleep(0.1)
local s = "pp\x04\x00\x00\x00\x00\x00\x00\x04a\x00\x00\x00ABCD"
assert(sock:send(s))
while true do
local data, err = sock:receiveany(4096)
if not data then
sock:close()
break
end
ngx.print(data)
end
}
}
--- request
GET /t
--- response_body eval
"pp\x04\x00\x00\x00\x00\x00\x00\x03a\x00\x00\x00ABC" .
"pp\x04\x00\x00\x00\x00\x00\x00\x04a\x00\x00\x00ABCD"
--- grep_error_log eval
qr/connect to \S+ while prereading client data/
--- grep_error_log_out
connect to 127.0.0.3:1995 while prereading client data
connect to 127.0.0.1:1995 while prereading client data
--- stream_conf_enable

View File

@@ -0,0 +1,753 @@
#
# 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');
}
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->extra_yaml_config) {
my $extra_yaml_config = <<_EOC_;
xrpc:
protocols:
- name: pingpong
_EOC_
$block->set_value("extra_yaml_config", $extra_yaml_config);
}
my $config = $block->config // <<_EOC_;
location /t {
content_by_lua_block {
ngx.req.read_body()
local sock = ngx.socket.tcp()
sock:settimeout(1000)
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(ngx.req.get_body_data())
if not bytes then
ngx.log(ngx.ERR, "send stream request error: ", err)
return ngx.exit(503)
end
while true do
local data, err = sock:receiveany(4096)
if not data then
sock:close()
break
end
ngx.print(data)
end
}
}
_EOC_
$block->set_value("config", $config);
my $stream_upstream_code = $block->stream_upstream_code // <<_EOC_;
local sock = ngx.req.socket(true)
sock:settimeout(10)
while true do
local data = sock:receiveany(4096)
if not data then
return
end
sock:send(data)
end
_EOC_
$block->set_value("stream_upstream_code", $stream_upstream_code);
if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]\nRPC is not finished");
}
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);
}
$block;
});
run_tests;
__DATA__
=== TEST 1: init
--- 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,
{
protocol = {
name = "pingpong"
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 2: check the default timeout
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- response_body eval
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- error_log
stream lua tcp socket connect timeout: 60000
lua tcp socket send timeout: 60000
stream lua tcp socket read timeout: 60000
--- log_level: debug
--- stream_conf_enable
=== TEST 3: bad loggger filter
--- 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,
{
protocol = {
name = "pingpong",
logger = {
{
name = "syslog",
filter = {
{}
},
conf = {}
}
}
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 4: failed to validate the 'filter' expression
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- stream_conf_enable
--- error_log
failed to validate the 'filter' expression: rule too short
=== TEST 5: set loggger filter(single rule)
--- 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,
{
protocol = {
name = "pingpong",
logger = {
{
name = "syslog",
filter = {
{"rpc_len", ">", 10}
},
conf = {}
}
}
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 6: log filter matched successful
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- stream_conf_enable
--- error_log
log filter: syslog filter result: true
=== TEST 7: update loggger filter
--- 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,
{
protocol = {
name = "pingpong",
logger = {
{
name = "syslog",
filter = {
{"rpc_len", "<", 10}
},
conf = {}
}
}
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 8: failed to match log filter
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- stream_conf_enable
--- error_log
log filter: syslog filter result: false
=== TEST 9: set loggger filter(multiple rules)
--- 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,
{
protocol = {
name = "pingpong",
logger = {
{
name = "syslog",
filter = {
{"rpc_len", ">", 12},
{"rpc_len", "<", 14}
},
conf = {}
}
}
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 10: log filter matched successful
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- stream_conf_enable
--- error_log
log filter: syslog filter result: true
=== TEST 11: update loggger filter
--- 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,
{
protocol = {
name = "pingpong",
logger = {
{
name = "syslog",
filter = {
{"rpc_len", "<", 10},
{"rpc_len", ">", 12}
},
conf = {}
}
}
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 12: failed to match log filter
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- stream_conf_enable
--- error_log
log filter: syslog filter result: false
=== TEST 13: 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 14: no loggger filter, defaulte executed logger 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,
{
protocol = {
name = "pingpong",
logger = {
{
name = "syslog",
conf = {
host = "127.0.0.1",
port = 8125,
sock_type = "udp",
batch_max_size = 1,
flush_limit = 1
}
}
}
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 15: verify the data received by the log server
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- stream_conf_enable
--- wait: 0.5
--- error_log eval
qr/message received:.*\"client_ip\"\:\"127.0.0.1\"/
=== TEST 16: set loggger filter
--- 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,
{
protocol = {
name = "pingpong",
logger = {
{
name = "syslog",
filter = {
{"rpc_len", ">", 10}
},
conf = {
host = "127.0.0.1",
port = 8125,
sock_type = "udp",
batch_max_size = 1,
flush_limit = 1
}
}
}
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 17: verify the data received by the log server
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- stream_conf_enable
--- wait: 0.5
--- error_log eval
qr/message received:.*\"client_ip\"\:\"127.0.0.1\"/
=== TEST 18: 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,
{
protocol = {
name = "pingpong",
logger = {
{
name = "syslog",
filter = {
{"rpc_len", ">", 10}
},
conf = {
host = "127.0.0.1",
port = 5044,
batch_max_size = 1,
flush_limit = 1
}
}
}
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
-- wait etcd sync
ngx.sleep(0.5)
local sock = ngx.socket.tcp()
sock:settimeout(1000)
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
assert(sock:send("pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"))
while true do
local data, err = sock:receiveany(4096)
if not data then
sock:close()
break
end
ngx.print(data)
end
-- wait flush log
ngx.sleep(2.5)
}
}
--- request
GET /t
--- response_body eval
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- timeout: 5
--- error_log
try to lock with key xrpc-pingpong-logger#table
unlock with key xrpc-pingpong-logger#table
=== TEST 19: check plugin configuration updating
--- 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,
{
protocol = {
name = "pingpong",
logger = {
{
name = "syslog",
filter = {
{"rpc_len", ">", 10}
},
conf = {
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("pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"))
local body1, err
while true do
body1, err = sock:receiveany(4096)
if not data then
sock:close()
break
end
end
local code, body = t('/apisix/admin/stream_routes/1',
ngx.HTTP_PUT,
{
protocol = {
name = "pingpong",
logger = {
{
name = "syslog",
filter = {
{"rpc_len", ">", 10}
},
conf = {
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("pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"))
local body2, err
while true do
body2, err = sock:receiveany(4096)
if not data then
sock:close()
break
end
end
ngx.print(body1)
ngx.print(body2)
}
}
--- request
GET /t
--- wait: 0.5
--- response_body eval
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- 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

View File

@@ -0,0 +1,193 @@
#
# 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');
}
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->extra_yaml_config) {
my $extra_yaml_config = <<_EOC_;
xrpc:
protocols:
- name: pingpong
_EOC_
$block->set_value("extra_yaml_config", $extra_yaml_config);
}
my $config = $block->config // <<_EOC_;
location /t {
content_by_lua_block {
ngx.req.read_body()
local sock = ngx.socket.tcp()
sock:settimeout(1000)
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(ngx.req.get_body_data())
if not bytes then
ngx.log(ngx.ERR, "send stream request error: ", err)
return ngx.exit(503)
end
while true do
local data, err = sock:receiveany(4096)
if not data then
sock:close()
break
end
ngx.print(data)
end
}
}
_EOC_
$block->set_value("config", $config);
my $stream_upstream_code = $block->stream_upstream_code // <<_EOC_;
local sock = ngx.req.socket(true)
sock:settimeout(10)
while true do
local data = sock:receiveany(4096)
if not data then
return
end
sock:send(data)
end
_EOC_
$block->set_value("stream_upstream_code", $stream_upstream_code);
if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]\nRPC is not finished");
}
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);
}
$block;
});
run_tests;
__DATA__
=== TEST 1: 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": {
"rpc_time": "$rpc_time"
}
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 2: use vae rpc_time
--- 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,
{
protocol = {
name = "pingpong",
logger = {
{
name = "syslog",
filter = {
{"rpc_time", ">=", 0}
},
conf = {
host = "127.0.0.1",
port = 8125,
sock_type = "udp",
batch_max_size = 1,
flush_limit = 1
}
}
}
},
upstream = {
nodes = {
["127.0.0.1:1995"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 3: verify the data received by the log server
--- request eval
"POST /t
" .
"pp\x02\x00\x00\x00\x00\x00\x00\x03ABC"
--- stream_conf_enable
--- wait: 0.5
--- error_log eval
qr/message received:.*\"rpc_time\"\:(0.\d+|0)\}/

View File

@@ -0,0 +1,273 @@
#
# 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');
}
$ENV{TEST_NGINX_REDIS_PORT} ||= 1985;
add_block_preprocessor(sub {
my ($block) = @_;
if (!$block->extra_yaml_config) {
my $extra_yaml_config = <<_EOC_;
stream_plugins:
- prometheus
xrpc:
protocols:
- name: redis
_EOC_
$block->set_value("extra_yaml_config", $extra_yaml_config);
}
if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]\nRPC is not finished");
}
if (!defined $block->request) {
$block->set_value("request", "GET /t");
}
$block;
});
worker_connections(1024);
run_tests;
__DATA__
=== TEST 1: route with metrics
--- 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 = "/apisix/prometheus/metrics",
plugins = {
["public-api"] = {}
}
}
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
local code, body = t('/apisix/admin/stream_routes/1',
ngx.HTTP_PUT,
{
protocol = {
name = "redis",
conf = {
faults = {
{delay = 0.08, commands = {"hmset"}},
{delay = 0.3, commands = {"hmget"}},
}
},
metric = {
enable = true,
}
},
upstream = {
nodes = {
["127.0.0.1:6379"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 2: hit
--- 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
local res, err = red:hmget("animals", "dog", "cat")
if not res then
ngx.say("failed to get animals: ", err)
return
end
}
}
--- response_body
--- stream_conf_enable
=== TEST 3: check metrics
--- request
GET /apisix/prometheus/metrics
--- response_body eval
qr/apisix_redis_commands_latency_seconds_bucket\{route="1",command="hmget",le="0.5"\} 1/ and
qr/apisix_redis_commands_latency_seconds_bucket\{route="1",command="hmset",le="0.1"\} 1/ and
qr/apisix_redis_commands_total\{route="1",command="hmget"\} 1
apisix_redis_commands_total\{route="1",command="hmset"\} 1/
=== TEST 4: ignore metric if prometheus is disabled
--- 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
}
}
--- response_body
--- extra_yaml_config
stream_plugins:
- ip-restriction
xrpc:
protocols:
- name: redis
--- stream_conf_enable
=== TEST 5: check metrics
--- request
GET /apisix/prometheus/metrics
--- response_body eval
qr/apisix_redis_commands_total\{route="1",command="hmset"\} 1/
=== TEST 6: ignore metric if metric is disabled
--- 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,
{
protocol = {
name = "redis",
conf = {
faults = {
{delay = 0.08, commands = {"hmset"}},
{delay = 0.3, commands = {"hmget"}},
}
},
metric = {
enable = false
}
},
upstream = {
nodes = {
["127.0.0.1:6379"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 7: hit
--- 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
}
}
--- response_body
--- stream_conf_enable
=== TEST 8: check metrics
--- request
GET /apisix/prometheus/metrics
--- response_body eval
qr/apisix_redis_commands_total\{route="1",command="hmset"\} 1/

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;
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->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]\nRPC is not finished");
}
if (!defined $block->request) {
$block->set_value("request", "GET /t");
}
$block;
});
worker_connections(1024);
run_tests;
__DATA__
=== TEST 1: init
--- 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,
{
protocol = {
name = "redis"
},
upstream = {
nodes = {
["127.0.0.1:6379"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 2: sanity
--- 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)
local res, err = red:hget("animals", "dog")
if not res then
ngx.say("failed to get animals: ", err)
return
end
ngx.say("hget animals: ", res)
local res, err = red:hget("animals", "not_found")
if not res then
ngx.say("failed to get animals: ", err)
return
end
ngx.say("hget animals: ", res)
}
}
--- response_body
hmset animals: OK
hmget animals: barkmeow
hget animals: bark
hget animals: null
--- stream_conf_enable
=== TEST 3: error
--- 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:get("animals")
if not res then
ngx.say("failed to set animals: ", err)
end
local res, err = red:hget("animals", "dog")
if not res then
ngx.say("failed to get animals: ", err)
return
end
ngx.say("hget animals: ", res)
}
}
--- response_body
failed to set animals: WRONGTYPE Operation against a key holding the wrong kind of value
hget animals: bark
--- stream_conf_enable
=== TEST 4: big value
--- 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:set("big-key", ("\r\n"):rep(1024 * 1024 * 16))
if not res then
ngx.say("failed to set: ", err)
return
end
local res, err = red:get("big-key")
if not res then
ngx.say("failed to get: ", err)
return
end
ngx.print(res)
}
}
--- response_body eval
"\r\n" x 16777216
--- stream_conf_enable
=== TEST 5: pipeline
--- config
location /t {
content_by_lua_block {
local cjson = require("cjson")
local redis = require "resty.redis"
local t = {}
for i = 1, 180 do
local th = assert(ngx.thread.spawn(function(i)
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
red:init_pipeline()
red:set("mark_" .. i, i)
red:get("mark_" .. i)
red:get("counter")
for j = 1, 4 do
red:incr("counter")
end
local results, err = red:commit_pipeline()
if not results then
ngx.say("failed to commit: ", err)
return
end
local begin = tonumber(results[3])
for j = 1, 4 do
local incred = results[3 + j]
if incred ~= results[2 + j] + 1 then
ngx.log(ngx.ERR, cjson.encode(results))
end
end
end, i))
table.insert(t, th)
end
for i, th in ipairs(t) do
ngx.thread.wait(th)
end
}
}
--- response_body
--- stream_conf_enable
=== TEST 6: delay
--- 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,
{
protocol = {
name = "redis",
conf = {
faults = {
{delay = 0.01, key = "ignored", commands = {"Ping", "time"}}
}
}
},
upstream = {
nodes = {
["127.0.0.1:6379"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 7: hit
--- 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 start = ngx.now()
local res, err = red:ping()
if not res then
ngx.say(err)
return
end
local now = ngx.now()
-- use integer to bypass float point number precision problem
if math.ceil((now - start) * 1000) < 10 then
ngx.say(now, " ", start)
return
end
start = now
local res, err = red:time()
if not res then
ngx.say(err)
return
end
local now = ngx.now()
if math.ceil((now - start) * 1000) < 10 then
ngx.say(now, " ", start)
return
end
start = now
red:init_pipeline()
red:time()
red:time()
red:get("A")
local results, err = red:commit_pipeline()
if not results then
ngx.say("failed to commit: ", err)
return
end
local now = ngx.now()
if math.ceil((now - start) * 1000) < 20 or math.ceil((now - start) * 1000) > 30 then
ngx.say(now, " ", start)
return
end
ngx.say("ok")
}
}
--- response_body
ok
--- stream_conf_enable
=== TEST 8: DFS match
--- 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,
{
protocol = {
name = "redis",
conf = {
faults = {
{delay = 0.02, key = "a", commands = {"get"}},
{delay = 0.01, commands = {"get", "set"}},
}
}
},
upstream = {
nodes = {
["127.0.0.1:6379"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 9: hit
--- 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 start = ngx.now()
local res, err = red:get("a")
if not res then
ngx.say(err)
return
end
local now = ngx.now()
if math.ceil((now - start) * 1000) < 20 then
ngx.say(now, " ", start)
return
end
start = now
local res, err = red:set("a", "a")
if not res then
ngx.say(err)
return
end
local now = ngx.now()
if math.ceil((now - start) * 1000) < 10 then
ngx.say(now, " ", start)
return
end
start = now
red:init_pipeline()
red:get("b")
red:set("A", "a")
local results, err = red:commit_pipeline()
if not results then
ngx.say("failed to commit: ", err)
return
end
local now = ngx.now()
if math.ceil((now - start) * 1000) < 20 or math.ceil((now - start) * 1000) > 30 then
ngx.say(now, " ", start)
return
end
ngx.say("ok")
}
}
--- response_body
ok
--- stream_conf_enable
=== TEST 10: multi keys
--- 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,
{
protocol = {
name = "redis",
conf = {
faults = {
{delay = 0.06, key = "b", commands = {"del"}},
{delay = 0.04, key = "a", commands = {"mset"}},
{delay = 0.02, key = "b", commands = {"mset"}},
}
}
},
upstream = {
nodes = {
["127.0.0.1:6379"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 11: hit
--- 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 start = ngx.now()
local res, err = red:mset("c", 1, "a", 2, "b", 3)
if not res then
ngx.say(err)
return
end
local now = ngx.now()
if math.ceil((now - start) * 1000) < 40 then
ngx.say("mset a ", now, " ", start)
return
end
start = now
local res, err = red:mset("b", 2, "a", 3)
if not res then
ngx.say(err)
return
end
local now = ngx.now()
if math.ceil((now - start) * 1000) < 20 or math.ceil((now - start) * 1000) > 35 then
ngx.say("mset b ", now, " ", start)
return
end
start = now
local res, err = red:mset("c", "a")
if not res then
ngx.say(err)
return
end
local now = ngx.now()
if math.ceil((now - start) * 1000) > 20 then
ngx.say("mset mismatch ", now, " ", start)
return
end
start = now
local res, err = red:del("a", "b")
if not res then
ngx.say(err)
return
end
local now = ngx.now()
if math.ceil((now - start) * 1000) < 60 then
ngx.say("del b ", now, " ", start)
return
end
start = now
ngx.say("ok")
}
}
--- response_body
ok
--- stream_conf_enable
=== TEST 12: publish & subscribe
--- stream_extra_init_by_lua
local cjson = require "cjson"
local redis_proto = require("apisix.stream.xrpc.protocols.redis")
redis_proto.log = function(sess, ctx)
ngx.log(ngx.WARN, "log redis request ", cjson.encode(ctx.cmd_line))
end
--- config
location /t {
content_by_lua_block {
local cjson = require "cjson"
local redis = require "resty.redis"
local red = redis:new()
local red2 = redis:new()
red:set_timeout(1000) -- 1 sec
red2:set_timeout(1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)
if not ok then
ngx.say("1: failed to connect: ", err)
return
end
ok, err = red2:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)
if not ok then
ngx.say("2: failed to connect: ", err)
return
end
local res, err = red:subscribe("dog")
if not res then
ngx.say("1: failed to subscribe: ", err)
return
end
ngx.say("1: subscribe dog: ", cjson.encode(res))
res, err = red:subscribe("cat")
if not res then
ngx.say("1: failed to subscribe: ", err)
return
end
ngx.say("1: subscribe cat: ", cjson.encode(res))
res, err = red2:publish("dog", "Hello")
if not res then
ngx.say("2: failed to publish: ", err)
return
end
ngx.say("2: publish: ", cjson.encode(res))
res, err = red:read_reply()
if not res then
ngx.say("1: failed to read reply: ", err)
else
ngx.say("1: receive: ", cjson.encode(res))
end
red:set_timeout(10) -- 10ms
res, err = red:read_reply()
if not res then
ngx.say("1: failed to read reply: ", err)
else
ngx.say("1: receive: ", cjson.encode(res))
end
red:set_timeout(1000) -- 1s
res, err = red:unsubscribe()
if not res then
ngx.say("1: failed to unscribe: ", err)
else
ngx.say("1: unsubscribe: ", cjson.encode(res))
end
res, err = red:read_reply()
if not res then
ngx.say("1: failed to read reply: ", err)
else
ngx.say("1: receive: ", cjson.encode(res))
end
red:set_timeout(10) -- 10ms
res, err = red:read_reply()
if not res then
ngx.say("1: failed to read reply: ", err)
else
ngx.say("1: receive: ", cjson.encode(res))
end
red:set_timeout(1000) -- 1s
res, err = red:set("dog", 1)
if not res then
ngx.say("1: failed to set: ", err)
else
ngx.say("1: receive: ", cjson.encode(res))
end
red:close()
red2:close()
}
}
--- response_body_like chop
^1: subscribe dog: \["subscribe","dog",1\]
1: subscribe cat: \["subscribe","cat",2\]
2: publish: 1
1: receive: \["message","dog","Hello"\]
1: failed to read reply: timeout
1: unsubscribe: \[\["unsubscribe","(?:dog|cat)",1\],\["unsubscribe","(?:dog|cat)",0\]\]
1: failed to read reply: not subscribed
1: failed to read reply: not subscribed
1: receive: "OK"
$
--- stream_conf_enable
--- grep_error_log eval
qr/log redis request \[[^]]+\]/
--- grep_error_log_out
log redis request ["subscribe","dog"]
log redis request ["subscribe","cat"]
log redis request ["publish","dog","Hello"]
log redis request ["unsubscribe"]
log redis request ["set","dog","1"]
=== TEST 13: psubscribe & punsubscribe
--- stream_extra_init_by_lua
local cjson = require "cjson"
local redis_proto = require("apisix.stream.xrpc.protocols.redis")
redis_proto.log = function(sess, ctx)
ngx.log(ngx.WARN, "log redis request ", cjson.encode(ctx.cmd_line))
end
--- config
location /t {
content_by_lua_block {
local cjson = require "cjson"
local redis = require "resty.redis"
local red = redis:new()
local red2 = redis:new()
red:set_timeout(1000) -- 1 sec
red2:set_timeout(1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)
if not ok then
ngx.say("1: failed to connect: ", err)
return
end
ok, err = red2:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)
if not ok then
ngx.say("2: failed to connect: ", err)
return
end
local res, err = red:psubscribe("dog*", "cat*")
if not res then
ngx.say("1: failed to subscribe: ", err)
return
end
ngx.say("1: psubscribe: ", cjson.encode(res))
res, err = red2:publish("dog1", "Hello")
if not res then
ngx.say("2: failed to publish: ", err)
return
end
ngx.say("2: publish: ", cjson.encode(res))
res, err = red:read_reply()
if not res then
ngx.say("1: failed to read reply: ", err)
else
ngx.say("1: receive: ", cjson.encode(res))
end
res, err = red:punsubscribe("cat*", "dog*")
if not res then
ngx.say("1: failed to unscribe: ", err)
else
ngx.say("1: punsubscribe: ", cjson.encode(res))
end
res, err = red:set("dog", 1)
if not res then
ngx.say("1: failed to set: ", err)
else
ngx.say("1: receive: ", cjson.encode(res))
end
red:close()
red2:close()
}
}
--- response_body_like chop
^1: psubscribe: \[\["psubscribe","dog\*",1\],\["psubscribe","cat\*",2\]\]
2: publish: 1
1: receive: \["pmessage","dog\*","dog1","Hello"\]
1: punsubscribe: \[\["punsubscribe","cat\*",1\],\["punsubscribe","dog\*",0\]\]
1: receive: "OK"
$
--- stream_conf_enable
--- grep_error_log eval
qr/log redis request \[[^]]+\]/
--- grep_error_log_out
log redis request ["psubscribe","dog*","cat*"]
log redis request ["publish","dog1","Hello"]
log redis request ["punsubscribe","cat*","dog*"]
log redis request ["set","dog","1"]

View File

@@ -0,0 +1,202 @@
#
# 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;
log_level("warn");
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->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]\nRPC is not finished");
}
if (!defined $block->request) {
$block->set_value("request", "GET /t");
}
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);
}
$block;
});
worker_connections(1024);
run_tests;
__DATA__
=== TEST 1: 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": {
"rpc_time": "$rpc_time",
"redis_cmd_line": "$redis_cmd_line"
}
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 2: use register vars(redis_cmd_line and rpc_time) in logger
--- 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,
{
protocol = {
name = "redis",
conf = {
faults = {
{delay = 0.01, commands = {"hmset", "hmget", "ping"}},
}
},
logger = {
{
name = "syslog",
filter = {
{"rpc_time", ">=", 0.001},
},
conf = {
host = "127.0.0.1",
port = 8125,
sock_type = "udp",
batch_max_size = 1,
flush_limit = 1
}
}
}
},
upstream = {
nodes = {
["127.0.0.1:6379"] = 1
},
type = "roundrobin"
}
}
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 3: verify the data received by the log server
--- stream_conf_enable
--- 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)
-- test for only one command in cmd_line
local res, err = red:ping()
if not res then
ngx.say(err)
return
end
ngx.say("ping: ", string.lower(res))
}
}
--- response_body
hmset animals: OK
hmget animals: barkmeow
ping: pong
--- wait: 1
--- grep_error_log eval
qr/message received:.*\"redis_cmd_line\":[^}|^,]+/
--- grep_error_log_out eval
qr{message received:.*\"redis_cmd_line\":\"hmset animals dog bark cat meow\"(?s).*
message received:.*\"redis_cmd_line\":\"hmget animals dog cat\"(?s).*
message received:.*\"redis_cmd_line\":\"ping\"}