route_service_upstream_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 route_test
  18. import (
  19. "net/http"
  20. "time"
  21. . "github.com/onsi/ginkgo/v2"
  22. . "github.com/onsi/gomega"
  23. "github.com/apisix/manager-api/test/e2e/base"
  24. )
  25. var _ = Describe("create route that not exists service or upstream", func() {
  26. DescribeTable("test create route that not exists service or upstream",
  27. func(tc base.HttpTestCase) {
  28. base.RunTestCase(tc)
  29. },
  30. Entry("make sure the route has not created", base.HttpTestCase{
  31. Object: base.APISIXExpect(),
  32. Method: http.MethodGet,
  33. Path: "/hello_",
  34. ExpectStatus: http.StatusNotFound,
  35. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  36. Sleep: base.SleepTime,
  37. }),
  38. Entry("create route that not exists service", base.HttpTestCase{
  39. Object: base.ManagerApiExpect(),
  40. Method: http.MethodPut,
  41. Path: "/apisix/admin/routes/r1",
  42. Body: `{
  43. "name": "route1",
  44. "uri": "/hello_",
  45. "service_id": "not-exists"
  46. }`,
  47. Headers: map[string]string{"Authorization": base.GetToken()},
  48. ExpectStatus: http.StatusBadRequest,
  49. }),
  50. Entry("verify not-exist route", base.HttpTestCase{
  51. Object: base.APISIXExpect(),
  52. Method: http.MethodGet,
  53. Path: "/hello_",
  54. ExpectStatus: http.StatusNotFound,
  55. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  56. Sleep: base.SleepTime,
  57. }),
  58. Entry("create route that not exists upstream", base.HttpTestCase{
  59. Object: base.ManagerApiExpect(),
  60. Method: http.MethodPut,
  61. Path: "/apisix/admin/routes/r1",
  62. Body: `{
  63. "name": "route1",
  64. "uri": "/hello_",
  65. "upstream_id": "not-exists"
  66. }`,
  67. Headers: map[string]string{"Authorization": base.GetToken()},
  68. ExpectStatus: http.StatusBadRequest,
  69. }),
  70. Entry("verify not-exist route", base.HttpTestCase{
  71. Object: base.APISIXExpect(),
  72. Method: http.MethodGet,
  73. Path: "/hello_",
  74. ExpectStatus: http.StatusNotFound,
  75. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  76. Sleep: base.SleepTime,
  77. }),
  78. Entry("create route that not exists service and upstream", base.HttpTestCase{
  79. Object: base.ManagerApiExpect(),
  80. Method: http.MethodPut,
  81. Path: "/apisix/admin/routes/r1",
  82. Body: `{
  83. "name": "route1",
  84. "uri": "/hello_",
  85. "service_id": "not-exists-service",
  86. "upstream_id": "not-exists-upstream"
  87. }`,
  88. Headers: map[string]string{"Authorization": base.GetToken()},
  89. ExpectStatus: http.StatusBadRequest,
  90. }),
  91. Entry("verify not-exist route", base.HttpTestCase{
  92. Object: base.APISIXExpect(),
  93. Method: http.MethodGet,
  94. Path: "/hello_",
  95. ExpectStatus: http.StatusNotFound,
  96. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  97. Sleep: base.SleepTime,
  98. }),
  99. Entry("create service with not-exist upstream", base.HttpTestCase{
  100. Object: base.ManagerApiExpect(),
  101. Method: http.MethodPut,
  102. Path: "/apisix/admin/services/100",
  103. Body: `{
  104. "upstream_id": "not-exists-upstream"
  105. }`,
  106. Headers: map[string]string{"Authorization": base.GetToken()},
  107. ExpectStatus: http.StatusBadRequest,
  108. }),
  109. Entry("create route with service(service with not exist upstream)", base.HttpTestCase{
  110. Object: base.ManagerApiExpect(),
  111. Method: http.MethodPut,
  112. Path: "/apisix/admin/routes/r1",
  113. Body: `{
  114. "name": "route1",
  115. "uri": "/hello_",
  116. "service_id": "not-exists"
  117. }`,
  118. Headers: map[string]string{"Authorization": base.GetToken()},
  119. ExpectStatus: http.StatusBadRequest,
  120. }),
  121. Entry("verify not-exist route", base.HttpTestCase{
  122. Object: base.APISIXExpect(),
  123. Method: http.MethodGet,
  124. Path: "/hello_",
  125. ExpectStatus: http.StatusNotFound,
  126. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  127. Sleep: base.SleepTime,
  128. }),
  129. )
  130. })
  131. var _ = Describe("route create with service", func() {
  132. DescribeTable("test route create with service",
  133. func(tc base.HttpTestCase) {
  134. base.RunTestCase(tc)
  135. },
  136. Entry("make sure the route has not created", base.HttpTestCase{
  137. Object: base.APISIXExpect(),
  138. Method: http.MethodGet,
  139. Path: "/server_port",
  140. ExpectStatus: http.StatusNotFound,
  141. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  142. Sleep: base.SleepTime,
  143. }),
  144. Entry("create service", base.HttpTestCase{
  145. Object: base.ManagerApiExpect(),
  146. Method: http.MethodPut,
  147. Path: "/apisix/admin/services/200",
  148. Body: `{
  149. "upstream": {
  150. "type": "roundrobin",
  151. "nodes": [
  152. {
  153. "host": "` + base.UpstreamIp + `",
  154. "port": 1980,
  155. "weight": 1
  156. },
  157. {
  158. "host": "` + base.UpstreamIp + `",
  159. "port": 1981,
  160. "weight": 1
  161. },
  162. {
  163. "host": "` + base.UpstreamIp + `",
  164. "port": 1982,
  165. "weight": 1
  166. }
  167. ]
  168. }
  169. }`,
  170. Headers: map[string]string{"Authorization": base.GetToken()},
  171. ExpectStatus: http.StatusOK,
  172. }),
  173. Entry("create route using the service just created", base.HttpTestCase{
  174. Object: base.ManagerApiExpect(),
  175. Method: http.MethodPut,
  176. Path: "/apisix/admin/routes/r1",
  177. Body: `{
  178. "name": "route1",
  179. "uri": "/server_port",
  180. "service_id": "200"
  181. }`,
  182. Headers: map[string]string{"Authorization": base.GetToken()},
  183. ExpectStatus: http.StatusOK,
  184. Sleep: base.SleepTime,
  185. }),
  186. )
  187. It("batch test /server_port api", func() {
  188. // sleep for etcd sync
  189. time.Sleep(time.Duration(300) * time.Millisecond)
  190. // batch test /server_port api
  191. res := base.BatchTestServerPort(18, nil, "")
  192. Expect(res["1980"]).Should(Equal(6))
  193. Expect(res["1981"]).Should(Equal(6))
  194. Expect(res["1982"]).Should(Equal(6))
  195. })
  196. DescribeTable("delete route and service",
  197. func(tc base.HttpTestCase) {
  198. base.RunTestCase(tc)
  199. },
  200. Entry("delete route", base.HttpTestCase{
  201. Object: base.ManagerApiExpect(),
  202. Method: http.MethodDelete,
  203. Path: "/apisix/admin/routes/r1",
  204. Headers: map[string]string{"Authorization": base.GetToken()},
  205. ExpectStatus: http.StatusOK,
  206. }),
  207. Entry("remove service", base.HttpTestCase{
  208. Object: base.ManagerApiExpect(),
  209. Method: http.MethodDelete,
  210. Path: "/apisix/admin/services/200",
  211. Headers: map[string]string{"Authorization": base.GetToken()},
  212. ExpectStatus: http.StatusOK,
  213. }),
  214. Entry("make sure the route deleted", base.HttpTestCase{
  215. Object: base.APISIXExpect(),
  216. Method: http.MethodGet,
  217. Path: "/server_port",
  218. ExpectStatus: http.StatusNotFound,
  219. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  220. }),
  221. )
  222. })
  223. var _ = Describe("route create upstream", func() {
  224. DescribeTable("test route create upstream",
  225. func(tc base.HttpTestCase) {
  226. base.RunTestCase(tc)
  227. },
  228. Entry("create upstream", base.HttpTestCase{
  229. Object: base.ManagerApiExpect(),
  230. Method: http.MethodPut,
  231. Path: "/apisix/admin/upstreams/1",
  232. Body: `{
  233. "nodes":[
  234. {
  235. "host": "` + base.UpstreamIp + `",
  236. "port": 1980,
  237. "weight": 1
  238. },
  239. {
  240. "host": "` + base.UpstreamIp + `",
  241. "port": 1981,
  242. "weight": 1
  243. },
  244. {
  245. "host": "` + base.UpstreamIp + `",
  246. "port": 1982,
  247. "weight": 1
  248. }
  249. ],
  250. "type": "roundrobin"
  251. }`,
  252. Headers: map[string]string{"Authorization": base.GetToken()},
  253. ExpectStatus: http.StatusOK,
  254. }),
  255. Entry("make sure the route has not created", base.HttpTestCase{
  256. Object: base.APISIXExpect(),
  257. Method: http.MethodGet,
  258. Path: "/server_port",
  259. ExpectStatus: http.StatusNotFound,
  260. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  261. Sleep: base.SleepTime,
  262. }),
  263. Entry("create route using the upstream just created", base.HttpTestCase{
  264. Object: base.ManagerApiExpect(),
  265. Method: http.MethodPut,
  266. Path: "/apisix/admin/routes/r1",
  267. Body: `{
  268. "name": "route1",
  269. "uri": "/server_port",
  270. "upstream_id": "1"
  271. }`,
  272. Headers: map[string]string{"Authorization": base.GetToken()},
  273. ExpectStatus: http.StatusOK,
  274. }),
  275. )
  276. It("batch test /server_port api", func() {
  277. // sleep for etcd sync
  278. time.Sleep(time.Duration(300) * time.Millisecond)
  279. // batch test /server_port api
  280. res := base.BatchTestServerPort(12, nil, "")
  281. Expect(res["1980"]).Should(Equal(4))
  282. Expect(res["1981"]).Should(Equal(4))
  283. Expect(res["1982"]).Should(Equal(4))
  284. })
  285. DescribeTable("delete route and upstream",
  286. func(tc base.HttpTestCase) {
  287. base.RunTestCase(tc)
  288. },
  289. Entry("delete route", base.HttpTestCase{
  290. Object: base.ManagerApiExpect(),
  291. Method: http.MethodDelete,
  292. Path: "/apisix/admin/routes/r1",
  293. Headers: map[string]string{"Authorization": base.GetToken()},
  294. ExpectStatus: http.StatusOK,
  295. }),
  296. Entry("remove upstream", base.HttpTestCase{
  297. Object: base.ManagerApiExpect(),
  298. Method: http.MethodDelete,
  299. Path: "/apisix/admin/upstreams/1",
  300. Headers: map[string]string{"Authorization": base.GetToken()},
  301. ExpectStatus: http.StatusOK,
  302. }),
  303. Entry("make sure the route deleted", base.HttpTestCase{
  304. Object: base.APISIXExpect(),
  305. Method: http.MethodGet,
  306. Path: "/server_port",
  307. ExpectStatus: http.StatusNotFound,
  308. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  309. }),
  310. )
  311. })
  312. var _ = Describe("route create with service that contains upstream", func() {
  313. DescribeTable("test route create with service that contains upstream",
  314. func(tc base.HttpTestCase) {
  315. base.RunTestCase(tc)
  316. },
  317. Entry("make sure the route has not created", base.HttpTestCase{
  318. Object: base.APISIXExpect(),
  319. Method: http.MethodGet,
  320. Path: "/server_port",
  321. ExpectStatus: http.StatusNotFound,
  322. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  323. Sleep: base.SleepTime,
  324. }),
  325. Entry("create upstream", base.HttpTestCase{
  326. Object: base.ManagerApiExpect(),
  327. Method: http.MethodPut,
  328. Path: "/apisix/admin/upstreams/1",
  329. Body: `{
  330. "nodes":[
  331. {
  332. "host": "` + base.UpstreamIp + `",
  333. "port": 1980,
  334. "weight": 1
  335. },
  336. {
  337. "host": "` + base.UpstreamIp + `",
  338. "port": 1981,
  339. "weight": 1
  340. },
  341. {
  342. "host": "` + base.UpstreamIp + `",
  343. "port": 1982,
  344. "weight": 1
  345. }
  346. ],
  347. "type": "roundrobin"
  348. }`,
  349. Headers: map[string]string{"Authorization": base.GetToken()},
  350. ExpectStatus: http.StatusOK,
  351. }),
  352. Entry("create service", base.HttpTestCase{
  353. Object: base.ManagerApiExpect(),
  354. Method: http.MethodPut,
  355. Path: "/apisix/admin/services/200",
  356. Body: `{
  357. "upstream_id": "1"
  358. }`,
  359. Headers: map[string]string{"Authorization": base.GetToken()},
  360. ExpectStatus: http.StatusOK,
  361. }),
  362. Entry("create route using the service just created", base.HttpTestCase{
  363. Object: base.ManagerApiExpect(),
  364. Method: http.MethodPut,
  365. Path: "/apisix/admin/routes/r1",
  366. Body: `{
  367. "name": "route1",
  368. "uri": "/server_port",
  369. "service_id": "200"
  370. }`,
  371. Headers: map[string]string{"Authorization": base.GetToken()},
  372. ExpectStatus: http.StatusOK,
  373. Sleep: base.SleepTime,
  374. }),
  375. )
  376. It("batch test /server_port api", func() {
  377. // sleep for etcd sync
  378. time.Sleep(time.Duration(300) * time.Millisecond)
  379. // batch test /server_port api
  380. res := base.BatchTestServerPort(18, nil, "")
  381. Expect(res["1980"]).Should(Equal(6))
  382. Expect(res["1981"]).Should(Equal(6))
  383. Expect(res["1982"]).Should(Equal(6))
  384. })
  385. DescribeTable("delete route and service",
  386. func(tc base.HttpTestCase) {
  387. base.RunTestCase(tc)
  388. },
  389. Entry("delete route", base.HttpTestCase{
  390. Object: base.ManagerApiExpect(),
  391. Method: http.MethodDelete,
  392. Path: "/apisix/admin/routes/r1",
  393. Headers: map[string]string{"Authorization": base.GetToken()},
  394. ExpectStatus: http.StatusOK,
  395. }),
  396. Entry("remove service", base.HttpTestCase{
  397. Object: base.ManagerApiExpect(),
  398. Method: http.MethodDelete,
  399. Path: "/apisix/admin/services/200",
  400. Headers: map[string]string{"Authorization": base.GetToken()},
  401. ExpectStatus: http.StatusOK,
  402. }),
  403. Entry("make sure the route deleted", base.HttpTestCase{
  404. Object: base.APISIXExpect(),
  405. Method: http.MethodGet,
  406. Path: "/server_port",
  407. ExpectStatus: http.StatusNotFound,
  408. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  409. }),
  410. )
  411. })