From 0cb7d9ac8def4a490c8b49b7755a9484f3018eb2 Mon Sep 17 00:00:00 2001 From: Shreemaan Abhishek Date: Wed, 22 Jan 2025 12:45:03 +0545 Subject: [PATCH] fix(traffic-split): `upstream_obj.upstream` should not be a string (#11932) --- apisix/plugins/traffic-split.lua | 3 +- t/plugin/traffic-split5.t | 99 ++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/apisix/plugins/traffic-split.lua b/apisix/plugins/traffic-split.lua index f546225c8c95..da6014e8b5f3 100644 --- a/apisix/plugins/traffic-split.lua +++ b/apisix/plugins/traffic-split.lua @@ -217,8 +217,7 @@ local function new_rr_obj(weighted_upstreams) -- If the upstream object has only the weight value, it means -- that the upstream weight value on the default route has been reached. -- Mark empty upstream services in the plugin. - upstream_obj.upstream = "plugin#upstream#is#empty" - server_list[upstream_obj.upstream] = upstream_obj.weight + server_list["plugin#upstream#is#empty"] = upstream_obj.weight end end diff --git a/t/plugin/traffic-split5.t b/t/plugin/traffic-split5.t index 96e284d5bf06..01df76e3cb57 100644 --- a/t/plugin/traffic-split5.t +++ b/t/plugin/traffic-split5.t @@ -535,3 +535,102 @@ id=1 Content-Type: application/x-www-form-urlencoded;charset=UTF-8 --- response_body 1970 + + + +=== TEST 12: failure after plugin reload +--- extra_yaml_config +nginx_config: + worker_processes: 1 + +--- 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, + [[{ + "type": "roundrobin", + "nodes": { + "127.0.0.1:1970":10 + } + }]] + ) + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end + + local code, body = t('/apisix/admin/upstreams/2', + ngx.HTTP_PUT, + [[{ + "type": "roundrobin", + "nodes": { + "127.0.0.1:1971":10 + } + }]] + ) + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end + + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/hello", + "plugins": { + "traffic-split": { + "rules": [ + { + "weighted_upstreams": [ + { + "upstream_id": "2", + "weight": 1 + }, + { + "weight": 1 + } + ] + } + ] + } + }, + "upstream_id": "1" + }]] + ) + if code >= 300 then + ngx.status = code + return + end + + local code, body = t('/hello') + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end + + local code, body = t('/apisix/admin/plugins/reload', ngx.HTTP_PUT) + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end + + local code, body = t('/hello') + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end + + ngx.say("passed.") + } + } +--- request +GET /t +--- response_body +passed.