2023-10-19 11:00:01 +00:00
|
|
|
// Copyright 2023 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 compat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-kit/log"
|
2023-11-24 11:26:39 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2023-10-19 11:00:01 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/pkg/labels"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFallbackMatcherParser(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
expected *labels.Matcher
|
|
|
|
err string
|
|
|
|
}{{
|
|
|
|
name: "is accepted in both",
|
|
|
|
input: "foo=bar",
|
|
|
|
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
|
|
|
|
}, {
|
|
|
|
name: "is accepted in new parser but not old",
|
|
|
|
input: "foo🙂=bar",
|
|
|
|
expected: mustNewMatcher(t, labels.MatchEqual, "foo🙂", "bar"),
|
|
|
|
}, {
|
|
|
|
name: "is accepted in old parser but not new",
|
2023-10-30 13:56:54 +00:00
|
|
|
input: "foo=!bar\\n",
|
|
|
|
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "!bar\n"),
|
2023-10-19 11:00:01 +00:00
|
|
|
}, {
|
|
|
|
name: "is accepted in neither",
|
|
|
|
input: "foo!bar",
|
|
|
|
err: "bad matcher format: foo!bar",
|
|
|
|
}}
|
2024-01-04 17:42:58 +00:00
|
|
|
f := FallbackMatcherParser(log.NewNopLogger())
|
2023-10-19 11:00:01 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
matcher, err := f(test.input)
|
|
|
|
if test.err != "" {
|
|
|
|
require.EqualError(t, err, test.err)
|
|
|
|
} else {
|
2023-12-10 08:33:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-10-19 11:00:01 +00:00
|
|
|
require.EqualValues(t, test.expected, matcher)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFallbackMatchersParser(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
expected labels.Matchers
|
|
|
|
err string
|
|
|
|
}{{
|
|
|
|
name: "is accepted in both",
|
|
|
|
input: "{foo=bar,bar=baz}",
|
|
|
|
expected: labels.Matchers{
|
|
|
|
mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
|
|
|
|
mustNewMatcher(t, labels.MatchEqual, "bar", "baz"),
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "is accepted in new parser but not old",
|
|
|
|
input: "{foo🙂=bar,bar=baz🙂}",
|
|
|
|
expected: labels.Matchers{
|
|
|
|
mustNewMatcher(t, labels.MatchEqual, "foo🙂", "bar"),
|
|
|
|
mustNewMatcher(t, labels.MatchEqual, "bar", "baz🙂"),
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "is accepted in old parser but not new",
|
2023-10-30 13:56:54 +00:00
|
|
|
input: "{foo=!bar,bar=$baz\\n}",
|
2023-10-19 11:00:01 +00:00
|
|
|
expected: labels.Matchers{
|
|
|
|
mustNewMatcher(t, labels.MatchEqual, "foo", "!bar"),
|
2023-10-30 13:56:54 +00:00
|
|
|
mustNewMatcher(t, labels.MatchEqual, "bar", "$baz\n"),
|
2023-10-19 11:00:01 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "is accepted in neither",
|
|
|
|
input: "{foo!bar}",
|
|
|
|
err: "bad matcher format: foo!bar",
|
|
|
|
}}
|
2024-01-04 17:42:58 +00:00
|
|
|
f := FallbackMatchersParser(log.NewNopLogger())
|
2023-10-19 11:00:01 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
matchers, err := f(test.input)
|
|
|
|
if test.err != "" {
|
|
|
|
require.EqualError(t, err, test.err)
|
|
|
|
} else {
|
2023-12-10 08:33:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-10-19 11:00:01 +00:00
|
|
|
require.EqualValues(t, test.expected, matchers)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustNewMatcher(t *testing.T, op labels.MatchType, name, value string) *labels.Matcher {
|
|
|
|
m, err := labels.NewMatcher(op, name, value)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return m
|
|
|
|
}
|
2023-11-24 11:26:39 +00:00
|
|
|
|
|
|
|
func TestIsValidClassicLabelName(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input model.LabelName
|
|
|
|
expected bool
|
|
|
|
}{{
|
|
|
|
name: "is accepted",
|
|
|
|
input: "foo",
|
|
|
|
expected: true,
|
|
|
|
}, {
|
|
|
|
name: "is also accepted",
|
|
|
|
input: "_foo1",
|
|
|
|
expected: true,
|
|
|
|
}, {
|
|
|
|
name: "is not accepted",
|
|
|
|
input: "0foo",
|
|
|
|
expected: false,
|
|
|
|
}, {
|
|
|
|
name: "is also not accepted",
|
|
|
|
input: "foo🙂",
|
|
|
|
expected: false,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
fn := isValidClassicLabelName(log.NewNopLogger())
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, test.expected, fn(test.input))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsValidUTF8LabelName(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input model.LabelName
|
|
|
|
expected bool
|
|
|
|
}{{
|
|
|
|
name: "is accepted",
|
|
|
|
input: "foo",
|
|
|
|
expected: true,
|
|
|
|
}, {
|
|
|
|
name: "is also accepted",
|
|
|
|
input: "_foo1",
|
|
|
|
expected: true,
|
|
|
|
}, {
|
|
|
|
name: "is accepted in UTF-8",
|
|
|
|
input: "0foo",
|
|
|
|
expected: true,
|
|
|
|
}, {
|
|
|
|
name: "is also accepted with UTF-8",
|
|
|
|
input: "foo🙂",
|
|
|
|
expected: true,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
fn := isValidUTF8LabelName(log.NewNopLogger())
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, test.expected, fn(test.input))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|