upstream-discovery.t 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. no_root_location();
  20. no_shuffle();
  21. add_block_preprocessor(sub {
  22. my ($block) = @_;
  23. if (!$block->yaml_config) {
  24. my $yaml_config = <<_EOC_;
  25. apisix:
  26. node_listen: 1984
  27. deployment:
  28. role: data_plane
  29. role_data_plane:
  30. config_provider: yaml
  31. _EOC_
  32. $block->set_value("yaml_config", $yaml_config);
  33. }
  34. if ($block->apisix_yaml) {
  35. my $upstream = <<_EOC_;
  36. upstreams:
  37. - service_name: mock
  38. discovery_type: mock
  39. type: roundrobin
  40. id: 1
  41. #END
  42. _EOC_
  43. $block->set_value("apisix_yaml", $block->apisix_yaml . $upstream);
  44. }
  45. if (!$block->request) {
  46. $block->set_value("request", "GET /t");
  47. }
  48. });
  49. run_tests();
  50. __DATA__
  51. === TEST 1: create new server picker when nodes change
  52. --- apisix_yaml
  53. routes:
  54. -
  55. uris:
  56. - /hello
  57. upstream_id: 1
  58. --- config
  59. location /t {
  60. content_by_lua_block {
  61. local discovery = require("apisix.discovery.init").discovery
  62. discovery.mock = {
  63. nodes = function()
  64. return {
  65. {host = "127.0.0.1", port = 1980, weight = 1},
  66. {host = "0.0.0.0", port = 1980, weight = 1},
  67. }
  68. end
  69. }
  70. local http = require "resty.http"
  71. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  72. local httpc = http.new()
  73. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  74. ngx.say(res.status)
  75. discovery.mock = {
  76. nodes = function()
  77. return {
  78. {host = "127.0.0.2", port = 1980, weight = 1},
  79. {host = "127.0.0.3", port = 1980, weight = 1},
  80. }
  81. end
  82. }
  83. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  84. local httpc = http.new()
  85. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  86. }
  87. }
  88. --- grep_error_log eval
  89. qr/create_obj_fun\(\): upstream nodes:/
  90. --- grep_error_log_out
  91. create_obj_fun(): upstream nodes:
  92. create_obj_fun(): upstream nodes:
  93. === TEST 2: don't create new server picker if nodes don't change
  94. --- apisix_yaml
  95. routes:
  96. -
  97. uris:
  98. - /hello
  99. upstream_id: 1
  100. --- config
  101. location /t {
  102. content_by_lua_block {
  103. local discovery = require("apisix.discovery.init").discovery
  104. discovery.mock = {
  105. nodes = function()
  106. return {
  107. {host = "127.0.0.1", port = 1980, weight = 1},
  108. {host = "0.0.0.0", port = 1980, weight = 1},
  109. }
  110. end
  111. }
  112. local http = require "resty.http"
  113. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  114. local httpc = http.new()
  115. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  116. ngx.say(res.status)
  117. discovery.mock = {
  118. nodes = function()
  119. return {
  120. {host = "0.0.0.0", port = 1980, weight = 1},
  121. {host = "127.0.0.1", port = 1980, weight = 1},
  122. }
  123. end
  124. }
  125. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  126. local httpc = http.new()
  127. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  128. }
  129. }
  130. --- grep_error_log eval
  131. qr/create_obj_fun\(\): upstream nodes:/
  132. --- grep_error_log_out
  133. create_obj_fun(): upstream nodes:
  134. === TEST 3: create new server picker when nodes change, up_conf doesn't come from upstream
  135. --- apisix_yaml
  136. routes:
  137. - uris:
  138. - /hello
  139. service_id: 1
  140. services:
  141. - id: 1
  142. upstream:
  143. service_name: mock
  144. discovery_type: mock
  145. type: roundrobin
  146. --- config
  147. location /t {
  148. content_by_lua_block {
  149. local discovery = require("apisix.discovery.init").discovery
  150. discovery.mock = {
  151. nodes = function()
  152. return {
  153. {host = "127.0.0.1", port = 1980, weight = 1},
  154. {host = "0.0.0.0", port = 1980, weight = 1},
  155. }
  156. end
  157. }
  158. local http = require "resty.http"
  159. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  160. local httpc = http.new()
  161. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  162. ngx.say(res.status)
  163. discovery.mock = {
  164. nodes = function()
  165. return {
  166. {host = "127.0.0.2", port = 1980, weight = 1},
  167. {host = "0.0.0.0", port = 1980, weight = 1},
  168. }
  169. end
  170. }
  171. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  172. local httpc = http.new()
  173. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  174. }
  175. }
  176. --- grep_error_log eval
  177. qr/create_obj_fun\(\): upstream nodes:/
  178. --- grep_error_log_out
  179. create_obj_fun(): upstream nodes:
  180. create_obj_fun(): upstream nodes:
  181. === TEST 4: don't create new server picker if nodes don't change (port missing)
  182. --- apisix_yaml
  183. routes:
  184. -
  185. uris:
  186. - /hello
  187. upstream_id: 1
  188. --- config
  189. location /t {
  190. content_by_lua_block {
  191. local discovery = require("apisix.discovery.init").discovery
  192. discovery.mock = {
  193. nodes = function()
  194. return {
  195. {host = "127.0.0.1", weight = 1},
  196. {host = "0.0.0.0", weight = 1},
  197. }
  198. end
  199. }
  200. local http = require "resty.http"
  201. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  202. local httpc = http.new()
  203. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  204. ngx.say(res.status)
  205. discovery.mock = {
  206. nodes = function()
  207. return {
  208. {host = "0.0.0.0", weight = 1},
  209. {host = "127.0.0.1", weight = 1},
  210. }
  211. end
  212. }
  213. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  214. local httpc = http.new()
  215. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  216. }
  217. }
  218. --- grep_error_log eval
  219. qr/create_obj_fun\(\): upstream nodes:/
  220. --- grep_error_log_out
  221. create_obj_fun(): upstream nodes:
  222. --- error_log
  223. connect() failed
  224. === TEST 5: create new server picker when priority change
  225. --- apisix_yaml
  226. routes:
  227. -
  228. uris:
  229. - /hello
  230. upstream_id: 1
  231. --- config
  232. location /t {
  233. content_by_lua_block {
  234. local discovery = require("apisix.discovery.init").discovery
  235. discovery.mock = {
  236. nodes = function()
  237. return {
  238. {host = "127.0.0.1", port = 1980, weight = 1},
  239. {host = "0.0.0.0", port = 1980, weight = 1},
  240. }
  241. end
  242. }
  243. local http = require "resty.http"
  244. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  245. local httpc = http.new()
  246. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  247. ngx.say(res.status)
  248. discovery.mock = {
  249. nodes = function()
  250. return {
  251. {host = "127.0.0.1", port = 1980, weight = 1},
  252. {host = "0.0.0.0", port = 1980, weight = 1, priority = 1},
  253. }
  254. end
  255. }
  256. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  257. local httpc = http.new()
  258. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  259. }
  260. }
  261. --- grep_error_log eval
  262. qr/create_obj_fun\(\): upstream nodes:/
  263. --- grep_error_log_out
  264. create_obj_fun(): upstream nodes:
  265. create_obj_fun(): upstream nodes:
  266. === TEST 6: default priority of discovered node is 0
  267. --- apisix_yaml
  268. routes:
  269. -
  270. uris:
  271. - /hello
  272. upstream_id: 1
  273. --- config
  274. location /t {
  275. content_by_lua_block {
  276. local discovery = require("apisix.discovery.init").discovery
  277. discovery.mock = {
  278. nodes = function()
  279. return {
  280. {host = "127.0.0.1", port = 1979, weight = 1, priority = 1},
  281. {host = "0.0.0.0", port = 1980, weight = 1},
  282. {host = "127.0.0.2", port = 1979, weight = 1, priority = -1},
  283. }
  284. end
  285. }
  286. local http = require "resty.http"
  287. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  288. local httpc = http.new()
  289. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  290. ngx.say(res.status)
  291. }
  292. }
  293. --- error_log
  294. connect() failed
  295. --- grep_error_log eval
  296. qr/proxy request to \S+/
  297. --- grep_error_log_out
  298. proxy request to 127.0.0.1:1979
  299. proxy request to 0.0.0.0:1980
  300. === TEST 7: create new server picker when metadata change
  301. --- apisix_yaml
  302. routes:
  303. -
  304. uris:
  305. - /hello
  306. upstream_id: 1
  307. --- config
  308. location /t {
  309. content_by_lua_block {
  310. local discovery = require("apisix.discovery.init").discovery
  311. discovery.mock = {
  312. nodes = function()
  313. return {
  314. {host = "127.0.0.1", port = 1980, weight = 1, metadata = {a = 1}},
  315. {host = "0.0.0.0", port = 1980, weight = 1, metadata = {}},
  316. }
  317. end
  318. }
  319. local http = require "resty.http"
  320. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  321. local httpc = http.new()
  322. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  323. ngx.say(res.status)
  324. discovery.mock = {
  325. nodes = function()
  326. return {
  327. {host = "127.0.0.1", port = 1980, weight = 1, metadata = {a = 1}},
  328. {host = "0.0.0.0", port = 1980, weight = 1, metadata = {b = 1}},
  329. }
  330. end
  331. }
  332. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  333. local httpc = http.new()
  334. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  335. }
  336. }
  337. --- grep_error_log eval
  338. qr/create_obj_fun\(\): upstream nodes:/
  339. --- grep_error_log_out
  340. create_obj_fun(): upstream nodes:
  341. create_obj_fun(): upstream nodes:
  342. === TEST 8: don't create new server picker when metadata doesn't change
  343. --- apisix_yaml
  344. routes:
  345. -
  346. uris:
  347. - /hello
  348. upstream_id: 1
  349. --- config
  350. location /t {
  351. content_by_lua_block {
  352. local discovery = require("apisix.discovery.init").discovery
  353. local meta1 = {a = 1}
  354. local meta2 = {b = 2}
  355. discovery.mock = {
  356. nodes = function()
  357. return {
  358. {host = "127.0.0.1", port = 1980, weight = 1, metadata = meta1},
  359. {host = "0.0.0.0", port = 1980, weight = 1, metadata = meta2},
  360. }
  361. end
  362. }
  363. local http = require "resty.http"
  364. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  365. local httpc = http.new()
  366. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  367. ngx.say(res.status)
  368. discovery.mock = {
  369. nodes = function()
  370. return {
  371. {host = "127.0.0.1", port = 1980, weight = 1, metadata = meta1},
  372. {host = "0.0.0.0", port = 1980, weight = 1, metadata = meta2},
  373. }
  374. end
  375. }
  376. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  377. local httpc = http.new()
  378. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  379. }
  380. }
  381. --- grep_error_log eval
  382. qr/create_obj_fun\(\): upstream nodes:/
  383. --- grep_error_log_out
  384. create_obj_fun(): upstream nodes:
  385. === TEST 9: bad nodes return by the discovery
  386. --- apisix_yaml
  387. routes:
  388. -
  389. uris:
  390. - /hello
  391. upstream_id: 1
  392. --- config
  393. location /t {
  394. content_by_lua_block {
  395. local discovery = require("apisix.discovery.init").discovery
  396. discovery.mock = {
  397. nodes = function()
  398. return {
  399. {host = "127.0.0.1", port = 1980, weight = "0"},
  400. }
  401. end
  402. }
  403. local http = require "resty.http"
  404. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  405. local httpc = http.new()
  406. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  407. ngx.say(res.status)
  408. }
  409. }
  410. --- response_body
  411. 503
  412. --- error_log
  413. invalid nodes format: failed to validate item 1: property "weight" validation failed: wrong type: expected integer, got string