healthcheck-stop-checker.t 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. BEGIN {
  18. if ($ENV{TEST_NGINX_CHECK_LEAK}) {
  19. $SkipReason = "unavailable for the hup tests";
  20. } else {
  21. $ENV{TEST_NGINX_USE_HUP} = 1;
  22. undef $ENV{TEST_NGINX_USE_STAP};
  23. }
  24. }
  25. use t::APISIX 'no_plan';
  26. repeat_each(1);
  27. log_level('info');
  28. no_root_location();
  29. no_shuffle();
  30. worker_connections(256);
  31. # the healthcheck stop test requires exiting worker to keep watching etcd for a while,
  32. # which is not the case when using gRPC.
  33. my $yaml_config = <<_EOC_;
  34. deployment:
  35. role: traditional
  36. role_traditional:
  37. config_provider: etcd
  38. etcd:
  39. prefix: "/apisix"
  40. host:
  41. - "http://127.0.0.1:2379"
  42. use_grpc: false
  43. admin:
  44. admin_key: null
  45. _EOC_
  46. add_block_preprocessor(sub {
  47. my ($block) = @_;
  48. if (!$block->yaml_config) {
  49. $block->set_value("yaml_config", $yaml_config);
  50. }
  51. });
  52. run_tests();
  53. __DATA__
  54. === TEST 1: set route(two healthy upstream nodes)
  55. --- request
  56. PUT /apisix/admin/routes/1
  57. {"uri":"/server_port","upstream":{"type":"roundrobin","nodes":{"127.0.0.1:1980":1,"127.0.0.1:1981":1},"checks":{"active":{"http_path":"/status","host":"foo.com","healthy":{"interval":1,"successes":1},"unhealthy":{"interval":1,"http_failures":2}}}}}
  58. --- error_code_like: ^20\d$
  59. === TEST 2: update + delete
  60. --- config
  61. location /t {
  62. content_by_lua_block {
  63. local t = require("lib.test_admin").test
  64. local code, status, body = t('/apisix/admin/routes/1',
  65. "PUT",
  66. [[{"uri":"/server_port","upstream":{"type":"roundrobin","nodes":{"127.0.0.1:1980":1,"127.0.0.1:1981":1},"checks":{"active":{"http_path":"/status","healthy":{"interval":1,"successes":1},"unhealthy":{"interval":1,"http_failures":2}}}}}]]
  67. )
  68. if code < 300 then
  69. code = 200
  70. end
  71. ngx.say("1 code: ", code)
  72. ngx.sleep(0.2)
  73. local code, body = t('/server_port', "GET")
  74. ngx.say("2 code: ", code)
  75. ngx.sleep(0.2)
  76. code = t('/apisix/admin/routes/1', "DELETE")
  77. ngx.say("3 code: ", code)
  78. ngx.sleep(0.2)
  79. local code, body = t('/server_port', "GET")
  80. ngx.say("4 code: ", code)
  81. }
  82. }
  83. --- request
  84. GET /t
  85. --- response_body
  86. 1 code: 200
  87. 2 code: 200
  88. 3 code: 200
  89. 4 code: 404
  90. --- grep_error_log eval
  91. qr/create new checker: table: 0x|try to release checker: table: 0x/
  92. --- grep_error_log_out
  93. create new checker: table: 0x
  94. try to release checker: table: 0x
  95. === TEST 3: set route(two healthy upstream nodes)
  96. --- request
  97. PUT /apisix/admin/routes/1
  98. {"uri":"/server_port","upstream":{"type":"roundrobin","nodes":{"127.0.0.1:1980":1,"127.0.0.1:1981":1},"checks":{"active":{"http_path":"/status","host":"foo.com","healthy":{"interval":1,"successes":1},"unhealthy":{"interval":1,"http_failures":2}}}}}
  99. --- error_code: 201
  100. === TEST 4: update
  101. --- config
  102. location /t {
  103. content_by_lua_block {
  104. local t = require("lib.test_admin").test
  105. local code, body = t('/server_port', "GET")
  106. ngx.say("1 code: ", code)
  107. local code, status, body = t('/apisix/admin/routes/1',
  108. "PUT",
  109. [[{"uri":"/server_port","upstream":{"type":"roundrobin","nodes":{"127.0.0.1:1980":1,"127.0.0.1:1981":1},"checks":{"active":{"http_path":"/status","healthy":{"interval":1,"successes":1},"unhealthy":{"interval":1,"http_failures":2}}}}}]]
  110. )
  111. if code < 300 then
  112. code = 200
  113. end
  114. ngx.say("2 code: ", code)
  115. ngx.sleep(0.2)
  116. local code, body = t('/server_port', "GET")
  117. ngx.say("3 code: ", code)
  118. }
  119. }
  120. --- request
  121. GET /t
  122. --- response_body
  123. 1 code: 200
  124. 2 code: 200
  125. 3 code: 200
  126. --- grep_error_log eval
  127. qr/create new checker: table: 0x|try to release checker: table: 0x/
  128. --- grep_error_log_out
  129. create new checker: table: 0x
  130. try to release checker: table: 0x
  131. create new checker: table: 0x
  132. === TEST 5: update + delete for /upstreams
  133. --- config
  134. location /t {
  135. content_by_lua_block {
  136. local t = require("lib.test_admin").test
  137. local code, _, body = t('/apisix/admin/upstreams/stopchecker',
  138. "PUT",
  139. [[{"type":"roundrobin","nodes":{"127.0.0.1:1980":1,"127.0.0.1:1981":1},"checks":{"active":{"http_path":"/status","healthy":{"interval":1,"successes":1},"unhealthy":{"interval":1,"http_failures":2}}}}]]
  140. )
  141. if code > 300 then
  142. ngx.status = code
  143. ngx.say(body)
  144. return
  145. end
  146. local code, _, body = t('/apisix/admin/routes/1',
  147. "PUT",
  148. [[{"uri":"/server_port","upstream_id":"stopchecker"}]]
  149. )
  150. if code > 300 then
  151. ngx.status = code
  152. ngx.say(body)
  153. return
  154. end
  155. ngx.sleep(0.2)
  156. code, _, body = t('/server_port', "GET")
  157. if code > 300 then
  158. ngx.status = code
  159. ngx.say(body)
  160. return
  161. end
  162. ngx.sleep(0.5)
  163. -- update
  164. code, _, body = t('/apisix/admin/upstreams/stopchecker',
  165. "PUT",
  166. [[{"type":"roundrobin","nodes":{"127.0.0.1:1980":1,"127.0.0.1:1981":1},"checks":{"active":{"http_path":"/void","healthy":{"interval":1,"successes":1},"unhealthy":{"interval":1,"http_failures":1}}}}]]
  167. )
  168. if code > 300 then
  169. ngx.status = code
  170. ngx.say(body)
  171. return
  172. end
  173. ngx.sleep(0.2)
  174. code, _, body = t('/server_port', "GET")
  175. if code > 300 then
  176. ngx.status = code
  177. ngx.say(body)
  178. return
  179. end
  180. -- delete
  181. code, _, body = t('/apisix/admin/routes/1', "DELETE")
  182. if code > 300 then
  183. ngx.status = code
  184. ngx.say(body)
  185. return
  186. end
  187. ngx.sleep(0.5) -- wait for routes delete event synced
  188. code, _, body = t('/apisix/admin/upstreams/stopchecker', "DELETE")
  189. if code > 300 then
  190. ngx.status = code
  191. ngx.say(body)
  192. return
  193. end
  194. ngx.say("ok")
  195. }
  196. }
  197. --- request
  198. GET /t
  199. --- response_body
  200. ok
  201. --- grep_error_log eval
  202. qr/create new checker: table: 0x|try to release checker: table: 0x/
  203. --- grep_error_log_out
  204. create new checker: table: 0x
  205. try to release checker: table: 0x
  206. create new checker: table: 0x
  207. try to release checker: table: 0x