upstream-status-5xx.t 9.5 KB

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