From be39c5117ad2dc96a667c9cf402571d6eee277a3 Mon Sep 17 00:00:00 2001 From: wangye Date: Sat, 11 Nov 2023 11:11:36 +0800 Subject: [PATCH 1/9] [2023-11-11][optional port for admin schema] --- apisix/schema_def.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/schema_def.lua b/apisix/schema_def.lua index e3e9a05aca26..ae35ba4f0ffb 100644 --- a/apisix/schema_def.lua +++ b/apisix/schema_def.lua @@ -325,7 +325,7 @@ local nodes_schema = { type = "object", } }, - required = {"host", "port", "weight"}, + required = {"host", "weight"}, }, } } From 74dd11d6300bab904749ad0e23958a7eae6b6f39 Mon Sep 17 00:00:00 2001 From: wangye Date: Tue, 14 Nov 2023 13:37:33 +0800 Subject: [PATCH 2/9] [2023-11-14][add test case] --- t/core/schema_def_optional_port.t | 89 +++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 t/core/schema_def_optional_port.t diff --git a/t/core/schema_def_optional_port.t b/t/core/schema_def_optional_port.t new file mode 100644 index 000000000000..1e720ec90115 --- /dev/null +++ b/t/core/schema_def_optional_port.t @@ -0,0 +1,89 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +repeat_each(2); +no_long_string(); +no_root_location(); +log_level("info"); + +add_block_preprocessor(sub { + my ($block) = @_; + + if (!$block->request) { + $block->set_value("request", "GET /t"); + } + + if (!$block->error_log && !$block->no_error_log) { + $block->set_value("no_error_log", "[error]\n[alert]"); + } +}); + +run_tests; + +__DATA__ + +=== TEST 1: Sanity check node_schema optional port +--- config + location /t { + content_by_lua_block { + local schema_def = require("apisix.schema_def") + local core = require("apisix.core") + + + upstream = { + nodes = { + {host= "127.0.0.1", weight= 1}, + }, + type = "roundrobin", + } + local ok, err = core.schema.check(schema_def.upstream, upstream) + assert(ok) + assert(err == nil) + + + upstream = { + nodes = { + {host= "127.0.0.1", weight= 2, port= 8080}, + }, + type = "roundrobin", + } + local ok, err = core.schema.check(schema_def.upstream, upstream) + assert(ok) + assert(err == nil) + + + upstream = { + nodes = { + {host= "127.0.0.1", weight= 1}, + {host= "127.0.0.1", weight= 2}, + {host= "127.0.0.1", weight= 2, port= 8080}, + {host= "127.0.0.1", weight= 2, port= 8081}, + }, + type = "roundrobin", + } + local ok, err = core.schema.check(schema_def.upstream, upstream) + assert(ok) + assert(err == nil) + + ngx.say("passed") + } + } +--- request +GET /t +--- response_body +passed From 25a3c3faeb417471ad1abeba1c397af07d90e78c Mon Sep 17 00:00:00 2001 From: wangye Date: Tue, 14 Nov 2023 20:03:27 +0800 Subject: [PATCH 3/9] [2023-11-14][test route avaliable] --- t/admin/schema-validate.t | 68 +++++++++++++++++++++++ t/core/schema_def_optional_port.t | 89 ------------------------------- 2 files changed, 68 insertions(+), 89 deletions(-) delete mode 100644 t/core/schema_def_optional_port.t diff --git a/t/admin/schema-validate.t b/t/admin/schema-validate.t index 46f51021edfd..7d8d353656af 100644 --- a/t/admin/schema-validate.t +++ b/t/admin/schema-validate.t @@ -398,3 +398,71 @@ location /t { --- error_code: 400 --- response {"error_msg":"allOf 1 failed: value should match only one schema, but matches none"} + +=== TEST 14: Check node_schema optional port +--- config + location /t { + content_by_lua_block { + local http = require "resty.http" + local json = require("toolkit.json") + + local uri = "http://127.0.0.1:" .. "9180" .. "/apisix/admin/routes/1" + local httpc = http.new() + local body = { + uri = "/ip", + upstream = { + type = "roundrobin", + nodes = { + { host = "httpbin.org", weight = 1,} + } + } + } + body = json.encode(body) + headers = {} + headers["X-API-KEY"] = "edd1c9f034335f136f87ad84b625c8f1" + + local res, err = httpc:request_uri(uri, {method = "PUT", body=body, headers=headers}) + if not res then + ngx.say(err) + return + end + + if res.status > 300 then + ngx.say(res.body) + return + end + ngx.say("passed") + + } + } +--- request +GET /t +--- response_body +passed + + +=== TEST 2: Test route upstream +--- config + location /t { + content_by_lua_block { + local http = require "resty.http" + local uri = "http://127.0.0.1:" .. "9080" .. "/ip" + local httpc = http.new() + + local res, err = httpc:request_uri(uri, {method = "GET"}) + if not res then + ngx.say(err) + return + end + + if res.status > 300 then + ngx.say(res.body) + return + end + ngx.say("passed") + } + } +--- request +GET /t +--- response_body +passed diff --git a/t/core/schema_def_optional_port.t b/t/core/schema_def_optional_port.t deleted file mode 100644 index 1e720ec90115..000000000000 --- a/t/core/schema_def_optional_port.t +++ /dev/null @@ -1,89 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -use t::APISIX 'no_plan'; - -repeat_each(2); -no_long_string(); -no_root_location(); -log_level("info"); - -add_block_preprocessor(sub { - my ($block) = @_; - - if (!$block->request) { - $block->set_value("request", "GET /t"); - } - - if (!$block->error_log && !$block->no_error_log) { - $block->set_value("no_error_log", "[error]\n[alert]"); - } -}); - -run_tests; - -__DATA__ - -=== TEST 1: Sanity check node_schema optional port ---- config - location /t { - content_by_lua_block { - local schema_def = require("apisix.schema_def") - local core = require("apisix.core") - - - upstream = { - nodes = { - {host= "127.0.0.1", weight= 1}, - }, - type = "roundrobin", - } - local ok, err = core.schema.check(schema_def.upstream, upstream) - assert(ok) - assert(err == nil) - - - upstream = { - nodes = { - {host= "127.0.0.1", weight= 2, port= 8080}, - }, - type = "roundrobin", - } - local ok, err = core.schema.check(schema_def.upstream, upstream) - assert(ok) - assert(err == nil) - - - upstream = { - nodes = { - {host= "127.0.0.1", weight= 1}, - {host= "127.0.0.1", weight= 2}, - {host= "127.0.0.1", weight= 2, port= 8080}, - {host= "127.0.0.1", weight= 2, port= 8081}, - }, - type = "roundrobin", - } - local ok, err = core.schema.check(schema_def.upstream, upstream) - assert(ok) - assert(err == nil) - - ngx.say("passed") - } - } ---- request -GET /t ---- response_body -passed From 8b9fec0fc15f4c31049b76dc51590ac097b689c8 Mon Sep 17 00:00:00 2001 From: wangye Date: Thu, 16 Nov 2023 18:12:44 +0800 Subject: [PATCH 4/9] [2023-11-16][make lint and reindex] --- t/admin/schema-validate.t | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/t/admin/schema-validate.t b/t/admin/schema-validate.t index 7d8d353656af..30cfdfb3b0b4 100644 --- a/t/admin/schema-validate.t +++ b/t/admin/schema-validate.t @@ -399,14 +399,17 @@ location /t { --- response {"error_msg":"allOf 1 failed: value should match only one schema, but matches none"} -=== TEST 14: Check node_schema optional port + + +=== TEST 13: Check node_schema optional port --- config location /t { content_by_lua_block { local http = require "resty.http" local json = require("toolkit.json") + local uri = "http://127.0.0.1:" .. "9180" + .. "/apisix/admin/routes/1" - local uri = "http://127.0.0.1:" .. "9180" .. "/apisix/admin/routes/1" local httpc = http.new() local body = { uri = "/ip", @@ -417,10 +420,9 @@ location /t { } } } - body = json.encode(body) headers = {} headers["X-API-KEY"] = "edd1c9f034335f136f87ad84b625c8f1" - + body = json.encode(body) local res, err = httpc:request_uri(uri, {method = "PUT", body=body, headers=headers}) if not res then ngx.say(err) @@ -428,7 +430,7 @@ location /t { end if res.status > 300 then - ngx.say(res.body) + ngx.say(res_body) return end ngx.say("passed") @@ -441,14 +443,16 @@ GET /t passed -=== TEST 2: Test route upstream + +=== TEST 14: Test route upstream --- config location /t { content_by_lua_block { local http = require "resty.http" - local uri = "http://127.0.0.1:" .. "9080" .. "/ip" - local httpc = http.new() + local uri = "http://127.0.0.1:" .. "9080" + .. "/ip" + local httpc = http.new() local res, err = httpc:request_uri(uri, {method = "GET"}) if not res then ngx.say(err) @@ -456,10 +460,11 @@ passed end if res.status > 300 then - ngx.say(res.body) + ngx.say(res_body) return end ngx.say("passed") + } } --- request From 22d70f6fa11af0e8cc1ef18998a67f88b0b2d439 Mon Sep 17 00:00:00 2001 From: wangye Date: Fri, 17 Nov 2023 19:14:00 +0800 Subject: [PATCH 5/9] [2023-11-17][fix] --- t/admin/schema-validate.t | 68 +++++++++++---------------------------- 1 file changed, 18 insertions(+), 50 deletions(-) diff --git a/t/admin/schema-validate.t b/t/admin/schema-validate.t index 30cfdfb3b0b4..1ab75921cad1 100644 --- a/t/admin/schema-validate.t +++ b/t/admin/schema-validate.t @@ -405,35 +405,27 @@ location /t { --- config location /t { content_by_lua_block { - local http = require "resty.http" - local json = require("toolkit.json") - local uri = "http://127.0.0.1:" .. "9180" - .. "/apisix/admin/routes/1" - - local httpc = http.new() - local body = { - uri = "/ip", - upstream = { - type = "roundrobin", - nodes = { - { host = "httpbin.org", weight = 1,} - } - } - } + local t = require("lib.test_admin").test headers = {} headers["X-API-KEY"] = "edd1c9f034335f136f87ad84b625c8f1" - body = json.encode(body) - local res, err = httpc:request_uri(uri, {method = "PUT", body=body, headers=headers}) - if not res then - ngx.say(err) - return - end + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + { + uri = "/ip", + upstream = { + type = "roundrobin", + nodes = { + { host = "httpbin.org", weight = 1,} + } + }, + methods = {"GET"}, + }, nil, headers + ) - if res.status > 300 then - ngx.say(res_body) - return + if code >= 300 then + ngx.status = code end - ngx.say("passed") + ngx.say(body) } } @@ -445,29 +437,5 @@ passed === TEST 14: Test route upstream ---- config - location /t { - content_by_lua_block { - local http = require "resty.http" - local uri = "http://127.0.0.1:" .. "9080" - .. "/ip" - - local httpc = http.new() - local res, err = httpc:request_uri(uri, {method = "GET"}) - if not res then - ngx.say(err) - return - end - - if res.status > 300 then - ngx.say(res_body) - return - end - ngx.say("passed") - - } - } --- request -GET /t ---- response_body -passed +GET /ip From f0bec6adca85876ffb889bd105ed3d2c395328bf Mon Sep 17 00:00:00 2001 From: Ashish Tiwari Date: Mon, 20 Nov 2023 14:30:41 +0530 Subject: [PATCH 6/9] make tests consistent --- t/admin/schema-validate.t | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/t/admin/schema-validate.t b/t/admin/schema-validate.t index 1ab75921cad1..5ff7a0e4b1d4 100644 --- a/t/admin/schema-validate.t +++ b/t/admin/schema-validate.t @@ -406,16 +406,14 @@ location /t { location /t { content_by_lua_block { local t = require("lib.test_admin").test - headers = {} - headers["X-API-KEY"] = "edd1c9f034335f136f87ad84b625c8f1" - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, + local code, body = t('/apisix/admin/routes', + ngx.HTTP_POST, { uri = "/ip", upstream = { type = "roundrobin", nodes = { - { host = "httpbin.org", weight = 1,} + { host = "nghttp2.org", weight = 1,} } }, methods = {"GET"}, From 24b5e8b88956e270c9aff41e6b3627bc5b65dfcf Mon Sep 17 00:00:00 2001 From: wangye Date: Mon, 20 Nov 2023 17:10:48 +0800 Subject: [PATCH 7/9] [2023-11-20][remove useless nil and headers] --- t/admin/schema-validate.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/admin/schema-validate.t b/t/admin/schema-validate.t index 5ff7a0e4b1d4..4e7c55f8f18e 100644 --- a/t/admin/schema-validate.t +++ b/t/admin/schema-validate.t @@ -417,7 +417,7 @@ location /t { } }, methods = {"GET"}, - }, nil, headers + } ) if code >= 300 then From 90e4adf3e3c6cee8a2d02faf1864cf890662fff0 Mon Sep 17 00:00:00 2001 From: wangye Date: Tue, 21 Nov 2023 10:35:04 +0800 Subject: [PATCH 8/9] [2023-11-21][change node_schema optional port test stable] --- t/admin/schema-validate.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/admin/schema-validate.t b/t/admin/schema-validate.t index 4e7c55f8f18e..e33ec055e2eb 100644 --- a/t/admin/schema-validate.t +++ b/t/admin/schema-validate.t @@ -413,7 +413,7 @@ location /t { upstream = { type = "roundrobin", nodes = { - { host = "nghttp2.org", weight = 1,} + { host = "127.0.0.1:1980", weight = 1,} } }, methods = {"GET"}, From 959881c0e1e7f8bd3215c4fc4ed2b3e4ba2b4d69 Mon Sep 17 00:00:00 2001 From: wangye Date: Tue, 21 Nov 2023 10:35:06 +0800 Subject: [PATCH 9/9] [2023-11-21][change node_schema optional port test stable] --- t/admin/schema-validate.t | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/t/admin/schema-validate.t b/t/admin/schema-validate.t index e33ec055e2eb..81698b243ee7 100644 --- a/t/admin/schema-validate.t +++ b/t/admin/schema-validate.t @@ -409,7 +409,7 @@ location /t { local code, body = t('/apisix/admin/routes', ngx.HTTP_POST, { - uri = "/ip", + uri = "/hello", upstream = { type = "roundrobin", nodes = { @@ -436,4 +436,6 @@ passed === TEST 14: Test route upstream --- request -GET /ip +GET /hello +--- response_body +hello world