route_with_script_luacode_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 script lucacode", func() {
  26. It("clean APISIX error log", func() {
  27. base.CleanAPISIXErrorLog()
  28. })
  29. DescribeTable("test route with script lucacode",
  30. func(tc base.HttpTestCase) {
  31. base.RunTestCase(tc)
  32. },
  33. Entry("create route with script of valid lua code", base.HttpTestCase{
  34. Object: base.ManagerApiExpect(),
  35. Method: http.MethodPut,
  36. Path: "/apisix/admin/routes/r1",
  37. Body: `{
  38. "name": "route1",
  39. "uri": "/hello",
  40. "upstream": {
  41. "type": "roundrobin",
  42. "nodes": {
  43. "` + base.UpstreamIp + `:1980": 1
  44. }
  45. },
  46. "script": "local _M = {} \n function _M.access(api_ctx) \n ngx.log(ngx.WARN,\"hit access phase\") \n end \nreturn _M"
  47. }`,
  48. Headers: map[string]string{"Authorization": base.GetToken()},
  49. ExpectStatus: http.StatusOK,
  50. }),
  51. Entry("get the route", base.HttpTestCase{
  52. Object: base.ManagerApiExpect(),
  53. Method: http.MethodGet,
  54. Path: "/apisix/admin/routes/r1",
  55. Headers: map[string]string{"Authorization": base.GetToken()},
  56. ExpectStatus: http.StatusOK,
  57. ExpectBody: "hit access phase",
  58. Sleep: base.SleepTime,
  59. }),
  60. Entry("hit the route", base.HttpTestCase{
  61. Object: base.APISIXExpect(),
  62. Method: http.MethodGet,
  63. Path: "/hello",
  64. Headers: map[string]string{"Authorization": base.GetToken()},
  65. ExpectStatus: http.StatusOK,
  66. ExpectBody: "hello world\n",
  67. }),
  68. Entry("update route with script of valid lua code", base.HttpTestCase{
  69. Object: base.ManagerApiExpect(),
  70. Method: http.MethodPut,
  71. Path: "/apisix/admin/routes/r1",
  72. Body: `{
  73. "name": "route1",
  74. "uri": "/hello",
  75. "upstream": {
  76. "type": "roundrobin",
  77. "nodes": {
  78. "` + base.UpstreamIp + `:1981": 1
  79. }
  80. },
  81. "script": "local _M = {} \n function _M.access(api_ctx) \n ngx.log(ngx.WARN,\"hit access phase\") \n end \nreturn _M"
  82. }`,
  83. Headers: map[string]string{"Authorization": base.GetToken()},
  84. ExpectStatus: http.StatusOK,
  85. }),
  86. Entry("update route with script of invalid lua code", base.HttpTestCase{
  87. Object: base.ManagerApiExpect(),
  88. Method: http.MethodPut,
  89. Path: "/apisix/admin/routes/r1",
  90. Body: `{
  91. "name": "route1",
  92. "uri": "/hello",
  93. "upstream": {
  94. "type": "roundrobin",
  95. "nodes": {
  96. "` + base.UpstreamIp + `:1980": 1
  97. }
  98. },
  99. "script": "local _M = {} \n function _M.access(api_ctx) \n ngx.log(ngx.WARN,\"hit access phase\")"
  100. }`,
  101. Headers: map[string]string{"Authorization": base.GetToken()},
  102. ExpectStatus: http.StatusBadRequest,
  103. }),
  104. Entry("delete the route (r1)", base.HttpTestCase{
  105. Object: base.ManagerApiExpect(),
  106. Method: http.MethodDelete,
  107. Path: "/apisix/admin/routes/r1",
  108. Headers: map[string]string{"Authorization": base.GetToken()},
  109. ExpectStatus: http.StatusOK,
  110. Sleep: base.SleepTime,
  111. }),
  112. Entry("hit the route just delete", base.HttpTestCase{
  113. Object: base.APISIXExpect(),
  114. Method: http.MethodGet,
  115. Path: "/hello",
  116. Headers: map[string]string{"Authorization": base.GetToken()},
  117. ExpectStatus: http.StatusNotFound,
  118. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  119. Sleep: base.SleepTime,
  120. }),
  121. Entry("create route with script of invalid lua code", base.HttpTestCase{
  122. Object: base.ManagerApiExpect(),
  123. Method: http.MethodPut,
  124. Path: "/apisix/admin/routes/r1",
  125. Body: `{
  126. "name": "route1",
  127. "uri": "/hello",
  128. "upstream": {
  129. "type": "roundrobin",
  130. "nodes": {
  131. "` + base.UpstreamIp + `:1980": 1
  132. }
  133. },
  134. "script": "local _M = {} \n function _M.access(api_ctx) \n ngx.log(ngx.WARN,\"hit access phase\")"
  135. }`,
  136. Headers: map[string]string{"Authorization": base.GetToken()},
  137. ExpectStatus: http.StatusBadRequest,
  138. }),
  139. )
  140. It("verify the log generated by script set in Step-3 above", func() {
  141. //sleep for process log
  142. time.Sleep(1500 * time.Millisecond)
  143. //verify the log generated by script set in Step-3 above
  144. logContent := base.ReadAPISIXErrorLog()
  145. Expect(logContent).Should(ContainSubstring(`\"hit access phase\"`))
  146. //clean log
  147. base.CleanAPISIXErrorLog()
  148. })
  149. })
  150. var _ = Describe("route with script id", func() {
  151. It("clean APISIX error log", func() {
  152. base.CleanAPISIXErrorLog()
  153. })
  154. DescribeTable("test route with script id",
  155. func(tc base.HttpTestCase) {
  156. base.RunTestCase(tc)
  157. },
  158. Entry("create route with invalid script_id - not equal to id", base.HttpTestCase{
  159. Object: base.ManagerApiExpect(),
  160. Method: http.MethodPost,
  161. Path: "/apisix/admin/routes",
  162. Body: `{
  163. "id": "r1",
  164. "name": "route1",
  165. "uri": "/hello",
  166. "upstream": {
  167. "type": "roundrobin",
  168. "nodes": {
  169. "` + base.UpstreamIp + `:1980": 1
  170. }
  171. },
  172. "script": "local _M = {} \n function _M.access(api_ctx) \n ngx.log(ngx.WARN,\"hit access phase\") \n end \nreturn _M",
  173. "script_id": "not-r1"
  174. }`,
  175. Headers: map[string]string{"Authorization": base.GetToken()},
  176. ExpectStatus: http.StatusBadRequest,
  177. }),
  178. Entry("create route with invalid script_id - set script_id but without id", base.HttpTestCase{
  179. Object: base.ManagerApiExpect(),
  180. Method: http.MethodPost,
  181. Path: "/apisix/admin/routes",
  182. Body: `{
  183. "name": "route1",
  184. "uri": "/hello",
  185. "upstream": {
  186. "type": "roundrobin",
  187. "nodes": {
  188. "` + base.UpstreamIp + `:1980": 1
  189. }
  190. },
  191. "script": "local _M = {} \n function _M.access(api_ctx) \n ngx.log(ngx.WARN,\"hit access phase\") \n end \nreturn _M",
  192. "script_id": "r1"
  193. }`,
  194. Headers: map[string]string{"Authorization": base.GetToken()},
  195. ExpectStatus: http.StatusBadRequest,
  196. }),
  197. Entry("create route with invalid script_id - set script_id but without script", base.HttpTestCase{
  198. Object: base.ManagerApiExpect(),
  199. Method: http.MethodPost,
  200. Path: "/apisix/admin/routes",
  201. Body: `{
  202. "id": "r1",
  203. "name": "route1",
  204. "uri": "/hello",
  205. "upstream": {
  206. "type": "roundrobin",
  207. "nodes": {
  208. "` + base.UpstreamIp + `:1980": 1
  209. }
  210. },
  211. "script_id": "r1"
  212. }`,
  213. Headers: map[string]string{"Authorization": base.GetToken()},
  214. ExpectStatus: http.StatusBadRequest,
  215. }),
  216. Entry("create route with valid script_id", base.HttpTestCase{
  217. Object: base.ManagerApiExpect(),
  218. Method: http.MethodPost,
  219. Path: "/apisix/admin/routes",
  220. Body: `{
  221. "id": "r1",
  222. "name": "route1",
  223. "uri": "/hello",
  224. "upstream": {
  225. "type": "roundrobin",
  226. "nodes": {
  227. "` + base.UpstreamIp + `:1980": 1
  228. }
  229. },
  230. "script": "local _M = {} \n function _M.access(api_ctx) \n ngx.log(ngx.WARN,\"hit access phase\") \n end \nreturn _M",
  231. "script_id": "r1"
  232. }`,
  233. Headers: map[string]string{"Authorization": base.GetToken()},
  234. ExpectStatus: http.StatusOK,
  235. }),
  236. Entry("get the route", base.HttpTestCase{
  237. Object: base.ManagerApiExpect(),
  238. Method: http.MethodGet,
  239. Path: "/apisix/admin/routes/r1",
  240. Headers: map[string]string{"Authorization": base.GetToken()},
  241. ExpectStatus: http.StatusOK,
  242. ExpectBody: "\"script_id\":\"r1\"",
  243. Sleep: base.SleepTime,
  244. }),
  245. Entry("hit the route", base.HttpTestCase{
  246. Object: base.APISIXExpect(),
  247. Method: http.MethodGet,
  248. Path: "/hello",
  249. Headers: map[string]string{"Authorization": base.GetToken()},
  250. ExpectStatus: http.StatusOK,
  251. ExpectBody: "hello world\n",
  252. }),
  253. Entry("update route with valid script_id", base.HttpTestCase{
  254. Object: base.ManagerApiExpect(),
  255. Method: http.MethodPut,
  256. Path: "/apisix/admin/routes/r1",
  257. Body: `{
  258. "name": "route1",
  259. "uri": "/hello",
  260. "upstream": {
  261. "type": "roundrobin",
  262. "nodes": {
  263. "` + base.UpstreamIp + `:1981": 1
  264. }
  265. },
  266. "script": "local _M = {} \n function _M.access(api_ctx) \n ngx.log(ngx.WARN,\"hit access phase\") \n end \nreturn _M",
  267. "script_id": "r1"
  268. }`,
  269. Headers: map[string]string{"Authorization": base.GetToken()},
  270. ExpectStatus: http.StatusOK,
  271. }),
  272. Entry("update route with invalid script_id - not equal to id", base.HttpTestCase{
  273. Object: base.ManagerApiExpect(),
  274. Method: http.MethodPut,
  275. Path: "/apisix/admin/routes/r1",
  276. Body: `{
  277. "name": "route1",
  278. "uri": "/hello",
  279. "upstream": {
  280. "type": "roundrobin",
  281. "nodes": {
  282. "` + base.UpstreamIp + `:1980": 1
  283. }
  284. },
  285. "script": "local _M = {} \n function _M.access(api_ctx) \n ngx.log(ngx.WARN,\"hit access phase\")",
  286. "script_id": "not-r1"
  287. }`,
  288. Headers: map[string]string{"Authorization": base.GetToken()},
  289. ExpectStatus: http.StatusBadRequest,
  290. }),
  291. Entry("update route with invalid script_id - set script_id but without script", base.HttpTestCase{
  292. Object: base.ManagerApiExpect(),
  293. Method: http.MethodPut,
  294. Path: "/apisix/admin/routes/r1",
  295. Body: `{
  296. "name": "route1",
  297. "uri": "/hello",
  298. "upstream": {
  299. "type": "roundrobin",
  300. "nodes": {
  301. "` + base.UpstreamIp + `:1980": 1
  302. }
  303. },
  304. "script_id": "r1"
  305. }`,
  306. Headers: map[string]string{"Authorization": base.GetToken()},
  307. ExpectStatus: http.StatusBadRequest,
  308. }),
  309. Entry("delete the route (r1)", base.HttpTestCase{
  310. Object: base.ManagerApiExpect(),
  311. Method: http.MethodDelete,
  312. Path: "/apisix/admin/routes/r1",
  313. Headers: map[string]string{"Authorization": base.GetToken()},
  314. ExpectStatus: http.StatusOK,
  315. Sleep: base.SleepTime,
  316. }),
  317. Entry("hit the route just delete", base.HttpTestCase{
  318. Object: base.APISIXExpect(),
  319. Method: http.MethodGet,
  320. Path: "/hello",
  321. Headers: map[string]string{"Authorization": base.GetToken()},
  322. ExpectStatus: http.StatusNotFound,
  323. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  324. Sleep: base.SleepTime,
  325. }),
  326. )
  327. It("verify the log generated by script set in Step-4 above", func() {
  328. //sleep for process log
  329. time.Sleep(1500 * time.Millisecond)
  330. //verify the log generated by script set in Step-4 above
  331. logContent := base.ReadAPISIXErrorLog()
  332. Expect(logContent).Should(ContainSubstring(`\"hit access phase\"`))
  333. //clean log
  334. base.CleanAPISIXErrorLog()
  335. })
  336. })