You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by mo...@apache.org on 2023/05/24 01:07:09 UTC

[apisix] branch master updated: test: replace mock tests with real tests in udp-logger (#9506)

This is an automated email from the ASF dual-hosted git repository.

monkeydluffy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new eaed6c815 test: replace mock tests with real tests in udp-logger (#9506)
eaed6c815 is described below

commit eaed6c815d1cfa979851cdc108cfa5a0cf8be0ff
Author: Ashish Tiwari <as...@gmail.com>
AuthorDate: Wed May 24 06:36:59 2023 +0530

    test: replace mock tests with real tests in udp-logger (#9506)
---
 ci/pod/docker-compose.plugin.yml |   1 +
 ci/pod/vector/vector.toml        |  15 ++++-
 t/plugin/udp-logger.t            | 133 +++++++++++++++------------------------
 3 files changed, 65 insertions(+), 84 deletions(-)

diff --git a/ci/pod/docker-compose.plugin.yml b/ci/pod/docker-compose.plugin.yml
index 5f7fa7f1d..606929125 100644
--- a/ci/pod/docker-compose.plugin.yml
+++ b/ci/pod/docker-compose.plugin.yml
@@ -320,6 +320,7 @@ services:
       - ./t/certs:/certs
     ports:
       - '3000:3000'
+      - '8127:8127/udp'
       - '43000:43000'
       - '5140:5140'
       - '5150:5150/udp'
diff --git a/ci/pod/vector/vector.toml b/ci/pod/vector/vector.toml
index 953f30746..e536744ce 100644
--- a/ci/pod/vector/vector.toml
+++ b/ci/pod/vector/vector.toml
@@ -23,6 +23,13 @@ port_key = "port"
 shutdown_timeout_secs = 30
 socket_file_mode = 511
 
+[sources.log-from-udp]
+type = "socket"
+address = "0.0.0.0:8127"
+host_key = "host"
+mode = "udp"
+port_key = "port"
+
 [sources.log-from-tls]
 type = "socket"
 address = "0.0.0.0:43000"
@@ -46,7 +53,7 @@ address = "0.0.0.0:5150"
 mode = "udp"
 
 [sinks.log-2-console]
-inputs = [ "log-from-tcp",  "log-from-tls", "log-from-syslog-tcp", "log-from-syslog-udp" ]
+inputs = [ "log-from-tcp",  "log-from-tls", "log-from-syslog-tcp", "log-from-syslog-udp", "log-from-udp"]
 type = "console"
 encoding.codec = "json"
 
@@ -56,6 +63,12 @@ type = "file"
 encoding.codec = "text"
 path = "/etc/vector/tcp.log"
 
+[sinks.log-2-udp-file]
+inputs = [ "log-from-udp" ]
+type = "file"
+encoding.codec = "json"
+path = "/etc/vector/udp.log"
+
 [sinks.tls-log-2-file]
 inputs = [ "log-from-tls" ]
 type = "file"
diff --git a/t/plugin/udp-logger.t b/t/plugin/udp-logger.t
index 934b47c68..2301703e5 100644
--- a/t/plugin/udp-logger.t
+++ b/t/plugin/udp-logger.t
@@ -223,7 +223,7 @@ failed to connect to UDP server: host[312.0.0.1] port[2000]
                         "plugins": {
                             "udp-logger": {
                                 "host": "127.0.0.1",
-                                "port": 2001,
+                                "port": 2002,
                                 "tls": false,
                                 "batch_max_size": 1
                             }
@@ -267,7 +267,7 @@ passedopentracing
 qr/sending a batch logs to 127.0.0.1:(\d+)/
 --- grep_error_log_out
 sending a batch logs to 127.0.0.1:2000
-sending a batch logs to 127.0.0.1:2001
+sending a batch logs to 127.0.0.1:2002
 
 
 
@@ -298,7 +298,7 @@ GET /t
 
 
 
-=== TEST 9: add plugin
+=== TEST 9: configure plugin and access route /hello
 --- config
     location /t {
         content_by_lua_block {
@@ -309,7 +309,7 @@ GET /t
                         "plugins": {
                             "udp-logger": {
                                 "host": "127.0.0.1",
-                                "port": 8125,
+                                "port": 8127,
                                 "tls": false,
                                 "batch_max_size": 1,
                                 "inactive_timeout": 1
@@ -336,17 +336,26 @@ GET /t
                  [[{
                         "log_format": {
                             "host": "$host",
+                            "case name": "plugin_metadata",
                             "@timestamp": "$time_iso8601",
                             "client_ip": "$remote_addr"
                         }
                 }]]
                 )
+
             if code >= 300 then
                 ngx.status = code
                 ngx.say(body)
                 return
             end
+
             ngx.say(body)
+            local code, _, _ = t("/hello", "GET")
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
         }
     }
 --- request
@@ -356,47 +365,15 @@ passed
 
 
 
-=== TEST 10: access
---- stream_conf_enable
---- extra_stream_config
-    server {
-        listen 8125 udp;
-        content_by_lua_block {
-            local decode = require("toolkit.json").decode
-            ngx.log(ngx.WARN, "the mock backend is hit")
-
-            local sock, err = ngx.req.socket()
-            if not sock then
-                ngx.log(ngx.ERR, "failed to get the request socket: ", err)
-                return
-            end
-
-            local data, err = sock:receive()
-
-            if not data then
-                if err and err ~= "no more data" then
-                    ngx.log(ngx.ERR, "socket error, returning: ", err)
-                end
-                return
-            end
-
-            data = decode(data)
-            assert(data.client_ip == "127.0.0.1")
-        }
-    }
---- request
-GET /hello
---- response_body
-hello world
---- wait: 2
---- error_log
-the mock backend is hit
---- no_error_log
-[error]
+=== TEST 10: check if log exists to confirm if logging server was hit
+--- exec
+tail -n 1 ci/pod/vector/udp.log
+--- response_body eval
+qr/.*plugin_metadata.*/
 
 
 
-=== TEST 11: log format in plugin
+=== TEST 11: configure plugin and access route /hello
 --- config
     location /t {
         content_by_lua_block {
@@ -407,11 +384,8 @@ the mock backend is hit
                         "plugins": {
                             "udp-logger": {
                                 "host": "127.0.0.1",
-                                "port": 8125,
+                                "port": 8127,
                                 "tls": false,
-                                "log_format": {
-                                    "vip": "$remote_addr"
-                                },
                                 "batch_max_size": 1,
                                 "inactive_timeout": 1
                             }
@@ -432,50 +406,43 @@ the mock backend is hit
                 return
             end
 
-            ngx.say(body)
-        }
-    }
---- request
-GET /t
---- response_body
-passed
-
-
-
-=== TEST 12: access
---- stream_conf_enable
---- extra_stream_config
-    server {
-        listen 8125 udp;
-        content_by_lua_block {
-            local decode = require("toolkit.json").decode
-            ngx.log(ngx.WARN, "the mock backend is hit")
+            local code, body = t('/apisix/admin/plugin_metadata/udp-logger',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "log_format": {
+                            "host": "$host",
+                            "case name": "logger format in plugin",
+                            "@timestamp": "$time_iso8601",
+                            "client_ip": "$remote_addr"
+                        }
+                }]]
+                )
 
-            local sock, err = ngx.req.socket()
-            if not sock then
-                ngx.log(ngx.ERR, "failed to get the request socket: ", err)
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
                 return
             end
 
-            local data, err = sock:receive()
-
-            if not data then
-                if err and err ~= "no more data" then
-                    ngx.log(ngx.ERR, "socket error, returning: ", err)
-                end
+            ngx.say(body)
+            
+            local code, _, _ = t("/hello", "GET")
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
                 return
             end
-
-            data = decode(data)
-            assert(data.vip == "127.0.0.1")
         }
     }
 --- request
-GET /hello
+GET /t
 --- response_body
-hello world
---- wait: 2
---- error_log
-the mock backend is hit
---- no_error_log
-[error]
+passed
+
+
+
+=== TEST 12: check log format from logging server
+--- exec
+tail -n 1 ci/pod/vector/udp.log
+--- response_body eval
+qr/.*logger format in plugin.*/