label_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 label_test
  18. import (
  19. "net/http"
  20. . "github.com/onsi/ginkgo/v2"
  21. "github.com/apisix/manager-api/test/e2e/base"
  22. )
  23. var _ = DescribeTable("Test label",
  24. func(tc base.HttpTestCase) {
  25. base.RunTestCase(tc)
  26. },
  27. Entry("config route", base.HttpTestCase{
  28. Object: base.ManagerApiExpect(),
  29. Path: "/apisix/admin/routes/r1",
  30. Method: http.MethodPut,
  31. Body: `{
  32. "name": "route1",
  33. "uri": "/hello",
  34. "labels": {
  35. "build":"16",
  36. "env":"production",
  37. "version":"v2"
  38. },
  39. "upstream": {
  40. "type": "roundrobin",
  41. "nodes": [{
  42. "host": "` + base.UpstreamIp + `",
  43. "port": 1980,
  44. "weight": 1
  45. }]
  46. }
  47. }`,
  48. Headers: map[string]string{"Authorization": base.GetToken()},
  49. ExpectStatus: http.StatusOK,
  50. }),
  51. Entry("create consumer", base.HttpTestCase{
  52. Object: base.ManagerApiExpect(),
  53. Path: "/apisix/admin/consumers/c1",
  54. Method: http.MethodPut,
  55. Body: `{
  56. "username": "c1",
  57. "plugins": {
  58. "key-auth": {
  59. "key": "auth-one"
  60. }
  61. },
  62. "labels": {
  63. "build":"16",
  64. "env":"production",
  65. "version":"v3"
  66. },
  67. "desc": "test description"
  68. }`,
  69. Headers: map[string]string{"Authorization": base.GetToken()},
  70. ExpectStatus: http.StatusOK,
  71. }),
  72. Entry("create upstream", base.HttpTestCase{
  73. Object: base.ManagerApiExpect(),
  74. Method: http.MethodPut,
  75. Path: "/apisix/admin/upstreams/u1",
  76. Body: `{
  77. "nodes": [{
  78. "host": "` + base.UpstreamIp + `",
  79. "port": 1980,
  80. "weight": 1
  81. }],
  82. "labels": {
  83. "build":"17",
  84. "env":"production",
  85. "version":"v2"
  86. },
  87. "type": "roundrobin"
  88. }`,
  89. Headers: map[string]string{"Authorization": base.GetToken()},
  90. ExpectStatus: http.StatusOK,
  91. }),
  92. Entry("create service", base.HttpTestCase{
  93. Object: base.ManagerApiExpect(),
  94. Method: http.MethodPost,
  95. Path: "/apisix/admin/services",
  96. Body: `{
  97. "id": "s1",
  98. "plugins": {
  99. "limit-count": {
  100. "count": 2,
  101. "time_window": 60,
  102. "rejected_code": 503,
  103. "key": "remote_addr",
  104. "policy": "local"
  105. }
  106. },
  107. "upstream": {
  108. "type": "roundrobin",
  109. "nodes": [{
  110. "host": "39.97.63.215",
  111. "port": 80,
  112. "weight": 1
  113. }]
  114. },
  115. "labels": {
  116. "build":"16",
  117. "env":"production",
  118. "version":"v2",
  119. "extra": "test"
  120. }
  121. }`,
  122. Headers: map[string]string{"Authorization": base.GetToken()},
  123. ExpectStatus: http.StatusOK,
  124. }),
  125. Entry("create plugin_config", base.HttpTestCase{
  126. Object: base.ManagerApiExpect(),
  127. Method: http.MethodPut,
  128. Path: "/apisix/admin/plugin_configs/1",
  129. Body: `{
  130. "plugins": {
  131. "response-rewrite": {
  132. "headers": {
  133. "X-VERSION":"22.0"
  134. }
  135. }
  136. },
  137. "labels": {
  138. "version": "v2",
  139. "build": "17",
  140. "extra": "test"
  141. }
  142. }`,
  143. Headers: map[string]string{"Authorization": base.GetToken()},
  144. ExpectStatus: http.StatusOK,
  145. }),
  146. Entry("get route label", base.HttpTestCase{
  147. Object: base.ManagerApiExpect(),
  148. Method: http.MethodGet,
  149. Headers: map[string]string{"Authorization": base.GetToken()},
  150. Path: "/apisix/admin/labels/route",
  151. ExpectStatus: http.StatusOK,
  152. ExpectBody: "{\"build\":\"16\"},{\"env\":\"production\"},{\"version\":\"v2\"}",
  153. Sleep: base.SleepTime,
  154. }),
  155. Entry("get consumer label", base.HttpTestCase{
  156. Object: base.ManagerApiExpect(),
  157. Method: http.MethodGet,
  158. Headers: map[string]string{"Authorization": base.GetToken()},
  159. Path: "/apisix/admin/labels/consumer",
  160. ExpectStatus: http.StatusOK,
  161. ExpectBody: "{\"build\":\"16\"},{\"env\":\"production\"},{\"version\":\"v3\"}",
  162. }),
  163. Entry("get upstream label", base.HttpTestCase{
  164. Object: base.ManagerApiExpect(),
  165. Method: http.MethodGet,
  166. Headers: map[string]string{"Authorization": base.GetToken()},
  167. Path: "/apisix/admin/labels/upstream",
  168. ExpectStatus: http.StatusOK,
  169. ExpectBody: "{\"build\":\"17\"},{\"env\":\"production\"},{\"version\":\"v2\"}",
  170. }),
  171. Entry("get service label", base.HttpTestCase{
  172. Object: base.ManagerApiExpect(),
  173. Method: http.MethodGet,
  174. Headers: map[string]string{"Authorization": base.GetToken()},
  175. Path: "/apisix/admin/labels/service",
  176. ExpectStatus: http.StatusOK,
  177. ExpectBody: "{\"build\":\"16\"},{\"env\":\"production\"},{\"extra\":\"test\"},{\"version\":\"v2\"}",
  178. }),
  179. Entry("get plugin_config label", base.HttpTestCase{
  180. Object: base.ManagerApiExpect(),
  181. Method: http.MethodGet,
  182. Headers: map[string]string{"Authorization": base.GetToken()},
  183. Path: "/apisix/admin/labels/plugin_config",
  184. ExpectStatus: http.StatusOK,
  185. ExpectBody: "{\"build\":\"17\"},{\"extra\":\"test\"},{\"version\":\"v2\"}",
  186. }),
  187. Entry("update plugin_config", base.HttpTestCase{
  188. Object: base.ManagerApiExpect(),
  189. Method: http.MethodPut,
  190. Path: "/apisix/admin/plugin_configs/1",
  191. Body: `{
  192. "plugins": {
  193. "response-rewrite": {
  194. "headers": {
  195. "X-VERSION":"22.0"
  196. }
  197. }
  198. },
  199. "labels": {
  200. "version": "v3",
  201. "build": "16",
  202. "extra": "test"
  203. }
  204. }`,
  205. Headers: map[string]string{"Authorization": base.GetToken()},
  206. ExpectStatus: http.StatusOK,
  207. }),
  208. Entry("get plugin_config label again to verify update", base.HttpTestCase{
  209. Object: base.ManagerApiExpect(),
  210. Method: http.MethodGet,
  211. Headers: map[string]string{"Authorization": base.GetToken()},
  212. Path: "/apisix/admin/labels/plugin_config",
  213. ExpectStatus: http.StatusOK,
  214. ExpectBody: "{\"build\":\"16\"},{\"extra\":\"test\"},{\"version\":\"v3\"}",
  215. Sleep: base.SleepTime,
  216. }),
  217. Entry("get all label", base.HttpTestCase{
  218. Object: base.ManagerApiExpect(),
  219. Method: http.MethodGet,
  220. Headers: map[string]string{"Authorization": base.GetToken()},
  221. Path: "/apisix/admin/labels/all",
  222. ExpectStatus: http.StatusOK,
  223. ExpectBody: "{\"build\":\"16\"},{\"build\":\"17\"},{\"env\":\"production\"},{\"extra\":\"test\"},{\"version\":\"v2\"},{\"version\":\"v3\"}",
  224. }),
  225. Entry("get label with page", base.HttpTestCase{
  226. Object: base.ManagerApiExpect(),
  227. Method: http.MethodGet,
  228. Query: "page=1&page_size=1",
  229. Headers: map[string]string{"Authorization": base.GetToken()},
  230. Path: "/apisix/admin/labels/all",
  231. ExpectStatus: http.StatusOK,
  232. ExpectBody: "{\"build\":\"16\"}",
  233. }),
  234. Entry("get label with page", base.HttpTestCase{
  235. Object: base.ManagerApiExpect(),
  236. Method: http.MethodGet,
  237. Query: "page=3&page_size=1",
  238. Headers: map[string]string{"Authorization": base.GetToken()},
  239. Path: "/apisix/admin/labels/all",
  240. ExpectStatus: http.StatusOK,
  241. ExpectBody: "{\"env\":\"production\"}",
  242. }),
  243. Entry("get labels (key = build)", base.HttpTestCase{
  244. Object: base.ManagerApiExpect(),
  245. Method: http.MethodGet,
  246. Headers: map[string]string{"Authorization": base.GetToken()},
  247. Query: "label=build",
  248. Path: "/apisix/admin/labels/all",
  249. ExpectStatus: http.StatusOK,
  250. ExpectBody: "{\"build\":\"16\"},{\"build\":\"17\"}",
  251. }),
  252. Entry("get labels with the same key (key = build)", base.HttpTestCase{
  253. Object: base.ManagerApiExpect(),
  254. Method: http.MethodGet,
  255. Headers: map[string]string{"Authorization": base.GetToken()},
  256. Query: "label=build:16,build:17",
  257. Path: "/apisix/admin/labels/all",
  258. ExpectStatus: http.StatusOK,
  259. ExpectBody: "{\"build\":\"16\"},{\"build\":\"17\"}",
  260. }),
  261. Entry("get labels (key = build) with page", base.HttpTestCase{
  262. Object: base.ManagerApiExpect(),
  263. Method: http.MethodGet,
  264. Headers: map[string]string{"Authorization": base.GetToken()},
  265. Query: "label=build&page=2&page_size=1",
  266. Path: "/apisix/admin/labels/all",
  267. ExpectStatus: http.StatusOK,
  268. ExpectBody: "{\"build\":\"17\"}",
  269. }),
  270. Entry("get labels with same key (key = build) and page", base.HttpTestCase{
  271. Object: base.ManagerApiExpect(),
  272. Method: http.MethodGet,
  273. Headers: map[string]string{"Authorization": base.GetToken()},
  274. Query: "label=build:16,build:17&page=1&page_size=2",
  275. Path: "/apisix/admin/labels/all",
  276. ExpectStatus: http.StatusOK,
  277. ExpectBody: "{\"build\":\"16\"},{\"build\":\"17\"}",
  278. }),
  279. Entry("get labels with same key (key = build) and page", base.HttpTestCase{
  280. Object: base.ManagerApiExpect(),
  281. Method: http.MethodGet,
  282. Headers: map[string]string{"Authorization": base.GetToken()},
  283. Query: "label=build:16,build:17&page=2&page_size=1",
  284. Path: "/apisix/admin/labels/all",
  285. ExpectStatus: http.StatusOK,
  286. ExpectBody: "{\"build\":\"17\"}",
  287. }),
  288. Entry("get labels (key = build && env = production)", base.HttpTestCase{
  289. Object: base.ManagerApiExpect(),
  290. Method: http.MethodGet,
  291. Headers: map[string]string{"Authorization": base.GetToken()},
  292. Query: "label=build,env:production",
  293. Path: "/apisix/admin/labels/all",
  294. ExpectStatus: http.StatusOK,
  295. ExpectBody: "{\"build\":\"16\"},{\"build\":\"17\"},{\"env\":\"production\"}",
  296. }),
  297. Entry("get labels (build=16 | 17 and env = production)", base.HttpTestCase{
  298. Object: base.ManagerApiExpect(),
  299. Method: http.MethodGet,
  300. Headers: map[string]string{"Authorization": base.GetToken()},
  301. Query: "label=build:16,build:17,env:production",
  302. Path: "/apisix/admin/labels/all",
  303. ExpectStatus: http.StatusOK,
  304. ExpectBody: "{\"build\":\"16\"},{\"build\":\"17\"},{\"env\":\"production\"}",
  305. }),
  306. Entry("get labels (key = build && env = production) with page", base.HttpTestCase{
  307. Object: base.ManagerApiExpect(),
  308. Method: http.MethodGet,
  309. Headers: map[string]string{"Authorization": base.GetToken()},
  310. Query: "label=build,env:production&page=3&page_size=1",
  311. Path: "/apisix/admin/labels/all",
  312. ExpectStatus: http.StatusOK,
  313. ExpectBody: "{\"env\":\"production\"}",
  314. }),
  315. Entry("delete route", base.HttpTestCase{
  316. Object: base.ManagerApiExpect(),
  317. Method: http.MethodDelete,
  318. Path: "/apisix/admin/routes/r1",
  319. Headers: map[string]string{"Authorization": base.GetToken()},
  320. ExpectStatus: http.StatusOK,
  321. }),
  322. Entry("delete consumer", base.HttpTestCase{
  323. Object: base.ManagerApiExpect(),
  324. Method: http.MethodDelete,
  325. Path: "/apisix/admin/consumers/c1",
  326. Headers: map[string]string{"Authorization": base.GetToken()},
  327. ExpectStatus: http.StatusOK,
  328. }),
  329. Entry("delete service", base.HttpTestCase{
  330. Object: base.ManagerApiExpect(),
  331. Method: http.MethodDelete,
  332. Path: "/apisix/admin/services/s1",
  333. Headers: map[string]string{"Authorization": base.GetToken()},
  334. ExpectStatus: http.StatusOK,
  335. }),
  336. Entry("delete upstream", base.HttpTestCase{
  337. Object: base.ManagerApiExpect(),
  338. Method: http.MethodDelete,
  339. Path: "/apisix/admin/upstreams/u1",
  340. Headers: map[string]string{"Authorization": base.GetToken()},
  341. ExpectStatus: http.StatusOK,
  342. }),
  343. Entry("delete plugin_config", base.HttpTestCase{
  344. Object: base.ManagerApiExpect(),
  345. Method: http.MethodDelete,
  346. Path: "/apisix/admin/plugin_configs/1",
  347. Headers: map[string]string{"Authorization": base.GetToken()},
  348. ExpectStatus: http.StatusOK,
  349. }),
  350. )