2019-02-09 09:17:52 +00:00
|
|
|
// Copyright 2019 The Prometheus Authors
|
|
|
|
// 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 labels
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-12-27 09:32:19 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2019-02-09 09:17:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLabels_MatchLabels(t *testing.T) {
|
|
|
|
labels := Labels{
|
|
|
|
{
|
|
|
|
Name: "__name__",
|
|
|
|
Value: "ALERTS",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alertname",
|
|
|
|
Value: "HTTPRequestRateLow",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alertstate",
|
|
|
|
Value: "pending",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "instance",
|
|
|
|
Value: "0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "job",
|
|
|
|
Value: "app-server",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "severity",
|
|
|
|
Value: "critical",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:32:19 +00:00
|
|
|
tests := []struct {
|
|
|
|
providedNames []string
|
|
|
|
on bool
|
|
|
|
expected Labels
|
|
|
|
}{
|
|
|
|
// on = true, explicitly including metric name in matching.
|
2019-02-09 09:17:52 +00:00
|
|
|
{
|
2019-12-27 09:32:19 +00:00
|
|
|
providedNames: []string{
|
|
|
|
"__name__",
|
|
|
|
"alertname",
|
|
|
|
"alertstate",
|
|
|
|
"instance",
|
|
|
|
},
|
|
|
|
on: true,
|
|
|
|
expected: Labels{
|
|
|
|
{
|
|
|
|
Name: "__name__",
|
|
|
|
Value: "ALERTS",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alertname",
|
|
|
|
Value: "HTTPRequestRateLow",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alertstate",
|
|
|
|
Value: "pending",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "instance",
|
|
|
|
Value: "0",
|
|
|
|
},
|
|
|
|
},
|
2019-02-09 09:17:52 +00:00
|
|
|
},
|
2019-12-27 09:32:19 +00:00
|
|
|
// on = false, explicitly excluding metric name from matching.
|
2019-02-09 09:17:52 +00:00
|
|
|
{
|
2019-12-27 09:32:19 +00:00
|
|
|
providedNames: []string{
|
|
|
|
"__name__",
|
|
|
|
"alertname",
|
|
|
|
"alertstate",
|
|
|
|
"instance",
|
|
|
|
},
|
|
|
|
on: false,
|
|
|
|
expected: Labels{
|
|
|
|
{
|
|
|
|
Name: "job",
|
|
|
|
Value: "app-server",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "severity",
|
|
|
|
Value: "critical",
|
|
|
|
},
|
|
|
|
},
|
2019-02-09 09:17:52 +00:00
|
|
|
},
|
2019-12-27 09:32:19 +00:00
|
|
|
// on = true, explicitly excluding metric name from matching.
|
2019-02-09 09:17:52 +00:00
|
|
|
{
|
2019-12-27 09:32:19 +00:00
|
|
|
providedNames: []string{
|
|
|
|
"alertname",
|
|
|
|
"alertstate",
|
|
|
|
"instance",
|
|
|
|
},
|
|
|
|
on: true,
|
|
|
|
expected: Labels{
|
|
|
|
{
|
|
|
|
Name: "alertname",
|
|
|
|
Value: "HTTPRequestRateLow",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alertstate",
|
|
|
|
Value: "pending",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "instance",
|
|
|
|
Value: "0",
|
|
|
|
},
|
|
|
|
},
|
2019-02-09 09:17:52 +00:00
|
|
|
},
|
2019-12-27 09:32:19 +00:00
|
|
|
// on = false, implicitly excluding metric name from matching.
|
2019-02-09 09:17:52 +00:00
|
|
|
{
|
2019-12-27 09:32:19 +00:00
|
|
|
providedNames: []string{
|
|
|
|
"alertname",
|
|
|
|
"alertstate",
|
|
|
|
"instance",
|
|
|
|
},
|
|
|
|
on: false,
|
|
|
|
expected: Labels{
|
|
|
|
{
|
|
|
|
Name: "job",
|
|
|
|
Value: "app-server",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "severity",
|
|
|
|
Value: "critical",
|
|
|
|
},
|
|
|
|
},
|
2019-02-09 09:17:52 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:32:19 +00:00
|
|
|
for i, test := range tests {
|
|
|
|
got := labels.MatchLabels(test.on, test.providedNames...)
|
|
|
|
testutil.Equals(t, test.expected, got, "unexpected labelset for test case %d", i)
|
2019-02-09 09:17:52 +00:00
|
|
|
}
|
|
|
|
}
|