From 5180461ed3ec34c5c03d7ee20f484517b06c2fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sat, 11 May 2019 23:48:10 +0100 Subject: [PATCH] chore(backend): add test coverage for metrics handler --- main.go | 10 +++++++--- main_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 814d3507c..dca37e46c 100644 --- a/main.go +++ b/main.go @@ -100,6 +100,12 @@ func setupRouter(router *gin.Engine) { router.NoRoute(notFound) } +func setupMetrics(router *gin.Engine) { + prom := ginprometheus.NewPrometheus("gin") + prom.MetricsPath = getViewURL("/metrics") + prom.Use(router) +} + func setupUpstreams() { for _, s := range config.Config.Alertmanager.Servers { @@ -207,9 +213,7 @@ func main() { t = loadTemplate(t, "ui/build/index.html") router.SetHTMLTemplate(t) - prom := ginprometheus.NewPrometheus("gin") - prom.MetricsPath = getViewURL("/metrics") - prom.Use(router) + setupMetrics(router) if config.Config.Debug { ginpprof.Wrapper(router) diff --git a/main_test.go b/main_test.go index eb12e1d77..dafd23e21 100644 --- a/main_test.go +++ b/main_test.go @@ -1,6 +1,8 @@ package main import ( + "net/http" + "net/http/httptest" "testing" "github.com/prymitive/karma/internal/config" @@ -26,3 +28,15 @@ func TestLogConfig(t *testing.T) { } } } + +func TestMetrics(t *testing.T) { + mockConfig() + r := ginTestEngine() + setupMetrics(r) + req := httptest.NewRequest("GET", "/metrics", nil) + resp := httptest.NewRecorder() + r.ServeHTTP(resp, req) + if resp.Code != http.StatusOK { + t.Errorf("GET /metrics returned status %d", resp.Code) + } +}