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 7e3b4021b fix: replace mock tests with real tests in http logger 
(#9538)
7e3b4021b is described below

commit 7e3b4021b3b3370c06d15dd86f4f43cc7b470265
Author: Ashish Tiwari <ashishjaitiwari15112...@gmail.com>
AuthorDate: Tue Jun 6 13:09:58 2023 +0530

    fix: replace mock tests with real tests in http logger (#9538)
---
 ci/pod/docker-compose.plugin.yml  |   3 +-
 ci/pod/vector/vector.toml         |  12 ++-
 t/plugin/http-logger-log-format.t | 207 ++++++++++++++++++++------------------
 t/plugin/udp-logger.t             |   1 -
 4 files changed, 121 insertions(+), 102 deletions(-)

diff --git a/ci/pod/docker-compose.plugin.yml b/ci/pod/docker-compose.plugin.yml
index a7b5fb981..4ea069b8c 100644
--- a/ci/pod/docker-compose.plugin.yml
+++ b/ci/pod/docker-compose.plugin.yml
@@ -316,12 +316,13 @@ services:
       - ./ci/pod/vector:/etc/vector/
       - ./t/certs:/certs
     ports:
-      - '3000:3000'
+      - '3000:3000' #tcp logger
       - '8127:8127/udp'
       - '43000:43000'
       - '5140:5140'
       - "18088:18088" # For splunk logging tests
       - '5150:5150/udp'
+      - "3001:3001" #http logger
     networks:
       vector_net:
 
diff --git a/ci/pod/vector/vector.toml b/ci/pod/vector/vector.toml
index 0fe35295c..26716bc38 100644
--- a/ci/pod/vector/vector.toml
+++ b/ci/pod/vector/vector.toml
@@ -23,6 +23,10 @@ port_key = "port"
 shutdown_timeout_secs = 30
 socket_file_mode = 511
 
+[sources.log-from-http]
+type = "http_server"
+address = "0.0.0.0:3001"
+
 [sources.log-from-udp]
 type = "socket"
 address = "0.0.0.0:8127"
@@ -60,7 +64,7 @@ valid_tokens = [
 ]
 
 [sinks.log-2-console]
-inputs = [ "log-from-tcp",  "log-from-tls", "log-from-syslog-tcp", 
"log-from-syslog-udp", "log-from-udp", "log-from-splunk"]
+inputs = [ "log-from-tcp",  "log-from-tls", "log-from-syslog-tcp", 
"log-from-syslog-udp", "log-from-udp", "log-from-splunk", "log-from-http"]
 type = "console"
 encoding.codec = "json"
 
@@ -70,6 +74,12 @@ type = "file"
 encoding.codec = "text"
 path = "/etc/vector/tcp.log"
 
+[sinks.log-2-http-file]
+inputs = [ "log-from-http" ]
+type = "file"
+encoding.codec = "text"
+path = "/etc/vector/http.log"
+
 [sinks.log-2-udp-file]
 inputs = [ "log-from-udp" ]
 type = "file"
diff --git a/t/plugin/http-logger-log-format.t 
b/t/plugin/http-logger-log-format.t
index d2c6daa9e..0bc8ceab3 100644
--- a/t/plugin/http-logger-log-format.t
+++ b/t/plugin/http-logger-log-format.t
@@ -63,7 +63,7 @@ passed
                  [[{
                         "plugins": {
                             "http-logger": {
-                                "uri": "http://127.0.0.1:1980/log";,
+                                "uri": "http://127.0.0.1:3001";,
                                 "batch_max_size": 1,
                                 "max_retry_count": 1,
                                 "retry_delay": 2,
@@ -85,6 +85,13 @@ passed
             if code >= 300 then
                 ngx.status = code
             end
+
+            local code, _, body2 = t("/hello", "GET")
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
             ngx.say(body)
         }
     }
@@ -95,14 +102,11 @@ passed
 
 
 
-=== TEST 3: hit route and report http logger
---- request
-GET /hello
---- response_body
-hello world
---- wait: 0.5
---- error_log eval
-qr/request log: \{.*route_id":"1".*\}/
+=== TEST 3: report http logger
+--- exec
+tail -n 1 ci/pod/vector/http.log
+--- response_body eval
+qr/.*route_id":"1".*/
 
 
 
@@ -116,7 +120,7 @@ qr/request log: \{.*route_id":"1".*\}/
                  [[{
                         "plugins": {
                             "http-logger": {
-                                "uri": "http://127.0.0.1:1980/log";,
+                                "uri": "http://127.0.0.1:3001";,
                                 "batch_max_size": 2,
                                 "max_retry_count": 1,
                                 "retry_delay": 2,
@@ -138,6 +142,13 @@ qr/request log: \{.*route_id":"1".*\}/
             if code >= 300 then
                 ngx.status = code
             end
+
+            local code, _, body2 = t("/hello", "GET")
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
             ngx.say(body)
         }
     }
@@ -148,28 +159,11 @@ passed
 
 
 
-=== TEST 5: hit route and report http logger
---- config
-location /t {
-    content_by_lua_block {
-        local t = require("lib.test_admin").test
-
-        for i = 1, 2 do
-            t('/hello', ngx.HTTP_GET)
-        end
-
-        ngx.sleep(3)
-        ngx.say("done")
-    }
-}
---- request
-GET /t
---- error_code: 200
---- grep_error_log eval
+=== TEST 5: report http logger
+--- exec
+tail -n 1 ci/pod/vector/http.log
+--- response_body eval
 qr/"\@timestamp":"20/
---- grep_error_log_out
-"@timestamp":"20
-"@timestamp":"20
 
 
 
@@ -183,7 +177,7 @@ qr/"\@timestamp":"20/
                  [[{
                         "plugins": {
                             "http-logger": {
-                                "uri": "http://127.0.0.1:1980/log";,
+                                "uri": "http://127.0.0.1:3001";,
                                 "batch_max_size": 1,
                                 "max_retry_count": 1,
                                 "retry_delay": 2,
@@ -205,6 +199,13 @@ qr/"\@timestamp":"20/
             if code >= 300 then
                 ngx.status = code
             end
+
+            local code, _, body2 = t("/hello", "GET")
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
             ngx.say(body)
         }
     }
@@ -215,14 +216,11 @@ passed
 
 
 
-=== TEST 7: hit route and report http logger
---- request
-GET /hello
---- response_body
-hello world
---- wait: 0.5
---- error_log eval
-qr/request log: 
\{"\@timestamp":.*,"client_ip":"127.0.0.1","host":"localhost","route_id":"1"\}/
+=== TEST 7: report http logger
+--- exec
+tail -n 1 ci/pod/vector/http.log
+--- response_body eval
+qr/"route_id":"1"/
 
 
 
@@ -236,7 +234,7 @@ qr/request log: 
\{"\@timestamp":.*,"client_ip":"127.0.0.1","host":"localhost","r
                  [[{
                         "plugins": {
                             "http-logger": {
-                                "uri": "http://127.0.0.1:1980/log";,
+                                "uri": "http://127.0.0.1:3001";,
                                 "batch_max_size": 2,
                                 "max_retry_count": 1,
                                 "retry_delay": 2,
@@ -258,6 +256,20 @@ qr/request log: 
\{"\@timestamp":.*,"client_ip":"127.0.0.1","host":"localhost","r
             if code >= 300 then
                 ngx.status = code
             end
+
+            local code, _, body2 = t("/hello", "GET")
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+
+            local code, _, body2 = t("/hello", "GET")
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
             ngx.say(body)
         }
     }
@@ -268,25 +280,11 @@ passed
 
 
 
-=== TEST 9: hit route and report http logger
---- config
-location /t {
-    content_by_lua_block {
-        local t = require("lib.test_admin").test
-
-        for i = 1, 2 do
-            t('/hello', ngx.HTTP_GET)
-        end
-
-        ngx.sleep(3)
-        ngx.say("done")
-    }
-}
---- request
-GET /t
---- error_code: 200
---- error_log eval
-qr/request log: 
\[\{"\@timestamp":".*","client_ip":"127.0.0.1","host":"127.0.0.1","route_id":"1"\},\{"\@timestamp":".*","client_ip":"127.0.0.1","host":"127.0.0.1","route_id":"1"\}\]/
+=== TEST 9: report http logger to confirm two json in array
+--- exec
+tail -n 1 ci/pod/vector/http.log
+--- response_body eval
+qr/\[\{.*?\},\{.*?\}\]/
 
 
 
@@ -356,7 +354,7 @@ passed
                  [[{
                         "plugins": {
                             "http-logger": {
-                                "uri": "http://127.0.0.1:1982/log";,
+                                "uri": "http://127.0.0.1:3001";,
                                 "batch_max_size": 1,
                                 "max_retry_count": 1,
                                 "retry_delay": 2,
@@ -378,6 +376,13 @@ passed
             if code >= 300 then
                 ngx.status = code
             end
+
+            local code, _, _ = t("/hello", "GET",null,null,{apikey = 
"auth-one"})
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
             ngx.say(body)
         }
     }
@@ -388,15 +393,11 @@ passed
 
 
 
-=== TEST 13: hit
---- request
-GET /hello
---- more_headers
-apikey: auth-one
---- grep_error_log eval
-qr/request log: \{.+\}/
---- grep_error_log_out eval
-qr/\Q{"apisix_latency":\E[^,]+\Q,"client_ip":"127.0.0.1","consumer":{"username":"jack"},"latency":\E[^,]+\Q,"request":{"headers":{"apikey":"auth-one","connection":"close","host":"localhost"},"method":"GET","querystring":{},"size":\E\d+\Q,"uri":"\/hello","url":"http:\/\/localhost:1984\/hello"},"response":{"headers":{"connection":"close","content-length":"\E\d+\Q","content-type":"text\/plain","server":"\E[^"]+\Q"},"size":\E\d+\Q,"status":200},"route_id":"1","server":{"hostname":"\E[^"]+\Q"
 [...]
+=== TEST 13: check logs
+--- exec
+tail -n 1 ci/pod/vector/http.log
+--- response_body eval
+qr/"consumer":\{"username":"jack"\}/
 --- wait: 0.5
 
 
@@ -428,6 +429,16 @@ done
 
 
 === TEST 15: use custom variable in the logger
+--- extra_init_by_lua
+    local core = require "apisix.core"
+
+    core.ctx.register_var("a6_route_labels", function(ctx)
+        local route = ctx.matched_route and ctx.matched_route.value
+        if route and route.labels then
+            return route.labels
+        end
+        return nil
+    end)
 --- config
     location /t {
         content_by_lua_block {
@@ -453,20 +464,20 @@ done
                  [[{
                         "plugins": {
                             "http-logger": {
-                                "uri": "http://127.0.0.1:1980/log";,
+                                "uri": "http://127.0.0.1:3001";,
                                 "batch_max_size": 1,
                                 "concat_method": "json"
                             }
                         },
+                        "labels":{
+                            "key":"testvalue"
+                        },
                         "upstream": {
                             "nodes": {
                                 "127.0.0.1:1982": 1
                             },
                             "type": "roundrobin"
                         },
-                        "labels": {
-                            "k": "v"
-                        },
                         "uri": "/hello"
                 }]]
                 )
@@ -474,6 +485,13 @@ done
             if code >= 300 then
                 ngx.status = code
             end
+
+            local code, _, body2 = t("/hello", "GET")
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
             ngx.say(body)
         }
     }
@@ -485,23 +503,10 @@ passed
 
 
 === TEST 16: hit route and report http logger
---- extra_init_by_lua
-    local core = require "apisix.core"
-
-    core.ctx.register_var("a6_route_labels", function(ctx)
-        local route = ctx.matched_route and ctx.matched_route.value
-        if route and route.labels then
-            return route.labels
-        end
-        return nil
-    end)
---- request
-GET /hello
---- response_body
-hello world
---- wait: 0.5
---- error_log eval
-qr/request log: 
\{"client_ip":"127.0.0.1","host":"localhost","labels":\{"k":"v"\},"route_id":"1"\}/
+--- exec
+tail -n 1 ci/pod/vector/http.log
+--- response_body eval
+qr/.*testvalue.*/
 
 
 
@@ -515,7 +520,7 @@ qr/request log: 
\{"client_ip":"127.0.0.1","host":"localhost","labels":\{"k":"v"\
                  [[{
                         "plugins": {
                             "http-logger": {
-                                "uri": "http://127.0.0.1:1980/log";,
+                                "uri": "http://127.0.0.1:3001";,
                                 "batch_max_size": 1,
                                 "max_retry_count": 1,
                                 "retry_delay": 2,
@@ -540,6 +545,13 @@ qr/request log: 
\{"client_ip":"127.0.0.1","host":"localhost","labels":\{"k":"v"\
             if code >= 300 then
                 ngx.status = code
             end
+
+            local code, _, body2 = t("/hello", "GET")
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
             ngx.say(body)
         }
     }
@@ -551,10 +563,7 @@ passed
 
 
 === TEST 18: hit route and report http logger
---- request
-GET /hello
---- response_body
-hello world
---- wait: 0.5
---- error_log eval
-qr/request log: \{.*"x_ip":"127.0.0.1".*\}/
+--- exec
+tail -n 1 ci/pod/vector/http.log
+--- response_body eval
+qr/"x_ip":"127.0.0.1".*\}/
diff --git a/t/plugin/udp-logger.t b/t/plugin/udp-logger.t
index 4e27b7695..447448236 100644
--- a/t/plugin/udp-logger.t
+++ b/t/plugin/udp-logger.t
@@ -425,7 +425,6 @@ qr/.*plugin_metadata.*/
             end
 
             ngx.say(body)
-
             local code, _, _ = t("/hello", "GET")
             if code >= 300 then
                 ngx.status = code

Reply via email to