-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitoring_prometheus_test.go
109 lines (92 loc) · 2.74 KB
/
monitoring_prometheus_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package gz
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gazebo-web/gz-go/v10/monitoring"
"github.com/gazebo-web/gz-go/v10/monitoring/prometheus"
"github.com/gorilla/mux"
"github.com/stretchr/testify/suite"
"gorm.io/gorm"
)
func TestMonitoringPrometheusTestSuite(t *testing.T) {
suite.Run(t, &MonitoringPrometheusTestSuite{})
}
type MonitoringPrometheusTestSuite struct {
suite.Suite
monitoring monitoring.Provider
metricsRoute string
router *mux.Router
server *Server
}
func (suite *MonitoringPrometheusTestSuite) SetupSuite() {
suite.monitoring = prometheus.NewPrometheusProvider("")
suite.metricsRoute = suite.monitoring.MetricsRoute()
suite.server = &Server{
HTTPPort: ":8000",
SSLport: ":4430",
monitoring: suite.monitoring,
}
// Prepare the router
suite.router = NewRouter()
// Test route
suite.server.ConfigureRouterWithRoutes("/", suite.router, Routes{
{
Name: "test",
Description: "Test route.",
URI: "/test",
Headers: nil,
Methods: Methods{
{
Type: "GET",
Description: "Test GET route.",
Handlers: FormatHandlers{
{
Extension: "",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set the body
_, err := w.Write([]byte("OK"))
// Set the status code
code := 200
if err != nil {
code = 500
}
w.WriteHeader(code)
}),
},
},
},
},
},
})
// SetRouter automatically configures the router with a monitoring route
suite.server.SetRouter(suite.router)
// Server needs to be initialized to prevent the logging middleware from panicking
gServer = suite.server
suite.server.Db = &gorm.DB{}
}
func (suite *MonitoringPrometheusTestSuite) TestMonitoringRouteExists() {
// Check that the route exists and returns successfully
match := &mux.RouteMatch{}
req, err := http.NewRequest("GET", suite.metricsRoute, nil)
suite.NoError(err)
suite.True(suite.router.Match(req, match))
suite.NoError(match.MatchErr)
}
func (suite *MonitoringPrometheusTestSuite) TestMonitoringRoute() {
sendRequest := func(route string) *httptest.ResponseRecorder {
req, err := http.NewRequest("GET", route, nil)
suite.NoError(err)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)
suite.Equal(200, w.Code)
return w
}
responseBody := sendRequest("/test").Body.String()
suite.Equal("OK", responseBody)
responseBody = sendRequest("/test").Body.String()
suite.Equal("OK", responseBody)
responseBody = sendRequest("/metrics").Body.String()
suite.Contains(responseBody, "http_requests_total{status=\"200\"} 2")
suite.Contains(responseBody, "http_request_duration_seconds_count{method=\"GET\",path=\"test\",status=\"200\"} 2")
}