forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerting_contact_point_test.go
173 lines (146 loc) · 3.64 KB
/
alerting_contact_point_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package gapi
import (
"testing"
"github.com/gobs/pretty"
)
func TestContactPoints(t *testing.T) {
t.Run("get contact points succeeds", func(t *testing.T) {
client := gapiTestTools(t, 200, getContactPointsJSON)
ps, err := client.ContactPoints()
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(ps))
if len(ps) != 2 {
t.Errorf("wrong number of contact points returned, got %d", len(ps))
}
if ps[0].UID != "" {
t.Errorf("incorrect UID - expected %s on element %d, got %s", "", 0, ps[0].UID)
}
if ps[1].UID != "rc5r0bjnz" {
t.Errorf("incorrect UID - expected %s on element %d, got %s", "rc5r0bjnz", 1, ps[1].UID)
}
})
t.Run("get contact points by name succeeds", func(t *testing.T) {
client := gapiTestTools(t, 200, getContactPointsQueryJSON)
ps, err := client.ContactPointsByName("slack-receiver-1")
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(ps))
if len(ps) != 1 {
t.Errorf("wrong number of contact points returned, got %d", len(ps))
}
if ps[0].UID != "rc5r0bjnz" {
t.Errorf("incorrect UID - expected %s on element %d, got %s", "rc5r0bjnz", 0, ps[0].UID)
}
})
t.Run("get contact point succeeds", func(t *testing.T) {
client := gapiTestTools(t, 200, getContactPointsJSON)
p, err := client.ContactPoint("rc5r0bjnz")
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(p))
if p.UID != "rc5r0bjnz" {
t.Errorf("incorrect UID - expected %s got %s", "rc5r0bjnz", p.UID)
}
})
t.Run("get non-existent contact point fails", func(t *testing.T) {
client := gapiTestTools(t, 200, getContactPointsJSON)
p, err := client.ContactPoint("does not exist")
if err == nil {
t.Errorf("expected error but got nil")
t.Log(pretty.PrettyFormat(p))
}
})
t.Run("create contact point succeeds", func(t *testing.T) {
client := gapiTestTools(t, 201, writeContactPointJSON)
p := createContactPoint()
uid, err := client.NewContactPoint(&p)
if err != nil {
t.Error(err)
}
if uid != "rc5r0bjnz" {
t.Errorf("unexpected UID returned, got %s", uid)
}
})
t.Run("update contact point succeeds", func(t *testing.T) {
client := gapiTestTools(t, 200, writeContactPointJSON)
p := createContactPoint()
p.UID = "on7otbj7k"
err := client.UpdateContactPoint(&p)
if err != nil {
t.Error(err)
}
})
t.Run("delete contact point succeeds", func(t *testing.T) {
client := gapiTestTools(t, 204, "")
err := client.DeleteContactPoint("rc5r0bjnz")
if err != nil {
t.Error(err)
}
})
}
func createContactPoint() ContactPoint {
return ContactPoint{
Name: "slack-receiver-123",
Type: "slack",
DisableResolveMessage: false,
Settings: map[string]interface{}{
"recipient": "@zxcv",
"token": "test-token",
"url": "https://test-url",
},
}
}
const getContactPointsJSON = `
[
{
"uid": "",
"name": "default-email-receiver",
"type": "email",
"disableResolveMessage": false,
"settings": {
"addresses": "<[email protected]>"
}
},
{
"uid": "rc5r0bjnz",
"name": "slack-receiver-1",
"type": "slack",
"disableResolveMessage": false,
"settings": {
"recipient": "@foo",
"token": "[REDACTED]",
"url": "[REDACTED]"
}
}
]`
const getContactPointsQueryJSON = `
[
{
"uid": "rc5r0bjnz",
"name": "slack-receiver-1",
"type": "slack",
"disableResolveMessage": false,
"settings": {
"recipient": "@foo",
"token": "[REDACTED]",
"url": "[REDACTED]"
}
}
]`
const writeContactPointJSON = `
{
"uid": "rc5r0bjnz",
"name": "slack-receiver-1",
"type": "slack",
"disableResolveMessage": false,
"settings": {
"recipient": "@foo",
"token": "[REDACTED]",
"url": "[REDACTED]"
}
}
`