2
0

health-checker.t 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. 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. #END
  44. _EOC_
  45. $block->set_value("apisix_yaml", $block->apisix_yaml . $route);
  46. }
  47. if (!$block->request) {
  48. $block->set_value("request", "GET /hello");
  49. }
  50. });
  51. run_tests();
  52. __DATA__
  53. === TEST 1: all are down detected by health checker
  54. --- apisix_yaml
  55. upstreams:
  56. - id: 1
  57. type: least_conn
  58. nodes:
  59. - host: 127.0.0.1
  60. port: 1979
  61. weight: 2
  62. priority: 123
  63. - host: 127.0.0.2
  64. port: 1979
  65. weight: 3
  66. priority: -1
  67. checks:
  68. active:
  69. http_path: "/status"
  70. healthy:
  71. interval: 1
  72. successes: 1
  73. unhealthy:
  74. interval: 1
  75. http_failures: 1
  76. --- config
  77. location /t {
  78. content_by_lua_block {
  79. local http = require "resty.http"
  80. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  81. .. "/hello"
  82. local httpc = http.new()
  83. httpc:request_uri(uri, {method = "GET"})
  84. ngx.sleep(2.5)
  85. -- still use all nodes
  86. httpc:request_uri(uri, {method = "GET"})
  87. }
  88. }
  89. --- request
  90. GET /t
  91. --- error_log
  92. connect() failed
  93. unhealthy TCP increment (2/2) for '127.0.0.1(127.0.0.1:1979)
  94. unhealthy TCP increment (2/2) for '127.0.0.2(127.0.0.2:1979)
  95. --- grep_error_log eval
  96. qr/proxy request to \S+/
  97. --- grep_error_log_out
  98. proxy request to 127.0.0.1:1979
  99. proxy request to 127.0.0.2:1979
  100. proxy request to 127.0.0.1:1979
  101. proxy request to 127.0.0.2:1979
  102. === TEST 2: use priority as backup (setup rule)
  103. --- config
  104. location /t {
  105. content_by_lua_block {
  106. local t = require("lib.test_admin").test
  107. local code, body = t('/apisix/admin/routes/1',
  108. ngx.HTTP_PUT,
  109. [[{
  110. "uri": "/hello",
  111. "upstream": {
  112. "type": "roundrobin",
  113. "nodes": [
  114. {"host": "127.0.0.1", "port": 1979, "weight": 2000},
  115. {"host": "127.0.0.1", "port": 1980,
  116. "weight": 1, "priority": -1}
  117. ],
  118. "checks": {
  119. "active": {
  120. "http_path": "/status",
  121. "healthy": {
  122. "interval": 1,
  123. "successes": 1
  124. },
  125. "unhealthy": {
  126. "interval": 1,
  127. "http_failures": 1
  128. }
  129. }
  130. }
  131. }
  132. }]]
  133. )
  134. if code >= 300 then
  135. ngx.status = code
  136. end
  137. ngx.say(body)
  138. }
  139. }
  140. --- request
  141. GET /t
  142. --- response_body
  143. passed
  144. === TEST 3: use priority as backup
  145. --- config
  146. location /t {
  147. content_by_lua_block {
  148. local http = require "resty.http"
  149. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  150. .. "/hello"
  151. local httpc = http.new()
  152. httpc:request_uri(uri, {method = "GET"})
  153. ngx.sleep(2.5)
  154. httpc:request_uri(uri, {method = "GET"})
  155. }
  156. }
  157. --- request
  158. GET /t
  159. --- error_log
  160. connect() failed
  161. unhealthy TCP increment (2/2) for '127.0.0.1(127.0.0.1:1979)
  162. --- grep_error_log eval
  163. qr/proxy request to \S+/
  164. --- grep_error_log_out
  165. proxy request to 127.0.0.1:1979
  166. proxy request to 127.0.0.1:1980
  167. proxy request to 127.0.0.1:1980