Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support UTF-8 label matchers: Add test for PromQL braces when parsing lists of matchers #3507

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions test/cli/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,10 @@ func (am *Alertmanager) addAlertCommand(alert *TestAlert) ([]byte, error) {
}

// QueryAlerts uses the amtool cli to query alerts.
func (am *Alertmanager) QueryAlerts() ([]TestAlert, error) {
func (am *Alertmanager) QueryAlerts(match ...string) ([]TestAlert, error) {
amURLFlag := "--alertmanager.url=" + am.getURL("/")
cmd := exec.Command(amtool, amURLFlag, "alert", "query")
args := append([]string{amURLFlag, "alert", "query"}, match...)
cmd := exec.Command(amtool, args...)
output, err := cmd.CombinedOutput()
if err != nil {
return nil, err
Expand Down Expand Up @@ -558,9 +559,9 @@ func (am *Alertmanager) addSilenceCommand(sil *TestSilence) ([]byte, error) {
}

// QuerySilence queries the current silences using the 'amtool silence query' command.
func (am *Alertmanager) QuerySilence() ([]TestSilence, error) {
func (am *Alertmanager) QuerySilence(match ...string) ([]TestSilence, error) {
amURLFlag := "--alertmanager.url=" + am.getURL("/")
args := []string{amURLFlag, "silence", "query"}
args := append([]string{amURLFlag, "silence", "query"}, match...)
cmd := exec.Command(amtool, args...)
out, err := cmd.CombinedOutput()
if err != nil {
Expand Down
56 changes: 40 additions & 16 deletions test/cli/acceptance/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/prometheus/alertmanager/api/v2/models"
. "github.com/prometheus/alertmanager/test/cli"
)

Expand Down Expand Up @@ -114,13 +115,26 @@ receivers:
am.AddAlerts(alert1, alert2)

alerts, err := am.QueryAlerts()
if err != nil {
t.Fatal("Failed to query alerts", err)
}
expectedAlerts := 2
if len(alerts) != expectedAlerts {
t.Fatalf("Incorrect number of alerts, expected %v, got %v", expectedAlerts, len(alerts))
}
require.NoError(t, err)
require.Len(t, alerts, 2)

// Get the first alert using the alertname heuristic
alerts, err = am.QueryAlerts("test1")
require.NoError(t, err)
require.Len(t, alerts, 1)

// QueryAlerts uses the simple output option, which means just the alertname
// label is printed. We can assert that querying works as expected as we know
// there are two alerts called "test1" and "test2".
expectedLabels := models.LabelSet{"name": "test1"}
require.True(t, alerts[0].HasLabels(expectedLabels))

// Get the second alert
alerts, err = am.QueryAlerts("alertname=test2")
require.NoError(t, err)
require.Len(t, alerts, 1)
expectedLabels = models.LabelSet{"name": "test2"}
require.True(t, alerts[0].HasLabels(expectedLabels))
}

func TestQuerySilence(t *testing.T) {
Expand Down Expand Up @@ -153,18 +167,28 @@ receivers:

am := amc.Members()[0]

silence1 := Silence(0, 4).Match("alertname=test1", "severity=warn").Comment("test1")
silence2 := Silence(0, 4).Match("foo").Comment("test foo")
silence1 := Silence(0, 4).Match("test1", "severity=warn").Comment("test1")
silence2 := Silence(0, 4).Match("alertname=test2", "severity=warn").Comment("test2")
silence3 := Silence(0, 4).Match("{alertname=test3}", "severity=warn").Comment("test3")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gotjosh here I create a silence with the following labels:

alertname="{alertname=test3}"
severity=warn

We will be able to then see this behaviour break when we use the new fallback parser as without the additional check we talked about in #3453 (comment), it will create a silence with the following labels instead:

alertname=test3
severity=warn


am.SetSilence(0, silence1)
am.SetSilence(0, silence2)
am.SetSilence(0, silence3)

// Get all silences
sils, err := am.QuerySilence()
if err != nil {
t.Error("Failed to query silences: ", err)
}
expectedSils := 2
if len(sils) != expectedSils {
t.Errorf("Incorrect number of silences queried, expected: %v, actual: %v", expectedSils, len(sils))
}
require.NoError(t, err)
require.Len(t, sils, 3)
expected1 := []string{"alertname=\"test1\"", "severity=\"warn\""}
require.Equal(t, expected1, sils[0].GetMatches())
expected2 := []string{"alertname=\"test2\"", "severity=\"warn\""}
require.Equal(t, expected2, sils[1].GetMatches())
expected3 := []string{"alertname=\"{alertname=test3}\"", "severity=\"warn\""}
require.Equal(t, expected3, sils[2].GetMatches())

// Get the first silence using the alertname heuristic
sils, err = am.QuerySilence("test1")
require.NoError(t, err)
require.Len(t, sils, 1)
require.Equal(t, expected1, sils[0].GetMatches())
}
17 changes: 16 additions & 1 deletion test/cli/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ func (s *TestSilence) Match(v ...string) *TestSilence {
return s
}

// MatchRE adds a new regex matcher to the silence
// GetMatches returns the plain matchers for the silence.
func (s TestSilence) GetMatches() []string {
return s.match
}

// MatchRE adds a new regex matcher to the silence.
func (s *TestSilence) MatchRE(v ...string) *TestSilence {
if len(v)%2 == 1 {
panic("bad key/values")
Expand All @@ -85,6 +90,11 @@ func (s *TestSilence) MatchRE(v ...string) *TestSilence {
return s
}

// GetMatchREs returns the regex matchers for the silence.
func (s *TestSilence) GetMatchREs() []string {
return s.matchRE
}

// Comment sets the comment to the silence.
func (s *TestSilence) Comment(c string) *TestSilence {
s.comment = c
Expand Down Expand Up @@ -185,6 +195,11 @@ func (a *TestAlert) Active(tss ...float64) *TestAlert {
return a
}

// HasLabels returns true if the two label sets are equivalent, otherwise false.
func (a *TestAlert) HasLabels(labels models.LabelSet) bool {
return reflect.DeepEqual(a.labels, labels)
}

func equalAlerts(a, b *models.GettableAlert, opts *AcceptanceOpts) bool {
if !reflect.DeepEqual(a.Labels, b.Labels) {
return false
Expand Down