mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
refactored completed metric test refactor for assert
This commit is contained in:
parent
8c1ffb59e3
commit
c9f678e828
@ -20,6 +20,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/blang/semver"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||
)
|
||||
@ -84,16 +85,11 @@ func TestGauge(t *testing.T) {
|
||||
registry.MustRegister(c)
|
||||
|
||||
ms, err := registry.Gather()
|
||||
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)
|
||||
}
|
||||
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)
|
||||
|
||||
for _, metric := range ms {
|
||||
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)
|
||||
}
|
||||
|
||||
// let's increment the counter and verify that the metric still works
|
||||
@ -101,14 +97,11 @@ func TestGauge(t *testing.T) {
|
||||
c.Set(101)
|
||||
expected := 101
|
||||
ms, err = registry.Gather()
|
||||
if err != nil {
|
||||
t.Fatalf("Gather failed %v", err)
|
||||
}
|
||||
assert.Nil(t, err, "Gather failed %v", err)
|
||||
|
||||
for _, mf := range ms {
|
||||
for _, m := range mf.GetMetric() {
|
||||
if int(m.GetGauge().GetValue()) != expected {
|
||||
t.Errorf("Got %v, wanted %v as the count", m.GetGauge().GetValue(), expected)
|
||||
}
|
||||
assert.Equalf(t, expected, int(m.GetGauge().GetValue()), "Got %v, wanted %v as the count", m.GetGauge().GetValue(), expected)
|
||||
t.Logf("%v\n", m.GetGauge().GetValue())
|
||||
}
|
||||
}
|
||||
@ -180,30 +173,20 @@ func TestGaugeVec(t *testing.T) {
|
||||
registry.MustRegister(c)
|
||||
c.WithLabelValues("1", "2").Set(1.0)
|
||||
ms, err := registry.Gather()
|
||||
|
||||
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)
|
||||
}
|
||||
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)
|
||||
for _, metric := range ms {
|
||||
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)
|
||||
}
|
||||
|
||||
// let's increment the counter and verify that the metric still works
|
||||
c.WithLabelValues("1", "3").Set(1.0)
|
||||
c.WithLabelValues("2", "3").Set(1.0)
|
||||
ms, err = registry.Gather()
|
||||
if err != nil {
|
||||
t.Fatalf("Gather failed %v", err)
|
||||
}
|
||||
assert.Nil(t, err, "Gather failed %v", err)
|
||||
|
||||
for _, mf := range ms {
|
||||
if len(mf.GetMetric()) != 3 {
|
||||
t.Errorf("Got %v metrics, wanted 2 as the count", len(mf.GetMetric()))
|
||||
}
|
||||
assert.Equalf(t, 3, len(mf.GetMetric()), "Got %v metrics, wanted 3 as the count", len(mf.GetMetric()))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
|
||||
"github.com/blang/semver"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||
)
|
||||
@ -88,16 +89,11 @@ func TestHistogram(t *testing.T) {
|
||||
registry.MustRegister(c)
|
||||
|
||||
ms, err := registry.Gather()
|
||||
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)
|
||||
}
|
||||
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)
|
||||
|
||||
for _, metric := range ms {
|
||||
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)
|
||||
}
|
||||
|
||||
// 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)
|
||||
expected := 4
|
||||
ms, err = registry.Gather()
|
||||
if err != nil {
|
||||
t.Fatalf("Gather failed %v", err)
|
||||
}
|
||||
assert.Nil(t, err, "Gather failed %v", err)
|
||||
|
||||
for _, mf := range ms {
|
||||
for _, m := range mf.GetMetric() {
|
||||
if int(m.GetHistogram().GetSampleCount()) != expected {
|
||||
t.Errorf("Got %v, want %v as the sample count", m.GetHistogram().GetSampleCount(), expected)
|
||||
}
|
||||
assert.Equalf(t, expected, int(m.GetHistogram().GetSampleCount()), "Got %v, want %v as the sample count", m.GetHistogram().GetSampleCount(), expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -188,16 +181,11 @@ func TestHistogramVec(t *testing.T) {
|
||||
registry.MustRegister(c)
|
||||
c.WithLabelValues("1", "2").Observe(1.0)
|
||||
ms, err := registry.Gather()
|
||||
|
||||
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)
|
||||
}
|
||||
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)
|
||||
for _, metric := range ms {
|
||||
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("2", "3").Observe(1.0)
|
||||
ms, err = registry.Gather()
|
||||
if err != nil {
|
||||
t.Fatalf("Gather failed %v", err)
|
||||
}
|
||||
assert.Nil(t, err, "Gather failed %v", err)
|
||||
|
||||
for _, mf := range ms {
|
||||
if len(mf.GetMetric()) != 3 {
|
||||
t.Errorf("Got %v metrics, wanted 2 as the count", len(mf.GetMetric()))
|
||||
}
|
||||
assert.Equalf(t, 3, len(mf.GetMetric()), "Got %v metrics, wanted 3 as the count", len(mf.GetMetric()))
|
||||
for _, m := range mf.GetMetric() {
|
||||
if m.GetHistogram().GetSampleCount() != 1 {
|
||||
t.Errorf(
|
||||
"Got %v metrics, expected histogram sample count to equal 1",
|
||||
m.GetHistogram().GetSampleCount())
|
||||
}
|
||||
assert.Equalf(t, uint64(1), m.GetHistogram().GetSampleCount(), "Got %v metrics, expected histogram sample count to equal 1", m.GetHistogram().GetSampleCount())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -18,6 +18,8 @@ package metrics
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDefaultStabilityLevel(t *testing.T) {
|
||||
@ -53,9 +55,7 @@ func TestDefaultStabilityLevel(t *testing.T) {
|
||||
var stability = tc.inputValue
|
||||
|
||||
stability.setDefaults()
|
||||
if stability != tc.expectValue {
|
||||
t.Errorf("Got %s, expected: %v ", stability, tc.expectValue)
|
||||
}
|
||||
assert.Equalf(t, tc.expectValue, stability, "Got %s, expected: %v ", stability, tc.expectValue)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -210,9 +210,8 @@ func TestShowHiddenMetric(t *testing.T) {
|
||||
registry.MustRegister(alphaHiddenCounter)
|
||||
|
||||
ms, err := registry.Gather()
|
||||
if len(ms) != expectedMetricCount {
|
||||
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), expectedMetricCount)
|
||||
}
|
||||
assert.Equalf(t, expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), expectedMetricCount)
|
||||
|
||||
showHidden.Store(true)
|
||||
defer showHidden.Store(false)
|
||||
registry.MustRegister(NewCounter(
|
||||
@ -228,11 +227,7 @@ func TestShowHiddenMetric(t *testing.T) {
|
||||
expectedMetricCount = 1
|
||||
|
||||
ms, err = registry.Gather()
|
||||
if len(ms) != expectedMetricCount {
|
||||
t.Errorf("Got %v metrics, Want: %v metrics", len(ms), expectedMetricCount)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("Gather failed %v", err)
|
||||
}
|
||||
assert.Equalf(t, expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), expectedMetricCount)
|
||||
assert.Nil(t, err, "Gather failed %v", err)
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/blang/semver"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||
)
|
||||
@ -86,16 +87,11 @@ func TestSummary(t *testing.T) {
|
||||
registry.MustRegister(c)
|
||||
|
||||
ms, err := registry.Gather()
|
||||
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)
|
||||
}
|
||||
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)
|
||||
|
||||
for _, metric := range ms {
|
||||
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)
|
||||
}
|
||||
|
||||
// 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)
|
||||
expected := 4
|
||||
ms, err = registry.Gather()
|
||||
if err != nil {
|
||||
t.Fatalf("Gather failed %v", err)
|
||||
}
|
||||
assert.Nil(t, err, "Gather failed %v", err)
|
||||
|
||||
for _, mf := range ms {
|
||||
for _, m := range mf.GetMetric() {
|
||||
if int(m.GetSummary().GetSampleCount()) != expected {
|
||||
t.Errorf("Got %v, want %v as the sample count", m.GetHistogram().GetSampleCount(), expected)
|
||||
}
|
||||
assert.Equalf(t, expected, int(m.GetSummary().GetSampleCount()), "Got %v, want %v as the sample count", m.GetHistogram().GetSampleCount(), expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -183,36 +176,23 @@ func TestSummaryVec(t *testing.T) {
|
||||
registry.MustRegister(c)
|
||||
c.WithLabelValues("1", "2").Observe(1.0)
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
|
||||
// let's increment the counter and verify that the metric still works
|
||||
c.WithLabelValues("1", "3").Observe(1.0)
|
||||
c.WithLabelValues("2", "3").Observe(1.0)
|
||||
ms, err = registry.Gather()
|
||||
if err != nil {
|
||||
t.Fatalf("Gather failed %v", err)
|
||||
}
|
||||
assert.Nil(t, err, "Gather failed %v", err)
|
||||
|
||||
for _, mf := range ms {
|
||||
if len(mf.GetMetric()) != 3 {
|
||||
t.Errorf("Got %v metrics, wanted 2 as the count", len(mf.GetMetric()))
|
||||
}
|
||||
assert.Equalf(t, 3, len(mf.GetMetric()), "Got %v metrics, wanted 2 as the count", len(mf.GetMetric()))
|
||||
for _, m := range mf.GetMetric() {
|
||||
if m.GetSummary().GetSampleCount() != 1 {
|
||||
t.Errorf(
|
||||
"Got %v metrics, wanted 2 as the summary sample count",
|
||||
m.GetSummary().GetSampleCount())
|
||||
}
|
||||
assert.Equalf(t, uint64(1), m.GetSummary().GetSampleCount(), "Got %v metrics, wanted 1 as the summary sample count", m.GetSummary().GetSampleCount())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -19,6 +19,8 @@ package metrics
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||
)
|
||||
|
||||
@ -46,9 +48,7 @@ func TestVersionParsing(t *testing.T) {
|
||||
GitVersion: test.versionString,
|
||||
}
|
||||
parsedV := parseVersion(version)
|
||||
if test.expectedVersion != parsedV.String() {
|
||||
t.Errorf("Got %v, wanted %v", parsedV.String(), test.expectedVersion)
|
||||
}
|
||||
assert.Equalf(t, test.expectedVersion, parsedV.String(), "Got %v, wanted %v", parsedV.String(), test.expectedVersion)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user