Skip to content

Commit

Permalink
Add acceptance tests for creating silences
Browse files Browse the repository at this point in the history
Signed-off-by: George Robinson <[email protected]>
  • Loading branch information
grobinson-grafana committed Nov 15, 2023
1 parent d2ef2ce commit ffaeb25
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 21 deletions.
40 changes: 19 additions & 21 deletions test/with_api_v2/acceptance/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,38 @@ receivers:

am := amc.Members()[0]

// assert we can create an alert with classic labels
// can create an alert with classic labels
now := time.Now()
expectedLabels := models.LabelSet{
"a": "b",
"b": "Σ",
"c": "\xf0\x9f\x99\x82",
"d": "eΘ",
}
pa := &models.PostableAlert{
StartsAt: strfmt.DateTime(now),
EndsAt: strfmt.DateTime(now.Add(5 * time.Minute)),
Alert: models.Alert{Labels: expectedLabels},
Alert: models.Alert{
Labels: models.LabelSet{
"a": "b",
"b": "Σ",
"c": "\xf0\x9f\x99\x82",
"d": "eΘ",
},
},
}
alertParams := alert.NewPostAlertsParams()
alertParams.Alerts = models.PostableAlerts{pa}

_, err := am.Client().Alert.PostAlerts(alertParams)
require.NoError(t, err)

// assert we cannot create an alert with UTF-8 labels
// cannot create an alert with UTF-8 labels
now = time.Now()
expectedLabels = models.LabelSet{
"a": "a",
"00": "b",
"Σ": "c",
"\xf0\x9f\x99\x82": "dΘ",
}
pa = &models.PostableAlert{
StartsAt: strfmt.DateTime(now),
EndsAt: strfmt.DateTime(now.Add(5 * time.Minute)),
Alert: models.Alert{
Labels: expectedLabels,
Labels: models.LabelSet{
"a": "a",
"00": "b",
"Σ": "c",
"\xf0\x9f\x99\x82": "dΘ",
},
},
}
alertParams = alert.NewPostAlertsParams()
Expand Down Expand Up @@ -129,7 +129,7 @@ receivers:
am := amc.Members()[0]

now := time.Now()
expectedLabels := models.LabelSet{
labels := models.LabelSet{
"a": "a",
"00": "b",
"Σ": "c",
Expand All @@ -138,9 +138,7 @@ receivers:
pa := &models.PostableAlert{
StartsAt: strfmt.DateTime(now),
EndsAt: strfmt.DateTime(now.Add(5 * time.Minute)),
Alert: models.Alert{
Labels: expectedLabels,
},
Alert: models.Alert{Labels: labels},
}
alertParams := alert.NewPostAlertsParams()
alertParams.Alerts = models.PostableAlerts{pa}
Expand All @@ -153,7 +151,7 @@ receivers:
require.Len(t, resp.Payload, 1)

alert := resp.Payload[0]
require.Equal(t, expectedLabels, alert.Labels)
require.Equal(t, labels, alert.Labels)
}

// TestAlertGetReturnsCurrentStatus checks that querying the API returns the
Expand Down
146 changes: 146 additions & 0 deletions test/with_api_v2/acceptance/silence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,146 @@ package test

import (
"fmt"
"github.com/go-openapi/strfmt"
"github.com/prometheus/alertmanager/api/v2/client/silence"
"github.com/prometheus/alertmanager/api/v2/models"
"github.com/stretchr/testify/require"
"strings"
"testing"
"time"

. "github.com/prometheus/alertmanager/test/with_api_v2"
)

func TestAddClassicSilence(t *testing.T) {
t.Parallel()

conf := `
route:
receiver: "default"
group_by: []
group_wait: 1s
group_interval: 1s
repeat_interval: 1ms
receivers:
- name: "default"
webhook_configs:
- url: 'http://%s'
`

at := NewAcceptanceTest(t, &AcceptanceOpts{
FeatureFlags: []string{"classic-matchers-parsing"},
Tolerance: 150 * time.Millisecond,
})

co := at.Collector("webhook")
wh := NewWebhook(t, co)

amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
require.NoError(t, amc.Start())
defer amc.Terminate()

am := amc.Members()[0]

// can create a silence with classic matchers
now := time.Now()
ps := models.PostableSilence{
Silence: models.Silence{
Comment: stringPtr("test"),
CreatedBy: stringPtr("test"),
Matchers: models.Matchers{{
Name: stringPtr("foo"),
IsEqual: boolPtr(true),
IsRegex: boolPtr(false),
Value: stringPtr("bar"),
}},
StartsAt: dateTimePtr(strfmt.DateTime(now)),
EndsAt: dateTimePtr(strfmt.DateTime(now.Add(24 * time.Hour))),
},
}
silenceParams := silence.NewPostSilencesParams()
silenceParams.Silence = &ps

_, err := am.Client().Silence.PostSilences(silenceParams)
require.NoError(t, err)

// cannot create a silence with UTF-8 matchers
now = time.Now()
ps = models.PostableSilence{
Silence: models.Silence{
Comment: stringPtr("test"),
CreatedBy: stringPtr("test"),
Matchers: models.Matchers{{
Name: stringPtr("fooΣ"),
IsEqual: boolPtr(true),
IsRegex: boolPtr(false),
Value: stringPtr("bar🙂"),
}},
StartsAt: dateTimePtr(strfmt.DateTime(now)),
EndsAt: dateTimePtr(strfmt.DateTime(now.Add(24 * time.Hour))),
},
}
silenceParams = silence.NewPostSilencesParams()
silenceParams.Silence = &ps

_, err = am.Client().Silence.PostSilences(silenceParams)
require.NotNil(t, err)
require.True(t, strings.Contains(err.Error(), "silence invalid: invalid label matcher"))
}

func TestAddUTF8Silence(t *testing.T) {
t.Parallel()

conf := `
route:
receiver: "default"
group_by: []
group_wait: 1s
group_interval: 1s
repeat_interval: 1ms
receivers:
- name: "default"
webhook_configs:
- url: 'http://%s'
`

at := NewAcceptanceTest(t, &AcceptanceOpts{
Tolerance: 150 * time.Millisecond,
})

co := at.Collector("webhook")
wh := NewWebhook(t, co)

amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
require.NoError(t, amc.Start())
defer amc.Terminate()

am := amc.Members()[0]

now := time.Now()
ps := models.PostableSilence{
Silence: models.Silence{
Comment: stringPtr("test"),
CreatedBy: stringPtr("test"),
Matchers: models.Matchers{{
Name: stringPtr("fooΣ"),
IsEqual: boolPtr(true),
IsRegex: boolPtr(false),
Value: stringPtr("bar🙂"),
}},
StartsAt: dateTimePtr(strfmt.DateTime(now)),
EndsAt: dateTimePtr(strfmt.DateTime(now.Add(24 * time.Hour))),
},
}
silenceParams := silence.NewPostSilencesParams()
silenceParams.Silence = &ps

_, err := am.Client().Silence.PostSilences(silenceParams)
require.NoError(t, err)
}

func TestSilencing(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -122,3 +256,15 @@ receivers:

t.Log(co.Check())
}

func boolPtr(b bool) *bool {
return &b
}

func stringPtr(s string) *string {
return &s
}

func dateTimePtr(t strfmt.DateTime) *strfmt.DateTime {
return &t
}

0 comments on commit ffaeb25

Please sign in to comment.