refactored completed metric test refactor for assert

This commit is contained in:
conwaychriscosmo 2019-10-24 09:40:20 -07:00
parent 8c1ffb59e3
commit c9f678e828
6 changed files with 54 additions and 115 deletions

View File

@ -20,6 +20,7 @@ import (
"testing" "testing"
"github.com/blang/semver" "github.com/blang/semver"
"github.com/stretchr/testify/assert"
apimachineryversion "k8s.io/apimachinery/pkg/version" apimachineryversion "k8s.io/apimachinery/pkg/version"
) )
@ -84,16 +85,11 @@ func TestGauge(t *testing.T) {
registry.MustRegister(c) registry.MustRegister(c)
ms, err := registry.Gather() ms, err := registry.Gather()
if len(ms) != test.expectedMetricCount { assert.Equalf(t, test.expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount)
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount) assert.Nil(t, err, "Gather failed %v", err)
}
if err != nil {
t.Fatalf("Gather failed %v", err)
}
for _, metric := range ms { for _, metric := range ms {
if metric.GetHelp() != test.expectedHelp { assert.Equalf(t, test.expectedHelp, metric.GetHelp(), "Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
t.Errorf("Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
}
} }
// let's increment the counter and verify that the metric still works // let's increment the counter and verify that the metric still works
@ -101,14 +97,11 @@ func TestGauge(t *testing.T) {
c.Set(101) c.Set(101)
expected := 101 expected := 101
ms, err = registry.Gather() ms, err = registry.Gather()
if err != nil { assert.Nil(t, err, "Gather failed %v", err)
t.Fatalf("Gather failed %v", err)
}
for _, mf := range ms { for _, mf := range ms {
for _, m := range mf.GetMetric() { for _, m := range mf.GetMetric() {
if int(m.GetGauge().GetValue()) != expected { assert.Equalf(t, expected, int(m.GetGauge().GetValue()), "Got %v, wanted %v as the count", m.GetGauge().GetValue(), expected)
t.Errorf("Got %v, wanted %v as the count", m.GetGauge().GetValue(), expected)
}
t.Logf("%v\n", m.GetGauge().GetValue()) t.Logf("%v\n", m.GetGauge().GetValue())
} }
} }
@ -180,30 +173,20 @@ func TestGaugeVec(t *testing.T) {
registry.MustRegister(c) registry.MustRegister(c)
c.WithLabelValues("1", "2").Set(1.0) c.WithLabelValues("1", "2").Set(1.0)
ms, err := registry.Gather() ms, err := registry.Gather()
assert.Equalf(t, test.expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount)
if len(ms) != test.expectedMetricCount { assert.Nil(t, err, "Gather failed %v", err)
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount)
}
if err != nil {
t.Fatalf("Gather failed %v", err)
}
for _, metric := range ms { for _, metric := range ms {
if metric.GetHelp() != test.expectedHelp { assert.Equalf(t, test.expectedHelp, metric.GetHelp(), "Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
t.Errorf("Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
}
} }
// let's increment the counter and verify that the metric still works // let's increment the counter and verify that the metric still works
c.WithLabelValues("1", "3").Set(1.0) c.WithLabelValues("1", "3").Set(1.0)
c.WithLabelValues("2", "3").Set(1.0) c.WithLabelValues("2", "3").Set(1.0)
ms, err = registry.Gather() ms, err = registry.Gather()
if err != nil { assert.Nil(t, err, "Gather failed %v", err)
t.Fatalf("Gather failed %v", err)
}
for _, mf := range ms { for _, mf := range ms {
if len(mf.GetMetric()) != 3 { assert.Equalf(t, 3, len(mf.GetMetric()), "Got %v metrics, wanted 3 as the count", len(mf.GetMetric()))
t.Errorf("Got %v metrics, wanted 2 as the count", len(mf.GetMetric()))
}
} }
}) })
} }

View File

