-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheckly_integration_test.go
381 lines (336 loc) · 10.6 KB
/
checkly_integration_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//go:build integration
// +build integration
package checkly_test
import (
"context"
"io"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
checkly "github.com/checkly/checkly-go-sdk"
)
func setupClient(t *testing.T) checkly.Client {
var debug io.Writer // to enable debug output set to => os.Stdout
baseUrl := os.Getenv("CHECKLY_API_URL")
if baseUrl == "" {
baseUrl = "http://localhost:3000"
}
apiKey := os.Getenv("CHECKLY_API_KEY")
if apiKey == "" {
t.Error("'CHECKLY_API_KEY' must be set for integration tests")
}
accountId := os.Getenv("CHECKLY_ACCOUNT_ID")
if accountId == "" {
t.Error("'CHECKLY_ACCOUNT_ID' must be set for integration tests")
}
client := checkly.NewClient(
baseUrl,
apiKey,
nil,
debug,
)
client.SetAccountId(accountId)
return client
}
func TestCreateIntegration(t *testing.T) {
client := setupClient(t)
gotCheck, err := client.Create(context.Background(), wantCheck)
if err != nil {
t.Error(err)
}
defer client.Delete(context.Background(), gotCheck.ID)
if !cmp.Equal(wantCheck, *gotCheck, ignoreCheckFields) {
t.Error(cmp.Diff(wantCheck, *gotCheck, ignoreCheckFields))
}
}
func TestGetIntegration(t *testing.T) {
client := setupClient(t)
check, err := client.Create(context.Background(), wantCheck)
if err != nil {
t.Error(err)
}
defer client.Delete(context.Background(), check.ID)
gotCheck, err := client.Get(context.Background(), check.ID)
if err != nil {
t.Error(err)
}
if !cmp.Equal(wantCheck, *gotCheck, ignoreCheckFields) {
t.Error(cmp.Diff(wantCheck, *gotCheck, ignoreCheckFields))
}
}
func TestUpdateIntegration(t *testing.T) {
t.Parallel()
client := setupClient(t)
check, err := client.Create(context.Background(), wantCheck)
if err != nil {
t.Error(err)
}
defer client.Delete(context.Background(), check.ID)
updatedCheck := wantCheck
updatedCheck.Name = "integrationTestUpdate"
gotCheck, err := client.Update(context.Background(), check.ID, updatedCheck)
if err != nil {
t.Error(err)
}
if !cmp.Equal(updatedCheck, *gotCheck, ignoreCheckFields) {
t.Error(cmp.Diff(updatedCheck, *gotCheck, ignoreCheckFields))
}
}
func TestDeleteIntegration(t *testing.T) {
t.Parallel()
client := setupClient(t)
check, err := client.Create(context.Background(), wantCheck)
if err != nil {
t.Error(err)
}
if err := client.Delete(context.Background(), check.ID); err != nil {
t.Error(err)
}
}
func makeTestAlertChannel(client checkly.Client) (*checkly.AlertChannel, error) {
return client.CreateAlertChannel(
context.Background(),
checkly.AlertChannel{
Type: checkly.AlertTypeEmail,
Email: &checkly.AlertChannelEmail{
Address: "[email protected]",
},
},
)
}
func TestCreateGroupIntegration(t *testing.T) {
client := setupClient(t)
wantGroupCopy := wantGroup
ac, err := makeTestAlertChannel(client)
if err != nil {
t.Error(err.Error())
return
}
wantGroupCopy.AlertChannelSubscriptions[0].ChannelID = ac.ID
gotGroup, err := client.CreateGroup(context.Background(), wantGroupCopy)
if err != nil {
t.Error(err)
}
defer client.DeleteGroup(context.Background(), gotGroup.ID)
// These are set by the APIs
ignored := cmpopts.IgnoreFields(checkly.Group{}, "ID", "AlertChannelSubscriptions", "AlertSettings.SSLCertificates", "PrivateLocations")
if !cmp.Equal(wantGroupCopy, *gotGroup, ignored) {
t.Error(cmp.Diff(wantGroupCopy, *gotGroup, ignored))
}
}
func TestGetGroupIntegration(t *testing.T) {
client := setupClient(t)
ac, err := makeTestAlertChannel(client)
wantGroupCopy := wantGroup
wantGroupCopy.AlertChannelSubscriptions[0].ChannelID = ac.ID
group, err := client.CreateGroup(context.Background(), wantGroupCopy)
if err != nil {
t.Error(err)
}
defer client.DeleteGroup(context.Background(), group.ID)
gotGroup, err := client.GetGroup(context.Background(), group.ID)
if err != nil {
t.Error(err)
}
// These are set by the API
wantGroupCopy.AlertChannelSubscriptions = gotGroup.AlertChannelSubscriptions
if !cmp.Equal(wantGroupCopy, *gotGroup, ignoreGroupFields) {
t.Error(cmp.Diff(wantGroupCopy, *gotGroup, ignoreGroupFields))
}
}
//Dashboard
func TestCreateDashboardIntegration(t *testing.T) {
client := setupClient(t)
gotDashboard, err := client.CreateDashboard(context.Background(), testDashboard)
if err != nil {
t.Error(err)
}
defer client.DeleteDashboard(context.Background(), gotDashboard.DashboardID)
if !cmp.Equal(testDashboard, *gotDashboard, ignoreDashboardFields) {
t.Error(cmp.Diff(testDashboard, *gotDashboard, ignoreDashboardFields))
}
}
func TestGetDashboardIntegration(t *testing.T) {
client := setupClient(t)
dash, err := client.CreateDashboard(context.Background(), testDashboard)
if err != nil {
t.Error(err)
}
defer client.DeleteDashboard(context.Background(), dash.DashboardID)
gotDashboard, err := client.GetDashboard(context.Background(), dash.DashboardID)
if err != nil {
t.Error(err)
}
if !cmp.Equal(testDashboard, *gotDashboard, ignoreDashboardFields) {
t.Error(cmp.Diff(testDashboard, *gotDashboard, ignoreDashboardFields))
}
}
//Maintenance Windows
func TestCreateMaintenanceWindowIntegration(t *testing.T) {
client := setupClient(t)
gotMaintenanceWindow, err := client.CreateMaintenanceWindow(context.Background(), testMaintenanceWindow)
if err != nil {
t.Error(err)
}
defer client.DeleteMaintenanceWindow(context.Background(), gotMaintenanceWindow.ID)
if !cmp.Equal(testMaintenanceWindow, *gotMaintenanceWindow, ignoreMaintenanceWindowFields) {
t.Error(cmp.Diff(testMaintenanceWindow, *gotMaintenanceWindow, ignoreMaintenanceWindowFields))
}
}
func TestGetMaintenanceWindowIntegration(t *testing.T) {
client := setupClient(t)
dash, err := client.CreateMaintenanceWindow(context.Background(), testMaintenanceWindow)
if err != nil {
t.Error(err)
}
defer client.DeleteMaintenanceWindow(context.Background(), dash.ID)
gotMaintenanceWindow, err := client.GetMaintenanceWindow(context.Background(), dash.ID)
if err != nil {
t.Error(err)
}
if !cmp.Equal(testMaintenanceWindow, *gotMaintenanceWindow, ignoreMaintenanceWindowFields) {
t.Error(cmp.Diff(testMaintenanceWindow, *gotMaintenanceWindow, ignoreMaintenanceWindowFields))
}
}
//TriggerCheck
func TestCreateTriggerCheckIntegration(t *testing.T) {
client := setupClient(t)
gotCheck, err := client.Create(context.Background(), wantCheck)
if err != nil {
t.Error(err)
}
gotTriggerCheck, err := client.CreateTriggerCheck(context.Background(), gotCheck.ID)
if err != nil {
t.Error(err)
}
defer client.Delete(context.Background(), gotCheck.ID)
if !cmp.Equal(gotCheck.ID, gotTriggerCheck.CheckId, ignoreTriggerCheck) {
t.Error(cmp.Diff(gotCheck.ID, gotTriggerCheck.CheckId, ignoreTriggerCheck))
}
}
func TestGetTriggerCheckIntegration(t *testing.T) {
client := setupClient(t)
gotCheck, err := client.Create(context.Background(), wantCheck)
if err != nil {
t.Error(err)
}
tc, err := client.CreateTriggerCheck(context.Background(), gotCheck.ID)
if err != nil {
t.Error(err)
}
gotTriggerCheck, err := client.GetTriggerCheck(context.Background(), tc.CheckId)
if err != nil {
t.Error(err)
}
defer client.Delete(context.Background(), gotCheck.ID)
if !cmp.Equal(gotCheck.ID, gotTriggerCheck.CheckId, ignoreTriggerCheck) {
t.Error(cmp.Diff(gotCheck.ID, gotTriggerCheck.CheckId, ignoreTriggerCheck))
}
}
//TriggerGroup
func TestCreateTriggerGroupIntegration(t *testing.T) {
client := setupClient(t)
gotGroup, err := client.CreateGroup(context.Background(), wantGroup)
if err != nil {
t.Error(err)
}
gotTriggerGroup, err := client.CreateTriggerGroup(context.Background(), gotGroup.ID)
if err != nil {
t.Error(err)
}
defer client.DeleteGroup(context.Background(), gotGroup.ID)
if !cmp.Equal(gotGroup.ID, gotTriggerGroup.GroupId, ignoreTriggerGroup) {
t.Error(cmp.Diff(gotGroup.ID, gotTriggerGroup.GroupId, ignoreTriggerGroup))
}
}
func TestGetTriggerGroupIntegration(t *testing.T) {
client := setupClient(t)
gotGroup, err := client.CreateGroup(context.Background(), wantGroup)
if err != nil {
t.Error(err)
}
tc, err := client.CreateTriggerGroup(context.Background(), gotGroup.ID)
if err != nil {
t.Error(err)
}
defer client.DeleteTriggerGroup(context.Background(), tc.GroupId)
gotTriggerGroup, err := client.GetTriggerGroup(context.Background(), tc.GroupId)
if err != nil {
t.Error(err)
}
if !cmp.Equal(gotGroup.ID, gotTriggerGroup.GroupId, ignoreTriggerGroup) {
t.Error(cmp.Diff(gotGroup.ID, gotTriggerGroup.GroupId, ignoreTriggerGroup))
}
}
// PrivateLocations
func TestCreatePrivateLocationIntegration(t *testing.T) {
client := setupClient(t)
gotPrivateLocation, err := client.CreatePrivateLocation(context.Background(), testPrivateLocation)
if err != nil {
t.Error(err)
}
defer client.DeletePrivateLocation(context.Background(), gotPrivateLocation.ID)
if !cmp.Equal(testPrivateLocation, *gotPrivateLocation, ignorePrivateLocationFields) {
t.Error(cmp.Diff(testPrivateLocation, *gotPrivateLocation, ignorePrivateLocationFields))
}
}
func TestGetPrivateLocationIntegration(t *testing.T) {
client := setupClient(t)
pl, err := client.CreatePrivateLocation(context.Background(), testPrivateLocation)
if err != nil {
t.Error(err)
}
defer client.DeletePrivateLocation(context.Background(), pl.ID)
gotPrivateLocation, err := client.GetPrivateLocation(context.Background(), pl.ID)
if err != nil {
t.Error(err)
}
if !cmp.Equal(testPrivateLocation, *gotPrivateLocation, ignorePrivateLocationFields) {
t.Error(cmp.Diff(testPrivateLocation, *gotPrivateLocation, ignorePrivateLocationFields))
}
}
func TestTCPCheckCRUD(t *testing.T) {
ctx := context.TODO()
client := setupClient(t)
pendingCheck := checkly.TCPCheck{
Name: "TestTCPCheckCRUD",
Muted: false,
Locations: []string{"eu-west-1"},
Request: checkly.TCPRequest{
Hostname: "api.checklyhq.com",
Port: 443,
},
}
createdCheck, err := client.CreateTCPCheck(ctx, pendingCheck)
if err != nil {
t.Fatalf("failed to create TCP check: %v", err)
}
var didDelete bool
defer func() {
if !didDelete {
_ = client.DeleteCheck(ctx, createdCheck.ID)
}
}()
if createdCheck.Muted != false {
t.Fatalf("expected Muted to be false after creation")
}
_, err = client.GetTCPCheck(ctx, createdCheck.ID)
if err != nil {
t.Fatalf("failed to get TCP check: %v", err)
}
updateCheck := *createdCheck
updateCheck.Muted = true
updatedCheck, err := client.UpdateTCPCheck(ctx, createdCheck.ID, updateCheck)
if err != nil {
t.Fatalf("failed to update TCP check: %v", err)
}
if updatedCheck.Muted != true {
t.Fatalf("expected Muted to be true after update")
}
didDelete = true
err = client.DeleteCheck(ctx, createdCheck.ID)
if err != nil {
t.Fatalf("failed to delete TCP check: %v", err)
}
}