You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ch...@apache.org on 2020/11/29 14:58:28 UTC

[apisix-dashboard] branch master updated: test: add e2e test cases for route with log plugin (#892)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new aec6d6c  test: add e2e test cases for route with log plugin (#892)
aec6d6c is described below

commit aec6d6cc7613cd7f2c583e99563e01e8a3391198
Author: nic-chen <33...@users.noreply.github.com>
AuthorDate: Sun Nov 29 22:58:16 2020 +0800

    test: add e2e test cases for route with log plugin (#892)
    
    * test: add e2e test cases for route with log plugin
    
    * fix: code format
    
    * fix: error log level for test case check
    
    * fix: test case
    
    * fix: CI fail
    
    * fix: code format
    
    * fix: according to review
    
    * test: disable http logger
    
    * fix: CI fail
    
    * fix: clean log first
    
    * test
    
    * fix: log file path
    
    * fix: try to use sudo
    
    * fix: error
---
 api/test/docker/apisix_config.yaml                |   3 +
 api/test/docker/docker-compose.yaml               |   3 +-
 api/test/e2e/route_with_log_plugin_test.go        | 226 ++++++++++++++++++++++
 api/test/e2e/route_with_valid_remote_addr_test.go |   2 +-
 4 files changed, 232 insertions(+), 2 deletions(-)

diff --git a/api/test/docker/apisix_config.yaml b/api/test/docker/apisix_config.yaml
index 1bb8680..c8e60e0 100644
--- a/api/test/docker/apisix_config.yaml
+++ b/api/test/docker/apisix_config.yaml
@@ -38,3 +38,6 @@ apisix:
       enable: true
       ssl_cert: "/usr/local/apisix/certs/apisix.crt"
       ssl_cert_key: "/usr/local/apisix/certs/apisix.key"
+
+nginx_config:
+  error_log_level: "debug"
diff --git a/api/test/docker/docker-compose.yaml b/api/test/docker/docker-compose.yaml
index 064bff1..91aec31 100644
--- a/api/test/docker/docker-compose.yaml
+++ b/api/test/docker/docker-compose.yaml
@@ -135,7 +135,8 @@ services:
     volumes:
       - ./apisix_config.yaml:/usr/local/apisix/conf/config.yaml:ro
       - ../certs/apisix.crt:/usr/local/apisix/certs/apisix.crt:ro
-      - ../certs/apisix.key:/usr/local/apisix/certs/apisix.key:ro      
+      - ../certs/apisix.key:/usr/local/apisix/certs/apisix.key:ro
+      - ./apisix_logs:/usr/local/apisix/logs
     depends_on:
       - node1
       - node2
diff --git a/api/test/e2e/route_with_log_plugin_test.go b/api/test/e2e/route_with_log_plugin_test.go
new file mode 100644
index 0000000..469dce8
--- /dev/null
+++ b/api/test/e2e/route_with_log_plugin_test.go
@@ -0,0 +1,226 @@
+/*
+ * 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.
+ */
+package e2e
+
+import (
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"os/exec"
+	"strings"
+	"testing"
+	"time"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func readAPISIXErrorLog(t *testing.T) string {
+	bytes, err := ioutil.ReadFile("../docker/apisix_logs/error.log")
+	assert.Nil(t, err)
+	logContent := string(bytes)
+	return logContent
+}
+
+func cleanAPISIXErrorLog(t *testing.T) {
+	cmd := exec.Command("pwd")
+	pwdByte, err := cmd.CombinedOutput()
+	pwd := string(pwdByte)
+
+	pwd = strings.Replace(pwd, "\n", "", 1)
+	pwd = strings.Replace(pwd, "/e2e", "", 1)
+
+	cmd = exec.Command("sudo", "echo", " > ", pwd+"/docker/apisix_logs/error.log")
+	_, err = cmd.CombinedOutput()
+	if err != nil {
+		fmt.Println("cmd error:", err.Error())
+	}
+	assert.Nil(t, err)
+}
+
+func TestRoute_With_Log_Plugin(t *testing.T) {
+	// clean log
+	cleanAPISIXErrorLog(t)
+
+	tests := []HttpTestCase{
+		{
+			caseDesc:     "make sure the route is not created ",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello_",
+			ExpectStatus: http.StatusNotFound,
+			ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
+		},
+		{
+			caseDesc: "create route",
+			Object:   MangerApiExpect(t),
+			Method:   http.MethodPut,
+			Path:     "/apisix/admin/routes/r1",
+			Body: `{
+				"uri": "/hello_",
+				"plugins": {
+					"http-logger": {
+						"uri": "http://172.16.238.20:1982/hello",
+						"batch_max_size": 1,
+						"max_retry_count": 1,
+						"retry_delay": 2,
+						"buffer_duration": 2,
+						"inactive_timeout": 2,
+						"name": "http logger",
+						"timeout": 3,
+						"concat_method": "json"
+					}
+				},
+				"upstream": {
+					"type": "roundrobin",
+					"nodes": [{
+						"host": "172.16.238.20",
+						"port": 1981,
+						"weight": 1
+					}]
+				}
+			}`,
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+		},
+		{
+			caseDesc:     "access route to trigger log",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello_",
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   "hello world",
+			Sleep:        sleepTime,
+		},
+	}
+
+	for _, tc := range tests {
+		testCaseCheck(tc)
+	}
+
+	// sleep for process log
+	time.Sleep(1500 * time.Millisecond)
+
+	// verify http logger by checking log
+	//todo: should use a fake upstream for confirming whether we got the log data.
+	logContent := readAPISIXErrorLog(t)
+	assert.Contains(t, logContent, "Batch Processor[http logger] successfully processed the entries")
+
+	// clean log
+	cleanAPISIXErrorLog(t)
+
+	tests = []HttpTestCase{
+		{
+			caseDesc: "create route with wrong https endpoint",
+			Object:   MangerApiExpect(t),
+			Method:   http.MethodPut,
+			Path:     "/apisix/admin/routes/r2",
+			Body: `{
+				"uri": "/hello",
+				"plugins": {
+					"http-logger": {
+						"uri": "https://127.0.0.1:8888/hello-world-http",
+						"batch_max_size": 1,
+						"max_retry_count": 1,
+						"retry_delay": 2,
+						"buffer_duration": 2,
+						"inactive_timeout": 2,
+						"name": "http logger",
+						"timeout": 3,
+						"concat_method": "json"
+					}
+				},
+				"upstream": {
+					"type": "roundrobin",
+					"nodes": [{
+						"host": "172.16.238.20",
+						"port": 1982,
+						"weight": 1
+					}]
+				}
+			}`,
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+		},
+		{
+			caseDesc:     "access route to trigger log",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   "hello world",
+			Sleep:        sleepTime,
+		},
+	}
+
+	for _, tc := range tests {
+		testCaseCheck(tc)
+	}
+
+	// sleep for process log
+	time.Sleep(1500 * time.Millisecond)
+
+	// verify http logger by checking log
+	//todo: should use a fake upstream for confirming whether we got the log data.
+	logContent = readAPISIXErrorLog(t)
+	assert.Contains(t, logContent, "Batch Processor[http logger] failed to process entries: failed to connect to host[127.0.0.1] port[8888] connection refused")
+
+	// clean log
+	cleanAPISIXErrorLog(t)
+
+	// todo: check disable http logger
+
+	tests = []HttpTestCase{
+		{
+			caseDesc:     "delete route",
+			Object:       MangerApiExpect(t),
+			Method:       http.MethodDelete,
+			Path:         "/apisix/admin/routes/r1",
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+		},
+		{
+			caseDesc:     "make sure the route has been deleted",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello_",
+			ExpectStatus: http.StatusNotFound,
+			ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
+			Sleep:        sleepTime,
+		},
+		{
+			caseDesc:     "delete route 2",
+			Object:       MangerApiExpect(t),
+			Method:       http.MethodDelete,
+			Path:         "/apisix/admin/routes/r2",
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+		},
+		{
+			caseDesc:     "make sure the route 2 has been deleted",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusNotFound,
+			ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
+			Sleep:        sleepTime,
+		},
+	}
+
+	for _, tc := range tests {
+		testCaseCheck(tc)
+	}
+}
diff --git a/api/test/e2e/route_with_valid_remote_addr_test.go b/api/test/e2e/route_with_valid_remote_addr_test.go
index 75afaa5..dd6f7cc 100644
--- a/api/test/e2e/route_with_valid_remote_addr_test.go
+++ b/api/test/e2e/route_with_valid_remote_addr_test.go
@@ -149,7 +149,7 @@ func TestRoute_with_valid_remote_addr(t *testing.T) {
 			Path:     "/apisix/admin/routes/r1",
 			Body: `{
 					"uri": "/hello",
-					"remote_addrs": ["10.10.10.10","11.11.11.1/24"]
+					"remote_addrs": ["10.10.10.10","11.11.11.1/24"],
 					"upstream": {
 						"type": "roundrobin",
 						"nodes": [{