nic-6443 commented on code in PR #13258:
URL: https://github.com/apache/apisix/pull/13258#discussion_r3110326414


##########
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:
   Refactored cleanup into a `deregister` helper and call it before each early 
return so the test service is always deregistered. See cdcc8775f.



##########
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:
   Same as above — the deregister helper is now invoked before the 
error/empty-nodes returns. See cdcc8775f.



##########
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:
   Already renumbered to TEST 17 in an earlier commit; this comment is outdated.



-- 
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]

Reply via email to