Skip to content

Commit

Permalink
(tests): Compare proto instead of texts
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
  • Loading branch information
ArthurSens committed Aug 10, 2023
1 parent e6d9092 commit 9e85129
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 54 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -7,7 +7,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0
github.com/davecgh/go-spew v1.1.1
github.com/json-iterator/go v1.1.12
github.com/prometheus/client_model v0.3.0
github.com/prometheus/client_model v0.4.0
github.com/prometheus/common v0.42.0
github.com/prometheus/procfs v0.11.1
golang.org/x/sys v0.10.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Expand Up @@ -151,8 +151,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY=
github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU=
github.com/prometheus/common v0.37.0/go.mod h1:phzohg0JFMnBEFGxTDbfu3QyL5GI8gTQJFhYO5B3mfA=
github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM=
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=
Expand Down
41 changes: 33 additions & 8 deletions prometheus/counter_test.go
Expand Up @@ -61,8 +61,15 @@ func TestCounterAdd(t *testing.T) {
m := &dto.Metric{}
counter.Write(m)

if expected, got := `label:<name:"a" value:"1" > label:<name:"b" value:"2" > counter:<value:67.42 > `, m.String(); expected != got {
t.Errorf("expected %q, got %q", expected, got)
expected := &dto.Metric{
Label: []*dto.LabelPair{
{Name: proto.String("a"), Value: proto.String("1")},
{Name: proto.String("b"), Value: proto.String("2")},
},
Counter: &dto.Counter{Value: proto.Float64(67.42)},
}
if !proto.Equal(expected, m) {
t.Errorf("expected %q, got %q", expected, m)
}
}

Expand Down Expand Up @@ -164,8 +171,14 @@ func TestCounterAddInf(t *testing.T) {
m := &dto.Metric{}
counter.Write(m)

if expected, got := `counter:<value:inf > `, m.String(); expected != got {
t.Errorf("expected %q, got %q", expected, got)
expected := &dto.Metric{
Counter: &dto.Counter{
Value: proto.Float64(math.Inf(1)),
},
}

if !proto.Equal(expected, m) {
t.Errorf("expected %q, got %q", expected, m)
}
}

Expand All @@ -188,8 +201,14 @@ func TestCounterAddLarge(t *testing.T) {
m := &dto.Metric{}
counter.Write(m)

if expected, got := fmt.Sprintf("counter:<value:%0.16e > ", large), m.String(); expected != got {
t.Errorf("expected %q, got %q", expected, got)
expected := &dto.Metric{
Counter: &dto.Counter{
Value: proto.Float64(large),
},
}

if !proto.Equal(expected, m) {
t.Errorf("expected %q, got %q", expected, m)
}
}

Expand All @@ -210,8 +229,14 @@ func TestCounterAddSmall(t *testing.T) {
m := &dto.Metric{}
counter.Write(m)

if expected, got := fmt.Sprintf("counter:<value:%0.0e > ", small), m.String(); expected != got {
t.Errorf("expected %q, got %q", expected, got)
expected := &dto.Metric{
Counter: &dto.Counter{
Value: proto.Float64(small),
},
}

if !proto.Equal(expected, m) {
t.Errorf("expected %q, got %q", expected, m)
}
}

Expand Down
15 changes: 13 additions & 2 deletions prometheus/gauge_test.go
Expand Up @@ -22,6 +22,7 @@ import (
"time"

dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
)

func listenGaugeStream(vals, result chan float64, done chan struct{}) {
Expand Down Expand Up @@ -177,8 +178,18 @@ func TestGaugeFunc(t *testing.T) {
m := &dto.Metric{}
gf.Write(m)

if expected, got := `label:<name:"a" value:"1" > label:<name:"b" value:"2" > gauge:<value:3.1415 > `, m.String(); expected != got {
t.Errorf("expected %q, got %q", expected, got)
expected := &dto.Metric{
Label: []*dto.LabelPair{
{Name: proto.String("a"), Value: proto.String("1")},
{Name: proto.String("b"), Value: proto.String("2")},
},
Gauge: &dto.Gauge{
Value: proto.Float64(3.1415),
},
}

if !proto.Equal(expected, m) {
t.Errorf("expected %q, got %q", expected, m)
}
}

Expand Down

0 comments on commit 9e85129

Please sign in to comment.