Skip to content

Commit dec6f84

Browse files
authored
test: fix metrics api test stability (#930)
* use httptest instead of host port binding * restore matrix and remove artificial delay * fix metrics api test expect calls
1 parent 61b715a commit dec6f84

File tree

2 files changed

+48
-56
lines changed

2 files changed

+48
-56
lines changed

.github/workflows/pull-request.yml

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ jobs:
3535
platform:
3636
- macos-latest
3737
- windows-latest
38+
- ubuntu-latest
3839
runs-on: ${{ matrix.platform }}
3940
steps:
4041
- name: Checkout
@@ -52,26 +53,6 @@ jobs:
5253
uses: codecov/codecov-action@v1
5354
with:
5455
token: ${{ secrets.CODECOV_TOKEN }}
55-
test-ubuntu:
56-
name: Test (Ubuntu)
57-
runs-on: ubuntu-latest
58-
needs: test
59-
steps:
60-
- name: Checkout
61-
uses: actions/checkout@v2
62-
with:
63-
fetch-depth: 0
64-
- name: Set up Go
65-
uses: actions/setup-go@v2
66-
with:
67-
go-version: 1.15.x
68-
- name: Run tests
69-
run: |
70-
go test -v -coverprofile coverage.out -covermode atomic ./...
71-
- name: Publish coverage
72-
uses: codecov/codecov-action@v1
73-
with:
74-
token: ${{ secrets.CODECOV_TOKEN }}
7556
build:
7657
name: Build
7758
runs-on: ubuntu-latest

pkg/api/metrics/metrics_test.go

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,89 @@ package metrics_test
22

33
import (
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

Comments
 (0)