@ -21,6 +21,7 @@ import (
"github.com/blang/semver" "github.com/blang/semver"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
apimachineryversion "k8s.io/apimachinery/pkg/version" apimachineryversion "k8s.io/apimachinery/pkg/version"
) )
@ -88,16 +89,11 @@ func TestHistogram(t *testing.T) {
registry.MustRegister(c) registry.MustRegister(c)
ms, err := registry.Gather() ms, err := registry.Gather()
if len(ms) != test.expectedMetricCount { assert.Equalf(t, test.expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount)
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount) assert.Nil(t, err, "Gather failed %v", err)
}
if err != nil {
t.Fatalf("Gather failed %v", err)
}
for _, metric := range ms { for _, metric := range ms {
if metric.GetHelp() != test.expectedHelp { assert.Equalf(t, test.expectedHelp, metric.GetHelp(), "Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
t.Errorf("Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
}
} }
// let's increment the counter and verify that the metric still works // let's increment the counter and verify that the metric still works
@ -107,14 +103,11 @@ func TestHistogram(t *testing.T) {
c.Observe(1.5) c.Observe(1.5)
expected := 4 expected := 4
ms, err = registry.Gather() ms, err = registry.Gather()
if err != nil { assert.Nil(t, err, "Gather failed %v", err)
t.Fatalf("Gather failed %v", err)
}
for _, mf := range ms { for _, mf := range ms {
for _, m := range mf.GetMetric() { for _, m := range mf.GetMetric() {
if int(m.GetHistogram().GetSampleCount()) != expected { assert.Equalf(t, expected, int(m.GetHistogram().GetSampleCount()), "Got %v, want %v as the sample count", m.GetHistogram().GetSampleCount(), expected)
t.Errorf("Got %v, want %v as the sample count", m.GetHistogram().GetSampleCount(), expected)
}
} }
} }
}) })
@ -188,16 +181,11 @@ func TestHistogramVec(t *testing.T) {
registry.MustRegister(c) registry.MustRegister(c)
c.WithLabelValues("1", "2").Observe(1.0) c.WithLabelValues("1", "2").Observe(1.0)
ms, err := registry.Gather() ms, err := registry.Gather()
assert.Equalf(t, test.expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount)
if len(ms) != test.expectedMetricCount { assert.Nil(t, err, "Gather failed %v", err)
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount)
}
if err != nil {
t.Fatalf("Gather failed %v", err)
}
for _, metric := range ms { for _, metric := range ms {
if metric.GetHelp() != test.expectedHelp { if metric.GetHelp() != test.expectedHelp {
t.Errorf("Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp) assert.Equalf(t, test.expectedHelp, metric.GetHelp(), "Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
} }
} }
@ -205,19 +193,12 @@ func TestHistogramVec(t *testing.T) {
c.WithLabelValues("1", "3").Observe(1.0) c.WithLabelValues("1", "3").Observe(1.0)
c.WithLabelValues("2", "3").Observe(1.0) c.WithLabelValues("2", "3").Observe(1.0)
ms, err = registry.Gather() ms, err = registry.Gather()
if err != nil { assert.Nil(t, err, "Gather failed %v", err)
t.Fatalf("Gather failed %v", err)
}
for _, mf := range ms { for _, mf := range ms {
if len(mf.GetMetric()) != 3 { assert.Equalf(t, 3, len(mf.GetMetric()), "Got %v metrics, wanted 3 as the count", len(mf.GetMetric()))
t.Errorf("Got %v metrics, wanted 2 as the count", len(mf.GetMetric()))
}
for _, m := range mf.GetMetric() { for _, m := range mf.GetMetric() {
if m.GetHistogram().GetSampleCount() != 1 { assert.Equalf(t, uint64(1), m.GetHistogram().GetSampleCount(), "Got %v metrics, expected histogram sample count to equal 1", m.GetHistogram().GetSampleCount())
t.Errorf(
"Got %v metrics, expected histogram sample count to equal 1",
m.GetHistogram().GetSampleCount())
}
} }
} }
}) })

View File

@ -18,6 +18,8 @@ package metrics
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestDefaultStabilityLevel(t *testing.T) { func TestDefaultStabilityLevel(t *testing.T) {
@ -53,9 +55,7 @@ func TestDefaultStabilityLevel(t *testing.T) {
var stability = tc.inputValue var stability = tc.inputValue
stability.setDefaults() stability.setDefaults()
if stability != tc.expectValue { assert.Equalf(t, tc.expectValue, stability, "Got %s, expected: %v ", stability, tc.expectValue)
t.Errorf("Got %s, expected: %v ", stability, tc.expectValue)
}
}) })
} }
} }

View File

@ -210,9 +210,8 @@ func TestShowHiddenMetric(t *testing.T) {
registry.MustRegister(alphaHiddenCounter) registry.MustRegister(alphaHiddenCounter)
ms, err := registry.Gather() ms, err := registry.Gather()
if len(ms) != expectedMetricCount { assert.Equalf(t, expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), expectedMetricCount)
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), expectedMetricCount)
}
showHidden.Store(true) showHidden.Store(true)
defer showHidden.Store(false) defer showHidden.Store(false)
registry.MustRegister(NewCounter( registry.MustRegister(NewCounter(
@ -228,11 +227,7 @@ func TestShowHiddenMetric(t *testing.T) {
expectedMetricCount = 1 expectedMetricCount = 1
ms, err = registry.Gather() ms, err = registry.Gather()
if len(ms) != expectedMetricCount { assert.Equalf(t, expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), expectedMetricCount)
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), expectedMetricCount) assert.Nil(t, err, "Gather failed %v", err)
}
if err != nil {
t.Fatalf("Gather failed %v", err)
}
} }

