@@ -2,78 +2,89 @@ package metrics_test
22
33import (
44 "fmt"
5- "github.com/containrrr/watchtower/pkg/metrics"
65 "io/ioutil"
76 "net/http"
7+ "net/http/httptest"
8+ "strings"
89 "testing"
910
1011 "github.com/containrrr/watchtower/pkg/api"
1112 metricsAPI "github.com/containrrr/watchtower/pkg/api/metrics"
13+ "github.com/containrrr/watchtower/pkg/metrics"
1214
1315 . "github.com/onsi/ginkgo"
1416 . "github.com/onsi/gomega"
1517)
1618
17- const Token = "123123123"
19+ const (
20+ token = "123123123"
21+ getURL = "http://localhost:8080/v1/metrics"
22+ )
1823
19- func TestContainer (t * testing.T ) {
24+ func TestMetrics (t * testing.T ) {
2025 RegisterFailHandler (Fail )
2126 RunSpecs (t , "Metrics Suite" )
2227}
2328
24- func runTestServer (m * metricsAPI.Handler ) {
25- http .Handle (m .Path , m .Handle )
26- go func () {
27- http .ListenAndServe (":8080" , nil )
28- }()
29- }
29+ func getWithToken (handler http.Handler ) map [string ]string {
30+ metricMap := map [string ]string {}
31+ respWriter := httptest .NewRecorder ()
3032
31- func getWithToken (c http.Client , url string ) (* http.Response , error ) {
32- req , _ := http .NewRequest ("GET" , url , nil )
33- req .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , Token ))
34- return c .Do (req )
33+ req := httptest .NewRequest ("GET" , getURL , nil )
34+ req .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , token ))
35+
36+ handler .ServeHTTP (respWriter , req )
37+
38+ res := respWriter .Result ()
39+ body , _ := ioutil .ReadAll (res .Body )
40+
41+ for _ , line := range strings .Split (string (body ), "\n " ) {
42+ if len (line ) < 1 || line [0 ] == '#' {
43+ continue
44+ }
45+ parts := strings .Split (line , " " )
46+ metricMap [parts [0 ]] = parts [1 ]
47+ }
48+
49+ return metricMap
3550}
3651
37- var _ = Describe ("the metrics" , func () {
38- httpAPI := api .New (Token )
52+ var _ = Describe ("the metrics API " , func () {
53+ httpAPI := api .New (token )
3954 m := metricsAPI .New ()
4055
41- httpAPI .RegisterHandler ( m . Path , m .Handle )
42- httpAPI . Start ( false )
56+ handleReq := httpAPI .RequireToken ( m .Handle )
57+ tryGetMetrics := func () map [ string ] string { return getWithToken ( handleReq ) }
4358
4459 It ("should serve metrics" , func () {
60+
61+ Expect (tryGetMetrics ()).To (HaveKeyWithValue ("watchtower_containers_updated" , "0" ))
62+
4563 metric := & metrics.Metric {
4664 Scanned : 4 ,
4765 Updated : 3 ,
4866 Failed : 1 ,
4967 }
68+
5069 metrics .RegisterScan (metric )
5170 Eventually (metrics .Default ().QueueIsEmpty ).Should (BeTrue ())
5271
53- c := http.Client {}
54-
55- res , err := getWithToken (c , "http://localhost:8080/v1/metrics" )
56- Expect (err ).ToNot (HaveOccurred ())
57-
58- contents , err := ioutil .ReadAll (res .Body )
59- Expect (err ).ToNot (HaveOccurred ())
60- Expect (string (contents )).To (ContainSubstring ("watchtower_containers_updated 3" ))
61- Expect (string (contents )).To (ContainSubstring ("watchtower_containers_failed 1" ))
62- Expect (string (contents )).To (ContainSubstring ("watchtower_containers_scanned 4" ))
63- Expect (string (contents )).To (ContainSubstring ("watchtower_scans_total 1" ))
64- Expect (string (contents )).To (ContainSubstring ("watchtower_scans_skipped 0" ))
72+ Eventually (tryGetMetrics ).Should (SatisfyAll (
73+ HaveKeyWithValue ("watchtower_containers_updated" , "3" ),
74+ HaveKeyWithValue ("watchtower_containers_failed" , "1" ),
75+ HaveKeyWithValue ("watchtower_containers_scanned" , "4" ),
76+ HaveKeyWithValue ("watchtower_scans_total" , "1" ),
77+ HaveKeyWithValue ("watchtower_scans_skipped" , "0" ),
78+ ))
6579
6680 for i := 0 ; i < 3 ; i ++ {
6781 metrics .RegisterScan (nil )
6882 }
6983 Eventually (metrics .Default ().QueueIsEmpty ).Should (BeTrue ())
7084
71- res , err = getWithToken (c , "http://localhost:8080/v1/metrics" )
72- Expect (err ).ToNot (HaveOccurred ())
73-
74- contents , err = ioutil .ReadAll (res .Body )
75- Expect (err ).ToNot (HaveOccurred ())
76- Expect (string (contents )).To (ContainSubstring ("watchtower_scans_total 4" ))
77- Expect (string (contents )).To (ContainSubstring ("watchtower_scans_skipped 3" ))
85+ Eventually (tryGetMetrics ).Should (SatisfyAll (
86+ HaveKeyWithValue ("watchtower_scans_total" , "4" ),
87+ HaveKeyWithValue ("watchtower_scans_skipped" , "3" ),
88+ ))
7889 })
7990})
0 commit comments