Skip to content

Commit

Permalink
expect prometheus tests to go through EVENTUALLY
Browse files Browse the repository at this point in the history
  • Loading branch information
simskij committed Apr 20, 2021
1 parent ef9b41f commit 9aef958
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions pkg/api/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,46 @@ func getWithToken(c http.Client, url string) (*http.Response, error) {
var _ = Describe("the metrics", func() {
httpAPI := api.New(Token)
m := metricsAPI.New()

httpAPI.RegisterHandler(m.Path, m.Handle)
httpAPI.Start(false)

// We should likely split this into multiple tests, but as prometheus requires a restart of the binary
// to reset the metrics and gauges, we'll just do it all at once.

It("should serve metrics", func() {
metric := &metrics.Metric{
Scanned: 4,
Updated: 3,
Failed: 1,
}
metrics.RegisterScan(metric)
c := http.Client{}
res, err := getWithToken(c, "http://localhost:8080/v1/metrics")

Expect(err).NotTo(HaveOccurred())
contents, err := ioutil.ReadAll(res.Body)
Eventually(func() {
c := http.Client{}

res, err := getWithToken(c, "http://localhost:8080/v1/metrics")

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"))
Expect(err).NotTo(HaveOccurred())
contents, err := ioutil.ReadAll(res.Body)

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++ {
metrics.RegisterScan(nil)
}

res, err = getWithToken(c, "http://localhost:8080/v1/metrics")
Expect(err).NotTo(HaveOccurred())
contents, err = ioutil.ReadAll(res.Body)
Eventually(func() {
c := http.Client{}

res, err := getWithToken(c, "http://localhost:8080/v1/metrics")
Expect(err).NotTo(HaveOccurred())
contents, err := ioutil.ReadAll(res.Body)

Expect(string(contents)).To(ContainSubstring("watchtower_scans_total 4"))
Expect(string(contents)).To(ContainSubstring("watchtower_scans_skipped 3"))
Expect(string(contents)).To(ContainSubstring("watchtower_scans_total 4"))
Expect(string(contents)).To(ContainSubstring("watchtower_scans_skipped 3"))
})
})
})

0 comments on commit 9aef958

Please sign in to comment.