From 2b510b84397eea4629a465159b22c05018ed807b Mon Sep 17 00:00:00 2001 From: "xin.gao" Date: Fri, 9 Jul 2021 15:19:29 +0800 Subject: [PATCH 01/11] =?UTF-8?q?admin=20api=20proto=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apisix/admin/proto.lua | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/apisix/admin/proto.lua b/apisix/admin/proto.lua index f7f7a912210f..03bbb911a28b 100644 --- a/apisix/admin/proto.lua +++ b/apisix/admin/proto.lua @@ -117,25 +117,31 @@ function _M.post(id, conf) return res.status, res.body end -function _M.check_proto_used(plugins, deleting, ptype, pid) +local function check_proto_used(plugins, deleting, ptype, pid) - core.log.info("plugins1: ", core.json.delay_encode(plugins, true)) + core.log.info("check_proto_used plugins: ", core.json.delay_encode(plugins, true)) + core.log.info("check_proto_used deleting: ", deleting) + core.log.info("check_proto_used ptype: ", ptype) + core.log.info("check_proto_used pid: ", pid) if plugins then if type(plugins) == "table" and plugins["grpc-transcode"] and plugins["grpc-transcode"].proto_id and tostring(plugins["grpc-transcode"].proto_id) == deleting then - return 400, {error_msg = "can not delete this proto," + --core.log.info("check_proto_used 此协议正在使用: ", core.json.delay_encode(plugins, true)) + return false, {error_msg = "can not delete this proto," .. ptype .. " [" .. pid .. "] is still using it now"} end end + return true,"ok" end function _M.delete(id) if not id then return 400, {error_msg = "missing proto id"} end + core.log.info("proto delete: ", id) local routes, routes_ver = get_routes() @@ -144,13 +150,16 @@ function _M.delete(id) if routes_ver and routes then for _, route in ipairs(routes) do - if type(route) == "table" and route.value - and route.value.plugins then - return _M.check_proto_used(route.value.plugins, id, "route", - route.value.id) + core.log.info("proto delete route item: ", core.json.delay_encode(route, true)) + if type(route) == "table" and route.value and route.value.plugins then + local ret, err = check_proto_used(route.value.plugins, id, "route",route.value.id) + if not ret then + return 400, err + end end end end + core.log.info("proto delete route ref check pass: ", id) local services, services_ver = get_services() @@ -159,13 +168,15 @@ function _M.delete(id) if services_ver and services then for _, service in ipairs(services) do - if type(service) == "table" and service.value - and service.value.plugins then - return _M.check_proto_used(service.value.plugins, id, - "service", service.value.id) + if type(service) == "table" and service.value and service.value.plugins then + local ret, err = check_proto_used(service.value.plugins, id, "service", service.value.id) + if not ret then + return 400, err + end end end end + core.log.info("proto delete service ref check pass: ", id) local key = "/proto/" .. id -- core.log.info("key: ", key) From 7ef9ae41d6777d70ea527364e1658db7c715feb6 Mon Sep 17 00:00:00 2001 From: "xin.gao" Date: Mon, 12 Jul 2021 11:33:28 +0800 Subject: [PATCH 02/11] fix: ref check while deleting proto via Admin api --- apisix/admin/proto.lua | 18 ++++---- t/admin/proto3.t | 99 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 t/admin/proto3.t diff --git a/apisix/admin/proto.lua b/apisix/admin/proto.lua index 03bbb911a28b..eac10002cfb4 100644 --- a/apisix/admin/proto.lua +++ b/apisix/admin/proto.lua @@ -119,16 +119,16 @@ end local function check_proto_used(plugins, deleting, ptype, pid) - core.log.info("check_proto_used plugins: ", core.json.delay_encode(plugins, true)) - core.log.info("check_proto_used deleting: ", deleting) - core.log.info("check_proto_used ptype: ", ptype) - core.log.info("check_proto_used pid: ", pid) + --core.log.info("check_proto_used plugins: ", core.json.delay_encode(plugins, true)) + --core.log.info("check_proto_used deleting: ", deleting) + --core.log.info("check_proto_used ptype: ", ptype) + --core.log.info("check_proto_used pid: ", pid) if plugins then if type(plugins) == "table" and plugins["grpc-transcode"] and plugins["grpc-transcode"].proto_id and tostring(plugins["grpc-transcode"].proto_id) == deleting then - --core.log.info("check_proto_used 此协议正在使用: ", core.json.delay_encode(plugins, true)) + -- core.log.info("check_proto_used proto is used: ", core.json.delay_encode(plugins, true)) return false, {error_msg = "can not delete this proto," .. ptype .. " [" .. pid .. "] is still using it now"} @@ -152,10 +152,10 @@ function _M.delete(id) for _, route in ipairs(routes) do core.log.info("proto delete route item: ", core.json.delay_encode(route, true)) if type(route) == "table" and route.value and route.value.plugins then - local ret, err = check_proto_used(route.value.plugins, id, "route",route.value.id) - if not ret then - return 400, err - end + local ret, err = check_proto_used(route.value.plugins, id, "route",route.value.id) + if not ret then + return 400, err + end end end end diff --git a/t/admin/proto3.t b/t/admin/proto3.t new file mode 100644 index 000000000000..6f369a1e58ae --- /dev/null +++ b/t/admin/proto3.t @@ -0,0 +1,99 @@ +# +# 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"); + +run_tests; + +__DATA__ + +=== TEST 1: post proto + delete +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local etcd = require("apisix.core.etcd") + local code, message, res = t('/apisix/admin/proto', + ngx.HTTP_POST, + [[{ + "content": "syntax = \"proto3\"; + package proto; + message HelloRequest{ + string name = 1; + } + + message HelloResponse{ + int32 code = 1; + string msg = 2; + } + // The greeting service definition. + service Hello { + // Sends a greeting + rpc SayHi (HelloRequest) returns (HelloResponse){} + }" + }]], + [[ + { + "node": { + "value": { + "create_time": 1625845753, + "update_time": 1625845753, + "content": "syntax = \"proto3\";\npackage proto;\nmessage HelloRequest{\n string name = 1;\n }\n\nmessage HelloResponse{\n int32 code = 1;\n string msg = 2;\n }\n // The greeting service definition.\nservice Hello {\n // Sends a greeting\n rpc SayHi (HelloRequest) returns (HelloResponse){}\n }" + } + }, + "action": "create" + } + ]] + ) + + if code ~= 201 then + ngx.status = code + ngx.say(message) + return + end + + ngx.say("[push] code: ", code, " message: ", message) + + local id = string.sub(res.node.key, #"/apisix/proto/" + 1) + local res = assert(etcd.get('/proto/' .. id)) + local create_time = res.body.node.value.create_time + assert(create_time ~= nil, "create_time is nil") + local update_time = res.body.node.value.update_time + assert(update_time ~= nil, "update_time is nil") + + code, message = t('/apisix/admin/proto/' .. id, + ngx.HTTP_DELETE, + nil, + [[{ + "action": "delete" + }]] + ) + ngx.say("[delete] code: ", code, " message: ", message) + } + } +--- request +GET /t +--- response_body +[push] code: 200 message: passed +[delete] code: 200 message: passed +--- no_error_log +[error] \ No newline at end of file From 04971a4b4b7fe330d2c7345bc1c57a23458c4256 Mon Sep 17 00:00:00 2001 From: "xin.gao" Date: Mon, 12 Jul 2021 12:25:47 +0800 Subject: [PATCH 03/11] fix: ref check while deleting proto via Admin api --- apisix/admin/proto.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apisix/admin/proto.lua b/apisix/admin/proto.lua index eac10002cfb4..c0d027c3eb40 100644 --- a/apisix/admin/proto.lua +++ b/apisix/admin/proto.lua @@ -169,10 +169,10 @@ function _M.delete(id) if services_ver and services then for _, service in ipairs(services) do if type(service) == "table" and service.value and service.value.plugins then - local ret, err = check_proto_used(service.value.plugins, id, "service", service.value.id) - if not ret then - return 400, err - end + local ret, err = check_proto_used(service.value.plugins, id, "service", service.value.id) + if not ret then + return 400, err + end end end end From 37e5669d9e29c39ad3587908ecc78a77489d5b6e Mon Sep 17 00:00:00 2001 From: "xin.gao" Date: Mon, 12 Jul 2021 12:33:52 +0800 Subject: [PATCH 04/11] bad indent, reutrn true --- apisix/admin/proto.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/admin/proto.lua b/apisix/admin/proto.lua index c0d027c3eb40..b76d5c3821e2 100644 --- a/apisix/admin/proto.lua +++ b/apisix/admin/proto.lua @@ -134,7 +134,7 @@ local function check_proto_used(plugins, deleting, ptype, pid) .. "] is still using it now"} end end - return true,"ok" + return true end function _M.delete(id) From 9cd9cf13e68a19a820d563043953ed568322026a Mon Sep 17 00:00:00 2001 From: "xin.gao" Date: Mon, 12 Jul 2021 12:41:00 +0800 Subject: [PATCH 05/11] rename proto3.t->proto.t, fix line to long --- apisix/admin/proto.lua | 3 ++- t/admin/{proto3.t => proto.t} | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) rename t/admin/{proto3.t => proto.t} (99%) diff --git a/apisix/admin/proto.lua b/apisix/admin/proto.lua index b76d5c3821e2..ba228cc31847 100644 --- a/apisix/admin/proto.lua +++ b/apisix/admin/proto.lua @@ -169,7 +169,8 @@ function _M.delete(id) if services_ver and services then for _, service in ipairs(services) do if type(service) == "table" and service.value and service.value.plugins then - local ret, err = check_proto_used(service.value.plugins, id, "service", service.value.id) + local ret, err = check_proto_used(service.value.plugins, id, + "service", service.value.id) if not ret then return 400, err end diff --git a/t/admin/proto3.t b/t/admin/proto.t similarity index 99% rename from t/admin/proto3.t rename to t/admin/proto.t index 6f369a1e58ae..620c440c521f 100644 --- a/t/admin/proto3.t +++ b/t/admin/proto.t @@ -96,4 +96,4 @@ GET /t [push] code: 200 message: passed [delete] code: 200 message: passed --- no_error_log -[error] \ No newline at end of file +[error] From 5baa0fa1cf5effd540a5a732c74ac8b700c13a68 Mon Sep 17 00:00:00 2001 From: "xin.gao" Date: Mon, 12 Jul 2021 15:30:13 +0800 Subject: [PATCH 06/11] proto test --- t/admin/proto.t | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/t/admin/proto.t b/t/admin/proto.t index 620c440c521f..84f343f304e8 100644 --- a/t/admin/proto.t +++ b/t/admin/proto.t @@ -53,27 +53,21 @@ __DATA__ }]], [[ { - "node": { - "value": { - "create_time": 1625845753, - "update_time": 1625845753, - "content": "syntax = \"proto3\";\npackage proto;\nmessage HelloRequest{\n string name = 1;\n }\n\nmessage HelloResponse{\n int32 code = 1;\n string msg = 2;\n }\n // The greeting service definition.\nservice Hello {\n // Sends a greeting\n rpc SayHi (HelloRequest) returns (HelloResponse){}\n }" - } - }, "action": "create" } ]] ) - if code ~= 201 then + if code ~= 200 then ngx.status = code - ngx.say(message) + ngx.say("[push error] code: ", code, " message: ", message) return end ngx.say("[push] code: ", code, " message: ", message) local id = string.sub(res.node.key, #"/apisix/proto/" + 1) + ngx.say("[push] id: ", id) local res = assert(etcd.get('/proto/' .. id)) local create_time = res.body.node.value.create_time assert(create_time ~= nil, "create_time is nil") From 9800d861ceef5f972809bb7aa95646c31323ae5f Mon Sep 17 00:00:00 2001 From: "xin.gao" Date: Tue, 13 Jul 2021 14:05:08 +0800 Subject: [PATCH 07/11] fix line to long --- apisix/admin/proto.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/apisix/admin/proto.lua b/apisix/admin/proto.lua index ba228cc31847..687bc4f8d1df 100644 --- a/apisix/admin/proto.lua +++ b/apisix/admin/proto.lua @@ -128,7 +128,6 @@ local function check_proto_used(plugins, deleting, ptype, pid) if type(plugins) == "table" and plugins["grpc-transcode"] and plugins["grpc-transcode"].proto_id and tostring(plugins["grpc-transcode"].proto_id) == deleting then - -- core.log.info("check_proto_used proto is used: ", core.json.delay_encode(plugins, true)) return false, {error_msg = "can not delete this proto," .. ptype .. " [" .. pid .. "] is still using it now"} From bd34f918363c36f5c2dd785137d15b33c44dc0ef Mon Sep 17 00:00:00 2001 From: "xin.gao" Date: Tue, 13 Jul 2021 17:37:52 +0800 Subject: [PATCH 08/11] proto test case --- t/admin/proto.t | 179 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 162 insertions(+), 17 deletions(-) diff --git a/t/admin/proto.t b/t/admin/proto.t index 84f343f304e8..386bf18b5cd0 100644 --- a/t/admin/proto.t +++ b/t/admin/proto.t @@ -26,14 +26,14 @@ run_tests; __DATA__ -=== TEST 1: post proto + delete +=== TEST 1: put proto (id:1) --- config location /t { content_by_lua_block { local t = require("lib.test_admin").test local etcd = require("apisix.core.etcd") - local code, message, res = t('/apisix/admin/proto', - ngx.HTTP_POST, + local code, message = t('/apisix/admin/proto/1', + ngx.HTTP_PUT, [[{ "content": "syntax = \"proto3\"; package proto; @@ -53,41 +53,186 @@ __DATA__ }]], [[ { - "action": "create" + "action": "set" } ]] ) if code ~= 200 then ngx.status = code - ngx.say("[push error] code: ", code, " message: ", message) + ngx.say("[put proto] code: ", code, " message: ", message) return end - ngx.say("[push] code: ", code, " message: ", message) + ngx.say("[put proto] code: ", code, " message: ", message) + } + } +--- request +GET /t +--- response_body +[put proto] code: 200 message: passed +--- no_error_log +[error] + - local id = string.sub(res.node.key, #"/apisix/proto/" + 1) - ngx.say("[push] id: ", id) - local res = assert(etcd.get('/proto/' .. id)) - local create_time = res.body.node.value.create_time - assert(create_time ~= nil, "create_time is nil") - local update_time = res.body.node.value.update_time - assert(update_time ~= nil, "update_time is nil") - code, message = t('/apisix/admin/proto/' .. id, +=== TEST 2: delete proto(id:1) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local etcd = require("apisix.core.etcd") + local code, message = t('/apisix/admin/proto/1', ngx.HTTP_DELETE, nil, [[{ "action": "delete" }]] ) - ngx.say("[delete] code: ", code, " message: ", message) + + if code ~= 200 then + ngx.status = code + ngx.say("[delete proto] code: ", code, " message: ", message) + return + end + + ngx.say("[delete proto] code: ", code, " message: ", message) + } + } +--- request +GET /t +--- response_body +[delete proto] code: 200 message: passed +--- no_error_log +[error] + + +=== TEST 3: put proto (id:2) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local etcd = require("apisix.core.etcd") + local code, message = t('/apisix/admin/proto/2', + ngx.HTTP_PUT, + [[{ + "content": "syntax = \"proto3\"; + package proto; + message HelloRequest{ + string name = 1; + } + + message HelloResponse{ + int32 code = 1; + string msg = 2; + } + // The greeting service definition. + service Hello { + // Sends a greeting + rpc SayHi (HelloRequest) returns (HelloResponse){} + }" + }]], + [[ + { + "action": "set" + } + ]] + ) + + if code ~= 200 then + ngx.status = code + ngx.say("[put proto] code: ", code, " message: ", message) + return + end + + ngx.say("[put proto] code: ", code, " message: ", message) + } + } +--- request +GET /t +--- response_body +[put proto] code: 200 message: passed +--- no_error_log +[error] + + + +=== TEST 4: route refer proto(proto id 2) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local etcd = require("apisix.core.etcd") + local code, message = t('/apisix/admin/routes/2', + ngx.HTTP_PUT, + [[{ + "methods": ["GET"], + "plugins": { + "grpc-transcode": { + "disable": false, + "method": "SayHi", + "proto_id": 2, + "service": "proto.Hello" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:8080": 1 + }, + "type": "roundrobin" + }, + "uri": "/grpc/sayhi", + "name": "hi-grpc" + }]], + [[{ + "action": "set" + }]] + ) + + if code ~= 200 then + ngx.status = code + ngx.say("[route refer proto] code: ", code, " message: ", message) + return + end + + ngx.say("[route refer proto] code: ", code, " message: ", message) + } + } +--- request +GET /t +--- response_body +[route refer proto] code: 200 message: passed +--- no_error_log +[error] + + + +=== TEST 5: delete proto(proto id 2) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local etcd = require("apisix.core.etcd") + local code, message = t('/apisix/admin/proto/2', + ngx.HTTP_DELETE, + nil, + [[{ + "action": "delete" + }]] + ) + + if code ~= 200 then + ngx.status = code + ngx.say("[delete proto] code: ", code, " message: ", message) + return + end + + ngx.say("[delete proto] code: ", code, " message: ", message) } } --- request GET /t --- response_body -[push] code: 200 message: passed -[delete] code: 200 message: passed +[delete proto] code: 400 message: {"error_msg":"can not delete this proto,route [2] is still using it now"} --- no_error_log [error] From c8a2d90f39ec95dcfd70b03196c0cafd73ed4dae Mon Sep 17 00:00:00 2001 From: "xin.gao" Date: Tue, 13 Jul 2021 19:45:00 +0800 Subject: [PATCH 09/11] proto test case delete --- t/admin/proto.t | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/t/admin/proto.t b/t/admin/proto.t index 386bf18b5cd0..5eaddb39d306 100644 --- a/t/admin/proto.t +++ b/t/admin/proto.t @@ -221,18 +221,12 @@ GET /t }]] ) - if code ~= 200 then - ngx.status = code - ngx.say("[delete proto] code: ", code, " message: ", message) - return - end - - ngx.say("[delete proto] code: ", code, " message: ", message) + ngx.say("[delete proto] code: ", code) } } --- request GET /t --- response_body -[delete proto] code: 400 message: {"error_msg":"can not delete this proto,route [2] is still using it now"} +[delete proto] code: 400 --- no_error_log [error] From d14f4ab57c7b79a339328489d23d981460f043bc Mon Sep 17 00:00:00 2001 From: "xin.gao" Date: Wed, 14 Jul 2021 11:01:14 +0800 Subject: [PATCH 10/11] proto test code style --- t/admin/proto.t | 1 + 1 file changed, 1 insertion(+) diff --git a/t/admin/proto.t b/t/admin/proto.t index 5eaddb39d306..69a09722253a 100644 --- a/t/admin/proto.t +++ b/t/admin/proto.t @@ -107,6 +107,7 @@ GET /t [error] + === TEST 3: put proto (id:2) --- config location /t { From 5dadb5e8fb7ad90a0029c1343395864cee129f7e Mon Sep 17 00:00:00 2001 From: "xin.gao" Date: Wed, 14 Jul 2021 13:57:27 +0800 Subject: [PATCH 11/11] proto test case: put+refer+delete --- t/admin/proto.t | 40 +++++----------------------------------- 1 file changed, 5 insertions(+), 35 deletions(-) diff --git a/t/admin/proto.t b/t/admin/proto.t index 69a09722253a..85de3e8f3a06 100644 --- a/t/admin/proto.t +++ b/t/admin/proto.t @@ -108,7 +108,7 @@ GET /t -=== TEST 3: put proto (id:2) +=== TEST 3: put proto (id:2) + route refer proto(proto id 2) + delete proto(proto id 2) --- config location /t { content_by_lua_block { @@ -145,26 +145,10 @@ GET /t ngx.say("[put proto] code: ", code, " message: ", message) return end - ngx.say("[put proto] code: ", code, " message: ", message) - } - } ---- request -GET /t ---- response_body -[put proto] code: 200 message: passed ---- no_error_log -[error] - -=== TEST 4: route refer proto(proto id 2) ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local etcd = require("apisix.core.etcd") - local code, message = t('/apisix/admin/routes/2', + code, message = t('/apisix/admin/routes/2', ngx.HTTP_PUT, [[{ "methods": ["GET"], @@ -195,26 +179,10 @@ GET /t ngx.say("[route refer proto] code: ", code, " message: ", message) return end - ngx.say("[route refer proto] code: ", code, " message: ", message) - } - } ---- request -GET /t ---- response_body -[route refer proto] code: 200 message: passed ---- no_error_log -[error] - -=== TEST 5: delete proto(proto id 2) ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local etcd = require("apisix.core.etcd") - local code, message = t('/apisix/admin/proto/2', + code, message = t('/apisix/admin/proto/2', ngx.HTTP_DELETE, nil, [[{ @@ -228,6 +196,8 @@ GET /t --- request GET /t --- response_body +[put proto] code: 200 message: passed +[route refer proto] code: 200 message: passed [delete proto] code: 400 --- no_error_log [error]