|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 2 | + |
| 3 | +package mimirpb |
| 4 | + |
| 5 | +import ( |
| 6 | + "reflect" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + rw2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "github.com/xlab/treeprint" |
| 13 | + |
| 14 | + "github.com/grafana/mimir/pkg/util/test" |
| 15 | +) |
| 16 | + |
| 17 | +// Tests related to Prometheus Remote Write v2 (RW2) compatibility. |
| 18 | + |
| 19 | +func TestRW2TypesCompatible(t *testing.T) { |
| 20 | + expectedType := reflect.TypeOf(rw2.Request{}) |
| 21 | + actualType := reflect.TypeOf(WriteRequestRW2{}) |
| 22 | + |
| 23 | + expectedTree := treeprint.NewWithRoot("<root>") |
| 24 | + // We ignore the XXX_ fields because RW2 in Prometheus has them, |
| 25 | + // but we don't. Which also means that the offsets would be different. |
| 26 | + // But we are not going to cast between the two types, so offsets |
| 27 | + // don't matter. |
| 28 | + test.AddTypeToTree(expectedType, expectedTree, false, true, true, false) |
| 29 | + |
| 30 | + actualTree := treeprint.NewWithRoot("<root>") |
| 31 | + test.AddTypeToTree(actualType, actualTree, false, true, true, false) |
| 32 | + |
| 33 | + // mimirpb.Sample fields order MUST match promql.FPoint so that we can |
| 34 | + // cast types between them. However this makes test.RequireSameShape |
| 35 | + // fail because the order is different. |
| 36 | + // So we need to reverse the order of the fields in the tree. |
| 37 | + // Also the name of the Timestamp field is slightly different in the |
| 38 | + // two types. |
| 39 | + var firstValue, secondValue string |
| 40 | + rootNode, _ := actualTree.(*treeprint.Node) |
| 41 | + firstValue, _ = rootNode.Nodes[1].Nodes[1].Nodes[0].Value.(string) |
| 42 | + secondValue, _ = rootNode.Nodes[1].Nodes[1].Nodes[1].Value.(string) |
| 43 | + rootNode.Nodes[1].Nodes[1].Nodes[0].Value = secondValue |
| 44 | + rootNode.Nodes[1].Nodes[1].Nodes[1].Value = strings.ReplaceAll(firstValue, "TimestampMs", "Timestamp") |
| 45 | + |
| 46 | + require.Equal(t, expectedTree.String(), actualTree.String(), "Proto types are not compatible") |
| 47 | +} |
0 commit comments