-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
alerts_test.go
97 lines (72 loc) · 2.27 KB
/
alerts_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package grabana
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/K-Phoen/grabana/alertmanager"
"github.com/stretchr/testify/require"
)
func TestConfigureAlertManager(t *testing.T) {
req := require.New(t)
endpointCalled := false
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
endpointCalled = true
w.WriteHeader(http.StatusAccepted)
}))
defer ts.Close()
client := NewClient(http.DefaultClient, ts.URL)
manager := alertmanager.New()
err := client.ConfigureAlertManager(context.TODO(), manager)
req.NoError(err)
req.True(endpointCalled)
}
func TestConfigureAlertManagerForwardsErrorOnFailure(t *testing.T) {
req := require.New(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, `{
"message": "something when wrong"
}`)
}))
defer ts.Close()
client := NewClient(http.DefaultClient, ts.URL)
manager := alertmanager.New()
err := client.ConfigureAlertManager(context.TODO(), manager)
req.Error(err)
req.Contains(err.Error(), "something when wrong")
}
func TestDeleteAlertGroup(t *testing.T) {
req := require.New(t)
groupDeleted := false
groupNamespace := "group namespace"
groupName := "group name"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
groupDeleted = true
req.Equal(http.MethodDelete, r.Method)
req.Equal("/api/ruler/grafana/api/v1/rules/group namespace/group name", r.URL.Path)
w.WriteHeader(http.StatusAccepted)
_, _ = fmt.Fprintln(w, `{}`)
}))
defer ts.Close()
client := NewClient(http.DefaultClient, ts.URL)
err := client.DeleteAlertGroup(context.TODO(), groupNamespace, groupName)
req.NoError(err)
req.True(groupDeleted)
}
func TestDeleteAlertGroupThatDoesNotExist(t *testing.T) {
req := require.New(t)
groupDeleted := false
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
groupDeleted = true
w.WriteHeader(http.StatusNotFound)
_, _ = fmt.Fprintln(w, `{}`)
}))
defer ts.Close()
client := NewClient(http.DefaultClient, ts.URL)
err := client.DeleteAlertGroup(context.TODO(), "ns", "name")
req.Error(err)
req.ErrorIs(err, ErrAlertNotFound)
req.True(groupDeleted)
}