rr-balance.t 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. run_tests();
  23. __DATA__
  24. === TEST 1: set route(two upstream node)
  25. --- config
  26. location /t {
  27. content_by_lua_block {
  28. local t = require("lib.test_admin").test
  29. local code, body = t('/apisix/admin/routes/1',
  30. ngx.HTTP_PUT,
  31. [[{
  32. "uri": "/server_port",
  33. "upstream": {
  34. "type": "roundrobin",
  35. "nodes": {
  36. "127.0.0.1:1980": 1,
  37. "127.0.0.1:1981": 1
  38. }
  39. }
  40. }]]
  41. )
  42. if code >= 300 then
  43. ngx.status = code
  44. end
  45. ngx.say(body)
  46. }
  47. }
  48. --- request
  49. GET /t
  50. --- response_body
  51. passed
  52. === TEST 2: hit routes
  53. --- config
  54. location /t {
  55. content_by_lua_block {
  56. local http = require "resty.http"
  57. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  58. .. "/server_port"
  59. local ports_count = {}
  60. for i = 1, 12 do
  61. local httpc = http.new()
  62. local res, err = httpc:request_uri(uri, {method = "GET"})
  63. if not res then
  64. ngx.say(err)
  65. return
  66. end
  67. ports_count[res.body] = (ports_count[res.body] or 0) + 1
  68. end
  69. local ports_arr = {}
  70. for port, count in pairs(ports_count) do
  71. table.insert(ports_arr, {port = port, count = count})
  72. end
  73. local function cmd(a, b)
  74. return a.port > b.port
  75. end
  76. table.sort(ports_arr, cmd)
  77. ngx.say(require("toolkit.json").encode(ports_arr))
  78. ngx.exit(200)
  79. }
  80. }
  81. --- request
  82. GET /t
  83. --- response_body
  84. [{"count":6,"port":"1981"},{"count":6,"port":"1980"}]
  85. === TEST 3: set route(three upstream node)
  86. --- config
  87. location /t {
  88. content_by_lua_block {
  89. local t = require("lib.test_admin").test
  90. local code, body = t('/apisix/admin/routes/1',
  91. ngx.HTTP_PUT,
  92. [[{
  93. "uri": "/server_port",
  94. "upstream": {
  95. "type": "roundrobin",
  96. "nodes": {
  97. "127.0.0.1:1980": 1,
  98. "127.0.0.1:1981": 1,
  99. "127.0.0.1:1982": 1
  100. }
  101. }
  102. }]]
  103. )
  104. if code >= 300 then
  105. ngx.status = code
  106. end
  107. ngx.say(body)
  108. }
  109. }
  110. --- request
  111. GET /t
  112. --- response_body
  113. passed
  114. === TEST 4: hit routes
  115. --- config
  116. location /t {
  117. content_by_lua_block {
  118. local http = require "resty.http"
  119. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  120. .. "/server_port"
  121. local ports_count = {}
  122. for i = 1, 12 do
  123. local httpc = http.new()
  124. local res, err = httpc:request_uri(uri, {method = "GET"})
  125. if not res then
  126. ngx.say(err)
  127. return
  128. end
  129. ports_count[res.body] = (ports_count[res.body] or 0) + 1
  130. end
  131. local ports_arr = {}
  132. for port, count in pairs(ports_count) do
  133. table.insert(ports_arr, {port = port, count = count})
  134. end
  135. local function cmd(a, b)
  136. return a.port > b.port
  137. end
  138. table.sort(ports_arr, cmd)
  139. ngx.say(require("toolkit.json").encode(ports_arr))
  140. ngx.exit(200)
  141. }
  142. }
  143. --- request
  144. GET /t
  145. --- response_body
  146. [{"count":4,"port":"1982"},{"count":4,"port":"1981"},{"count":4,"port":"1980"}]
  147. === TEST 5: set route(three upstream node and different weight)
  148. --- config
  149. location /t {
  150. content_by_lua_block {
  151. local t = require("lib.test_admin").test
  152. local code, body = t('/apisix/admin/routes/1',
  153. ngx.HTTP_PUT,
  154. [[{
  155. "uri": "/server_port",
  156. "upstream": {
  157. "type": "roundrobin",
  158. "nodes": {
  159. "127.0.0.1:1980": 3,
  160. "127.0.0.1:1981": 2,
  161. "127.0.0.1:1982": 1
  162. }
  163. }
  164. }]]
  165. )
  166. if code >= 300 then
  167. ngx.status = code
  168. end
  169. ngx.say(body)
  170. }
  171. }
  172. --- request
  173. GET /t
  174. --- response_body
  175. passed
  176. === TEST 6: hit routes
  177. --- config
  178. location /t {
  179. content_by_lua_block {
  180. local http = require "resty.http"
  181. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  182. .. "/server_port"
  183. local ports_count = {}
  184. for i = 1, 12 do
  185. local httpc = http.new()
  186. local res, err = httpc:request_uri(uri, {method = "GET"})
  187. if not res then
  188. ngx.say(err)
  189. return
  190. end
  191. ports_count[res.body] = (ports_count[res.body] or 0) + 1
  192. end
  193. local ports_arr = {}
  194. for port, count in pairs(ports_count) do
  195. table.insert(ports_arr, {port = port, count = count})
  196. end
  197. local function cmd(a, b)
  198. return a.port > b.port
  199. end
  200. table.sort(ports_arr, cmd)
  201. ngx.say(require("toolkit.json").encode(ports_arr))
  202. ngx.exit(200)
  203. }
  204. }
  205. --- request
  206. GET /t
  207. --- response_body
  208. [{"count":2,"port":"1982"},{"count":4,"port":"1981"},{"count":6,"port":"1980"}]
  209. === TEST 7: set route(weight is 0)
  210. --- config
  211. location /t {
  212. content_by_lua_block {
  213. local t = require("lib.test_admin").test
  214. local code, body = t('/apisix/admin/routes/1',
  215. ngx.HTTP_PUT,
  216. [[{
  217. "uri": "/server_port",
  218. "upstream": {
  219. "type": "roundrobin",
  220. "nodes": {
  221. "127.0.0.1:1980": 3,
  222. "127.0.0.1:1981": 0,
  223. "127.0.0.1:1982": 1
  224. }
  225. }
  226. }]]
  227. )
  228. if code >= 300 then
  229. ngx.status = code
  230. end
  231. ngx.say(body)
  232. }
  233. }
  234. --- request
  235. GET /t
  236. --- response_body
  237. passed
  238. === TEST 8: hit routes
  239. --- config
  240. location /t {
  241. content_by_lua_block {
  242. local http = require "resty.http"
  243. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  244. .. "/server_port"
  245. local ports_count = {}
  246. for i = 1, 12 do
  247. local httpc = http.new()
  248. local res, err = httpc:request_uri(uri, {method = "GET"})
  249. if not res then
  250. ngx.say(err)
  251. return
  252. end
  253. ports_count[res.body] = (ports_count[res.body] or 0) + 1
  254. end
  255. local ports_arr = {}
  256. for port, count in pairs(ports_count) do
  257. table.insert(ports_arr, {port = port, count = count})
  258. end
  259. local function cmd(a, b)
  260. return a.port > b.port
  261. end
  262. table.sort(ports_arr, cmd)
  263. ngx.say(require("toolkit.json").encode(ports_arr))
  264. ngx.exit(200)
  265. }
  266. }
  267. --- request
  268. GET /t
  269. --- response_body
  270. [{"count":3,"port":"1982"},{"count":9,"port":"1980"}]