mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Merge pull request #84117 from conwaychriscosmo/refactor-counter_test
refactored counter_test to use assert statements and renamed variable…
This commit is contained in:
commit
e0cc347f6e
@ -22,6 +22,7 @@ import (
|
|||||||
|
|
||||||
"github.com/blang/semver"
|
"github.com/blang/semver"
|
||||||
"github.com/prometheus/common/expfmt"
|
"github.com/prometheus/common/expfmt"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||||
)
|
)
|
||||||
@ -79,27 +80,19 @@ func TestCounter(t *testing.T) {
|
|||||||
Minor: "15",
|
Minor: "15",
|
||||||
GitVersion: "v1.15.0-alpha-1.12345",
|
GitVersion: "v1.15.0-alpha-1.12345",
|
||||||
})
|
})
|
||||||
|
// c is a pointer to a Counter
|
||||||
c := NewCounter(test.CounterOpts)
|
c := NewCounter(test.CounterOpts)
|
||||||
registry.MustRegister(c)
|
registry.MustRegister(c)
|
||||||
|
// mfs is a pointer to a dto.MetricFamily slice
|
||||||
ms, err := registry.Gather()
|
mfs, err := registry.Gather()
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
enc := expfmt.NewEncoder(&buf, "text/plain; version=0.0.4; charset=utf-8")
|
enc := expfmt.NewEncoder(&buf, "text/plain; version=0.0.4; charset=utf-8")
|
||||||
|
assert.Equalf(t, test.expectedMetricCount, len(mfs), "Got %v metrics, Want: %v metrics", len(mfs), 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)
|
for _, metric := range mfs {
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Gather failed %v", err)
|
|
||||||
}
|
|
||||||
for _, metric := range ms {
|
|
||||||
err := enc.Encode(metric)
|
err := enc.Encode(metric)
|
||||||
if err != nil {
|
assert.Nil(t, err, "Unexpected err %v in encoding the metric", err)
|
||||||
t.Fatalf("Unexpected err %v in encoding the metric", err)
|
assert.Equalf(t, test.expectedHelp, metric.GetHelp(), "Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
|
||||||
}
|
|
||||||
if metric.GetHelp() != test.expectedHelp {
|
|
||||||
t.Errorf("Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// increment the counter N number of times and verify that the metric retains the count correctly
|
// increment the counter N number of times and verify that the metric retains the count correctly
|
||||||
@ -107,15 +100,13 @@ func TestCounter(t *testing.T) {
|
|||||||
for i := 0; i < numberOfTimesToIncrement; i++ {
|
for i := 0; i < numberOfTimesToIncrement; i++ {
|
||||||
c.Inc()
|
c.Inc()
|
||||||
}
|
}
|
||||||
ms, err = registry.Gather()
|
mfs, err = registry.Gather()
|
||||||
if err != nil {
|
assert.Nil(t, err, "Gather failed %v", err)
|
||||||
t.Fatalf("Gather failed %v", err)
|
|
||||||
}
|
for _, mf := range mfs {
|
||||||
for _, mf := range ms {
|
mfMetric := mf.GetMetric()
|
||||||
for _, m := range mf.GetMetric() {
|
for _, m := range mfMetric {
|
||||||
if int(m.GetCounter().GetValue()) != numberOfTimesToIncrement {
|
assert.Equalf(t, numberOfTimesToIncrement, int(m.GetCounter().GetValue()), "Got %v, wanted %v as the count", m.GetCounter().GetValue(), numberOfTimesToIncrement)
|
||||||
t.Errorf("Got %v, wanted %v as the count", m.GetCounter().GetValue(), numberOfTimesToIncrement)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -195,35 +186,24 @@ func TestCounterVec(t *testing.T) {
|
|||||||
registry.MustRegister(c)
|
registry.MustRegister(c)
|
||||||
c.WithLabelValues("1", "2").Inc()
|
c.WithLabelValues("1", "2").Inc()
|
||||||
mfs, err := registry.Gather()
|
mfs, err := registry.Gather()
|
||||||
if len(mfs) != test.expectedMetricFamilyCount {
|
assert.Equalf(t, test.expectedMetricFamilyCount, len(mfs), "Got %v metric families, Want: %v metric families", len(mfs), test.expectedMetricFamilyCount)
|
||||||
t.Errorf("Got %v metric families, Want: %v metric families", len(mfs), test.expectedMetricFamilyCount)
|
assert.Nil(t, err, "Gather failed %v", err)
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Gather failed %v", err)
|
|
||||||
}
|
|
||||||
// this no-opts here when there are no metric families (i.e. when the metric is hidden)
|
// this no-opts here when there are no metric families (i.e. when the metric is hidden)
|
||||||
for _, mf := range mfs {
|
for _, mf := range mfs {
|
||||||
if len(mf.GetMetric()) != 1 {
|
assert.Equalf(t, 1, len(mf.GetMetric()), "Got %v metrics, wanted 1 as the count", len(mf.GetMetric()))
|
||||||
t.Errorf("Got %v metrics, wanted 1 as the count", len(mf.GetMetric()))
|
assert.Equalf(t, test.expectedHelp, mf.GetHelp(), "Got %s as help message, want %s", mf.GetHelp(), test.expectedHelp)
|
||||||
}
|
|
||||||
if mf.GetHelp() != test.expectedHelp {
|
|
||||||
t.Errorf("Got %s as help message, want %s", mf.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").Inc()
|
c.WithLabelValues("1", "3").Inc()
|
||||||
c.WithLabelValues("2", "3").Inc()
|
c.WithLabelValues("2", "3").Inc()
|
||||||
mfs, err = registry.Gather()
|
mfs, err = registry.Gather()
|
||||||
if err != nil {
|
assert.Nil(t, err, "Gather failed %v", err)
|
||||||
t.Fatalf("Gather failed %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// this no-opts here when there are no metric families (i.e. when the metric is hidden)
|
// this no-opts here when there are no metric families (i.e. when the metric is hidden)
|
||||||
for _, mf := range mfs {
|
for _, mf := range mfs {
|
||||||
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 3 as the count", len(mf.GetMetric()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user