View File

@ -20,6 +20,7 @@ import (
"testing" "testing"
"github.com/blang/semver" "github.com/blang/semver"
"github.com/stretchr/testify/assert"
apimachineryversion "k8s.io/apimachinery/pkg/version" apimachineryversion "k8s.io/apimachinery/pkg/version"
) )
@ -86,16 +87,11 @@ func TestSummary(t *testing.T) {
registry.MustRegister(c) registry.MustRegister(c)
ms, err := registry.Gather() ms, err := registry.Gather()
if len(ms) != test.expectedMetricCount { assert.Equalf(t, test.expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount)
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount) assert.Nil(t, err, "Gather failed %v", err)
}
if err != nil {
t.Fatalf("Gather failed %v", err)
}
for _, metric := range ms { for _, metric := range ms {
if metric.GetHelp() != test.expectedHelp { assert.Equalf(t, test.expectedHelp, metric.GetHelp(), "Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
t.Errorf("Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
}
} }
// let's increment the counter and verify that the metric still works // let's increment the counter and verify that the metric still works
@ -105,14 +101,11 @@ func TestSummary(t *testing.T) {
c.Observe(1.5) c.Observe(1.5)
expected := 4 expected := 4
ms, err = registry.Gather() ms, err = registry.Gather()
if err != nil { assert.Nil(t, err, "Gather failed %v", err)
t.Fatalf("Gather failed %v", err)
}
for _, mf := range ms { for _, mf := range ms {
for _, m := range mf.GetMetric() { for _, m := range mf.GetMetric() {
if int(m.GetSummary().GetSampleCount()) != expected { assert.Equalf(t, expected, int(m.GetSummary().GetSampleCount()), "Got %v, want %v as the sample count", m.GetHistogram().GetSampleCount(), expected)
t.Errorf("Got %v, want %v as the sample count", m.GetHistogram().GetSampleCount(), expected)
}
} }
} }
}) })
@ -183,36 +176,23 @@ func TestSummaryVec(t *testing.T) {
registry.MustRegister(c) registry.MustRegister(c)
c.WithLabelValues("1", "2").Observe(1.0) c.WithLabelValues("1", "2").Observe(1.0)
ms, err := registry.Gather() ms, err := registry.Gather()
assert.Equalf(t, test.expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount)
assert.Nil(t, err, "Gather failed %v", err)
if len(ms) != test.expectedMetricCount {
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount)
}
if err != nil {
t.Fatalf("Gather failed %v", err)
}
for _, metric := range ms { for _, metric := range ms {
if metric.GetHelp() != test.expectedHelp { assert.Equalf(t, test.expectedHelp, metric.GetHelp(), "Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
t.Errorf("Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
}
} }
// let's increment the counter and verify that the metric still works // let's increment the counter and verify that the metric still works
c.WithLabelValues("1", "3").Observe(1.0) c.WithLabelValues("1", "3").Observe(1.0)
c.WithLabelValues("2", "3").Observe(1.0) c.WithLabelValues("2", "3").Observe(1.0)
ms, err = registry.Gather() ms, err = registry.Gather()
if err != nil { assert.Nil(t, err, "Gather failed %v", err)
t.Fatalf("Gather failed %v", err)
}
for _, mf := range ms { for _, mf := range ms {
if len(mf.GetMetric()) != 3 { assert.Equalf(t, 3, len(mf.GetMetric()), "Got %v metrics, wanted 2 as the count", len(mf.GetMetric()))
t.Errorf("Got %v metrics, wanted 2 as the count", len(mf.GetMetric()))
}
for _, m := range mf.GetMetric() { for _, m := range mf.GetMetric() {
if m.GetSummary().GetSampleCount() != 1 { assert.Equalf(t, uint64(1), m.GetSummary().GetSampleCount(), "Got %v metrics, wanted 1 as the summary sample count", m.GetSummary().GetSampleCount())
t.Errorf(
"Got %v metrics, wanted 2 as the summary sample count",
m.GetSummary().GetSampleCount())
}
} }
} }
}) })

View File

@ -19,6 +19,8 @@ package metrics
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
apimachineryversion "k8s.io/apimachinery/pkg/version" apimachineryversion "k8s.io/apimachinery/pkg/version"
) )
@ -46,9 +48,7 @@ func TestVersionParsing(t *testing.T) {
GitVersion: test.versionString, GitVersion: test.versionString,
} }
parsedV := parseVersion(version) parsedV := parseVersion(version)
if test.expectedVersion != parsedV.String() { assert.Equalf(t, test.expectedVersion, parsedV.String(), "Got %v, wanted %v", parsedV.String(), test.expectedVersion)
t.Errorf("Got %v, wanted %v", parsedV.String(), test.expectedVersion)
}
}) })
} }
} }