service_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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 service_test
  18. import (
  19. "encoding/json"
  20. "net/http"
  21. "time"
  22. . "github.com/onsi/ginkgo/v2"
  23. . "github.com/onsi/gomega"
  24. "github.com/apisix/manager-api/test/e2e/base"
  25. )
  26. var _ = Describe("create service without plugin", func() {
  27. var createServiceBody map[string]interface{} = map[string]interface{}{
  28. "name": "testservice",
  29. "upstream": map[string]interface{}{
  30. "type": "roundrobin",
  31. "nodes": []map[string]interface{}{
  32. {
  33. "host": base.UpstreamIp,
  34. "port": 1980,
  35. "weight": 1,
  36. },
  37. {
  38. "host": base.UpstreamIp,
  39. "port": 1981,
  40. "weight": 2,
  41. },
  42. {
  43. "host": base.UpstreamIp,
  44. "port": 1982,
  45. "weight": 3,
  46. },
  47. },
  48. },
  49. }
  50. It("create service without plugin", func() {
  51. _createServiceBody, err := json.Marshal(createServiceBody)
  52. Expect(err).To(BeNil())
  53. base.RunTestCase(base.HttpTestCase{
  54. Object: base.ManagerApiExpect(),
  55. Method: http.MethodPut,
  56. Path: "/apisix/admin/services/s1",
  57. Headers: map[string]string{"Authorization": base.GetToken()},
  58. Body: string(_createServiceBody),
  59. ExpectStatus: http.StatusOK,
  60. ExpectBody: []string{"\"id\":\"s1\"", "\"name\":\"testservice\""},
  61. })
  62. })
  63. It("create service2 success", func() {
  64. createServiceBody["name"] = "testservice2"
  65. _createServiceBody, err := json.Marshal(createServiceBody)
  66. Expect(err).To(BeNil())
  67. base.RunTestCase(base.HttpTestCase{
  68. Object: base.ManagerApiExpect(),
  69. Method: http.MethodPut,
  70. Path: "/apisix/admin/services/s2",
  71. Headers: map[string]string{"Authorization": base.GetToken()},
  72. Body: string(_createServiceBody),
  73. ExpectStatus: http.StatusOK,
  74. ExpectBody: []string{"\"id\":\"s2\"", "\"name\":\"testservice2\""},
  75. })
  76. })
  77. It("create service failed, name existed", func() {
  78. createServiceBody["name"] = "testservice2"
  79. _createServiceBody, err := json.Marshal(createServiceBody)
  80. Expect(err).To(BeNil())
  81. base.RunTestCase(base.HttpTestCase{
  82. Object: base.ManagerApiExpect(),
  83. Method: http.MethodPost,
  84. Path: "/apisix/admin/services",
  85. Headers: map[string]string{"Authorization": base.GetToken()},
  86. Body: string(_createServiceBody),
  87. ExpectStatus: http.StatusBadRequest,
  88. ExpectBody: `service name exists`,
  89. Sleep: base.SleepTime,
  90. })
  91. })
  92. It("update service failed, name existed", func() {
  93. createServiceBody["name"] = "testservice2"
  94. _createServiceBody, err := json.Marshal(createServiceBody)
  95. Expect(err).To(BeNil())
  96. base.RunTestCase(base.HttpTestCase{
  97. Object: base.ManagerApiExpect(),
  98. Method: http.MethodPut,
  99. Path: "/apisix/admin/services/s1",
  100. Headers: map[string]string{"Authorization": base.GetToken()},
  101. Body: string(_createServiceBody),
  102. ExpectStatus: http.StatusBadRequest,
  103. ExpectBody: `service name exists`,
  104. })
  105. })
  106. It("get the service s1", func() {
  107. base.RunTestCase(base.HttpTestCase{
  108. Object: base.ManagerApiExpect(),
  109. Method: http.MethodGet,
  110. Path: "/apisix/admin/services/s1",
  111. Headers: map[string]string{"Authorization": base.GetToken()},
  112. ExpectCode: http.StatusOK,
  113. ExpectBody: "\"name\":\"testservice\",\"upstream\":{\"nodes\":[{\"host\":\"" + base.UpstreamIp + "\",\"port\":1980,\"weight\":1},{\"host\":\"" + base.UpstreamIp + "\",\"port\":1981,\"weight\":2},{\"host\":\"" + base.UpstreamIp + "\",\"port\":1982,\"weight\":3}],\"type\":\"roundrobin\"}",
  114. Sleep: base.SleepTime,
  115. })
  116. })
  117. It("create route using the service just created", func() {
  118. base.RunTestCase(base.HttpTestCase{
  119. Object: base.ManagerApiExpect(),
  120. Method: http.MethodPut,
  121. Path: "/apisix/admin/routes/r1",
  122. Body: `{
  123. "name": "route1",
  124. "uri": "/server_port",
  125. "service_id": "s1"
  126. }`,
  127. Headers: map[string]string{"Authorization": base.GetToken()},
  128. ExpectStatus: http.StatusOK,
  129. Sleep: base.SleepTime,
  130. })
  131. })
  132. It("batch test /server_port api", func() {
  133. time.Sleep(time.Duration(500) * time.Millisecond)
  134. res := base.BatchTestServerPort(18, nil, "")
  135. Expect(res["1980"]).Should(Equal(3))
  136. Expect(res["1981"]).Should(Equal(6))
  137. Expect(res["1982"]).Should(Equal(9))
  138. })
  139. It("delete route", func() {
  140. base.RunTestCase(base.HttpTestCase{
  141. Object: base.ManagerApiExpect(),
  142. Method: http.MethodDelete,
  143. Path: "/apisix/admin/routes/r1",
  144. Headers: map[string]string{"Authorization": base.GetToken()},
  145. ExpectStatus: http.StatusOK,
  146. })
  147. })
  148. It("delete service", func() {
  149. base.RunTestCase(base.HttpTestCase{
  150. Object: base.ManagerApiExpect(),
  151. Method: http.MethodDelete,
  152. Path: "/apisix/admin/services/s1",
  153. Headers: map[string]string{"Authorization": base.GetToken()},
  154. ExpectStatus: http.StatusOK,
  155. Sleep: base.SleepTime,
  156. })
  157. })
  158. It("delete service2", func() {
  159. base.RunTestCase(base.HttpTestCase{
  160. Object: base.ManagerApiExpect(),
  161. Method: http.MethodDelete,
  162. Path: "/apisix/admin/services/s2",
  163. Headers: map[string]string{"Authorization": base.GetToken()},
  164. ExpectStatus: http.StatusOK,
  165. Sleep: base.SleepTime,
  166. })
  167. })
  168. It("hit the route just deleted", func() {
  169. base.RunTestCase(base.HttpTestCase{
  170. Object: base.APISIXExpect(),
  171. Method: http.MethodGet,
  172. Path: "/server_port",
  173. ExpectStatus: http.StatusNotFound,
  174. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  175. Sleep: base.SleepTime,
  176. })
  177. })
  178. })
  179. var _ = Describe("create service with plugin", func() {
  180. It("create service without plugin", func() {
  181. var createServiceBody map[string]interface{} = map[string]interface{}{
  182. "name": "testservice",
  183. "plugins": map[string]interface{}{
  184. "limit-count": map[string]interface{}{
  185. "count": 100,
  186. "time_window": 60,
  187. "rejected_code": 503,
  188. "key": "remote_addr",
  189. "policy": "local",
  190. },
  191. },
  192. "upstream": map[string]interface{}{
  193. "type": "roundrobin",
  194. "nodes": []map[string]interface{}{
  195. {
  196. "host": base.UpstreamIp,
  197. "port": 1980,
  198. "weight": 1,
  199. },
  200. },
  201. },
  202. }
  203. _createServiceBody, err := json.Marshal(createServiceBody)
  204. Expect(err).To(BeNil())
  205. base.RunTestCase(base.HttpTestCase{
  206. Object: base.ManagerApiExpect(),
  207. Method: http.MethodPut,
  208. Path: "/apisix/admin/services/s1",
  209. Headers: map[string]string{"Authorization": base.GetToken()},
  210. Body: string(_createServiceBody),
  211. ExpectStatus: http.StatusOK,
  212. ExpectBody: []string{"\"id\":\"s1\"", "\"name\":\"testservice\""},
  213. })
  214. })
  215. It("get the service s1", func() {
  216. base.RunTestCase(base.HttpTestCase{
  217. Object: base.ManagerApiExpect(),
  218. Method: http.MethodGet,
  219. Path: "/apisix/admin/services/s1",
  220. Headers: map[string]string{"Authorization": base.GetToken()},
  221. ExpectCode: http.StatusOK,
  222. ExpectBody: "\"upstream\":{\"nodes\":[{\"host\":\"" + base.UpstreamIp + "\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"},\"plugins\":{\"limit-count\":{\"count\":100,\"key\":\"remote_addr\",\"policy\":\"local\",\"rejected_code\":503,\"time_window\":60}}",
  223. Sleep: base.SleepTime,
  224. })
  225. })
  226. It("create route using the service just created", func() {
  227. base.RunTestCase(base.HttpTestCase{
  228. Object: base.ManagerApiExpect(),
  229. Method: http.MethodPut,
  230. Path: "/apisix/admin/routes/r1",
  231. Body: `{
  232. "name": "route1",
  233. "uri": "/server_port",
  234. "service_id": "s1"
  235. }`,
  236. Headers: map[string]string{"Authorization": base.GetToken()},
  237. ExpectStatus: http.StatusOK,
  238. Sleep: base.SleepTime,
  239. })
  240. })
  241. It(" hit routes and check the response header", func() {
  242. time.Sleep(time.Duration(500) * time.Millisecond)
  243. basepath := base.APISIXHost
  244. request, err := http.NewRequest("GET", basepath+"/server_port", nil)
  245. Expect(err).To(BeNil())
  246. request.Header.Add("Authorization", base.GetToken())
  247. resp, err := http.DefaultClient.Do(request)
  248. Expect(err).To(BeNil())
  249. defer resp.Body.Close()
  250. Expect(resp.StatusCode).Should(Equal(200))
  251. Expect(resp.Header["X-Ratelimit-Limit"][0]).Should(Equal("100"))
  252. Expect(resp.Header["X-Ratelimit-Remaining"][0]).Should(Equal("99"))
  253. })
  254. It("delete route", func() {
  255. base.RunTestCase(base.HttpTestCase{
  256. Object: base.ManagerApiExpect(),
  257. Method: http.MethodDelete,
  258. Path: "/apisix/admin/routes/r1",
  259. Headers: map[string]string{"Authorization": base.GetToken()},
  260. ExpectStatus: http.StatusOK,
  261. })
  262. })
  263. It("delete service", func() {
  264. base.RunTestCase(base.HttpTestCase{
  265. Object: base.ManagerApiExpect(),
  266. Method: http.MethodDelete,
  267. Path: "/apisix/admin/services/s1",
  268. Headers: map[string]string{"Authorization": base.GetToken()},
  269. ExpectStatus: http.StatusOK,
  270. Sleep: base.SleepTime,
  271. })
  272. })
  273. It("hit the route just deleted", func() {
  274. base.RunTestCase(base.HttpTestCase{
  275. Object: base.APISIXExpect(),
  276. Method: http.MethodGet,
  277. Path: "/server_port",
  278. ExpectStatus: http.StatusNotFound,
  279. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  280. Sleep: base.SleepTime,
  281. })
  282. })
  283. })
  284. var _ = Describe("create service with all options via POST method", func() {
  285. It("create service with all options via POST method", func() {
  286. var createServiceBody map[string]interface{} = map[string]interface{}{
  287. "id": "s2",
  288. "name": "testservice22",
  289. "desc": "testservice_desc",
  290. "labels": map[string]interface{}{
  291. "build": "16",
  292. "env": "production",
  293. "version": "v2",
  294. },
  295. "enable_websocket": true,
  296. "plugins": map[string]interface{}{
  297. "limit-count": map[string]interface{}{
  298. "count": 100,
  299. "time_window": 60,
  300. "rejected_code": 503,
  301. "key": "remote_addr",
  302. "policy": "local",
  303. },
  304. },
  305. "upstream": map[string]interface{}{
  306. "type": "roundrobin",
  307. "create_time": 1602883670,
  308. "update_time": 1602893670,
  309. "nodes": []map[string]interface{}{
  310. {
  311. "host": base.UpstreamIp,
  312. "port": 1980,
  313. "weight": 1,
  314. },
  315. },
  316. },
  317. }
  318. _createServiceBody, err := json.Marshal(createServiceBody)
  319. Expect(err).To(BeNil())
  320. base.RunTestCase(base.HttpTestCase{
  321. Desc: "create service with all options via POST method",
  322. Object: base.ManagerApiExpect(),
  323. Method: http.MethodPost,
  324. Path: "/apisix/admin/services",
  325. Headers: map[string]string{"Authorization": base.GetToken()},
  326. Body: string(_createServiceBody),
  327. ExpectStatus: http.StatusOK,
  328. ExpectBody: "\"id\":\"s2\"",
  329. })
  330. })
  331. It("get the service s2", func() {
  332. base.RunTestCase(base.HttpTestCase{
  333. Object: base.ManagerApiExpect(),
  334. Method: http.MethodGet,
  335. Path: "/apisix/admin/services/s2",
  336. Headers: map[string]string{"Authorization": base.GetToken()},
  337. ExpectCode: http.StatusOK,
  338. ExpectBody: "\"name\":\"testservice22\",\"desc\":\"testservice_desc\",\"upstream\":{\"nodes\":[{\"host\":\"" + base.UpstreamIp + "\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"},\"plugins\":{\"limit-count\":{\"count\":100,\"key\":\"remote_addr\",\"policy\":\"local\",\"rejected_code\":503,\"time_window\":60}},\"labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"enable_websocket\":true}",
  339. Sleep: base.SleepTime,
  340. })
  341. })
  342. It("create route using the service just created", func() {
  343. base.RunTestCase(base.HttpTestCase{
  344. Object: base.ManagerApiExpect(),
  345. Method: http.MethodPut,
  346. Path: "/apisix/admin/routes/r1",
  347. Body: `{
  348. "name": "route1",
  349. "uri": "/hello",
  350. "service_id": "s2"
  351. }`,
  352. Headers: map[string]string{"Authorization": base.GetToken()},
  353. ExpectStatus: http.StatusOK,
  354. Sleep: base.SleepTime,
  355. })
  356. })
  357. It("verify route", func() {
  358. base.RunTestCase(base.HttpTestCase{
  359. Object: base.APISIXExpect(),
  360. Method: http.MethodGet,
  361. Path: "/hello",
  362. ExpectStatus: http.StatusOK,
  363. ExpectBody: "hello world",
  364. Sleep: base.SleepTime,
  365. })
  366. })
  367. It("delete route", func() {
  368. base.RunTestCase(base.HttpTestCase{
  369. Object: base.ManagerApiExpect(),
  370. Method: http.MethodDelete,
  371. Path: "/apisix/admin/routes/r1",
  372. Headers: map[string]string{"Authorization": base.GetToken()},
  373. ExpectStatus: http.StatusOK,
  374. })
  375. })
  376. It("delete service", func() {
  377. base.RunTestCase(base.HttpTestCase{
  378. Object: base.ManagerApiExpect(),
  379. Method: http.MethodDelete,
  380. Path: "/apisix/admin/services/s2",
  381. Headers: map[string]string{"Authorization": base.GetToken()},
  382. ExpectStatus: http.StatusOK,
  383. Sleep: base.SleepTime,
  384. })
  385. })
  386. It("hit the route just deleted", func() {
  387. base.RunTestCase(base.HttpTestCase{
  388. Object: base.APISIXExpect(),
  389. Method: http.MethodGet,
  390. Path: "/hello",
  391. ExpectStatus: http.StatusNotFound,
  392. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  393. Sleep: base.SleepTime,
  394. })
  395. })
  396. })
  397. var _ = Describe("service update use patch method", func() {
  398. It("create service without plugin", func() {
  399. var createServiceBody map[string]interface{} = map[string]interface{}{
  400. "name": "testservice",
  401. "upstream": map[string]interface{}{
  402. "type": "roundrobin",
  403. "nodes": []map[string]interface{}{
  404. {
  405. "host": base.UpstreamIp,
  406. "port": 1980,
  407. "weight": 1,
  408. },
  409. },
  410. },
  411. }
  412. _createServiceBody, err := json.Marshal(createServiceBody)
  413. Expect(err).To(BeNil())
  414. base.RunTestCase(base.HttpTestCase{
  415. Desc: "create service without plugin",
  416. Object: base.ManagerApiExpect(),
  417. Method: http.MethodPut,
  418. Path: "/apisix/admin/services/s5",
  419. Headers: map[string]string{"Authorization": base.GetToken()},
  420. Body: string(_createServiceBody),
  421. ExpectStatus: http.StatusOK,
  422. ExpectBody: "\"name\":\"testservice\",\"upstream\":{\"nodes\":[{\"host\":\"" + base.UpstreamIp + "\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"}}",
  423. })
  424. })
  425. It("update service use patch method", func() {
  426. var createServiceBody map[string]interface{} = map[string]interface{}{
  427. "name": "testpatch",
  428. "upstream": map[string]interface{}{
  429. "type": "roundrobin",
  430. "nodes": []map[string]interface{}{
  431. {
  432. "host": base.UpstreamIp,
  433. "port": 1981,
  434. "weight": 1,
  435. },
  436. },
  437. },
  438. }
  439. _createServiceBody, err := json.Marshal(createServiceBody)
  440. Expect(err).To(BeNil())
  441. base.RunTestCase(base.HttpTestCase{
  442. Object: base.ManagerApiExpect(),
  443. Method: http.MethodPatch,
  444. Path: "/apisix/admin/services/s5",
  445. Body: string(_createServiceBody),
  446. Headers: map[string]string{"Authorization": base.GetToken()},
  447. ExpectStatus: http.StatusOK,
  448. Sleep: base.SleepTime,
  449. })
  450. })
  451. It("get the service s5", func() {
  452. base.RunTestCase(base.HttpTestCase{
  453. Object: base.ManagerApiExpect(),
  454. Method: http.MethodGet,
  455. Path: "/apisix/admin/services/s5",
  456. Headers: map[string]string{"Authorization": base.GetToken()},
  457. ExpectCode: http.StatusOK,
  458. ExpectBody: "\"name\":\"testpatch\",\"upstream\":{\"nodes\":[{\"host\":\"" + base.UpstreamIp + "\",\"port\":1981,\"weight\":1}],\"type\":\"roundrobin\"}}",
  459. Sleep: base.SleepTime,
  460. })
  461. })
  462. It("Update service using path parameter patch method", func() {
  463. var createUpstreamBody map[string]interface{} = map[string]interface{}{
  464. "type": "roundrobin",
  465. "nodes": []map[string]interface{}{
  466. {
  467. "host": base.UpstreamIp,
  468. "port": 1980,
  469. "weight": 1,
  470. },
  471. },
  472. }
  473. _createUpstreamBody, err := json.Marshal(createUpstreamBody)
  474. Expect(err).To(BeNil())
  475. base.RunTestCase(base.HttpTestCase{
  476. Object: base.ManagerApiExpect(),
  477. Method: http.MethodPatch,
  478. Path: "/apisix/admin/services/s5/upstream",
  479. Body: string(_createUpstreamBody),
  480. Headers: map[string]string{
  481. "Authorization": base.GetToken(),
  482. "Content-Type": "text/plain",
  483. },
  484. ExpectStatus: http.StatusOK,
  485. })
  486. })
  487. It("get service data", func() {
  488. base.RunTestCase(base.HttpTestCase{
  489. Object: base.ManagerApiExpect(),
  490. Method: http.MethodGet,
  491. Path: "/apisix/admin/services/s5",
  492. Headers: map[string]string{"Authorization": base.GetToken()},
  493. ExpectStatus: http.StatusOK,
  494. ExpectBody: "\"name\":\"testpatch\",\"upstream\":{\"nodes\":[{\"host\":\"" + base.UpstreamIp + "\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"}}",
  495. Sleep: base.SleepTime,
  496. })
  497. })
  498. It("delete service", func() {
  499. base.RunTestCase(base.HttpTestCase{
  500. Object: base.ManagerApiExpect(),
  501. Method: http.MethodDelete,
  502. Path: "/apisix/admin/services/s5",
  503. Headers: map[string]string{"Authorization": base.GetToken()},
  504. ExpectStatus: http.StatusOK,
  505. })
  506. })
  507. })
  508. var _ = Describe("test service delete", func() {
  509. var createServiceBody map[string]interface{} = map[string]interface{}{
  510. "name": "testservice",
  511. "upstream": map[string]interface{}{
  512. "type": "roundrobin",
  513. "nodes": []map[string]interface{}{
  514. {
  515. "host": base.UpstreamIp,
  516. "port": 1980,
  517. "weight": 1,
  518. },
  519. },
  520. },
  521. }
  522. _createServiceBody, err := json.Marshal(createServiceBody)
  523. Expect(err).To(BeNil())
  524. DescribeTable("test service delete",
  525. func(tc base.HttpTestCase) {
  526. base.RunTestCase(tc)
  527. },
  528. Entry("create service without plugin", base.HttpTestCase{
  529. Desc: "create service without plugin",
  530. Object: base.ManagerApiExpect(),
  531. Method: http.MethodPut,
  532. Path: "/apisix/admin/services/s1",
  533. Headers: map[string]string{"Authorization": base.GetToken()},
  534. Body: string(_createServiceBody),
  535. ExpectStatus: http.StatusOK,
  536. ExpectBody: "\"name\":\"testservice\",\"upstream\":{\"nodes\":[{\"host\":\"" + base.UpstreamIp + "\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"}}",
  537. }),
  538. Entry("create route use service s1", base.HttpTestCase{
  539. Desc: "create route use service s1",
  540. Object: base.ManagerApiExpect(),
  541. Method: http.MethodPut,
  542. Path: "/apisix/admin/routes/r1",
  543. Body: `{
  544. "id": "r1",
  545. "name": "route1",
  546. "uri": "/hello",
  547. "upstream": {
  548. "type": "roundrobin",
  549. "nodes": {
  550. "` + base.UpstreamIp + `:1980": 1
  551. }
  552. },
  553. "service_id": "s1"
  554. }`,
  555. Headers: map[string]string{"Authorization": base.GetToken()},
  556. ExpectStatus: http.StatusOK,
  557. ExpectBody: "\"service_id\":\"s1\"",
  558. }),
  559. Entry("hit route on apisix", base.HttpTestCase{
  560. Object: base.APISIXExpect(),
  561. Method: http.MethodGet,
  562. Path: "/hello",
  563. ExpectStatus: http.StatusOK,
  564. ExpectBody: "hello world",
  565. Sleep: base.SleepTime,
  566. }),
  567. Entry("delete service failed", base.HttpTestCase{
  568. Desc: "delete service failed",
  569. Object: base.ManagerApiExpect(),
  570. Method: http.MethodDelete,
  571. Path: "/apisix/admin/services/s1",
  572. Headers: map[string]string{"Authorization": base.GetToken()},
  573. ExpectStatus: http.StatusBadRequest,
  574. ExpectBody: "route: route1 is using this service",
  575. }),
  576. Entry("delete route first", base.HttpTestCase{
  577. Desc: "delete route first",
  578. Object: base.ManagerApiExpect(),
  579. Method: http.MethodDelete,
  580. Path: "/apisix/admin/routes/r1",
  581. Headers: map[string]string{"Authorization": base.GetToken()},
  582. ExpectStatus: http.StatusOK,
  583. }),
  584. Entry("check route exist", base.HttpTestCase{
  585. Desc: "check route exist",
  586. Object: base.ManagerApiExpect(),
  587. Method: http.MethodDelete,
  588. Path: "/apisix/admin/routes/r1",
  589. Headers: map[string]string{"Authorization": base.GetToken()},
  590. ExpectStatus: http.StatusNotFound,
  591. }),
  592. Entry("delete service success", base.HttpTestCase{
  593. Desc: "delete service success",
  594. Object: base.ManagerApiExpect(),
  595. Method: http.MethodDelete,
  596. Path: "/apisix/admin/services/s1",
  597. Headers: map[string]string{"Authorization": base.GetToken()},
  598. ExpectStatus: http.StatusOK,
  599. }),
  600. Entry("check the service exist", base.HttpTestCase{
  601. Desc: "check the exist",
  602. Object: base.ManagerApiExpect(),
  603. Method: http.MethodGet,
  604. Path: "/apisix/admin/services/s1",
  605. Headers: map[string]string{"Authorization": base.GetToken()},
  606. ExpectStatus: http.StatusNotFound,
  607. }))
  608. })
  609. var _ = Describe("test service with hosts", func() {
  610. var createServiceBody = map[string]interface{}{
  611. "name": "testservice",
  612. "upstream": map[string]interface{}{
  613. "type": "roundrobin",
  614. "nodes": []map[string]interface{}{
  615. {
  616. "host": base.UpstreamIp,
  617. "port": 1980,
  618. "weight": 1,
  619. },
  620. },
  621. },
  622. "hosts": []string{
  623. "test.com",
  624. "test1.com",
  625. },
  626. }
  627. _createServiceBody, err := json.Marshal(createServiceBody)
  628. Expect(err).To(BeNil())
  629. var createRouteBody = map[string]interface{}{
  630. "id": "r1",
  631. "name": "route1",
  632. "uri": "/hello",
  633. "upstream": map[string]interface{}{
  634. "type": "roundrobin",
  635. "nodes": map[string]interface{}{
  636. base.UpstreamIp + ":1980": 1,
  637. },
  638. },
  639. "service_id": "s1",
  640. }
  641. _createRouteBody, err := json.Marshal(createRouteBody)
  642. Expect(err).To(BeNil())
  643. DescribeTable("test service with hosts",
  644. func(tc func() base.HttpTestCase) {
  645. base.RunTestCase(tc())
  646. },
  647. Entry("create service with hosts params", func() base.HttpTestCase {
  648. return base.HttpTestCase{
  649. Desc: "create service with hosts params",
  650. Object: base.ManagerApiExpect(),
  651. Method: http.MethodPut,
  652. Path: "/apisix/admin/services/s1",
  653. Headers: map[string]string{"Authorization": base.GetToken()},
  654. Body: string(_createServiceBody),
  655. ExpectStatus: http.StatusOK,
  656. }
  657. }),
  658. Entry("create route use service s1", func() base.HttpTestCase {
  659. return base.HttpTestCase{
  660. Desc: "create route use service s1",
  661. Object: base.ManagerApiExpect(),
  662. Method: http.MethodPut,
  663. Path: "/apisix/admin/routes/r1",
  664. Body: string(_createRouteBody),
  665. Headers: map[string]string{"Authorization": base.GetToken()},
  666. ExpectStatus: http.StatusOK,
  667. }
  668. }),
  669. Entry("hit route by test.com", func() base.HttpTestCase {
  670. return base.HttpTestCase{
  671. Object: base.APISIXExpect(),
  672. Method: http.MethodGet,
  673. Path: "/hello",
  674. Headers: map[string]string{
  675. "Host": "test.com",
  676. },
  677. ExpectStatus: http.StatusOK,
  678. ExpectBody: "hello world",
  679. Sleep: base.SleepTime,
  680. }
  681. }),
  682. Entry("hit route by test1.com", func() base.HttpTestCase {
  683. return base.HttpTestCase{
  684. Object: base.APISIXExpect(),
  685. Method: http.MethodGet,
  686. Path: "/hello",
  687. Headers: map[string]string{
  688. "Host": "test1.com",
  689. },
  690. ExpectStatus: http.StatusOK,
  691. ExpectBody: "hello world",
  692. Sleep: base.SleepTime,
  693. }
  694. }),
  695. Entry("hit route by test2.com", func() base.HttpTestCase {
  696. return base.HttpTestCase{
  697. Object: base.APISIXExpect(),
  698. Method: http.MethodGet,
  699. Path: "/hello",
  700. Headers: map[string]string{
  701. "Host": "test2.com",
  702. },
  703. ExpectStatus: http.StatusNotFound,
  704. Sleep: base.SleepTime,
  705. }
  706. }),
  707. Entry("delete route", func() base.HttpTestCase {
  708. return base.HttpTestCase{
  709. Desc: "delete route first",
  710. Object: base.ManagerApiExpect(),
  711. Method: http.MethodDelete,
  712. Path: "/apisix/admin/routes/r1",
  713. Headers: map[string]string{"Authorization": base.GetToken()},
  714. ExpectStatus: http.StatusOK,
  715. }
  716. }),
  717. Entry("delete service", func() base.HttpTestCase {
  718. return base.HttpTestCase{
  719. Desc: "delete service success",
  720. Object: base.ManagerApiExpect(),
  721. Method: http.MethodDelete,
  722. Path: "/apisix/admin/services/s1",
  723. Headers: map[string]string{"Authorization": base.GetToken()},
  724. ExpectStatus: http.StatusOK,
  725. }
  726. }),
  727. )
  728. })