prometheus/storage/remote/codec_test.go
2017-10-26 11:09:14 +01:00

114 lines
2.5 KiB
Go

package remote
import (
"testing"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/storage"
)
func TestValidateLabelsAndMetricName(t *testing.T) {
tests := []struct {
input labels.Labels
expectedErr string
shouldPass bool
}{
{
input: labels.FromStrings(
"__name__", "name",
"labelName", "labelValue",
),
expectedErr: "",
shouldPass: true,
},
{
input: labels.FromStrings(
"__name__", "name",
"_labelName", "labelValue",
),
expectedErr: "",
shouldPass: true,
},
{
input: labels.FromStrings(
"__name__", "name",
"@labelName", "labelValue",
),
expectedErr: "Invalid label name: @labelName",
shouldPass: false,
},
{
input: labels.FromStrings(
"__name__", "name",
"123labelName", "labelValue",
),
expectedErr: "Invalid label name: 123labelName",
shouldPass: false,
},
{
input: labels.FromStrings(
"__name__", "name",
"", "labelValue",
),
expectedErr: "Invalid label name: ",
shouldPass: false,
},
{
input: labels.FromStrings(
"__name__", "name",
"labelName", string([]byte{0xff}),
),
expectedErr: "Invalid label value: " + string([]byte{0xff}),
shouldPass: false,
},
{
input: labels.FromStrings(
"__name__", "@invalid_name",
),
expectedErr: "Invalid metric name: @invalid_name",
shouldPass: false,
},
}
for _, test := range tests {
err := validateLabelsAndMetricName(test.input)
if test.shouldPass != (err == nil) {
if test.shouldPass {
t.Fatalf("Test should pass, got unexpected error: %v", err)
} else {
t.Fatalf("Test should fail, unexpected error, got: %v, expected: %v", err, test.expectedErr)
}
}
}
}
func TestConcreteSeriesSet(t *testing.T) {
series1 := &concreteSeries{
labels: labels.FromStrings("foo", "bar"),
samples: []*prompb.Sample{&prompb.Sample{Value: 1, Timestamp: 2}},
}
series2 := &concreteSeries{
labels: labels.FromStrings("foo", "baz"),
samples: []*prompb.Sample{&prompb.Sample{Value: 3, Timestamp: 4}},
}
c := &concreteSeriesSet{
series: []storage.Series{series1, series2},
}
if !c.Next() {
t.Fatalf("Expected Next() to be true.")
}
if c.At() != series1 {
t.Fatalf("Unexpected series returned.")
}
if !c.Next() {
t.Fatalf("Expected Next() to be true.")
}
if c.At() != series2 {
t.Fatalf("Unexpected series returned.")
}
if c.Next() {
t.Fatalf("Expected Next() to be false.")
}
}