|
|
@ -2,78 +2,89 @@ package metrics_test
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"github.com/containrrr/watchtower/pkg/metrics"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/containrrr/watchtower/pkg/api"
|
|
|
|
"github.com/containrrr/watchtower/pkg/api"
|
|
|
|
metricsAPI "github.com/containrrr/watchtower/pkg/api/metrics"
|
|
|
|
metricsAPI "github.com/containrrr/watchtower/pkg/api/metrics"
|
|
|
|
|
|
|
|
"github.com/containrrr/watchtower/pkg/metrics"
|
|
|
|
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const Token = "123123123"
|
|
|
|
const (
|
|
|
|
|
|
|
|
token = "123123123"
|
|
|
|
|
|
|
|
getURL = "http://localhost:8080/v1/metrics"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func TestContainer(t *testing.T) {
|
|
|
|
func TestMetrics(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
RunSpecs(t, "Metrics Suite")
|
|
|
|
RunSpecs(t, "Metrics Suite")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func runTestServer(m *metricsAPI.Handler) {
|
|
|
|
func getWithToken(handler http.Handler) map[string]string {
|
|
|
|
http.Handle(m.Path, m.Handle)
|
|
|
|
metricMap := map[string]string{}
|
|
|
|
go func() {
|
|
|
|
respWriter := httptest.NewRecorder()
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getWithToken(c http.Client, url string) (*http.Response, error) {
|
|
|
|
req := httptest.NewRequest("GET", getURL, nil)
|
|
|
|
req, _ := http.NewRequest("GET", url, nil)
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", Token))
|
|
|
|
|
|
|
|
return c.Do(req)
|
|
|
|
handler.ServeHTTP(respWriter, req)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res := respWriter.Result()
|
|
|
|
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, line := range strings.Split(string(body), "\n") {
|
|
|
|
|
|
|
|
if len(line) < 1 || line[0] == '#' {
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(line, " ")
|
|
|
|
|
|
|
|
metricMap[parts[0]] = parts[1]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return metricMap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var _ = Describe("the metrics", func() {
|
|
|
|
var _ = Describe("the metrics API", func() {
|
|
|
|
httpAPI := api.New(Token)
|
|
|
|
httpAPI := api.New(token)
|
|
|
|
m := metricsAPI.New()
|
|
|
|
m := metricsAPI.New()
|
|
|
|
|
|
|
|
|
|
|
|
httpAPI.RegisterHandler(m.Path, m.Handle)
|
|
|
|
handleReq := httpAPI.RequireToken(m.Handle)
|
|
|
|
httpAPI.Start(false)
|
|
|
|
tryGetMetrics := func() map[string]string { return getWithToken(handleReq) }
|
|
|
|
|
|
|
|
|
|
|
|
It("should serve metrics", func() {
|
|
|
|
It("should serve metrics", func() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Expect(tryGetMetrics()).To(HaveKeyWithValue("watchtower_containers_updated", "0"))
|
|
|
|
|
|
|
|
|
|
|
|
metric := &metrics.Metric{
|
|
|
|
metric := &metrics.Metric{
|
|
|
|
Scanned: 4,
|
|
|
|
Scanned: 4,
|
|
|
|
Updated: 3,
|
|
|
|
Updated: 3,
|
|
|
|
Failed: 1,
|
|
|
|
Failed: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
metrics.RegisterScan(metric)
|
|
|
|
metrics.RegisterScan(metric)
|
|
|
|
Eventually(metrics.Default().QueueIsEmpty).Should(BeTrue())
|
|
|
|
Eventually(metrics.Default().QueueIsEmpty).Should(BeTrue())
|
|
|
|
|
|
|
|
|
|
|
|
c := http.Client{}
|
|
|
|
Eventually(tryGetMetrics).Should(SatisfyAll(
|
|
|
|
|
|
|
|
HaveKeyWithValue("watchtower_containers_updated", "3"),
|
|
|
|
res, err := getWithToken(c, "http://localhost:8080/v1/metrics")
|
|
|
|
HaveKeyWithValue("watchtower_containers_failed", "1"),
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
HaveKeyWithValue("watchtower_containers_scanned", "4"),
|
|
|
|
|
|
|
|
HaveKeyWithValue("watchtower_scans_total", "1"),
|
|
|
|
contents, err := ioutil.ReadAll(res.Body)
|
|
|
|
HaveKeyWithValue("watchtower_scans_skipped", "0"),
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
))
|
|
|
|
Expect(string(contents)).To(ContainSubstring("watchtower_containers_updated 3"))
|
|
|
|
|
|
|
|
Expect(string(contents)).To(ContainSubstring("watchtower_containers_failed 1"))
|
|
|
|
|
|
|
|
Expect(string(contents)).To(ContainSubstring("watchtower_containers_scanned 4"))
|
|
|
|
|
|
|
|
Expect(string(contents)).To(ContainSubstring("watchtower_scans_total 1"))
|
|
|
|
|
|
|
|
Expect(string(contents)).To(ContainSubstring("watchtower_scans_skipped 0"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
metrics.RegisterScan(nil)
|
|
|
|
metrics.RegisterScan(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Eventually(metrics.Default().QueueIsEmpty).Should(BeTrue())
|
|
|
|
Eventually(metrics.Default().QueueIsEmpty).Should(BeTrue())
|
|
|
|
|
|
|
|
|
|
|
|
res, err = getWithToken(c, "http://localhost:8080/v1/metrics")
|
|
|
|
Eventually(tryGetMetrics).Should(SatisfyAll(
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
HaveKeyWithValue("watchtower_scans_total", "4"),
|
|
|
|
|
|
|
|
HaveKeyWithValue("watchtower_scans_skipped", "3"),
|
|
|
|
contents, err = ioutil.ReadAll(res.Body)
|
|
|
|
))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(string(contents)).To(ContainSubstring("watchtower_scans_total 4"))
|
|
|
|
|
|
|
|
Expect(string(contents)).To(ContainSubstring("watchtower_scans_skipped 3"))
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|