healthcheck-https.t 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. no_root_location();
  19. repeat_each(1);
  20. log_level('info');
  21. no_root_location();
  22. no_shuffle();
  23. add_block_preprocessor(sub {
  24. my ($block) = @_;
  25. if (!$block->http_config) {
  26. my $http_config = <<'_EOC_';
  27. server {
  28. listen 8765 ssl;
  29. ssl_certificate ../../certs/mtls_server.crt;
  30. ssl_certificate_key ../../certs/mtls_server.key;
  31. ssl_client_certificate ../../certs/mtls_ca.crt;
  32. location /ping {
  33. return 200 '8765';
  34. }
  35. location /healthz {
  36. return 200 'ok';
  37. }
  38. }
  39. server {
  40. listen 8766 ssl;
  41. ssl_certificate ../../certs/mtls_server.crt;
  42. ssl_certificate_key ../../certs/mtls_server.key;
  43. ssl_client_certificate ../../certs/mtls_ca.crt;
  44. location /ping {
  45. return 200 '8766';
  46. }
  47. location /healthz {
  48. return 500;
  49. }
  50. }
  51. server {
  52. listen 8767 ssl;
  53. ssl_certificate ../../certs/mtls_server.crt;
  54. ssl_certificate_key ../../certs/mtls_server.key;
  55. ssl_client_certificate ../../certs/mtls_ca.crt;
  56. location /ping {
  57. return 200 '8766';
  58. }
  59. location /healthz {
  60. return 200 'ok';
  61. }
  62. }
  63. server {
  64. listen 8768 ssl;
  65. ssl_certificate ../../certs/mtls_server.crt;
  66. ssl_certificate_key ../../certs/mtls_server.key;
  67. ssl_client_certificate ../../certs/mtls_ca.crt;
  68. location /ping {
  69. return 200 '8766';
  70. }
  71. location /healthz {
  72. return 500;
  73. }
  74. }
  75. _EOC_
  76. $block->set_value("http_config", $http_config);
  77. }
  78. if (!$block->request) {
  79. $block->set_value("request", "GET /t");
  80. }
  81. });
  82. run_tests;
  83. __DATA__
  84. === TEST 1: https health check (two health nodes)
  85. --- config
  86. location /t {
  87. lua_ssl_trusted_certificate ../../certs/mtls_ca.crt;
  88. content_by_lua_block {
  89. local t = require("lib.test_admin")
  90. local core = require("apisix.core")
  91. local cert = t.read_file("t/certs/mtls_client.crt")
  92. local key = t.read_file("t/certs/mtls_client.key")
  93. local data = {
  94. uri = "/ping",
  95. upstream = {
  96. scheme = "https",
  97. nodes = {
  98. ["127.0.0.1:8765"] = 1,
  99. ["127.0.0.1:8767"] = 1
  100. },
  101. tls = {
  102. client_cert = cert,
  103. client_key = key
  104. },
  105. retries = 2,
  106. checks = {
  107. active = {
  108. type = "https",
  109. http_path = "/healthz",
  110. https_verify_certificate = false,
  111. healthy = {
  112. interval = 1,
  113. successes = 1
  114. },
  115. unhealthy = {
  116. interval = 1,
  117. http_failures = 1
  118. },
  119. }
  120. }
  121. }
  122. }
  123. local code, body = t.test('/apisix/admin/routes/1',
  124. ngx.HTTP_PUT, core.json.encode(data))
  125. if code >= 300 then
  126. ngx.status = code
  127. ngx.say(body)
  128. return
  129. end
  130. local http = require("resty.http")
  131. local httpc = http.new()
  132. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/ping"
  133. local _, _ = httpc:request_uri(uri, {method = "GET", keepalive = false})
  134. ngx.sleep(0.5)
  135. local healthcheck_uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/v1/healthcheck/routes/1"
  136. local httpc = http.new()
  137. local res, _ = httpc:request_uri(healthcheck_uri, {method = "GET", keepalive = false})
  138. local json_data = core.json.decode(res.body)
  139. assert(json_data.type == "https")
  140. assert(#json_data.nodes == 2)
  141. local function check_node_health(port, status)
  142. for _, node in ipairs(json_data.nodes) do
  143. if node.port == port and node.status == status then
  144. return true
  145. end
  146. end
  147. return false
  148. end
  149. assert(check_node_health(8765, "healthy"), "Port 8765 is not healthy")
  150. assert(check_node_health(8767, "healthy"), "Port 8767 is not healthy")
  151. }
  152. }
  153. --- request
  154. GET /t
  155. --- error_code: 200
  156. === TEST 2: https health check (one healthy node, one unhealthy node)
  157. --- config
  158. location /t {
  159. lua_ssl_trusted_certificate ../../certs/mtls_ca.crt;
  160. content_by_lua_block {
  161. local t = require("lib.test_admin")
  162. local core = require("apisix.core")
  163. local cert = t.read_file("t/certs/mtls_client.crt")
  164. local key = t.read_file("t/certs/mtls_client.key")
  165. local data = {
  166. uri = "/ping",
  167. upstream = {
  168. scheme = "https",
  169. nodes = {
  170. ["127.0.0.1:8765"] = 1,
  171. ["127.0.0.1:8766"] = 1
  172. },
  173. tls = {
  174. client_cert = cert,
  175. client_key = key
  176. },
  177. retries = 2,
  178. checks = {
  179. active = {
  180. type = "https",
  181. http_path = "/healthz",
  182. https_verify_certificate = false,
  183. healthy = {
  184. interval = 1,
  185. successes = 1
  186. },
  187. unhealthy = {
  188. interval = 1,
  189. http_failures = 1
  190. },
  191. }
  192. }
  193. }
  194. }
  195. local code, body = t.test('/apisix/admin/routes/1',
  196. ngx.HTTP_PUT, core.json.encode(data))
  197. if code >= 300 then
  198. ngx.status = code
  199. ngx.say(body)
  200. return
  201. end
  202. local http = require("resty.http")
  203. local httpc = http.new()
  204. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/ping"
  205. local _, _ = httpc:request_uri(uri, {method = "GET", keepalive = false})
  206. ngx.sleep(1.5)
  207. local healthcheck_uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/v1/healthcheck/routes/1"
  208. local httpc = http.new()
  209. local res, _ = httpc:request_uri(healthcheck_uri, {method = "GET", keepalive = false})
  210. local json_data = core.json.decode(res.body)
  211. assert(json_data.type == "https")
  212. assert(#json_data.nodes == 2)
  213. local function check_node_health(port, status)
  214. for _, node in ipairs(json_data.nodes) do
  215. if node.port == port and node.status == status then
  216. return true
  217. end
  218. end
  219. return false
  220. end
  221. assert(check_node_health(8765, "healthy"), "Port 8765 is not healthy")
  222. assert(check_node_health(8766, "unhealthy"), "Port 8766 is not unhealthy")
  223. }
  224. }
  225. --- request
  226. GET /t
  227. --- grep_error_log eval
  228. qr/\([^)]+\) unhealthy .* for '.*'/
  229. --- grep_error_log_out
  230. (upstream#/apisix/routes/1) unhealthy HTTP increment (1/1) for '127.0.0.1(127.0.0.1:8766)'
  231. === TEST 3: https health check (two unhealthy nodes)
  232. --- config
  233. location /t {
  234. lua_ssl_trusted_certificate ../../certs/mtls_ca.crt;
  235. content_by_lua_block {
  236. local t = require("lib.test_admin")
  237. local core = require("apisix.core")
  238. local cert = t.read_file("t/certs/mtls_client.crt")
  239. local key = t.read_file("t/certs/mtls_client.key")
  240. local data = {
  241. uri = "/ping",
  242. upstream = {
  243. scheme = "https",
  244. nodes = {
  245. ["127.0.0.1:8766"] = 1,
  246. ["127.0.0.1:8768"] = 1
  247. },
  248. tls = {
  249. client_cert = cert,
  250. client_key = key
  251. },
  252. retries = 2,
  253. checks = {
  254. active = {
  255. type = "https",
  256. http_path = "/healthz",
  257. https_verify_certificate = false,
  258. healthy = {
  259. interval = 1,
  260. successes = 1
  261. },
  262. unhealthy = {
  263. interval = 1,
  264. http_failures = 1
  265. },
  266. }
  267. }
  268. }
  269. }
  270. local code, body = t.test('/apisix/admin/routes/1',
  271. ngx.HTTP_PUT, core.json.encode(data))
  272. if code >= 300 then
  273. ngx.status = code
  274. ngx.say(body)
  275. return
  276. end
  277. local http = require("resty.http")
  278. local httpc = http.new()
  279. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/ping"
  280. local _, _ = httpc:request_uri(uri, {method = "GET", keepalive = false})
  281. ngx.sleep(1.5)
  282. local healthcheck_uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/v1/healthcheck/routes/1"
  283. local httpc = http.new()
  284. local res, _ = httpc:request_uri(healthcheck_uri, {method = "GET", keepalive = false})
  285. local json_data = core.json.decode(res.body)
  286. assert(json_data.type == "https")
  287. assert(#json_data.nodes == 2)
  288. local function check_node_health(port, status)
  289. for _, node in ipairs(json_data.nodes) do
  290. if node.port == port and node.status == status then
  291. return true
  292. end
  293. end
  294. return false
  295. end
  296. assert(check_node_health(8766, "unhealthy"), "Port 8766 is not unhealthy")
  297. assert(check_node_health(8768, "unhealthy"), "Port 8768 is not unhealthy")
  298. }
  299. }
  300. --- request
  301. GET /t
  302. --- error_code: 200