From ee9fcf3df37446f7007bfff4f09054f6432fe447 Mon Sep 17 00:00:00 2001 From: mango Date: Wed, 19 Jan 2022 23:28:25 +0800 Subject: [PATCH 1/7] fix(admin): correct the count field of plugin-metadata --- apisix/admin/plugin_metadata.lua | 1 + t/admin/plugin-metadata2.t | 63 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 t/admin/plugin-metadata2.t diff --git a/apisix/admin/plugin_metadata.lua b/apisix/admin/plugin_metadata.lua index 5cb4dd4dfbd5..cb5524e87e38 100644 --- a/apisix/admin/plugin_metadata.lua +++ b/apisix/admin/plugin_metadata.lua @@ -119,6 +119,7 @@ function _M.get(key) return 503, {error_msg = err} end + utils.fix_count(res.body, key) return res.status, res.body end diff --git a/t/admin/plugin-metadata2.t b/t/admin/plugin-metadata2.t new file mode 100644 index 000000000000..def0ba222b68 --- /dev/null +++ b/t/admin/plugin-metadata2.t @@ -0,0 +1,63 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +repeat_each(1); +no_long_string(); +no_root_location(); +no_shuffle(); +log_level("info"); + +add_block_preprocessor(sub { + my ($block) = @_; + + if (!$block->request) { + $block->set_value("request", "GET /t"); + } + + if (!$block->no_error_log && !$block->error_log) { + $block->set_value("no_error_log", "[error]\n[alert]"); + } +}); + +run_tests; + +__DATA__ + +=== TEST 1: list empty resources +--- config + location /t { + content_by_lua_block { + local json = require("toolkit.json") + local t = require("lib.test_admin").test + + local code, message, res = t('/apisix/admin/plugin_metadata', + ngx.HTTP_GET + ) + + if code >= 300 then + ngx.status = code + ngx.say(message) + return + end + + res = json.decode(res) + ngx.say(json.encode(res)) + } + } +--- response_body +{"action":"get","count":0,"node":{"dir":true,"key":"/apisix/plugin_metadata","nodes":{}}} From 3e40d8201f0f7f3f5f34ffdfe0957c268adb8a27 Mon Sep 17 00:00:00 2001 From: mango Date: Wed, 19 Jan 2022 23:42:32 +0800 Subject: [PATCH 2/7] require apisix.admin.utils --- apisix/admin/plugin_metadata.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/apisix/admin/plugin_metadata.lua b/apisix/admin/plugin_metadata.lua index cb5524e87e38..f2620fc9e279 100644 --- a/apisix/admin/plugin_metadata.lua +++ b/apisix/admin/plugin_metadata.lua @@ -18,6 +18,7 @@ local error = error local pcall = pcall local require = require local core = require("apisix.core") +local utils = require("apisix.admin.utils") local api_router = require("apisix.api_router") local injected_mark = "injected metadata_schema" From 27a077939a5ed65a069d162dfc2b46ae0a0cb393 Mon Sep 17 00:00:00 2001 From: mango Date: Thu, 20 Jan 2022 10:45:05 +0800 Subject: [PATCH 3/7] correct the count field of gloabl_rules --- apisix/admin/global_rules.lua | 1 + t/admin/global-rules2.t | 63 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 t/admin/global-rules2.t diff --git a/apisix/admin/global_rules.lua b/apisix/admin/global_rules.lua index caccc911996e..d4991be07310 100644 --- a/apisix/admin/global_rules.lua +++ b/apisix/admin/global_rules.lua @@ -96,6 +96,7 @@ function _M.get(id) return 503, {error_msg = err} end + utils.fix_count(res.body, id) return res.status, res.body end diff --git a/t/admin/global-rules2.t b/t/admin/global-rules2.t new file mode 100644 index 000000000000..a29ec2e05931 --- /dev/null +++ b/t/admin/global-rules2.t @@ -0,0 +1,63 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +repeat_each(1); +no_long_string(); +no_root_location(); +no_shuffle(); +log_level("info"); + +add_block_preprocessor(sub { + my ($block) = @_; + + if (!$block->request) { + $block->set_value("request", "GET /t"); + } + + if (!$block->no_error_log && !$block->error_log) { + $block->set_value("no_error_log", "[error]\n[alert]"); + } +}); + +run_tests; + +__DATA__ + +=== TEST 1: list empty resources +--- config + location /t { + content_by_lua_block { + local json = require("toolkit.json") + local t = require("lib.test_admin").test + + local code, message, res = t('/apisix/admin/global_rules', + ngx.HTTP_GET + ) + + if code >= 300 then + ngx.status = code + ngx.say(message) + return + end + + res = json.decode(res) + ngx.say(json.encode(res)) + } + } +--- response_body +{"action":"get","count":0,"node":{"dir":true,"key":"/apisix/global_rules","nodes":{}}} From dfdffdfc36ca72efeec2a69e355d7805f272d075 Mon Sep 17 00:00:00 2001 From: mango <35127166+mangoGoForward@users.noreply.github.com> Date: Thu, 20 Jan 2022 12:38:21 +0800 Subject: [PATCH 4/7] correct the count filed of global_rules --- apisix/admin/global_rules.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/apisix/admin/global_rules.lua b/apisix/admin/global_rules.lua index caccc911996e..d4991be07310 100644 --- a/apisix/admin/global_rules.lua +++ b/apisix/admin/global_rules.lua @@ -96,6 +96,7 @@ function _M.get(id) return 503, {error_msg = err} end + utils.fix_count(res.body, id) return res.status, res.body end From fd132efa97e7a31a5da27629136d57670995711b Mon Sep 17 00:00:00 2001 From: mango <35127166+mangoGoForward@users.noreply.github.com> Date: Thu, 20 Jan 2022 12:40:51 +0800 Subject: [PATCH 5/7] add test case --- t/admin/global-rules2.t | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 t/admin/global-rules2.t diff --git a/t/admin/global-rules2.t b/t/admin/global-rules2.t new file mode 100644 index 000000000000..a29ec2e05931 --- /dev/null +++ b/t/admin/global-rules2.t @@ -0,0 +1,63 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +repeat_each(1); +no_long_string(); +no_root_location(); +no_shuffle(); +log_level("info"); + +add_block_preprocessor(sub { + my ($block) = @_; + + if (!$block->request) { + $block->set_value("request", "GET /t"); + } + + if (!$block->no_error_log && !$block->error_log) { + $block->set_value("no_error_log", "[error]\n[alert]"); + } +}); + +run_tests; + +__DATA__ + +=== TEST 1: list empty resources +--- config + location /t { + content_by_lua_block { + local json = require("toolkit.json") + local t = require("lib.test_admin").test + + local code, message, res = t('/apisix/admin/global_rules', + ngx.HTTP_GET + ) + + if code >= 300 then + ngx.status = code + ngx.say(message) + return + end + + res = json.decode(res) + ngx.say(json.encode(res)) + } + } +--- response_body +{"action":"get","count":0,"node":{"dir":true,"key":"/apisix/global_rules","nodes":{}}} From b94a86c87a62195fc21dceaf9d84efc0da911b32 Mon Sep 17 00:00:00 2001 From: mango Date: Thu, 20 Jan 2022 17:58:08 +0800 Subject: [PATCH 6/7] add some test cases for non-empty resources --- t/admin/global-rules2.t | 88 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/t/admin/global-rules2.t b/t/admin/global-rules2.t index a29ec2e05931..ff2d8c07cbc6 100644 --- a/t/admin/global-rules2.t +++ b/t/admin/global-rules2.t @@ -61,3 +61,91 @@ __DATA__ } --- response_body {"action":"get","count":0,"node":{"dir":true,"key":"/apisix/global_rules","nodes":{}}} + + + +=== TEST 2: set global rule +--- config + location /t { + content_by_lua_block { + local json = require("toolkit.json") + local t = require("lib.test_admin").test + local code, message, res = t('/apisix/admin/global_rules/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "proxy-rewrite": { + "uri": "/" + } + } + }]] + ) + + if code >= 300 then + ngx.status = code + ngx.say(message) + return + end + + res = json.decode(res) + res.node.value.create_time = nil + res.node.value.update_time = nil + ngx.say(json.encode(res)) + } + } +--- response_body +{"action":"set","node":{"key":"/apisix/global_rules/1","value":{"id":"1","plugins":{"proxy-rewrite":{"uri":"/"}}}}} +--- request +GET /t +--- no_error_log +[error] + + + +=== TEST 3: list global rules +--- config + location /t { + content_by_lua_block { + local json = require("toolkit.json") + local t = require("lib.test_admin").test + + local code, message, res = t('/apisix/admin/global_rules', + ngx.HTTP_GET + ) + + if code >= 300 then + ngx.status = code + ngx.say(message) + return + end + + res = json.decode(res) + ngx.say(json.encode(res)) + } + } +--- response_body_like +{"action":"get","count":1,"node":\{"dir":true,"key":"/apisix/global_rules","nodes":.* + + + +=== TEST 4: delete global rules +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, message = t('/apisix/admin/global_rules/1', + ngx.HTTP_DELETE, + nil, + [[{ + "action": "delete" + }]] + ) + ngx.say("[delete] code: ", code, " message: ", message) + } + } +--- request +GET /t +--- response_body +[delete] code: 200 message: passed +--- no_error_log +[error] From 4a6b90fb4e088eae4bcbb0f1202546ace3df1967 Mon Sep 17 00:00:00 2001 From: mango Date: Fri, 21 Jan 2022 11:01:32 +0800 Subject: [PATCH 7/7] remove repeat request and error_log --- t/admin/global-rules2.t | 8 -------- 1 file changed, 8 deletions(-) diff --git a/t/admin/global-rules2.t b/t/admin/global-rules2.t index ff2d8c07cbc6..3835dcc0f107 100644 --- a/t/admin/global-rules2.t +++ b/t/admin/global-rules2.t @@ -95,10 +95,6 @@ __DATA__ } --- response_body {"action":"set","node":{"key":"/apisix/global_rules/1","value":{"id":"1","plugins":{"proxy-rewrite":{"uri":"/"}}}}} ---- request -GET /t ---- no_error_log -[error] @@ -143,9 +139,5 @@ GET /t ngx.say("[delete] code: ", code, " message: ", message) } } ---- request -GET /t --- response_body [delete] code: 200 message: passed ---- no_error_log -[error]