route_with_plugin_http_logger_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package route_test
  18. import (
  19. "net/http"
  20. "time"
  21. . "github.com/onsi/ginkgo/v2"
  22. . "github.com/onsi/gomega"
  23. "github.com/apisix/manager-api/test/e2e/base"
  24. )
  25. var _ = Describe("route with plugin http logger", func() {
  26. It("cleanup previous error logs", func() {
  27. base.CleanAPISIXErrorLog()
  28. })
  29. DescribeTable("test route with http logger plugin",
  30. func(tc base.HttpTestCase) {
  31. base.RunTestCase(tc)
  32. },
  33. Entry("make sure the route is not created ", base.HttpTestCase{
  34. Object: base.APISIXExpect(),
  35. Method: http.MethodGet,
  36. Path: "/hello_",
  37. ExpectStatus: http.StatusNotFound,
  38. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  39. }),
  40. Entry("create route", base.HttpTestCase{
  41. Object: base.ManagerApiExpect(),
  42. Method: http.MethodPut,
  43. Path: "/apisix/admin/routes/r1",
  44. Body: `{
  45. "name": "route1",
  46. "uri": "/hello_",
  47. "plugins": {
  48. "http-logger": {
  49. "uri": "http://` + base.UpstreamIp + `:1982/hello",
  50. "batch_max_size": 1,
  51. "max_retry_count": 1,
  52. "retry_delay": 2,
  53. "buffer_duration": 2,
  54. "inactive_timeout": 2,
  55. "name": "http logger",
  56. "timeout": 3,
  57. "concat_method": "json"
  58. }
  59. },
  60. "upstream": {
  61. "type": "roundrobin",
  62. "nodes": {
  63. "` + base.UpstreamIp + `:1981": 1
  64. }
  65. }
  66. }`,
  67. Headers: map[string]string{"Authorization": base.GetToken()},
  68. ExpectBody: []string{`"code":0`, `"id":"r1"`, `"uri":"/hello_"`, `"name":"route1"`, `"name":"http logger"`},
  69. ExpectStatus: http.StatusOK,
  70. }),
  71. Entry("access route to trigger log", base.HttpTestCase{
  72. Object: base.APISIXExpect(),
  73. Method: http.MethodGet,
  74. Path: "/hello_",
  75. ExpectStatus: http.StatusOK,
  76. ExpectBody: "hello world",
  77. Sleep: base.SleepTime,
  78. }),
  79. )
  80. It("verify http logger by checking log", func() {
  81. // sleep for process log
  82. time.Sleep(1500 * time.Millisecond)
  83. // verify http logger by checking log
  84. //todo: should use a fake upstream for confirming whether we got the log data.
  85. logContent := base.ReadAPISIXErrorLog()
  86. Expect(logContent).Should(ContainSubstring("Batch Processor[http logger] successfully processed the entries"))
  87. // clean log
  88. base.CleanAPISIXErrorLog()
  89. })
  90. DescribeTable("test route for unreachable logger uri",
  91. func(tc base.HttpTestCase) {
  92. base.RunTestCase(tc)
  93. },
  94. Entry("create route with wrong https endpoint", base.HttpTestCase{
  95. Object: base.ManagerApiExpect(),
  96. Method: http.MethodPut,
  97. Path: "/apisix/admin/routes/r2",
  98. Body: `{
  99. "name": "route2",
  100. "uri": "/hello",
  101. "plugins": {
  102. "http-logger": {
  103. "uri": "https://127.0.0.1:8888/hello-world-http",
  104. "batch_max_size": 1,
  105. "max_retry_count": 1,
  106. "retry_delay": 2,
  107. "buffer_duration": 2,
  108. "inactive_timeout": 2,
  109. "name": "http logger",
  110. "timeout": 3,
  111. "concat_method": "json"
  112. }
  113. },
  114. "upstream": {
  115. "type": "roundrobin",
  116. "nodes": {
  117. "` + base.UpstreamIp + `:1982": 1
  118. }
  119. }
  120. }`,
  121. Headers: map[string]string{"Authorization": base.GetToken()},
  122. ExpectBody: []string{`"code":0`, `"id":"r2"`, `"uri":"/hello"`, `"name":"route2"`, `"name":"http logger"`},
  123. ExpectStatus: http.StatusOK,
  124. }),
  125. Entry("access route to trigger log", base.HttpTestCase{
  126. Object: base.APISIXExpect(),
  127. Method: http.MethodGet,
  128. Path: "/hello",
  129. ExpectStatus: http.StatusOK,
  130. ExpectBody: "hello world",
  131. Sleep: base.SleepTime,
  132. }),
  133. )
  134. It("verify http logger by checking log for second route", func() {
  135. // sleep for process log
  136. time.Sleep(1500 * time.Millisecond)
  137. // verify http logger by checking log
  138. //todo: should use a fake upstream for confirming whether we got the log data.
  139. logContent := base.ReadAPISIXErrorLog()
  140. Expect(logContent).Should(ContainSubstring("Batch Processor[http logger] failed to process entries: failed to connect to host[127.0.0.1] port[8888] connection refused"))
  141. // clean log
  142. base.CleanAPISIXErrorLog()
  143. })
  144. // todo: check disable http logger - Done
  145. DescribeTable("rechecking logger after disabling plugin",
  146. func(tc base.HttpTestCase) {
  147. base.RunTestCase(tc)
  148. },
  149. Entry("disable route http logger plugin", base.HttpTestCase{
  150. Object: base.ManagerApiExpect(),
  151. Method: http.MethodPut,
  152. Path: "/apisix/admin/routes/r2",
  153. Body: `{
  154. "name": "route2",
  155. "uri": "/hello",
  156. "plugins": {},
  157. "upstream": {
  158. "type": "roundrobin",
  159. "nodes": {
  160. "` + base.UpstreamIp + `:1982": 1
  161. }
  162. }
  163. }`,
  164. Headers: map[string]string{"Authorization": base.GetToken()},
  165. ExpectBody: []string{`"code":0`, `"id":"r2"`, `"uri":"/hello"`, `"name":"route2"`},
  166. ExpectStatus: http.StatusOK,
  167. }),
  168. Entry("access route to trigger log (though should not be triggered)", base.HttpTestCase{
  169. Object: base.APISIXExpect(),
  170. Method: http.MethodGet,
  171. Path: "/hello",
  172. ExpectStatus: http.StatusOK,
  173. ExpectBody: "hello world",
  174. Sleep: base.SleepTime,
  175. }),
  176. )
  177. It("verify http logger has been successfully disabled", func() {
  178. // sleep for process log
  179. time.Sleep(1500 * time.Millisecond)
  180. // verify http logger by checking log
  181. logContent := base.ReadAPISIXErrorLog()
  182. Expect(logContent).ShouldNot(ContainSubstring("Batch Processor[http logger] successfully processed the entries"))
  183. // clean log
  184. base.CleanAPISIXErrorLog()
  185. })
  186. DescribeTable("cleanup test data",
  187. func(tc base.HttpTestCase) {
  188. base.RunTestCase(tc)
  189. },
  190. Entry("delete route", base.HttpTestCase{
  191. Object: base.ManagerApiExpect(),
  192. Method: http.MethodDelete,
  193. Path: "/apisix/admin/routes/r1",
  194. Headers: map[string]string{"Authorization": base.GetToken()},
  195. ExpectStatus: http.StatusOK,
  196. }),
  197. Entry("make sure the route has been deleted", base.HttpTestCase{
  198. Object: base.APISIXExpect(),
  199. Method: http.MethodGet,
  200. Path: "/hello_",
  201. ExpectStatus: http.StatusNotFound,
  202. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  203. Sleep: base.SleepTime,
  204. }),
  205. Entry("delete route 2", base.HttpTestCase{
  206. Object: base.ManagerApiExpect(),
  207. Method: http.MethodDelete,
  208. Path: "/apisix/admin/routes/r2",
  209. Headers: map[string]string{"Authorization": base.GetToken()},
  210. ExpectStatus: http.StatusOK,
  211. }),
  212. Entry("make sure the route 2 has been deleted", base.HttpTestCase{
  213. Object: base.APISIXExpect(),
  214. Method: http.MethodGet,
  215. Path: "/hello",
  216. ExpectStatus: http.StatusNotFound,
  217. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  218. Sleep: base.SleepTime,
  219. }),
  220. )
  221. })