healthcheck2.t 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. worker_connections(256);
  23. add_block_preprocessor(sub {
  24. my ($block) = @_;
  25. if (!$block->yaml_config) {
  26. my $yaml_config = <<_EOC_;
  27. apisix:
  28. node_listen: 1984
  29. deployment:
  30. role: data_plane
  31. role_data_plane:
  32. config_provider: yaml
  33. _EOC_
  34. $block->set_value("yaml_config", $yaml_config);
  35. }
  36. if (!$block->request) {
  37. $block->set_value("request", "GET /t");
  38. }
  39. });
  40. run_tests();
  41. __DATA__
  42. === TEST 1: can't use service_name with nodes
  43. --- apisix_yaml
  44. routes:
  45. -
  46. uris:
  47. - /hello
  48. upstream_id: 1
  49. upstreams:
  50. - service_name: abaaba
  51. discovery_type: eureka
  52. nodes:
  53. "127.0.0.1:80": 1
  54. type: roundrobin
  55. id: 1
  56. #END
  57. --- error_log
  58. value should match only one schema, but matches both schemas 1 and 2
  59. --- request
  60. GET /hello
  61. --- error_code: 502
  62. === TEST 2: route + service
  63. --- apisix_yaml
  64. services:
  65. - id: 1
  66. upstream:
  67. type: roundrobin
  68. nodes:
  69. "127.0.0.1:1980": 1
  70. "127.0.0.1:1970": 1
  71. checks:
  72. active:
  73. http_path: /status
  74. host: foo.com
  75. healthy:
  76. interval: 1
  77. successes: 1
  78. unhealthy:
  79. interval: 1
  80. http_failures: 2
  81. routes:
  82. - service_id: 1
  83. uri: /server_port
  84. #END
  85. --- config
  86. location /t {
  87. content_by_lua_block {
  88. local http = require "resty.http"
  89. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  90. .. "/server_port"
  91. do
  92. local httpc = http.new()
  93. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  94. end
  95. ngx.sleep(2.5)
  96. local ports_count = {}
  97. for i = 1, 12 do
  98. local httpc = http.new()
  99. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  100. if not res then
  101. ngx.say(err)
  102. return
  103. end
  104. ports_count[res.body] = (ports_count[res.body] or 0) + 1
  105. end
  106. local ports_arr = {}
  107. for port, count in pairs(ports_count) do
  108. table.insert(ports_arr, {port = port, count = count})
  109. end
  110. local function cmd(a, b)
  111. return a.port > b.port
  112. end
  113. table.sort(ports_arr, cmd)
  114. ngx.say(require("toolkit.json").encode(ports_arr))
  115. ngx.exit(200)
  116. }
  117. }
  118. --- response_body
  119. [{"count":12,"port":"1980"}]
  120. --- grep_error_log eval
  121. qr/\([^)]+\) unhealthy .* for '.*'/
  122. --- grep_error_log_out
  123. (upstream#/services/1) unhealthy TCP increment (1/2) for 'foo.com(127.0.0.1:1970)'
  124. (upstream#/services/1) unhealthy TCP increment (2/2) for 'foo.com(127.0.0.1:1970)'
  125. --- timeout: 10
  126. === TEST 3: route override service
  127. --- apisix_yaml
  128. services:
  129. - id: 1
  130. upstream:
  131. type: roundrobin
  132. nodes:
  133. "127.0.0.2:1980": 1
  134. "127.0.0.2:1970": 1
  135. checks:
  136. active:
  137. http_path: /status
  138. host: foo.com
  139. healthy:
  140. interval: 1
  141. successes: 1
  142. unhealthy:
  143. interval: 1
  144. http_failures: 2
  145. routes:
  146. - service_id: 1
  147. uri: /server_port
  148. upstream:
  149. type: roundrobin
  150. nodes:
  151. "127.0.0.1:1980": 1
  152. "127.0.0.1:1970": 1
  153. checks:
  154. active:
  155. http_path: /status
  156. host: foo.com
  157. healthy:
  158. interval: 1
  159. successes: 1
  160. unhealthy:
  161. interval: 1
  162. http_failures: 2
  163. #END
  164. --- config
  165. location /t {
  166. content_by_lua_block {
  167. local http = require "resty.http"
  168. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  169. .. "/server_port"
  170. do
  171. local httpc = http.new()
  172. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  173. end
  174. ngx.sleep(2.5)
  175. local ports_count = {}
  176. for i = 1, 12 do
  177. local httpc = http.new()
  178. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  179. if not res then
  180. ngx.say(err)
  181. return
  182. end
  183. ports_count[res.body] = (ports_count[res.body] or 0) + 1
  184. end
  185. local ports_arr = {}
  186. for port, count in pairs(ports_count) do
  187. table.insert(ports_arr, {port = port, count = count})
  188. end
  189. local function cmd(a, b)
  190. return a.port > b.port
  191. end
  192. table.sort(ports_arr, cmd)
  193. ngx.say(require("toolkit.json").encode(ports_arr))
  194. ngx.exit(200)
  195. }
  196. }
  197. --- response_body
  198. [{"count":12,"port":"1980"}]
  199. --- grep_error_log eval
  200. qr/\([^)]+\) unhealthy .* for '.*'/
  201. --- grep_error_log_out
  202. (upstream#/routes/arr_1) unhealthy TCP increment (1/2) for 'foo.com(127.0.0.1:1970)'
  203. (upstream#/routes/arr_1) unhealthy TCP increment (2/2) for 'foo.com(127.0.0.1:1970)'
  204. --- timeout: 10
  205. === TEST 4: pass the configured host (pass_host == "pass")
  206. --- apisix_yaml
  207. routes:
  208. - id: 1
  209. uri: /server_port
  210. upstream:
  211. type: roundrobin
  212. nodes:
  213. "localhost:1980": 1
  214. "127.0.0.1:1981": 1
  215. checks:
  216. active:
  217. http_path: /status
  218. healthy:
  219. interval: 1
  220. successes: 1
  221. unhealthy:
  222. interval: 1
  223. http_failures: 2
  224. #END
  225. --- config
  226. location /t {
  227. content_by_lua_block {
  228. local http = require "resty.http"
  229. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  230. .. "/server_port"
  231. do
  232. local httpc = http.new()
  233. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  234. end
  235. ngx.sleep(1)
  236. }
  237. }
  238. --- no_error_log
  239. client request host: localhost
  240. --- error_log
  241. client request host: 127.0.0.1
  242. === TEST 5: pass the configured host (pass_host == "node")
  243. --- apisix_yaml
  244. routes:
  245. - id: 1
  246. uri: /server_port
  247. upstream:
  248. type: roundrobin
  249. pass_host: node
  250. nodes:
  251. "localhost:1980": 1
  252. "127.0.0.1:1981": 1
  253. checks:
  254. active:
  255. http_path: /status
  256. healthy:
  257. interval: 1
  258. successes: 1
  259. unhealthy:
  260. interval: 1
  261. http_failures: 2
  262. #END
  263. --- config
  264. location /t {
  265. content_by_lua_block {
  266. local http = require "resty.http"
  267. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  268. .. "/server_port"
  269. do
  270. local httpc = http.new()
  271. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  272. end
  273. ngx.sleep(1)
  274. }
  275. }
  276. --- error_log
  277. client request host: localhost
  278. client request host: 127.0.0.1
  279. === TEST 6: pass the configured host (pass_host == "rewrite")
  280. --- apisix_yaml
  281. routes:
  282. - id: 1
  283. uri: /server_port
  284. upstream:
  285. type: roundrobin
  286. pass_host: rewrite
  287. upstream_host: foo.com
  288. nodes:
  289. "localhost:1980": 1
  290. "127.0.0.1:1981": 1
  291. checks:
  292. active:
  293. http_path: /status
  294. healthy:
  295. interval: 1
  296. successes: 1
  297. unhealthy:
  298. interval: 1
  299. http_failures: 2
  300. #END
  301. --- config
  302. location /t {
  303. content_by_lua_block {
  304. local http = require "resty.http"
  305. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  306. .. "/server_port"
  307. do
  308. local httpc = http.new()
  309. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  310. end
  311. ngx.sleep(1)
  312. }
  313. }
  314. --- no_error_log
  315. client request host: localhost
  316. client request host: 127.0.0.1
  317. --- error_log
  318. client request host: foo.com