From dc7c2b65409dc28744d30692e84d9fad99a3a532 Mon Sep 17 00:00:00 2001 From: j1sk1ss Date: Thu, 24 Sep 2026 12:34:02 +0300 Subject: [PATCH] Handle missing resource config in node version helpers --- apisix/resource.lua | 8 ++++++ t/core/resource.t | 61 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 t/core/resource.t diff --git a/apisix/resource.lua b/apisix/resource.lua index f99672221cf5..d40dd3645ebd 100644 --- a/apisix/resource.lua +++ b/apisix/resource.lua @@ -94,6 +94,10 @@ end function _M.get_nodes_ver(resource_path) local res_conf = fetch_latest_conf(resource_path) + if not res_conf then + return nil + end + local upstream = res_conf.value.upstream or res_conf.value return upstream._nodes_ver end @@ -101,6 +105,10 @@ end function _M.set_nodes_ver_and_nodes(resource_path, nodes_ver, nodes) local res_conf = fetch_latest_conf(resource_path) + if not res_conf then + return + end + local upstream = res_conf.value.upstream or res_conf.value upstream._nodes_ver = nodes_ver upstream.nodes = nodes diff --git a/t/core/resource.t b/t/core/resource.t new file mode 100644 index 000000000000..904ce626378f --- /dev/null +++ b/t/core/resource.t @@ -0,0 +1,61 @@ +# +# 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'; + +run_tests; + +__DATA__ + +=== TEST 1: get_nodes_ver returns nil when the resource does not exist +--- config + location /t { + content_by_lua_block { + local resource = require("apisix.resource") + ngx.say(resource.get_nodes_ver("/apisix/upstreams/missing-resource")) + } + } +--- request +GET /t +--- response_body +nil +--- error_log +resource not found: missing-resource in /upstreams +--- no_error_log +[error] +[alert] + + + +=== TEST 2: set_nodes_ver_and_nodes does not fail when the resource does not exist +--- config + location /t { + content_by_lua_block { + local resource = require("apisix.resource") + resource.set_nodes_ver_and_nodes("/apisix/upstreams/missing-resource", 1, + {["127.0.0.1:1980"] = 1}) + ngx.say("passed") + } + } +--- request +GET /t +--- response_body +passed +--- error_log +resource not found: missing-resource in /upstreams +--- no_error_log +[error] +[alert]