migrate_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 migrate_test
  18. import (
  19. "bytes"
  20. "encoding/binary"
  21. "encoding/json"
  22. "hash/crc32"
  23. "net/http"
  24. "time"
  25. . "github.com/onsi/ginkgo/v2"
  26. . "github.com/onsi/gomega"
  27. "github.com/apisix/manager-api/test/e2e/base"
  28. )
  29. const (
  30. checksumLength = 4 // 4 bytes (uint32)
  31. )
  32. type AllData struct {
  33. Consumers []interface{}
  34. Routes []interface{}
  35. Services []interface{}
  36. SSLs []interface{}
  37. Upstreams []interface{}
  38. Scripts []interface{}
  39. GlobalPlugins []interface{}
  40. PluginConfigs []interface{}
  41. }
  42. type response struct {
  43. Code int `json:"Code"`
  44. Message string `json:"Message"`
  45. Data struct {
  46. ConflictItems AllData `json:"ConflictItems"`
  47. } `json:"Data"`
  48. }
  49. var _ = Describe("Migrate", func() {
  50. var exportData []byte
  51. DescribeTable("Prepare config data",
  52. func(tc base.HttpTestCase) {
  53. base.RunTestCase(tc)
  54. },
  55. Entry("create test route", base.HttpTestCase{
  56. Object: base.ManagerApiExpect(),
  57. Method: http.MethodPut,
  58. Path: "/apisix/admin/routes/r1",
  59. Body: `{
  60. "name": "route",
  61. "uri": "/hello_",
  62. "upstream": {
  63. "nodes": {
  64. "` + base.UpstreamIp + `:1980": 1
  65. },
  66. "type": "roundrobin"
  67. }
  68. }`,
  69. Headers: map[string]string{"Authorization": base.GetToken()},
  70. ExpectStatus: http.StatusOK,
  71. }),
  72. Entry("create test upstream", base.HttpTestCase{
  73. Object: base.ManagerApiExpect(),
  74. Method: http.MethodPut,
  75. Path: "/apisix/admin/upstreams/u1",
  76. Body: `{
  77. "name": "upstream",
  78. "nodes": [
  79. {
  80. "host": "` + base.UpstreamIp + `",
  81. "port": 1980,
  82. "weight": 1
  83. }
  84. ],
  85. "type": "roundrobin"
  86. }`,
  87. Headers: map[string]string{"Authorization": base.GetToken()},
  88. ExpectStatus: http.StatusOK,
  89. }),
  90. Entry("create test service", base.HttpTestCase{
  91. Object: base.ManagerApiExpect(),
  92. Method: http.MethodPut,
  93. Path: "/apisix/admin/services/s1",
  94. Body: `{
  95. "name": "service",
  96. "upstream": {
  97. "nodes": [
  98. {
  99. "host": "` + base.UpstreamIp + `",
  100. "port": 1980,
  101. "weight": 1
  102. },
  103. {
  104. "host": "` + base.UpstreamIp + `",
  105. "port": 1981,
  106. "weight": 2
  107. },
  108. {
  109. "host": "` + base.UpstreamIp + `",
  110. "port": 1982,
  111. "weight": 3
  112. }
  113. ],
  114. "type": "roundrobin"
  115. }
  116. }`,
  117. Headers: map[string]string{"Authorization": base.GetToken()},
  118. ExpectStatus: http.StatusOK,
  119. Sleep: time.Second * 1,
  120. }),
  121. Entry("migrate export auth test", base.HttpTestCase{
  122. Object: base.ManagerApiExpect(),
  123. Method: http.MethodPost,
  124. Path: "/apisix/admin/migrate/export",
  125. ExpectStatus: http.StatusUnauthorized,
  126. ExpectBody: "request unauthorized",
  127. Sleep: base.SleepTime,
  128. }),
  129. Entry("migrate import auth test", base.HttpTestCase{
  130. Object: base.ManagerApiExpect(),
  131. Method: http.MethodPost,
  132. Path: "/apisix/admin/migrate/import",
  133. ExpectStatus: http.StatusUnauthorized,
  134. ExpectBody: "request unauthorized",
  135. Sleep: base.SleepTime,
  136. }),
  137. )
  138. It("export config success", func() {
  139. req := base.ManagerApiExpect().GET("/apisix/admin/migrate/export")
  140. req.WithHeader("Authorization", base.GetToken())
  141. resp := req.Expect()
  142. resp.Status(http.StatusOK)
  143. exportData = []byte(resp.Body().Raw())
  144. data := exportData[:len(exportData)-checksumLength]
  145. checksum := binary.BigEndian.Uint32(exportData[len(exportData)-checksumLength:])
  146. Expect(checksum).Should(Equal(crc32.ChecksumIEEE(data)))
  147. })
  148. It("import config conflict and return", func() {
  149. req := base.ManagerApiExpect().POST("/apisix/admin/migrate/import")
  150. buffer := bytes.NewBuffer(exportData)
  151. req.WithMultipart().WithForm(map[string]string{"mode": "return"})
  152. req.WithMultipart().WithFile("file", "apisix-config.bak", buffer)
  153. req.WithHeader("Authorization", base.GetToken())
  154. resp := req.Expect()
  155. resp.Status(http.StatusOK)
  156. rsp := &response{}
  157. err := json.Unmarshal([]byte(resp.Body().Raw()), rsp)
  158. Expect(err).Should(BeNil())
  159. Expect(rsp.Code).Should(Equal(20001))
  160. Expect(len(rsp.Data.ConflictItems.Routes)).Should(Equal(1))
  161. Expect(len(rsp.Data.ConflictItems.Upstreams)).Should(Equal(1))
  162. Expect(len(rsp.Data.ConflictItems.Services)).Should(Equal(1))
  163. })
  164. It("import config conflict and skip", func() {
  165. req := base.ManagerApiExpect().POST("/apisix/admin/migrate/import")
  166. buffer := bytes.NewBuffer(exportData)
  167. req.WithMultipart().WithForm(map[string]string{"mode": "skip"})
  168. req.WithMultipart().WithFile("file", "apisix-config.bak", buffer)
  169. req.WithHeader("Authorization", base.GetToken())
  170. resp := req.Expect()
  171. resp.Status(http.StatusOK)
  172. rsp := &response{}
  173. err := json.Unmarshal([]byte(resp.Body().Raw()), rsp)
  174. Expect(err).Should(BeNil())
  175. Expect(rsp.Code).Should(Equal(0))
  176. })
  177. It("import config conflict and overwrite", func() {
  178. req := base.ManagerApiExpect().POST("/apisix/admin/migrate/import")
  179. buffer := bytes.NewBuffer(exportData)
  180. req.WithMultipart().WithForm(map[string]string{"mode": "overwrite"})
  181. req.WithMultipart().WithFile("file", "apisix-config.bak", buffer)
  182. req.WithHeader("Authorization", base.GetToken())
  183. resp := req.Expect()
  184. resp.Status(http.StatusOK)
  185. rsp := &response{}
  186. err := json.Unmarshal([]byte(resp.Body().Raw()), rsp)
  187. Expect(err).Should(BeNil())
  188. Expect(rsp.Code).Should(Equal(0))
  189. })
  190. It("request hit route r1", func() {
  191. base.RunTestCase(base.HttpTestCase{
  192. Object: base.APISIXExpect(),
  193. Method: http.MethodGet,
  194. Path: "/hello_",
  195. ExpectStatus: http.StatusOK,
  196. ExpectBody: "hello world",
  197. Sleep: base.SleepTime,
  198. })
  199. })
  200. DescribeTable("Cleanup config data",
  201. func(tc base.HttpTestCase) {
  202. base.RunTestCase(tc)
  203. },
  204. Entry("remove test route", base.HttpTestCase{
  205. Object: base.ManagerApiExpect(),
  206. Method: http.MethodDelete,
  207. Path: "/apisix/admin/routes/r1",
  208. Headers: map[string]string{"Authorization": base.GetToken()},
  209. ExpectStatus: http.StatusOK,
  210. }),
  211. Entry("remove test upstream", base.HttpTestCase{
  212. Object: base.ManagerApiExpect(),
  213. Method: http.MethodDelete,
  214. Path: "/apisix/admin/upstreams/u1",
  215. Headers: map[string]string{"Authorization": base.GetToken()},
  216. ExpectStatus: http.StatusOK,
  217. }),
  218. Entry("remove test service", base.HttpTestCase{
  219. Object: base.ManagerApiExpect(),
  220. Method: http.MethodDelete,
  221. Path: "/apisix/admin/services/s1",
  222. Headers: map[string]string{"Authorization": base.GetToken()},
  223. ExpectStatus: http.StatusOK,
  224. }),
  225. )
  226. It("delete imported route failed", func() {
  227. base.RunTestCase(base.HttpTestCase{
  228. Object: base.ManagerApiExpect(),
  229. Method: http.MethodDelete,
  230. Path: "/apisix/admin/routes/r1",
  231. Headers: map[string]string{"Authorization": base.GetToken()},
  232. ExpectStatus: http.StatusNotFound,
  233. })
  234. })
  235. It("request route r1 not found", func() {
  236. base.RunTestCase(base.HttpTestCase{
  237. Object: base.APISIXExpect(),
  238. Method: http.MethodGet,
  239. Path: "/hello_",
  240. ExpectStatus: http.StatusNotFound,
  241. Sleep: base.SleepTime,
  242. })
  243. })
  244. It("import config success", func() {
  245. req := base.ManagerApiExpect().POST("/apisix/admin/migrate/import")
  246. buffer := bytes.NewBuffer(exportData)
  247. req.WithMultipart().WithForm(map[string]string{"mode": "return"})
  248. req.WithMultipart().WithFile("file", "apisix-config.bak", buffer)
  249. req.WithHeader("Authorization", base.GetToken())
  250. resp := req.Expect()
  251. resp.Status(http.StatusOK)
  252. rsp := &response{}
  253. err := json.Unmarshal([]byte(resp.Body().Raw()), rsp)
  254. Expect(err).Should(BeNil())
  255. Expect(rsp.Code).Should(Equal(0))
  256. })
  257. It("request hit route r1", func() {
  258. base.RunTestCase(base.HttpTestCase{
  259. Object: base.APISIXExpect(),
  260. Method: http.MethodGet,
  261. Path: "/hello_",
  262. ExpectStatus: http.StatusOK,
  263. ExpectBody: "hello world",
  264. Sleep: base.SleepTime,
  265. })
  266. })
  267. It("delete imported route success", func() {
  268. base.RunTestCase(base.HttpTestCase{
  269. Object: base.ManagerApiExpect(),
  270. Method: http.MethodDelete,
  271. Path: "/apisix/admin/routes/r1",
  272. Headers: map[string]string{"Authorization": base.GetToken()},
  273. ExpectStatus: http.StatusOK,
  274. })
  275. })
  276. It("delete imported upstream success", func() {
  277. base.RunTestCase(base.HttpTestCase{
  278. Object: base.ManagerApiExpect(),
  279. Method: http.MethodDelete,
  280. Path: "/apisix/admin/upstreams/u1",
  281. Headers: map[string]string{"Authorization": base.GetToken()},
  282. ExpectStatus: http.StatusOK,
  283. })
  284. })
  285. It("delete imported service success", func() {
  286. base.RunTestCase(base.HttpTestCase{
  287. Object: base.ManagerApiExpect(),
  288. Method: http.MethodDelete,
  289. Path: "/apisix/admin/services/s1",
  290. Headers: map[string]string{"Authorization": base.GetToken()},
  291. ExpectStatus: http.StatusOK,
  292. })
  293. })
  294. })