diff --git a/api/test/e2e/route_with_plugin_http_logger_test.go b/api/test/e2e/route_with_plugin_http_logger_test.go deleted file mode 100644 index 137d9620cf..0000000000 --- a/api/test/e2e/route_with_plugin_http_logger_test.go +++ /dev/null @@ -1,201 +0,0 @@ -/* - * 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 ( - "net/http" - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -func TestRoute_With_Log_Plugin(t *testing.T) { - // clean log - CleanAPISIXErrorLog(t) - - tests := []HttpTestCase{ - { - Desc: "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"}`, - }, - { - Desc: "create route", - Object: ManagerApiExpect(t), - Method: http.MethodPut, - Path: "/apisix/admin/routes/r1", - Body: `{ - "name": "route1", - "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, - }, - { - Desc: "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, t) - } - - // 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{ - { - Desc: "create route with wrong https endpoint", - Object: ManagerApiExpect(t), - Method: http.MethodPut, - Path: "/apisix/admin/routes/r2", - Body: `{ - "name": "route2", - "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, - }, - { - Desc: "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, t) - } - - // 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{ - { - Desc: "delete route", - Object: ManagerApiExpect(t), - Method: http.MethodDelete, - Path: "/apisix/admin/routes/r1", - Headers: map[string]string{"Authorization": token}, - ExpectStatus: http.StatusOK, - }, - { - Desc: "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, - }, - { - Desc: "delete route 2", - Object: ManagerApiExpect(t), - Method: http.MethodDelete, - Path: "/apisix/admin/routes/r2", - Headers: map[string]string{"Authorization": token}, - ExpectStatus: http.StatusOK, - }, - { - Desc: "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, t) - } -} diff --git a/api/test/e2enew/base/base.go b/api/test/e2enew/base/base.go index 6fa319c8c5..60fbcaa1a0 100644 --- a/api/test/e2enew/base/base.go +++ b/api/test/e2enew/base/base.go @@ -248,8 +248,9 @@ func CleanAPISIXErrorLog() { pwd := string(pwdByte) pwd = strings.Replace(pwd, "\n", "", 1) pwd = pwd[:strings.Index(pwd, "/e2e")] - cmd = exec.Command("sudo", "echo", " > ", pwd+"/docker/apisix_logs/error.log") - _, err = cmd.CombinedOutput() + cmdStr := "echo | sudo tee " + pwd + "/docker/apisix_logs/error.log" + cmd = exec.Command("bash", "-c", cmdStr) + _, err = cmd.Output() if err != nil { fmt.Println("cmd error:", err.Error()) } diff --git a/api/test/e2enew/route/route_with_plugin_http_logger_test.go b/api/test/e2enew/route/route_with_plugin_http_logger_test.go new file mode 100644 index 0000000000..faddf1bbbb --- /dev/null +++ b/api/test/e2enew/route/route_with_plugin_http_logger_test.go @@ -0,0 +1,239 @@ +/* + * 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 route + +import ( + "net/http" + "time" + + "github.com/onsi/ginkgo" + "github.com/onsi/ginkgo/extensions/table" + "github.com/onsi/gomega" + + "github.com/apisix/manager-api/test/e2enew/base" +) + +var _ = ginkgo.Describe("route with plugin http logger", func() { + ginkgo.It("cleanup previous error logs", func() { + base.CleanAPISIXErrorLog() + }) + + table.DescribeTable("test route with http logger plugin", + func(tc base.HttpTestCase) { + base.RunTestCase(tc) + }, + table.Entry("make sure the route is not created ", base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: "/hello_", + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + }), + table.Entry("create route", base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodPut, + Path: "/apisix/admin/routes/r1", + Body: `{ + "name": "route1", + "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": { + "` + base.UpstreamIp + `:1981": 1 + } + } + }`, + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectBody: []string{`"code":0`, `"id":"r1"`, `"uri":"/hello_"`, `"name":"route1"`, `"name":"http logger"`}, + ExpectStatus: http.StatusOK, + }), + table.Entry("access route to trigger log", base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: "/hello_", + ExpectStatus: http.StatusOK, + ExpectBody: "hello world", + Sleep: base.SleepTime, + }), + ) + + ginkgo.It("verify http logger by checking log", func() { + // 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 := base.ReadAPISIXErrorLog() + gomega.Expect(logContent).Should(gomega.ContainSubstring("Batch Processor[http logger] successfully processed the entries")) + + // clean log + base.CleanAPISIXErrorLog() + }) + + table.DescribeTable("test route for unreachable logger uri", + func(tc base.HttpTestCase) { + base.RunTestCase(tc) + }, + table.Entry("create route with wrong https endpoint", base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodPut, + Path: "/apisix/admin/routes/r2", + Body: `{ + "name": "route2", + "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": { + "` + base.UpstreamIp + `:1982": 1 + } + } + }`, + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectBody: []string{`"code":0`, `"id":"r2"`, `"uri":"/hello"`, `"name":"route2"`, `"name":"http logger"`}, + ExpectStatus: http.StatusOK, + }), + table.Entry("access route to trigger log", base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: "/hello", + ExpectStatus: http.StatusOK, + ExpectBody: "hello world", + Sleep: base.SleepTime, + }), + ) + + ginkgo.It("verify http logger by checking log for second route", func() { + // 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 := base.ReadAPISIXErrorLog() + gomega.Expect(logContent).Should(gomega.ContainSubstring("Batch Processor[http logger] failed to process entries: failed to connect to host[127.0.0.1] port[8888] connection refused")) + + // clean log + base.CleanAPISIXErrorLog() + }) + + // todo: check disable http logger - Done + table.DescribeTable("rechecking logger after disabling plugin", + func(tc base.HttpTestCase) { + base.RunTestCase(tc) + }, + table.Entry("disable route http logger plugin", base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodPut, + Path: "/apisix/admin/routes/r2", + Body: `{ + "name": "route2", + "uri": "/hello", + "plugins": {}, + "upstream": { + "type": "roundrobin", + "nodes": { + "` + base.UpstreamIp + `:1982": 1 + } + } + }`, + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectBody: []string{`"code":0`, `"id":"r2"`, `"uri":"/hello"`, `"name":"route2"`}, + ExpectStatus: http.StatusOK, + }), + table.Entry("access route to trigger log (though should not be triggered)", base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: "/hello", + ExpectStatus: http.StatusOK, + ExpectBody: "hello world", + Sleep: base.SleepTime, + }), + ) + + ginkgo.It("verify http logger has been successfully disabled", func() { + // sleep for process log + time.Sleep(1500 * time.Millisecond) + + // verify http logger by checking log + logContent := base.ReadAPISIXErrorLog() + gomega.Expect(logContent).ShouldNot(gomega.ContainSubstring("Batch Processor[http logger] successfully processed the entries")) + + // clean log + base.CleanAPISIXErrorLog() + }) + + table.DescribeTable("cleanup test data", + func(tc base.HttpTestCase) { + base.RunTestCase(tc) + }, + table.Entry("delete route", base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodDelete, + Path: "/apisix/admin/routes/r1", + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + }), + table.Entry("make sure the route has been deleted", base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: "/hello_", + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }), + table.Entry("delete route 2", base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodDelete, + Path: "/apisix/admin/routes/r2", + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + }), + table.Entry("make sure the route 2 has been deleted", base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: "/hello", + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }), + ) +})