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 more acceptance tests for braces when using amtool #3523

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add tests for PromQL braces when using amtool alert
Signed-off-by: George Robinson <george.robinson@grafana.com>
  • Loading branch information
grobinson-grafana committed Sep 13, 2023
commit 628b2c302d39f4548004ab8766e0a11029c97d23
29 changes: 21 additions & 8 deletions test/cli/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,27 +457,40 @@ func Version() (string, error) {

// AddAlertsAt declares alerts that are to be added to the Alertmanager
// server at a relative point in time.
func (am *Alertmanager) AddAlertsAt(at float64, alerts ...*TestAlert) {
func (am *Alertmanager) AddAlertsAt(useHeuristic bool, at float64, alerts ...*TestAlert) {
am.t.Do(at, func() {
am.AddAlerts(alerts...)
am.AddAlerts(useHeuristic, alerts...)
gotjosh marked this conversation as resolved.
Show resolved Hide resolved
})
}

// AddAlerts declares alerts that are to be added to the Alertmanager server.
func (am *Alertmanager) AddAlerts(alerts ...*TestAlert) {
func (am *Alertmanager) AddAlerts(useHeuristic bool, alerts ...*TestAlert) {
for _, alert := range alerts {
out, err := am.addAlertCommand(alert)
out, err := am.addAlertCommand(useHeuristic, alert)
if err != nil {
am.t.Errorf("Error adding alert: %v\nOutput: %s", err, string(out))
}
}
}

func (am *Alertmanager) addAlertCommand(alert *TestAlert) ([]byte, error) {
func (am *Alertmanager) addAlertCommand(useHeuristic bool, alert *TestAlert) ([]byte, error) {
amURLFlag := "--alertmanager.url=" + am.getURL("/")
args := []string{amURLFlag, "alert", "add"}
for key, val := range alert.labels {
args = append(args, key+"="+val)
// Make a copy of the labels
labels := make(models.LabelSet, len(alert.labels))
for k, v := range alert.labels {
labels[k] = v
}
if useHeuristic {
// If alertname is present and useHeuristic is true then the command should
// be `amtool alert add foo ...` and not `amtool alert add alertname=foo ...`.
grobinson-grafana marked this conversation as resolved.
Show resolved Hide resolved
if alertname, ok := labels["alertname"]; ok {
args = append(args, alertname)
delete(labels, "alertname")
}
}
for k, v := range labels {
args = append(args, k+"="+v)
}
startsAt := strfmt.DateTime(am.opts.expandTime(alert.startsAt))
args = append(args, "--start="+startsAt.String())
Expand Down Expand Up @@ -522,7 +535,7 @@ func parseAlertQueryResponse(data []byte) ([]TestAlert, error) {
}
summary := strings.TrimSpace(line[summPos:])
alert := TestAlert{
labels: models.LabelSet{"name": alertName},
labels: models.LabelSet{"alertname": alertName},
startsAt: float64(startsAt.Unix()),
summary: summary,
}
Expand Down
20 changes: 14 additions & 6 deletions test/cli/acceptance/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ receivers:
am := amc.Members()[0]

alert1 := Alert("alertname", "test1").Active(1, 2)
am.AddAlertsAt(0, alert1)
am.AddAlertsAt(false, 0, alert1)
co.Want(Between(1, 2), Alert("alertname", "test1").Active(1))

at.Run()
Expand Down Expand Up @@ -111,12 +111,13 @@ receivers:
am := amc.Members()[0]

alert1 := Alert("alertname", "test1", "severity", "warning").Active(1)
alert2 := Alert("alertname", "test2", "severity", "info").Active(1)
am.AddAlerts(alert1, alert2)
alert2 := Alert("alertname", "alertname=test2", "severity", "info").Active(1)
alert3 := Alert("alertname", "{alertname=test3}", "severity", "info").Active(1)
am.AddAlerts(true, alert1, alert2, alert3)

alerts, err := am.QueryAlerts()
require.NoError(t, err)
require.Len(t, alerts, 2)
require.Len(t, alerts, 3)

// Get the first alert using the alertname heuristic
alerts, err = am.QueryAlerts("test1")
Expand All @@ -126,14 +127,21 @@ receivers:
// 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"}
expectedLabels := models.LabelSet{"alertname": "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"}
expectedLabels = models.LabelSet{"alertname": "test2"}
require.True(t, alerts[0].HasLabels(expectedLabels))

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

Expand Down