upstream-retries.t 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 upstream(id: 1), by default retries count = number of nodes
  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/upstreams/1',
  31. ngx.HTTP_PUT,
  32. [[{
  33. "nodes": {
  34. "127.0.0.1:1": 1,
  35. "127.0.0.2:1": 1,
  36. "127.0.0.3:1": 1,
  37. "127.0.0.4:1": 1
  38. },
  39. "type": "roundrobin"
  40. }]]
  41. )
  42. if code >= 300 then
  43. ngx.status = code
  44. end
  45. ngx.say(body)
  46. }
  47. }
  48. --- request
  49. GET /t
  50. --- response_body
  51. passed
  52. === TEST 2: set route(id: 1)
  53. --- config
  54. location /t {
  55. content_by_lua_block {
  56. local t = require("lib.test_admin").test
  57. local code, body = t('/apisix/admin/routes/1',
  58. ngx.HTTP_PUT,
  59. [[{
  60. "uri": "/hello",
  61. "upstream_id": "1"
  62. }]]
  63. )
  64. if code >= 300 then
  65. ngx.status = code
  66. end
  67. ngx.say(body)
  68. }
  69. }
  70. --- request
  71. GET /t
  72. --- response_body
  73. passed
  74. === TEST 3: /not_found
  75. --- request
  76. GET /not_found
  77. --- error_code: 404
  78. --- response_body
  79. {"error_msg":"404 Route Not Found"}
  80. === TEST 4: hit routes
  81. --- request
  82. GET /hello
  83. --- error_code: 502
  84. --- grep_error_log eval
  85. qr/\[error\]/
  86. --- grep_error_log_out
  87. [error]
  88. [error]
  89. [error]
  90. [error]
  91. === TEST 5: hit routes
  92. --- request
  93. GET /hello
  94. --- error_code: 502
  95. --- error_log
  96. connect() failed
  97. === TEST 6: set upstream(id: 1), retries = 1
  98. --- config
  99. location /t {
  100. content_by_lua_block {
  101. local t = require("lib.test_admin").test
  102. local code, body = t('/apisix/admin/upstreams/1',
  103. ngx.HTTP_PUT,
  104. [[{
  105. "nodes": {
  106. "127.0.0.1:1": 1,
  107. "127.0.0.2:1": 1,
  108. "127.0.0.3:1": 1,
  109. "127.0.0.4:1": 1
  110. },
  111. "retries": 1,
  112. "type": "roundrobin"
  113. }]]
  114. )
  115. if code >= 300 then
  116. ngx.status = code
  117. end
  118. ngx.say(body)
  119. }
  120. }
  121. --- request
  122. GET /t
  123. --- response_body
  124. passed
  125. === TEST 7: hit routes
  126. --- request
  127. GET /hello
  128. --- error_code: 502
  129. --- grep_error_log eval
  130. qr/\[error\]/
  131. --- grep_error_log_out
  132. [error]
  133. [error]
  134. === TEST 8: set upstream(id: 1), retries = 0
  135. --- config
  136. location /t {
  137. content_by_lua_block {
  138. local t = require("lib.test_admin").test
  139. local code, body = t('/apisix/admin/upstreams/1',
  140. ngx.HTTP_PUT,
  141. [[{
  142. "nodes": {
  143. "127.0.0.1:1": 1,
  144. "127.0.0.2:1": 1,
  145. "127.0.0.3:1": 1,
  146. "127.0.0.4:1": 1
  147. },
  148. "retries": 0,
  149. "type": "roundrobin"
  150. }]]
  151. )
  152. if code >= 300 then
  153. ngx.status = code
  154. end
  155. ngx.say(body)
  156. }
  157. }
  158. --- request
  159. GET /t
  160. --- response_body
  161. passed
  162. === TEST 9: hit routes
  163. --- request
  164. GET /hello
  165. --- error_code: 502
  166. --- grep_error_log eval
  167. qr/\[error\]/
  168. --- grep_error_log_out
  169. [error]
  170. === TEST 10: set upstream, retries > number of nodes, only try number of nodes time
  171. --- config
  172. location /t {
  173. content_by_lua_block {
  174. local t = require("lib.test_admin").test
  175. local code, body = t('/apisix/admin/upstreams/1',
  176. ngx.HTTP_PUT,
  177. [[{
  178. "nodes": {
  179. "127.0.0.1:1": 1,
  180. "127.0.0.2:1": 1
  181. },
  182. "retries": 3,
  183. "type": "roundrobin"
  184. }]]
  185. )
  186. if code >= 300 then
  187. ngx.status = code
  188. end
  189. ngx.say(body)
  190. }
  191. }
  192. --- request
  193. GET /t
  194. --- response_body
  195. passed
  196. === TEST 11: hit routes
  197. --- request
  198. GET /hello
  199. --- error_code: 502
  200. --- error_log
  201. all upstream servers tried
  202. --- grep_error_log eval
  203. qr/connect\(\) failed/
  204. --- grep_error_log_out
  205. connect() failed
  206. connect() failed
  207. === TEST 12: don't retry the same node twice
  208. --- request
  209. GET /hello
  210. --- error_code: 502
  211. --- error_log
  212. Connection refused
  213. failed to find valid upstream server
  214. proxy request to 127.0.0.1:1
  215. proxy request to 127.0.0.2:1
  216. === TEST 13: stop proxy to next upstream by retry_timeout
  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. "upstream": {
  225. "nodes": {
  226. "127.0.0.1:1980": 100,
  227. "127.0.0.1:1981": 100,
  228. "127.0.0.1:1982": 100
  229. },
  230. "retries": 10,
  231. "retry_timeout": 0.15,
  232. "type": "roundrobin"
  233. },
  234. "uri": "/mysleep"
  235. }]]
  236. )
  237. if code ~= 200 then
  238. ngx.say(body)
  239. return
  240. end
  241. local http = require "resty.http"
  242. local httpc = http.new()
  243. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  244. .. "/mysleep?abort=true&seconds=0.1"
  245. local res, err = httpc:request_uri(uri)
  246. if not res then
  247. ngx.say(err)
  248. return
  249. end
  250. ngx.status = res.status
  251. ngx.say(res.status)
  252. }
  253. }
  254. --- request
  255. GET /t
  256. --- error_code: 502
  257. --- error_log
  258. proxy retry timeout, retry count: 1