Skip to content

Commit

Permalink
Add tests for PromQL braces when using amtool alert
Browse files Browse the repository at this point in the history
Signed-off-by: George Robinson <[email protected]>
  • Loading branch information
grobinson-grafana committed Sep 13, 2023
1 parent 5ba9b9c commit 628b2c3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
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...)
})
}

// 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 ...`.
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

0 comments on commit 628b2c3

Please sign in to comment.