Copilot commented on code in PR #13258: URL: https://github.com/apache/apisix/pull/13258#discussion_r3109285973
########## t/discovery/consul.t: ########## @@ -868,3 +868,95 @@ v1 nodes: 2 dev nodes: 0 --- no_error_log [error] + + + +=== TEST 14: fetch_services_from_server tolerates services without Meta (preserve_metadata=true) +--- yaml_config +apisix: + node_listen: 1984 +deployment: + role: data_plane + role_data_plane: + config_provider: yaml +discovery: + consul: + servers: + - "http://127.0.0.1:8500" + timeout: + connect: 1000 + read: 1000 + wait: 60 + weight: 1 + fetch_interval: 1 + keepalive: true +--- apisix_yaml +routes: [] +#END +--- config +location /consul1 { + rewrite ^/consul1/(.*) /v1/agent/service/$1 break; + proxy_pass http://127.0.0.1:8500; +} +location /t { + content_by_lua_block { + -- register a consul service WITHOUT a Meta field; consul returns + -- "Meta": null which cjson decodes as a userdata sentinel. + -- Before the fix, fetch_services_from_server crashed with + -- "bad argument #1 to 'next' (table expected, got userdata)" + -- on the very first scrape and never recovered. + local httpc = require("resty.http").new() + local register = function(body) + local res, err = httpc:request_uri( + "http://127.0.0.1:1984/consul1/register", + { method = "PUT", body = body } + ) + if not res or res.status ~= 200 then + ngx.say("register failed: ", err or res.status) + return false + end + return true + end + httpc:request_uri("http://127.0.0.1:1984/consul1/deregister/svc_no_meta_1", {method = "PUT"}) + if not register('{"ID":"svc_no_meta_1","Name":"service_no_meta",' + .. '"Address":"127.0.0.1","Port":30511}') then + return + end Review Comment: The early `return` here exits the handler without deregistering `svc_no_meta_1`, which can leave the service registered in Consul and potentially interfere with later tests. Consider ensuring deregistration runs on all exit paths (e.g., using a local cleanup function and calling it before each return). ########## t/discovery/consul.t: ########## @@ -868,3 +868,95 @@ v1 nodes: 2 dev nodes: 0 --- no_error_log [error] + + + +=== TEST 14: fetch_services_from_server tolerates services without Meta (preserve_metadata=true) Review Comment: This new block is labeled as TEST 14, but TEST 14 already exists earlier in this file ("bootstrap acl"). Please renumber this new test (e.g., TEST 17) to avoid duplicate test IDs and keep the file’s test sequence unambiguous. ```suggestion === TEST 17: fetch_services_from_server tolerates services without Meta (preserve_metadata=true) ``` ########## t/discovery/consul.t: ########## @@ -868,3 +868,95 @@ v1 nodes: 2 dev nodes: 0 --- no_error_log [error] + + + +=== TEST 14: fetch_services_from_server tolerates services without Meta (preserve_metadata=true) +--- yaml_config +apisix: + node_listen: 1984 +deployment: + role: data_plane + role_data_plane: + config_provider: yaml +discovery: + consul: + servers: + - "http://127.0.0.1:8500" + timeout: + connect: 1000 + read: 1000 + wait: 60 + weight: 1 + fetch_interval: 1 + keepalive: true +--- apisix_yaml +routes: [] +#END +--- config +location /consul1 { + rewrite ^/consul1/(.*) /v1/agent/service/$1 break; + proxy_pass http://127.0.0.1:8500; +} +location /t { + content_by_lua_block { + -- register a consul service WITHOUT a Meta field; consul returns + -- "Meta": null which cjson decodes as a userdata sentinel. + -- Before the fix, fetch_services_from_server crashed with + -- "bad argument #1 to 'next' (table expected, got userdata)" + -- on the very first scrape and never recovered. + local httpc = require("resty.http").new() + local register = function(body) + local res, err = httpc:request_uri( + "http://127.0.0.1:1984/consul1/register", + { method = "PUT", body = body } + ) + if not res or res.status ~= 200 then + ngx.say("register failed: ", err or res.status) + return false + end + return true + end + httpc:request_uri("http://127.0.0.1:1984/consul1/deregister/svc_no_meta_1", {method = "PUT"}) + if not register('{"ID":"svc_no_meta_1","Name":"service_no_meta",' + .. '"Address":"127.0.0.1","Port":30511}') then + return + end + + local consul_client = require("apisix.discovery.consul.client") + local servers = consul_client.format_consul_params({ + servers = {"http://127.0.0.1:8500"}, + timeout = {connect = 2000, read = 2000, wait = 60}, + weight = 1, + keepalive = true, + fetch_interval = 3, + }) + + local up, err = consul_client.fetch_services_from_server(servers[1], { + default_weight = 1, + preserve_metadata = true, + key_builder = function(name) return "no_meta_test/" .. name end, + }) + if err then + ngx.say("err: ", err) + return + end Review Comment: On error, this `return` skips the deregistration call at the end of the handler, leaving the Consul service registered. Please ensure the deregistration runs even when `fetch_services_from_server` returns an error (e.g., call a cleanup helper before returning). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
