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
pull/976/head
nils måsén 4 years ago committed by GitHub
parent 61b715abec
commit dec6f84a70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -35,6 +35,7 @@ jobs:
platform: platform:
- macos-latest - macos-latest
- windows-latest - windows-latest
- ubuntu-latest
runs-on: ${{ matrix.platform }} runs-on: ${{ matrix.platform }}
steps: steps:
- name: Checkout - name: Checkout
@ -52,26 +53,6 @@ jobs:
uses: codecov/codecov-action@v1 uses: codecov/codecov-action@v1
with: with:
token: ${{ secrets.CODECOV_TOKEN }} token: ${{ secrets.CODECOV_TOKEN }}
test-ubuntu:
name: Test (Ubuntu)
runs-on: ubuntu-latest
needs: test
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15.x
- name: Run tests
run: |
go test -v -coverprofile coverage.out -covermode atomic ./...
- name: Publish coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
build: build:
name: Build name: Build
runs-on: ubuntu-latest runs-on: ubuntu-latest

@ -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"))
}) })
}) })

Loading…
Cancel
Save