mirror of
https://gitee.com/iresty/apisix.git
synced 2024-12-15 17:31:45 +08:00
c3d61fcfa3
* feat: add control api for plugin server-info Previously we added plugin server-info without exposing API since the same port problem of admin api and proxy. Now we have Control API support so we expose server-info API through Control API. People now can insight the server info about APISIX instance easily.
184 lines
4.0 KiB
Perl
184 lines
4.0 KiB
Perl
#
|
|
# 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.
|
|
#
|
|
|
|
our $SkipReason;
|
|
|
|
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 Test::Nginx::Socket::Lua $SkipReason ? (skip_all => $SkipReason) : ();
|
|
use t::APISIX 'no_plan';
|
|
|
|
master_on();
|
|
repeat_each(1);
|
|
no_long_string();
|
|
no_root_location();
|
|
no_shuffle();
|
|
|
|
run_tests;
|
|
|
|
__DATA__
|
|
|
|
=== TEST 1: sanity check
|
|
--- yaml_config
|
|
apisix:
|
|
id: 123456
|
|
plugins:
|
|
- server-info
|
|
plugin_attr:
|
|
server-info:
|
|
report_interval: 60
|
|
--- config
|
|
location /t {
|
|
content_by_lua_block {
|
|
ngx.sleep(2)
|
|
local core = require("apisix.core")
|
|
local key = "/data_plane/server_info/" .. core.id.get()
|
|
local res, err = core.etcd.get(key)
|
|
if err ~= nil then
|
|
ngx.status = 500
|
|
ngx.say(err)
|
|
return
|
|
end
|
|
|
|
local keys = {}
|
|
local value = res.body.node.value
|
|
for k in pairs(value) do
|
|
keys[#keys + 1] = k
|
|
end
|
|
|
|
table.sort(keys)
|
|
for i = 1, #keys do
|
|
ngx.say(keys[i], ": ", value[keys[i]])
|
|
end
|
|
}
|
|
}
|
|
--- request
|
|
GET /t
|
|
--- response_body eval
|
|
qr{^boot_time: \d+
|
|
etcd_version: [\d\.]+
|
|
hostname: [a-zA-Z\-0-9\.]+
|
|
id: [a-zA-Z\-0-9]+
|
|
last_report_time: \d+
|
|
up_time: \d+
|
|
version: [\d\.]+
|
|
$}
|
|
--- no_error_log
|
|
[error]
|
|
--- error_log
|
|
timer created to report server info, interval: 60
|
|
|
|
|
|
|
|
=== TEST 2: verify the data integrity after reloading
|
|
--- yaml_config
|
|
apisix:
|
|
id: 123456
|
|
plugins:
|
|
- server-info
|
|
plugin_attr:
|
|
server-info:
|
|
report_interval: 60
|
|
--- config
|
|
location /t {
|
|
content_by_lua_block {
|
|
local core = require("apisix.core")
|
|
local key = "/data_plane/server_info/" .. core.id.get()
|
|
local res, err = core.etcd.get(key)
|
|
if err ~= nil then
|
|
ngx.status = 500
|
|
ngx.say(err)
|
|
return
|
|
end
|
|
|
|
local keys = {}
|
|
local value = res.body.node.value
|
|
for k in pairs(value) do
|
|
keys[#keys + 1] = k
|
|
end
|
|
|
|
if value.up_time >= 2 then
|
|
ngx.say("integral")
|
|
else
|
|
ngx.say("reset")
|
|
end
|
|
}
|
|
}
|
|
--- request
|
|
GET /t
|
|
--- response_body
|
|
integral
|
|
--- no_error_log
|
|
[error]
|
|
--- error_log
|
|
timer created to report server info, interval: 60
|
|
|
|
|
|
|
|
=== TEST 3: get server_info from plugin control API
|
|
--- yaml_config
|
|
apisix:
|
|
id: 123456
|
|
plugins:
|
|
- server-info
|
|
--- config
|
|
location /t {
|
|
content_by_lua_block {
|
|
local json_decode = require("apisix.core").json.decode
|
|
local t = require("lib.test_admin").test
|
|
local code, _, body = t("/v1/server_info")
|
|
if code >= 300 then
|
|
ngx.status = code
|
|
end
|
|
|
|
local keys = {}
|
|
local value, err = json_decode(body)
|
|
if not value then
|
|
ngx.say(err)
|
|
return
|
|
end
|
|
for k in pairs(value) do
|
|
keys[#keys + 1] = k
|
|
end
|
|
|
|
table.sort(keys)
|
|
for i = 1, #keys do
|
|
ngx.say(keys[i], ": ", value[keys[i]])
|
|
end
|
|
}
|
|
}
|
|
--- request
|
|
GET /t
|
|
--- response_body eval
|
|
qr{^boot_time: \d+
|
|
etcd_version: [\d\.]+
|
|
hostname: [a-zA-Z\-0-9\.]+
|
|
id: [a-zA-Z\-0-9]+
|
|
last_report_time: \d+
|
|
up_time: \d+
|
|
version: [\d\.]+
|
|
$}
|
|
--- no_error_log
|
|
[error]
|