Skip to content

Commit

Permalink
Deflake prometheus_test:TestWriteMultipleSnapshots.
Browse files Browse the repository at this point in the history
The Prometheus parser doesn't guarantee that it returns metric data in
timestamp-sorted order. So sort it in the test manually.

Before: Fails 7 out of 2048 times
After: Fails 0 out of 2048 times
PiperOrigin-RevId: 688171129
  • Loading branch information
EtiennePerot authored and gvisor-bot committed Oct 21, 2024
1 parent 17edc72 commit c90fd63
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pkg/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"math"
"sort"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -1631,14 +1632,18 @@ func TestWriteMultipleSnapshots(t *testing.T) {
snapshot1: {ExporterPrefix: "export_"},
snapshot2: {ExporterPrefix: "export_"},
})
fooIntName := "export_" + fooInt.PB.GetPrometheusName()
gotData, err := (&expfmt.TextParser{}).TextToMetricFamilies(&buf)
if err != nil {
t.Fatalf("cannot parse data written from snapshots: %v", err)
}
if len(gotData) != 1 || gotData["export_"+fooInt.PB.GetPrometheusName()] == nil {
if len(gotData) != 1 || gotData[fooIntName] == nil {
t.Fatalf("unexpected data: %v", gotData)
}
got := reflectProto(gotData["export_"+fooInt.PB.GetPrometheusName()])
sort.Slice(gotData[fooIntName].Metric, func(i, j int) bool {
return gotData[fooIntName].Metric[i].GetTimestampMs() < gotData[fooIntName].Metric[j].GetTimestampMs()
})
got := reflectProto(gotData[fooIntName])
var wantBuf bytes.Buffer
io.WriteString(&wantBuf, fmt.Sprintf(`
# HELP export_foo_int An integer about foo
Expand All @@ -1650,10 +1655,13 @@ func TestWriteMultipleSnapshots(t *testing.T) {
if err != nil {
t.Fatalf("cannot parse reference data: %v", err)
}
if len(wantData) != 1 || wantData["export_"+fooInt.PB.GetPrometheusName()] == nil {
if len(wantData) != 1 || wantData[fooIntName] == nil {
t.Fatalf("unexpected reference data: %v", gotData)
}
want := reflectProto(wantData["export_"+fooInt.PB.GetPrometheusName()])
sort.Slice(wantData[fooIntName].Metric, func(i, j int) bool {
return wantData[fooIntName].Metric[i].GetTimestampMs() < wantData[fooIntName].Metric[j].GetTimestampMs()
})
want := reflectProto(wantData[fooIntName])
if diff := cmp.Diff(want, got, protocmp.Transform()); diff != "" {
multiLineFormatter := &prototext.MarshalOptions{Multiline: true, Indent: " ", EmitUnknown: true}
wantText, err := multiLineFormatter.Marshal(want)
Expand Down

0 comments on commit c90fd63

Please sign in to comment.