Skip to content

Commit

Permalink
wait for queue to be empty before checking test conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
simskij committed Apr 20, 2021
1 parent 9aef958 commit d878b35
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
39 changes: 18 additions & 21 deletions pkg/api/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,32 @@ var _ = Describe("the metrics", func() {
Failed: 1,
}
metrics.RegisterScan(metric)
Eventually(metrics.Default().QueueIsEmpty).Should(BeTrue())

Eventually(func() {
c := http.Client{}
c := http.Client{}

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

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"))
})
contents, err := ioutil.ReadAll(res.Body)
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++ {
metrics.RegisterScan(nil)
}
Eventually(metrics.Default().QueueIsEmpty).Should(BeTrue())

Eventually(func() {
c := http.Client{}

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

Expect(string(contents)).To(ContainSubstring("watchtower_scans_total 4"))
Expect(string(contents)).To(ContainSubstring("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"))
})
})
18 changes: 11 additions & 7 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ type Metric struct {

// Metrics is the handler processing all individual scan metrics
type Metrics struct {
channel chan *Metric
scanned prometheus.Gauge
updated prometheus.Gauge
failed prometheus.Gauge
total prometheus.Counter
skipped prometheus.Counter
channel chan *Metric
scanned prometheus.Gauge
updated prometheus.Gauge
failed prometheus.Gauge
total prometheus.Counter
skipped prometheus.Counter
}

func (metrics *Metrics) QueueIsEmpty() bool {
return len(metrics.channel) == 0
}

// Register registers metrics for an executed scan
Expand Down Expand Up @@ -56,7 +60,7 @@ func Default() *Metrics {
Name: "watchtower_scans_skipped",
Help: "Number of skipped scans since watchtower started",
}),
channel: make(chan *Metric, 10),
channel: make(chan *Metric, 10),
}

go metrics.HandleUpdate(metrics.channel)
Expand Down

0 comments on commit d878b35

Please sign in to comment.