upstream-status-all.t 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. worker_connections(256);
  21. no_root_location();
  22. no_shuffle();
  23. add_block_preprocessor(sub {
  24. my ($block) = @_;
  25. if (!$block->request) {
  26. $block->set_value("request", "GET /t");
  27. }
  28. });
  29. run_tests();
  30. __DATA__
  31. === TEST 1: set route(id: 1) and available upstream and show_upstream_status_in_response_header: true
  32. --- config
  33. location /t {
  34. content_by_lua_block {
  35. local t = require("lib.test_admin").test
  36. local code, body = t('/apisix/admin/routes/1',
  37. ngx.HTTP_PUT,
  38. [[{
  39. "methods": ["GET"],
  40. "upstream": {
  41. "nodes": {
  42. "127.0.0.1:1980": 1
  43. },
  44. "type": "roundrobin"
  45. },
  46. "uri": "/hello"
  47. }]]
  48. )
  49. if code >= 300 then
  50. ngx.status = code
  51. end
  52. ngx.say(body)
  53. }
  54. }
  55. --- response_body
  56. passed
  57. === TEST 2: hit the route and $upstream_status is 200
  58. --- yaml_config
  59. apisix:
  60. show_upstream_status_in_response_header: true
  61. --- request
  62. GET /hello
  63. --- response_body
  64. hello world
  65. --- response_headers
  66. X-APISIX-Upstream-Status: 200
  67. === TEST 3: set route(id: 1) and set the timeout field
  68. --- config
  69. location /t {
  70. content_by_lua_block {
  71. local t = require("lib.test_admin").test
  72. local code, body = t('/apisix/admin/routes/1',
  73. ngx.HTTP_PUT,
  74. [[{
  75. "methods": ["GET"],
  76. "upstream": {
  77. "nodes": {
  78. "127.0.0.1:1980": 1
  79. },
  80. "type": "roundrobin",
  81. "timeout": {
  82. "connect": 0.5,
  83. "send": 0.5,
  84. "read": 0.5
  85. }
  86. },
  87. "uri": "/mysleep"
  88. }]]
  89. )
  90. if code >= 300 then
  91. ngx.status = code
  92. end
  93. ngx.say(body)
  94. }
  95. }
  96. --- response_body
  97. passed
  98. === TEST 4: hit routes (timeout) and $upstream_status is 504
  99. --- yaml_config
  100. apisix:
  101. show_upstream_status_in_response_header: true
  102. --- request
  103. GET /mysleep?seconds=1
  104. --- error_code: 504
  105. --- response_body eval
  106. qr/504 Gateway Time-out/
  107. --- response_headers
  108. X-APISIX-Upstream-Status: 504
  109. --- error_log
  110. Connection timed out
  111. === TEST 5: set route(id: 1), upstream service is not available
  112. --- config
  113. location /t {
  114. content_by_lua_block {
  115. local t = require("lib.test_admin").test
  116. local code, body = t('/apisix/admin/routes/1',
  117. ngx.HTTP_PUT,
  118. [[{
  119. "methods": ["GET"],
  120. "upstream": {
  121. "nodes": {
  122. "127.0.0.1:1": 1
  123. },
  124. "type": "roundrobin"
  125. },
  126. "uri": "/hello"
  127. }]]
  128. )
  129. if code >= 300 then
  130. ngx.status = code
  131. end
  132. ngx.say(body)
  133. }
  134. }
  135. --- response_body
  136. passed
  137. === TEST 6: hit routes and $upstream_status is 502
  138. --- yaml_config
  139. apisix:
  140. show_upstream_status_in_response_header: true
  141. --- request
  142. GET /hello
  143. --- error_code: 502
  144. --- response_body eval
  145. qr/502 Bad Gateway/
  146. --- response_headers
  147. X-APISIX-Upstream-Status: 502
  148. --- error_log
  149. Connection refused
  150. === TEST 7: set route(id: 1) and uri is `/server_error`
  151. --- config
  152. location /t {
  153. content_by_lua_block {
  154. local t = require("lib.test_admin").test
  155. local code, body = t('/apisix/admin/routes/1',
  156. ngx.HTTP_PUT,
  157. [[{
  158. "methods": ["GET"],
  159. "upstream": {
  160. "nodes": {
  161. "127.0.0.1:1980": 1
  162. },
  163. "type": "roundrobin"
  164. },
  165. "uri": "/server_error"
  166. }]]
  167. )
  168. if code >= 300 then
  169. ngx.status = code
  170. end
  171. ngx.say(body)
  172. }
  173. }
  174. --- response_body
  175. passed
  176. === TEST 8: hit routes and $upstream_status is 500
  177. --- yaml_config
  178. apisix:
  179. show_upstream_status_in_response_header: true
  180. --- request
  181. GET /server_error
  182. --- error_code: 500
  183. --- response_body eval
  184. qr/500 Internal Server Error/
  185. --- response_headers
  186. X-APISIX-Upstream-Status: 500
  187. --- error_log
  188. 500 Internal Server Error
  189. === TEST 9: set upstream(id: 1, retries = 2), has available upstream
  190. --- config
  191. location /t {
  192. content_by_lua_block {
  193. local t = require("lib.test_admin").test
  194. local code, body = t('/apisix/admin/upstreams/1',
  195. ngx.HTTP_PUT,
  196. [[{
  197. "nodes": {
  198. "127.1.0.2:1": 1,
  199. "127.0.0.1:1980": 1
  200. },
  201. "retries": 2,
  202. "type": "roundrobin"
  203. }]]
  204. )
  205. if code >= 300 then
  206. ngx.status = code
  207. end
  208. ngx.say(body)
  209. }
  210. }
  211. --- response_body
  212. passed
  213. === TEST 10: set route(id: 1) and bind the upstream_id
  214. --- config
  215. location /t {
  216. content_by_lua_block {
  217. local t = require("lib.test_admin").test
  218. local code, body = t('/apisix/admin/routes/1',
  219. ngx.HTTP_PUT,
  220. [[{
  221. "uri": "/hello",
  222. "upstream_id": "1"
  223. }]]
  224. )
  225. if code >= 300 then
  226. ngx.status = code
  227. end
  228. ngx.say(body)
  229. }
  230. }
  231. --- response_body
  232. passed
  233. === TEST 11: hit routes and $upstream_status is `502, 200`
  234. --- yaml_config
  235. apisix:
  236. show_upstream_status_in_response_header: true
  237. --- request
  238. GET /hello
  239. --- response_body
  240. hello world
  241. --- response_headers_raw_like eval
  242. qr/X-APISIX-Upstream-Status: 502, 200|X-APISIX-Upstream-Status: 200/
  243. --- error_log
  244. === TEST 12: set upstream(id: 1, retries = 2), all upstream nodes are unavailable
  245. --- config
  246. location /t {
  247. content_by_lua_block {
  248. local t = require("lib.test_admin").test
  249. local code, body = t('/apisix/admin/upstreams/1',
  250. ngx.HTTP_PUT,
  251. [[{
  252. "nodes": {
  253. "127.0.0.3:1": 1,
  254. "127.0.0.2:1": 1,
  255. "127.0.0.1:1": 1
  256. },
  257. "retries": 2,
  258. "type": "roundrobin"
  259. }]]
  260. )
  261. if code >= 300 then
  262. ngx.status = code
  263. end
  264. ngx.say(body)
  265. }
  266. }
  267. --- response_body
  268. passed
  269. === TEST 13: hit routes, retry between upstream failed, $upstream_status is `502, 502, 502`
  270. --- yaml_config
  271. apisix:
  272. show_upstream_status_in_response_header: true
  273. --- request
  274. GET /hello
  275. --- error_code: 502
  276. --- response_headers_raw_like eval
  277. qr/X-APISIX-Upstream-Status: 502, 502, 502/
  278. --- error_log
  279. Connection refused
  280. === TEST 14: return 500 status code from APISIX
  281. --- config
  282. location /t {
  283. content_by_lua_block {
  284. local t = require("lib.test_admin").test
  285. local code, body = t('/apisix/admin/routes/1',
  286. ngx.HTTP_PUT,
  287. [[{
  288. "plugins": {
  289. "fault-injection": {
  290. "abort": {
  291. "http_status": 500,
  292. "body": "Fault Injection!\n"
  293. }
  294. }
  295. },
  296. "uri": "/hello"
  297. }]]
  298. )
  299. if code >= 300 then
  300. ngx.status = code
  301. end
  302. ngx.say(body)
  303. }
  304. }
  305. --- response_body
  306. passed
  307. === TEST 15: hit routes, status code is 500
  308. --- yaml_config
  309. apisix:
  310. show_upstream_status_in_response_header: true
  311. --- request
  312. GET /hello
  313. --- error_code: 500
  314. --- response_body
  315. Fault Injection!
  316. --- grep_error_log eval
  317. qr/X-APISIX-Upstream-Status: 500/
  318. --- grep_error_log_out
  319. === TEST 16: return 200 status code from APISIX
  320. --- config
  321. location /t {
  322. content_by_lua_block {
  323. local t = require("lib.test_admin").test
  324. local code, body = t('/apisix/admin/routes/1',
  325. ngx.HTTP_PUT,
  326. [[{
  327. "plugins": {
  328. "fault-injection": {
  329. "abort": {
  330. "http_status": 200,
  331. "body": "Fault Injection!\n"
  332. }
  333. }
  334. },
  335. "uri": "/hello"
  336. }]]
  337. )
  338. if code >= 300 then
  339. ngx.status = code
  340. end
  341. ngx.say(body)
  342. }
  343. }
  344. --- response_body
  345. passed
  346. === TEST 17: hit routes, status code is 200
  347. --- yaml_config
  348. apisix:
  349. show_upstream_status_in_response_header: true
  350. --- request
  351. GET /hello
  352. --- response_body
  353. Fault Injection!
  354. --- grep_error_log eval
  355. qr/X-APISIX-Upstream-Status: 200/
  356. --- grep_error_log_out
  357. === TEST 18: return 200 status code from APISIX (with show_upstream_status_in_response_header:false)
  358. --- config
  359. location /t {
  360. content_by_lua_block {
  361. local t = require("lib.test_admin").test
  362. local code, body = t('/apisix/admin/routes/1',
  363. ngx.HTTP_PUT,
  364. [[{
  365. "plugins": {
  366. "fault-injection": {
  367. "abort": {
  368. "http_status": 200,
  369. "body": "Fault Injection!\n"
  370. }
  371. }
  372. },
  373. "uri": "/hello"
  374. }]]
  375. )
  376. if code >= 300 then
  377. ngx.status = code
  378. end
  379. ngx.say(body)
  380. }
  381. }
  382. --- response_body
  383. passed
  384. === TEST 19: hit routes, status code is 200
  385. --- yaml_config
  386. apisix:
  387. show_upstream_status_in_response_header: false
  388. --- request
  389. GET /hello
  390. --- response_body
  391. Fault Injection!
  392. --- grep_error_log eval
  393. qr/X-APISIX-Upstream-Status: 200/
  394. --- grep_error_log_out