sanity.t 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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(2); # repeat each test to ensure after_balance is called correctly
  19. log_level('info');
  20. no_root_location();
  21. worker_connections(1024);
  22. no_shuffle();
  23. add_block_preprocessor(sub {
  24. my ($block) = @_;
  25. if ($block->apisix_yaml) {
  26. if (!$block->yaml_config) {
  27. my $yaml_config = <<_EOC_;
  28. apisix:
  29. node_listen: 1984
  30. enable_admin: false
  31. deployment:
  32. role: data_plane
  33. role_data_plane:
  34. config_provider: yaml
  35. _EOC_
  36. $block->set_value("yaml_config", $yaml_config);
  37. }
  38. my $route = <<_EOC_;
  39. routes:
  40. - upstream_id: 1
  41. uris:
  42. - /hello
  43. - /mysleep
  44. #END
  45. _EOC_
  46. $block->set_value("apisix_yaml", $block->apisix_yaml . $route);
  47. }
  48. if (!$block->request) {
  49. $block->set_value("request", "GET /hello");
  50. }
  51. });
  52. run_tests();
  53. __DATA__
  54. === TEST 1: sanity
  55. --- apisix_yaml
  56. upstreams:
  57. - id: 1
  58. type: least_conn
  59. nodes:
  60. - host: 127.0.0.1
  61. port: 1979
  62. weight: 2
  63. priority: 1
  64. - host: 127.0.0.2
  65. port: 1979
  66. weight: 1
  67. priority: 1
  68. - host: 127.0.0.3
  69. port: 1979
  70. weight: 2
  71. priority: 0
  72. - host: 127.0.0.4
  73. port: 1979
  74. weight: 1
  75. priority: 0
  76. - host: 127.0.0.1
  77. port: 1980
  78. weight: 2
  79. priority: -1
  80. --- response_body
  81. hello world
  82. --- error_log
  83. connect() failed
  84. failed to get server from current priority 1, try next one
  85. failed to get server from current priority 0, try next one
  86. --- grep_error_log eval
  87. qr/proxy request to \S+/
  88. --- grep_error_log_out
  89. proxy request to 127.0.0.1:1979
  90. proxy request to 127.0.0.2:1979
  91. proxy request to 127.0.0.3:1979
  92. proxy request to 127.0.0.4:1979
  93. proxy request to 127.0.0.1:1980
  94. === TEST 2: all failed
  95. --- apisix_yaml
  96. upstreams:
  97. - id: 1
  98. type: least_conn
  99. nodes:
  100. - host: 127.0.0.1
  101. port: 1979
  102. weight: 2
  103. priority: 1
  104. - host: 127.0.0.2
  105. port: 1979
  106. weight: 1
  107. priority: 0
  108. - host: 127.0.0.1
  109. port: 1979
  110. weight: 2
  111. priority: -1
  112. --- error_code: 502
  113. --- error_log
  114. connect() failed
  115. --- grep_error_log eval
  116. qr/proxy request to \S+/
  117. --- grep_error_log_out
  118. proxy request to 127.0.0.1:1979
  119. proxy request to 127.0.0.2:1979
  120. proxy request to 127.0.0.1:1979
  121. === TEST 3: default priority is zero
  122. --- apisix_yaml
  123. upstreams:
  124. - id: 1
  125. type: least_conn
  126. nodes:
  127. - host: 127.0.0.1
  128. port: 1979
  129. weight: 2
  130. priority: 1
  131. - host: 127.0.0.2
  132. port: 1979
  133. weight: 1
  134. - host: 127.0.0.1
  135. port: 1980
  136. weight: 2
  137. priority: -1
  138. --- response_body
  139. hello world
  140. --- error_log
  141. connect() failed
  142. --- grep_error_log eval
  143. qr/proxy request to \S+/
  144. --- grep_error_log_out
  145. proxy request to 127.0.0.1:1979
  146. proxy request to 127.0.0.2:1979
  147. proxy request to 127.0.0.1:1980
  148. === TEST 4: least_conn
  149. --- apisix_yaml
  150. upstreams:
  151. - id: 1
  152. type: least_conn
  153. nodes:
  154. - host: 127.0.0.1
  155. port: 1979
  156. weight: 2
  157. priority: 1
  158. - host: 127.0.0.1
  159. port: 1980
  160. weight: 3
  161. priority: -1
  162. - host: 0.0.0.0
  163. port: 1980
  164. weight: 2
  165. priority: -1
  166. --- config
  167. location /t {
  168. content_by_lua_block {
  169. local http = require "resty.http"
  170. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  171. .. "/mysleep?seconds=0.1"
  172. local t = {}
  173. for i = 1, 3 do
  174. local th = assert(ngx.thread.spawn(function(i)
  175. local httpc = http.new()
  176. -- the retry can be happened before starting the new request
  177. -- so we exclude all the first tries from the expected log
  178. local res, err = httpc:request_uri(uri..i, {method = "GET"})
  179. if not res then
  180. ngx.log(ngx.ERR, err)
  181. return
  182. end
  183. end, i))
  184. table.insert(t, th)
  185. end
  186. for i, th in ipairs(t) do
  187. ngx.thread.wait(th)
  188. end
  189. }
  190. }
  191. --- request
  192. GET /t
  193. --- error_log
  194. connect() failed
  195. --- grep_error_log eval
  196. qr/proxy request to \S+:1980 while connecting to upstream/
  197. --- grep_error_log_out
  198. proxy request to 127.0.0.1:1980 while connecting to upstream
  199. proxy request to 0.0.0.0:1980 while connecting to upstream
  200. proxy request to 127.0.0.1:1980 while connecting to upstream
  201. === TEST 5: roundrobin
  202. --- apisix_yaml
  203. upstreams:
  204. - id: 1
  205. type: roundrobin
  206. nodes:
  207. - host: 127.0.0.1
  208. port: 1979
  209. weight: 1000
  210. priority: 1
  211. - host: 127.0.0.2
  212. port: 1979
  213. weight: 1
  214. priority: 1
  215. - host: 127.0.0.3
  216. port: 1979
  217. weight: 1000
  218. priority: -1
  219. - host: 127.0.0.4
  220. port: 1979
  221. weight: 1
  222. priority: -1
  223. --- error_code: 502
  224. --- error_log
  225. connect() failed
  226. --- grep_error_log eval
  227. qr/proxy request to \S+/
  228. --- grep_error_log_out
  229. proxy request to 127.0.0.1:1979
  230. proxy request to 127.0.0.2:1979
  231. proxy request to 127.0.0.3:1979
  232. proxy request to 127.0.0.4:1979
  233. === TEST 6: ewma
  234. --- apisix_yaml
  235. upstreams:
  236. - id: 1
  237. type: ewma
  238. key: remote_addr
  239. nodes:
  240. - host: 127.0.0.1
  241. port: 1979
  242. weight: 2
  243. priority: 1
  244. - host: 127.0.0.2
  245. port: 1979
  246. weight: 1
  247. priority: 0
  248. - host: 127.0.0.3
  249. port: 1979
  250. weight: 2
  251. priority: -1
  252. --- error_code: 502
  253. --- error_log
  254. connect() failed
  255. --- grep_error_log eval
  256. qr/proxy request to \S+/
  257. --- grep_error_log_out
  258. proxy request to 127.0.0.1:1979
  259. proxy request to 127.0.0.2:1979
  260. proxy request to 127.0.0.3:1979
  261. === TEST 7: chash
  262. --- apisix_yaml
  263. upstreams:
  264. - id: 1
  265. type: chash
  266. key: remote_addr
  267. nodes:
  268. - host: 127.0.0.1
  269. port: 1979
  270. weight: 2
  271. priority: 1
  272. - host: 127.0.0.2
  273. port: 1979
  274. weight: 1
  275. priority: 1
  276. - host: 127.0.0.3
  277. port: 1979
  278. weight: 2
  279. priority: -1
  280. - host: 127.0.0.4
  281. port: 1979
  282. weight: 1
  283. priority: -1
  284. --- error_code: 502
  285. --- error_log
  286. connect() failed
  287. --- grep_error_log eval
  288. qr/proxy request to \S+/
  289. --- grep_error_log_out
  290. proxy request to 127.0.0.1:1979
  291. proxy request to 127.0.0.2:1979
  292. proxy request to 127.0.0.4:1979
  293. proxy request to 127.0.0.3:1979