105 lines
2.4 KiB
Go
105 lines
2.4 KiB
Go
// Copyright 2014 Prometheus Team
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package extraction
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/prometheus/client_golang/model"
|
|
)
|
|
|
|
var (
|
|
ts = model.Now()
|
|
in = `
|
|
# Only a quite simple scenario with two metric families.
|
|
# More complicated tests of the parser itself can be found in the text package.
|
|
# TYPE mf2 counter
|
|
mf2 3
|
|
mf1{label="value1"} -3.14 123456
|
|
mf1{label="value2"} 42
|
|
mf2 4
|
|
`
|
|
out = map[model.LabelValue]*Result{
|
|
"mf1": {
|
|
Samples: model.Samples{
|
|
&model.Sample{
|
|
Metric: model.Metric{model.MetricNameLabel: "mf1", "label": "value1"},
|
|
Value: -3.14,
|
|
Timestamp: 123,
|
|
},
|
|
&model.Sample{
|
|
Metric: model.Metric{model.MetricNameLabel: "mf1", "label": "value2"},
|
|
Value: 42,
|
|
Timestamp: ts,
|
|
},
|
|
},
|
|
},
|
|
"mf2": {
|
|
Samples: model.Samples{
|
|
&model.Sample{
|
|
Metric: model.Metric{model.MetricNameLabel: "mf2"},
|
|
Value: 3,
|
|
Timestamp: ts,
|
|
},
|
|
&model.Sample{
|
|
Metric: model.Metric{model.MetricNameLabel: "mf2"},
|
|
Value: 4,
|
|
Timestamp: ts,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
)
|
|
|
|
type testIngester struct {
|
|
results []*Result
|
|
}
|
|
|
|
func (i *testIngester) Ingest(r *Result) error {
|
|
i.results = append(i.results, r)
|
|
return nil
|
|
}
|
|
|
|
func TestTextProcessor(t *testing.T) {
|
|
var ingester testIngester
|
|
i := strings.NewReader(in)
|
|
o := &ProcessOptions{
|
|
Timestamp: ts,
|
|
}
|
|
|
|
err := Processor004.ProcessSingle(i, &ingester, o)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if expected, got := len(out), len(ingester.results); expected != got {
|
|
t.Fatalf("Expected length %d, got %d", expected, got)
|
|
}
|
|
for _, r := range ingester.results {
|
|
expected, ok := out[r.Samples[0].Metric[model.MetricNameLabel]]
|
|
if !ok {
|
|
t.Fatalf(
|
|
"Unexpected metric name %q",
|
|
r.Samples[0].Metric[model.MetricNameLabel],
|
|
)
|
|
}
|
|
sort.Sort(expected.Samples)
|
|
sort.Sort(r.Samples)
|
|
if !expected.equal(r) {
|
|
t.Errorf("expected %s, got %s", expected, r)
|
|
}
|
|
}
|
|
}
|