Skip to content

Commit

Permalink
tlsprom: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abursavich committed Mar 24, 2020
1 parent e345759 commit e025e4d
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion tlsprom/tlsprom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,40 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"sort"
"testing"
"time"

"github.com/abursavich/dynamictls/internal/tlstest"
"github.com/prometheus/client_golang/prometheus"
)

func TestCollector(t *testing.T) {
m, err := NewMetrics()
check(t, "Failed to create metrics", err)
reg := prometheus.NewRegistry()
check(t, "Failed to register metrics", reg.Register(m))
fams, err := reg.Gather()
check(t, "Failed to gather metrics", err)
names := []string{
updateErrorName,
verifyErrorName,
expirationName,
}
if want, got := len(names), len(fams); want != got {
t.Fatalf("Expected %v metrics; got: %d", want, got)
}
sort.Strings(names)
sort.Slice(fams, func(i, k int) bool {
return fams[i].GetName() < fams[k].GetName()
})
for i, want := range names {
if got := fams[i].GetName(); want != got {
t.Fatalf("Unexpected metric name; want: %v; got: %v", want, got)
}
}
}

func TestMetricNames(t *testing.T) {
tests := []struct {
desc string
Expand Down Expand Up @@ -52,6 +79,12 @@ func TestMetricNames(t *testing.T) {
namespace: "foo",
subsystem: "bar",
},
{
desc: "foo_server",
options: []Option{WithNamespace("foo"), WithServer()},
namespace: "foo",
subsystem: "server",
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
Expand Down Expand Up @@ -251,7 +284,7 @@ func TestExpiration(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
m, err := NewMetrics()
m, err := NewMetrics(WithErrorLogger(t))
check(t, "Failed to create metrics", err)
m.Update(tt.config, nil)
got := readGauge(t, m.expiration)
Expand Down

0 comments on commit e025e4d

Please sign in to comment.