ccxhwmy commented on code in PR #7643:
URL: https://github.com/apache/apisix/pull/7643#discussion_r946858681


##########
t/plugin/elasticsearch-logging.t:
##########
@@ -0,0 +1,299 @@
+#
+# 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';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local ok, err
+            local configs = {
+                -- full configuration
+                {
+                    endpoint = {
+                        uri = "http://127.0.0.1:9200";,
+                        index = "services",
+                        type = "collector",
+                        timeout = 60,
+                        username = "elastic",
+                        password = "123456",
+                        ssl_verify = false
+                    },
+                    max_retry_count = 0,
+                    retry_delay = 1,
+                    buffer_duration = 60,
+                    inactive_timeout = 2,
+                    batch_max_size = 10,
+                },
+                -- minimize configuration
+                {
+                    endpoint = {
+                        uri = "http://127.0.0.1:9200";,
+                        index = "services"
+                    }
+                },
+                -- property "uri" is required
+                {
+                    endpoint = {
+                        index = "services",
+                    }
+                },
+                -- property "index" is required
+                {
+                    endpoint = {
+                        uri = "http://127.0.0.1:9200";,
+                    }
+                },
+                -- property "uri" validation failed
+                {
+                    endpoint = {
+                        uri = "127.0.0.1:9200",
+                        index = "services"
+                    }
+                }
+            }
+
+            local plugin = require("apisix.plugins.elasticsearch-logging")
+            for i = 1, #configs do
+                ok, err = plugin.check_schema(configs[i])
+                if err then
+                    ngx.say(err)
+                else
+                    ngx.say("passed")
+                end
+            end
+        }
+    }
+--- response_body_like
+passed
+passed
+property "endpoint" validation failed: property "uri" is required
+property "endpoint" validation failed: property "index" is required
+property "endpoint" validation failed: property "uri" validation failed.*
+
+
+
+=== TEST 2: set route
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, {
+                uri = "/hello",
+                upstream = {
+                    type = "roundrobin",
+                    nodes = {
+                        ["127.0.0.1:1980"] = 1
+                    }
+                },
+                plugins = {
+                    ["elasticsearch-logging"] = {
+                        endpoint = {
+                            uri = "http://127.0.0.1:9200";,
+                            index = "services"
+                        },
+                        batch_max_size = 1,
+                        inactive_timeout = 1
+                    }
+                }
+            })
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 3: test route (success write)
+--- request
+GET /hello
+--- wait: 2
+--- response_body
+hello world
+
+
+
+=== TEST 4: set route (auth)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, {
+                uri = "/hello",
+                upstream = {
+                    type = "roundrobin",
+                    nodes = {
+                        ["127.0.0.1:1980"] = 1
+                    }
+                },
+                plugins = {
+                    ["elasticsearch-logging"] = {
+                        endpoint = {
+                            uri = "http://127.0.0.1:9201";,
+                            index = "services",
+                            username = "elastic",
+                            password = "123456"
+                        },
+                        batch_max_size = 1,
+                        inactive_timeout = 1
+                    }
+                }
+            })
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 5: test route (auth success)
+--- request
+GET /hello
+--- wait: 2
+--- response_body

Review Comment:
   > > We should check the data sent to the elasticsearch, via injecting like: 
[#7593 
(comment)](https://github.com/apache/apisix/pull/7593#issuecomment-1210208755)
   > 
   > Does you mean I should hook the function `httpc:request_uri` and mock it 
with my own mock_request_uri, and check the request body with the 
mock_request_uri? If so, I am not quite understand the nessary to check request 
body.
   
   @spacewander 
   Please pay attention to this question when you are free, Thank you.



-- 
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: notifications-unsubscr...@apisix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to