balancer_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. package balancer_test
  18. import (
  19. "net/http"
  20. "time"
  21. . "github.com/onsi/ginkgo/v2"
  22. "github.com/stretchr/testify/assert"
  23. "github.com/apisix/manager-api/test/e2e/base"
  24. )
  25. var _ = Describe("Balancer", func() {
  26. DescribeTable("test create upstream and route",
  27. func(tc base.HttpTestCase) {
  28. base.RunTestCase(tc)
  29. },
  30. Entry("create upstream (roundrobin with same weight)", base.HttpTestCase{
  31. Object: base.ManagerApiExpect(),
  32. Method: http.MethodPut,
  33. Path: "/apisix/admin/upstreams/1",
  34. Body: `{
  35. "nodes": [{
  36. "host": "` + base.UpstreamIp + `",
  37. "port": 1980,
  38. "weight": 1
  39. },
  40. {
  41. "host": "` + base.UpstreamIp + `",
  42. "port": 1981,
  43. "weight": 1
  44. },
  45. {
  46. "host": "` + base.UpstreamIp + `",
  47. "port": 1982,
  48. "weight": 1
  49. }],
  50. "type": "roundrobin"
  51. }`,
  52. Headers: map[string]string{"Authorization": base.GetToken()},
  53. ExpectStatus: http.StatusOK,
  54. }),
  55. Entry("create route using the upstream just created", base.HttpTestCase{
  56. Object: base.ManagerApiExpect(),
  57. Method: http.MethodPut,
  58. Path: "/apisix/admin/routes/1",
  59. Body: `{
  60. "name": "route1",
  61. "uri": "/server_port",
  62. "upstream_id": "1"
  63. }`,
  64. Headers: map[string]string{"Authorization": base.GetToken()},
  65. ExpectStatus: http.StatusOK,
  66. Sleep: base.SleepTime,
  67. }),
  68. )
  69. It("verify balancer by access count(same weight)", func() {
  70. time.Sleep(base.SleepTime)
  71. // batch test /server_port api
  72. res := base.BatchTestServerPort(18, nil, "")
  73. assert.Equal(GinkgoT(), 6, res["1980"])
  74. assert.Equal(GinkgoT(), 6, res["1981"])
  75. assert.Equal(GinkgoT(), 6, res["1982"])
  76. })
  77. DescribeTable("test update upstream",
  78. func(tc base.HttpTestCase) {
  79. base.RunTestCase(tc)
  80. },
  81. Entry("update upstream (roundrobin with different weight)", base.HttpTestCase{
  82. Object: base.ManagerApiExpect(),
  83. Method: http.MethodPut,
  84. Path: "/apisix/admin/upstreams/1",
  85. Body: `{
  86. "nodes": [{
  87. "host": "` + base.UpstreamIp + `",
  88. "port": 1980,
  89. "weight": 1
  90. },
  91. {
  92. "host": "` + base.UpstreamIp + `",
  93. "port": 1981,
  94. "weight": 2
  95. },
  96. {
  97. "host": "` + base.UpstreamIp + `",
  98. "port": 1982,
  99. "weight": 3
  100. }],
  101. "type": "roundrobin"
  102. }`,
  103. Headers: map[string]string{"Authorization": base.GetToken()},
  104. ExpectStatus: http.StatusOK,
  105. }),
  106. )
  107. It("verify balancer by access count(different weight)", func() {
  108. time.Sleep(base.SleepTime)
  109. // batch test /server_port api
  110. res := base.BatchTestServerPort(18, nil, "")
  111. assert.Equal(GinkgoT(), 3, res["1980"])
  112. assert.Equal(GinkgoT(), 6, res["1981"])
  113. assert.Equal(GinkgoT(), 9, res["1982"])
  114. })
  115. DescribeTable("update upstream",
  116. func(tc base.HttpTestCase) {
  117. base.RunTestCase(tc)
  118. },
  119. Entry("update upstream (roundrobin with weight 1 and 0)", base.HttpTestCase{
  120. Object: base.ManagerApiExpect(),
  121. Method: http.MethodPut,
  122. Path: "/apisix/admin/upstreams/1",
  123. Body: `{
  124. "nodes": [{
  125. "host": "` + base.UpstreamIp + `",
  126. "port": 1980,
  127. "weight": 1
  128. },
  129. {
  130. "host": "` + base.UpstreamIp + `",
  131. "port": 1981,
  132. "weight": 0
  133. }],
  134. "type": "roundrobin"
  135. }`,
  136. Headers: map[string]string{"Authorization": base.GetToken()},
  137. ExpectStatus: http.StatusOK,
  138. }),
  139. )
  140. It("verify balancer by access count(weight 1 and 0)", func() {
  141. time.Sleep(base.SleepTime)
  142. // batch test /server_port api
  143. res := base.BatchTestServerPort(18, nil, "")
  144. assert.Equal(GinkgoT(), 18, res["1980"])
  145. })
  146. DescribeTable("update upstream",
  147. func(tc base.HttpTestCase) {
  148. base.RunTestCase(tc)
  149. },
  150. Entry("update upstream (roundrobin with weight only 1)", base.HttpTestCase{
  151. Object: base.ManagerApiExpect(),
  152. Method: http.MethodPut,
  153. Path: "/apisix/admin/upstreams/1",
  154. Body: `{
  155. "nodes": [{
  156. "host": "` + base.UpstreamIp + `",
  157. "port": 1980,
  158. "weight": 1
  159. }],
  160. "type": "roundrobin"
  161. }`,
  162. Headers: map[string]string{"Authorization": base.GetToken()},
  163. ExpectStatus: http.StatusOK,
  164. }),
  165. )
  166. It("verify balancer by access count(weight only 1)", func() {
  167. time.Sleep(base.SleepTime)
  168. // batch test /server_port api
  169. res := base.BatchTestServerPort(18, nil, "")
  170. assert.Equal(GinkgoT(), 18, res["1980"])
  171. })
  172. Context("test balancer delete", func() {
  173. It("delete route", func() {
  174. base.RunTestCase(base.HttpTestCase{
  175. Object: base.ManagerApiExpect(),
  176. Method: http.MethodDelete,
  177. Path: "/apisix/admin/routes/1",
  178. Headers: map[string]string{"Authorization": base.GetToken()},
  179. ExpectStatus: http.StatusOK,
  180. })
  181. })
  182. It("delete upstream", func() {
  183. base.RunTestCase(base.HttpTestCase{
  184. Object: base.ManagerApiExpect(),
  185. Method: http.MethodDelete,
  186. Path: "/apisix/admin/upstreams/1",
  187. Headers: map[string]string{"Authorization": base.GetToken()},
  188. ExpectStatus: http.StatusOK,
  189. })
  190. })
  191. It("hit the route just deleted", func() {
  192. base.RunTestCase(base.HttpTestCase{
  193. Object: base.APISIXExpect(),
  194. Method: http.MethodGet,
  195. Path: "/server_port",
  196. ExpectStatus: http.StatusNotFound,
  197. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  198. Sleep: base.SleepTime,
  199. })
  200. })
  201. })
  202. })