route_with_plugin_prometheus_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/apisix/manager-api/test/e2e/base"
  23. )
  24. var _ = DescribeTable("route with plugin prometheus",
  25. func(tc base.HttpTestCase) {
  26. base.RunTestCase(tc)
  27. },
  28. Entry("make sure the route is not created", base.HttpTestCase{
  29. Object: base.APISIXExpect(),
  30. Method: http.MethodGet,
  31. Path: "/hello",
  32. ExpectStatus: http.StatusNotFound,
  33. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  34. }),
  35. Entry("create route with plugin prometheus", base.HttpTestCase{
  36. Desc: "create route",
  37. Object: base.ManagerApiExpect(),
  38. Method: http.MethodPut,
  39. Path: "/apisix/admin/routes/r1",
  40. Body: `{
  41. "name": "route1",
  42. "uri": "/hello",
  43. "plugins": {
  44. "prometheus": {}
  45. },
  46. "upstream": {
  47. "type": "roundrobin",
  48. "nodes": {
  49. "` + base.UpstreamIp + `:1982": 1
  50. }
  51. }
  52. }`,
  53. Headers: map[string]string{"Authorization": base.GetToken()},
  54. ExpectStatus: http.StatusOK,
  55. }),
  56. Entry("fetch the prometheus metric data", base.HttpTestCase{
  57. Object: base.PrometheusExporterExpect(),
  58. Method: http.MethodGet,
  59. Path: "/apisix/prometheus/metrics",
  60. ExpectStatus: http.StatusOK,
  61. ExpectBody: "apisix_etcd_reachable 1",
  62. Sleep: base.SleepTime,
  63. }),
  64. Entry("request from client (200)", base.HttpTestCase{
  65. Object: base.APISIXExpect(),
  66. Method: http.MethodGet,
  67. Path: "/hello",
  68. ExpectStatus: http.StatusOK,
  69. ExpectBody: "hello world",
  70. }),
  71. Entry("create route that uri not exists in upstream", base.HttpTestCase{
  72. Object: base.ManagerApiExpect(),
  73. Method: http.MethodPut,
  74. Path: "/apisix/admin/routes/r1",
  75. Body: `{
  76. "name": "route1",
  77. "uri": "/hello-not-exists",
  78. "plugins": {
  79. "prometheus": {}
  80. },
  81. "upstream": {
  82. "type": "roundrobin",
  83. "nodes": {
  84. "` + base.UpstreamIp + `:1982": 1
  85. }
  86. }
  87. }`,
  88. Headers: map[string]string{"Authorization": base.GetToken()},
  89. ExpectStatus: http.StatusOK,
  90. }),
  91. Entry("request from client (404)", base.HttpTestCase{
  92. Object: base.APISIXExpect(),
  93. Method: http.MethodGet,
  94. Path: "/hello-not-exists",
  95. ExpectStatus: http.StatusNotFound,
  96. Sleep: base.SleepTime,
  97. }),
  98. Entry("verify the prometheus metric data (apisix_http_status 200)", base.HttpTestCase{
  99. Object: base.PrometheusExporterExpect(),
  100. Method: http.MethodGet,
  101. Path: "/apisix/prometheus/metrics",
  102. ExpectStatus: http.StatusOK,
  103. ExpectBody: `apisix_http_status{code="200",route="r1",matched_uri="/hello",matched_host="",service="",consumer=""`,
  104. Sleep: 1 * time.Second,
  105. }),
  106. Entry("verify the prometheus metric data (apisix_http_status 404)", base.HttpTestCase{
  107. Object: base.PrometheusExporterExpect(),
  108. Method: http.MethodGet,
  109. Path: "/apisix/prometheus/metrics",
  110. ExpectStatus: http.StatusOK,
  111. ExpectBody: `apisix_http_status{code="404",route="r1",matched_uri="/hello-not-exists",matched_host="",service="",consumer=""`,
  112. Sleep: base.SleepTime,
  113. }),
  114. Entry("delete route", base.HttpTestCase{
  115. Object: base.ManagerApiExpect(),
  116. Method: http.MethodDelete,
  117. Path: "/apisix/admin/routes/r1",
  118. Headers: map[string]string{"Authorization": base.GetToken()},
  119. ExpectStatus: http.StatusOK,
  120. Sleep: base.SleepTime,
  121. }),
  122. Entry("make sure the route deleted", base.HttpTestCase{
  123. Object: base.APISIXExpect(),
  124. Method: http.MethodGet,
  125. Path: "/hello",
  126. ExpectStatus: http.StatusNotFound,
  127. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  128. Sleep: base.SleepTime,
  129. }),
  130. )