2
0

dynamic-hook.t 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. use t::APISIX 'no_plan';
  18. repeat_each(1);
  19. log_level('info');
  20. no_root_location();
  21. no_shuffle();
  22. our $debug_config = t::APISIX::read_file("conf/debug.yaml");
  23. $debug_config =~ s/http_filter:\n enable: false/http_filter:\n enable: true/;
  24. $debug_config =~ s/hook_conf:\n enable: false/hook_conf:\n enable: true/;
  25. run_tests();
  26. __DATA__
  27. === TEST 1: dynamic enable
  28. # ai module would conflict with the debug module
  29. --- extra_yaml_config
  30. plugins:
  31. #- ai
  32. - example-plugin
  33. --- debug_config eval: $::debug_config
  34. --- config
  35. location /t {
  36. content_by_lua_block {
  37. local t = require("lib.test_admin").test
  38. local code, body = t('/apisix/admin/routes/1',
  39. ngx.HTTP_PUT,
  40. [[{
  41. "uri": "/hello",
  42. "upstream": {
  43. "nodes": {
  44. "127.0.0.1:1980": 1
  45. },
  46. "type": "roundrobin"
  47. }
  48. }]]
  49. )
  50. if code >= 300 then
  51. ngx.status = code
  52. return
  53. end
  54. ngx.sleep(0.6) -- wait for sync
  55. local headers = {}
  56. headers["X-APISIX-Dynamic-Debug"] = ""
  57. local code, body = t('/hello',
  58. ngx.HTTP_GET,
  59. "",
  60. nil,
  61. headers
  62. )
  63. ngx.status = code
  64. ngx.say(body)
  65. }
  66. }
  67. --- request
  68. GET /t
  69. --- wait: 2
  70. --- response_body
  71. passed
  72. --- error_log
  73. call require("apisix").http_header_filter_phase() args:{}
  74. call require("apisix").http_header_filter_phase() return:{}
  75. call require("apisix").http_body_filter_phase() args:{}
  76. call require("apisix").http_body_filter_phase() return:{}
  77. call require("apisix").http_log_phase() args:{}
  78. call require("apisix").http_log_phase() return:{}
  79. --- no_error_log
  80. call require("apisix").http_access_phase() return:{}
  81. call require("apisix").http_access_phase() args:{}
  82. === TEST 2: dynamic enable by per request and disable after handle request
  83. # ai module would conflict with the debug module
  84. --- extra_yaml_config
  85. plugins:
  86. #- ai
  87. - example-plugin
  88. --- debug_config eval: $::debug_config
  89. --- config
  90. location /t {
  91. content_by_lua_block {
  92. local t = require("lib.test_admin").test
  93. local code, body = t('/apisix/admin/routes/1',
  94. ngx.HTTP_PUT,
  95. [[{
  96. "uris": ["/hello","/hello1"],
  97. "upstream": {
  98. "nodes": {
  99. "127.0.0.1:1980": 1
  100. },
  101. "type": "roundrobin"
  102. }
  103. }]]
  104. )
  105. if code >= 300 then
  106. ngx.status = code
  107. return
  108. end
  109. ngx.sleep(0.6) -- wait for sync
  110. local http = require "resty.http"
  111. local httpc = http.new()
  112. local headers = {}
  113. headers["X-APISIX-Dynamic-Debug"] = ""
  114. local uri1 = "http://127.0.0.1:" .. ngx.var.server_port
  115. .. "/hello1"
  116. local res, err = httpc:request_uri(uri1, {method = "GET", headers = headers})
  117. if not res then
  118. ngx.say(err)
  119. return
  120. end
  121. local uri2 = "http://127.0.0.1:" .. ngx.var.server_port
  122. .. "/hello"
  123. res, err = httpc:request_uri(uri2)
  124. if not res then
  125. ngx.say(err)
  126. return
  127. end
  128. ngx.print(res.body)
  129. }
  130. }
  131. --- request
  132. GET /t
  133. --- wait: 2
  134. --- response_body
  135. hello world
  136. --- error_log eval
  137. [qr/call\srequire\(\"apisix\"\).http_header_filter_phase\(\)\sargs\:\{\}.*GET\s\/hello1\sHTTP\/1.1/,
  138. qr/call\srequire\(\"apisix\"\).http_header_filter_phase\(\)\sreturn\:\{\}.*GET\s\/hello1\sHTTP\/1.1/,
  139. qr/call\srequire\(\"apisix\"\).http_body_filter_phase\(\)\sargs\:\{\}.*GET\s\/hello1\sHTTP\/1.1/,
  140. qr/call\srequire\(\"apisix\"\).http_body_filter_phase\(\)\sreturn\:\{\}.*GET\s\/hello1\sHTTP\/1.1/,
  141. qr/call\srequire\(\"apisix\"\).http_log_phase\(\)\sargs\:\{\}.*GET\s\/hello1\sHTTP\/1.1/,
  142. qr/call\srequire\(\"apisix\"\).http_log_phase\(\)\sreturn\:\{\}.*GET\s\/hello1\sHTTP\/1.1/]
  143. --- no_error_log eval
  144. [qr/call\srequire\(\"apisix\"\).http_access_phase\(\)\sreturn\:\{\}.*GET\s\/hello1\sHTTP\/1.1/,
  145. qr/call\srequire\(\"apisix\"\).http_access_phase\(\)\sargs\:\{\}.*GET\s\/hello\sHTTP\/1.1/,
  146. qr/call\srequire\(\"apisix\"\).http_access_phase\(\)\sreturn\:\{\}.*GET\s\/hello\sHTTP\/1.1/,
  147. qr/call\srequire\(\"apisix\"\).http_header_filter_phase\(\)\sargs\:\{\}.*GET\s\/hello\sHTTP\/1.1/,
  148. qr/call\srequire\(\"apisix\"\).http_header_filter_phase\(\)\sreturn\:\{\}.*GET\s\/hello\sHTTP\/1.1/,
  149. qr/call\srequire\(\"apisix\"\).http_body_filter_phase\(\)\sargs\:\{\}.*GET\s\/hello\sHTTP\/1.1/,
  150. qr/call\srequire\(\"apisix\"\).http_body_filter_phase\(\)\sreturn\:\{\}.*GET\s\/hello\sHTTP\/1.1/,
  151. qr/call\srequire\(\"apisix\"\).http_log_phase\(\)\sargs\:\{\}.*GET\s\/hello\sHTTP\/1.1/,
  152. qr/call\srequire\(\"apisix\"\).http_log_phase\(\)\sreturn\:\{\}.*GET\s\/hello\sHTTP\/1.1/]
  153. === TEST 3: error dynamic enable header
  154. --- debug_config eval: $::debug_config
  155. --- config
  156. location /t {
  157. content_by_lua_block {
  158. local t = require("lib.test_admin").test
  159. local code, body = t('/apisix/admin/routes/1',
  160. ngx.HTTP_PUT,
  161. [[{
  162. "uri": "/hello",
  163. "upstream": {
  164. "nodes": {
  165. "127.0.0.1:1980": 1
  166. },
  167. "type": "roundrobin"
  168. }
  169. }]]
  170. )
  171. if code >= 300 then
  172. ngx.status = code
  173. return
  174. end
  175. ngx.sleep(0.6) -- wait for sync
  176. local headers = {}
  177. headers["X-APISIX-Dynamic-Error"] = ""
  178. local code, body = t('/hello',
  179. ngx.HTTP_GET,
  180. "",
  181. nil,
  182. headers
  183. )
  184. ngx.status = code
  185. ngx.say(body)
  186. }
  187. }
  188. --- request
  189. GET /t
  190. --- wait: 2
  191. --- response_body
  192. passed
  193. --- no_error_log
  194. call require("apisix").http_header_filter_phase() args:{}
  195. call require("apisix").http_header_filter_phase() return:{}
  196. call require("apisix").http_body_filter_phase() args:{}
  197. call require("apisix").http_body_filter_phase() return:{}
  198. call require("apisix").http_log_phase() args:{}
  199. call require("apisix").http_log_phase() return:{}
  200. === TEST 4: plugin filter log
  201. --- debug_config
  202. basic:
  203. enable: true
  204. http_filter:
  205. enable: true # enable or disable this feature
  206. enable_header_name: X-APISIX-Dynamic-Debug # the header name of dynamic enable
  207. hook_conf:
  208. enable: true # enable or disable this feature
  209. name: hook_test # the name of module and function list
  210. log_level: warn # log level
  211. is_print_input_args: true # print the input arguments
  212. is_print_return_value: true # print the return value
  213. hook_test: # module and function list, name: hook_test
  214. apisix.plugin: # required module name
  215. - filter # function name
  216. #END
  217. --- config
  218. location /t {
  219. content_by_lua_block {
  220. local t = require("lib.test_admin").test
  221. local code, body = t('/apisix/admin/routes/1',
  222. ngx.HTTP_PUT,
  223. [[{
  224. "uri": "/hello",
  225. "plugins": {
  226. "limit-count": {
  227. "count": 2,
  228. "time_window": 60,
  229. "rejected_code": 503,
  230. "key": "remote_addr"
  231. }
  232. },
  233. "upstream": {
  234. "nodes": {
  235. "127.0.0.1:1980": 1
  236. },
  237. "type": "roundrobin"
  238. },
  239. "upstream": {
  240. "nodes": {
  241. "127.0.0.1:1980": 1
  242. },
  243. "type": "roundrobin"
  244. }
  245. }]]
  246. )
  247. if code >= 300 then
  248. ngx.status = code
  249. return
  250. end
  251. ngx.sleep(0.6) -- wait for sync
  252. local headers = {}
  253. headers["X-APISIX-Dynamic-Debug"] = "" -- has the header name of dynamic debug
  254. local code, body = t('/hello',
  255. ngx.HTTP_GET,
  256. "",
  257. nil,
  258. headers
  259. )
  260. ngx.sleep(1.1)
  261. ngx.status = code
  262. ngx.say(body)
  263. }
  264. }
  265. --- request
  266. GET /t
  267. --- wait: 2
  268. --- response_body
  269. passed
  270. --- error_log
  271. filter(): call require("apisix.plugin").filter() args:{
  272. filter(): call require("apisix.plugin").filter() return:{
  273. === TEST 5: multiple requests, only output logs of the request with enable_header_name
  274. --- debug_config
  275. basic:
  276. enable: true
  277. http_filter:
  278. enable: true
  279. enable_header_name: X-APISIX-Dynamic-Debug
  280. hook_conf:
  281. enable: true
  282. name: hook_test
  283. log_level: warn
  284. is_print_input_args: true
  285. is_print_return_value: true
  286. hook_test:
  287. apisix.plugin:
  288. - filter
  289. #END
  290. --- config
  291. location /t {
  292. content_by_lua_block {
  293. local t = require("lib.test_admin").test
  294. local code, body = t('/apisix/admin/routes/1',
  295. ngx.HTTP_PUT,
  296. [[{
  297. "uri": "/mysleep*",
  298. "upstream": {
  299. "nodes": {
  300. "127.0.0.1:1980": 1
  301. },
  302. "type": "roundrobin"
  303. }
  304. }]]
  305. )
  306. if code >= 300 then
  307. ngx.status = code
  308. return
  309. end
  310. ngx.sleep(0.6) -- wait for sync
  311. local res, err
  312. local http = require "resty.http"
  313. local httpc = http.new()
  314. for i = 1, 3 do
  315. if i == 1 then
  316. local headers = {}
  317. headers["X-APISIX-Dynamic-Debug"] = ""
  318. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  319. .. "/mysleep?seconds=1"
  320. local res, err = httpc:request_uri(uri, {method = "GET", headers = headers})
  321. if not res then
  322. ngx.say(err)
  323. return
  324. end
  325. else
  326. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  327. .. "/mysleep?seconds=0.1"
  328. res, err = httpc:request_uri(uri)
  329. if not res then
  330. ngx.say(err)
  331. return
  332. end
  333. end
  334. end
  335. ngx.say("passed")
  336. }
  337. }
  338. --- request
  339. GET /t
  340. --- wait: 2
  341. --- response_body
  342. passed
  343. --- error_log eval
  344. qr/call\srequire\(\"apisix.plugin\"\).filter\(\)\sreturn.*GET\s\/mysleep\?seconds\=1\sHTTP\/1.1/
  345. --- no_error_log eval
  346. qr/call\srequire\(\"apisix.plugin\"\).filter\(\)\sreturn.*GET\s\/mysleep\?seconds\=0.1\sHTTP\/1.1/
  347. === TEST 6: hook function with ctx as param
  348. # ai module would conflict with the debug module
  349. --- extra_yaml_config
  350. plugins:
  351. #ai
  352. - example-plugin
  353. --- debug_config
  354. basic:
  355. enable: true
  356. http_filter:
  357. enable: true # enable or disable this feature
  358. enable_header_name: X-APISIX-Dynamic-Debug # the header name of dynamic enable
  359. hook_conf:
  360. enable: true # enable or disable this feature
  361. name: hook_test # the name of module and function list
  362. log_level: warn # log level
  363. is_print_input_args: true # print the input arguments
  364. is_print_return_value: true # print the return value
  365. hook_test: # module and function list, name: hook_test
  366. apisix.balancer: # required module name
  367. - pick_server # function name
  368. #END
  369. --- config
  370. location /t {
  371. content_by_lua_block {
  372. local t = require("lib.test_admin").test
  373. local code, body = t('/apisix/admin/routes/1',
  374. ngx.HTTP_PUT,
  375. [[{
  376. "uri": "/hello",
  377. "upstream": {
  378. "nodes": {
  379. "127.0.0.1:1980": 1
  380. },
  381. "type": "roundrobin"
  382. }
  383. }]]
  384. )
  385. if code >= 300 then
  386. ngx.status = code
  387. return
  388. end
  389. ngx.sleep(0.6) -- wait for sync
  390. local headers = {}
  391. headers["X-APISIX-Dynamic-Debug"] = ""
  392. local code, body = t('/hello',
  393. ngx.HTTP_GET,
  394. "",
  395. nil,
  396. headers
  397. )
  398. ngx.status = code
  399. ngx.say(body)
  400. }
  401. }
  402. --- request
  403. GET /t
  404. --- wait: 2
  405. --- response_body
  406. passed
  407. --- error_log
  408. call require("apisix.balancer").pick_server() args:{
  409. call require("apisix.balancer").pick_server() return:{