File: synthetics_test.go

package info (click to toggle)
golang-github-zorkian-go-datadog-api 2.30.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,628 kB
  • sloc: makefile: 28; sh: 13
file content (290 lines) | stat: -rw-r--r-- 8,944 bytes parent folder | download | duplicates (3)
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
package integration

import (
	"testing"

	"github.com/stretchr/testify/assert"
	datadog "github.com/zorkian/go-datadog-api"
)

func TestSyntheticsCreateAndDelete(t *testing.T) {
	expected := getTestSynthetics()
	// create the monitor and compare it
	actual := createTestSynthetics(t)
	defer cleanUpSynthetics(t, actual.GetPublicId())

	// Set ID of our original struct to zero so we can easily compare the results
	expected.SetPublicId(actual.GetPublicId())
	// Set Creator to the original struct as we can't predict details of the creator
	expected.SetCreatedAt(actual.GetCreatedAt())
	expected.SetModifiedAt(actual.GetModifiedAt())
	expected.SetMonitorId(actual.GetMonitorId())

	assert.Equal(t, expected, actual)

	actual, err := client.GetSyntheticsTest(*actual.PublicId)
	if err != nil {
		t.Fatalf("Retrieving a synthetics failed when it shouldn't: (%s)", err)
	}
	expected.SetStatus(actual.GetStatus())
	expected.SetCreatedBy(actual.GetCreatedBy())
	expected.SetModifiedBy(actual.GetModifiedBy())
	expected.SetMonitorId(actual.GetMonitorId())
	assert.Equal(t, expected, actual)
}

func TestSyntheticsCreateAndDeleteSSL(t *testing.T) {
	expected := getTestSyntheticsSSL()
	// create the monitor and compare it
	actual := createTestSyntheticsSSL(t)
	defer cleanUpSynthetics(t, actual.GetPublicId())

	// Set ID of our original struct to zero so we can easily compare the results
	expected.SetPublicId(actual.GetPublicId())
	// Set Creator to the original struct as we can't predict details of the creator
	expected.SetCreatedAt(actual.GetCreatedAt())
	expected.SetModifiedAt(actual.GetModifiedAt())
	expected.SetMonitorId(actual.GetMonitorId())

	assert.Equal(t, expected, actual)

	actual, err := client.GetSyntheticsTest(*actual.PublicId)
	if err != nil {
		t.Fatalf("Retrieving a synthetics failed when it shouldn't: (%s)", err)
	}
	expected.SetStatus(actual.GetStatus())
	expected.SetCreatedBy(actual.GetCreatedBy())
	expected.SetModifiedBy(actual.GetModifiedBy())
	expected.SetMonitorId(actual.GetMonitorId())
	assert.Equal(t, expected, actual)
}

func TestSyntheticsUpdate(t *testing.T) {

	syntheticsTest := createTestSynthetics(t)
	defer cleanUpSynthetics(t, syntheticsTest.GetPublicId())

	syntheticsTest.SetName("___New-Test-Synthetics___")
	publicId := syntheticsTest.GetPublicId()
	createdAt := syntheticsTest.GetCreatedAt()
	syntheticsTest.PublicId = nil
	syntheticsTest.CreatedAt = nil
	syntheticsTest.ModifiedAt = nil
	syntheticsTest.MonitorId = nil
	actual, err := client.UpdateSyntheticsTest(publicId, syntheticsTest)
	if err != nil {
		t.Fatalf("Updating a synthetics test failed when it shouldn't: %s", err)
	}

	syntheticsTest.SetPublicId(publicId)
	syntheticsTest.SetCreatedAt(createdAt)
	syntheticsTest.SetModifiedAt(actual.GetModifiedAt())
	syntheticsTest.SetMonitorId(actual.GetMonitorId())
	assert.Equal(t, syntheticsTest, actual)

}

func TestSyntheticsUpdateRemovingTags(t *testing.T) {

	syntheticsTest := createTestSynthetics(t)
	defer cleanUpSynthetics(t, syntheticsTest.GetPublicId())

	publicId := syntheticsTest.GetPublicId()
	createdAt := syntheticsTest.GetCreatedAt()
	syntheticsTest.PublicId = nil
	syntheticsTest.CreatedAt = nil
	syntheticsTest.ModifiedAt = nil
	syntheticsTest.MonitorId = nil
	syntheticsTest.Tags = []string{}
	actual, err := client.UpdateSyntheticsTest(publicId, syntheticsTest)
	if err != nil {
		t.Fatalf("Updating a synthetics test failed when it shouldn't: %s", err)
	}

	syntheticsTest.SetPublicId(publicId)
	syntheticsTest.SetCreatedAt(createdAt)
	syntheticsTest.SetModifiedAt(actual.GetModifiedAt())
	syntheticsTest.SetMonitorId(actual.GetMonitorId())
	assert.Equal(t, syntheticsTest, actual)

}

func TestSyntheticsGetAllTests(t *testing.T) {
	syntheticsTests, err := client.GetSyntheticsTestsByType("api")
	if err != nil {
		t.Fatalf("Retrieving synthetics tests failed when it shouldn't: %s", err)
	}
	num := len(syntheticsTests)

	syntheticsTest := createTestSynthetics(t)
	defer cleanUpSynthetics(t, syntheticsTest.GetPublicId())

	syntheticsTests, err = client.GetSyntheticsTestsByType("api")
	if err != nil {
		t.Fatalf("Retrieving synthetics tests failed when it shouldn't: %s", err)
	}

	if num+1 != len(syntheticsTests) {
		t.Fatalf("Number of synthetics tests didn't match expected: %d != %d", len(syntheticsTests), num+1)
	}
}

func TestMonitorPauseResume(t *testing.T) {
	syntheticsTest := createTestSynthetics(t)
	defer cleanUpSynthetics(t, syntheticsTest.GetPublicId())

	publicId := syntheticsTest.GetPublicId()

	// Pause SyntheticsTest
	_, err := client.PauseSyntheticsTest(publicId)
	if err != nil {
		t.Fatalf("Failed to pause test")
	}

	syntheticsTest, err = client.GetSyntheticsTest(publicId)
	if err != nil {
		t.Fatalf("Retrieving synthetics test failed when it shouldn't: %s", err)
	}

	assert.Equal(t, "paused", *syntheticsTest.Status)

	// Resume SyntheticsTest
	_, err = client.ResumeSyntheticsTest(publicId)
	if err != nil {
		t.Fatalf("Failed to resume synthetics test")
	}

	syntheticsTest, err = client.GetSyntheticsTest(publicId)
	if err != nil {
		t.Fatalf("Retrieving synthetics test failed when it shouldn't: %s", err)
	}

	assert.Equal(t, "live", *syntheticsTest.Status)
}

func TestSyntheticsGetAllLocations(t *testing.T) {
	syntheticsLocations, err := client.GetSyntheticsLocations()
	if err != nil {
		t.Fatalf("Retrieving synthetics locations failed when it shouldn't: %s", err)
	}
	num := len(syntheticsLocations)

	if num == 0 {
		t.Fatalf("Number of synthetics locations should be more than 0")
	}
}

func TestSyntheticsGetAllDevices(t *testing.T) {
	syntheticsDevices, err := client.GetSyntheticsBrowserDevices()
	if err != nil {
		t.Fatalf("Retrieving synthetics browser devices failed when it shouldn't: %s", err)
	}
	num := len(syntheticsDevices)

	if num == 0 {
		t.Fatalf("Number of synthetics devices should be more than 0")
	}
}

/*
	Testing of global mute and unmuting has not been added for following reasons:
	* Disabling and enabling of global monitoring does an @all mention which is noisy
	* It exposes risk to users that run integration tests in their main account
	* There is no endpoint to verify success
*/

func getTestSynthetics() *datadog.SyntheticsTest {
	c := &datadog.SyntheticsConfig{
		Request: &datadog.SyntheticsRequest{
			Method:  datadog.String("GET"),
			Url:     datadog.String("https://example.org"),
			Timeout: datadog.Int(30),
		},
		Assertions: []datadog.SyntheticsAssertion{{
			Type:     datadog.String("statusCode"),
			Operator: datadog.String("is"),
			Target:   float64(200),
		}},
	}
	o := &datadog.SyntheticsOptions{
		TickEvery: datadog.Int(60),
	}

	return &datadog.SyntheticsTest{
		Message:   datadog.String("Test message"),
		Name:      datadog.String("Test synthetics"),
		Config:    c,
		Options:   o,
		Locations: []string{"aws:eu-central-1"},
		Status:    datadog.String("live"),
		Type:      datadog.String("api"),
		Subtype:   datadog.String("http"),
		Tags:      []string{"tag1:value1", "tag2:value2"},
	}
}

func createTestSynthetics(t *testing.T) *datadog.SyntheticsTest {
	synthetics := getTestSynthetics()
	synthetics, err := client.CreateSyntheticsTest(synthetics)
	if err != nil {
		t.Fatalf("Creating a synthetics failed when it shouldn't: %s", err)
	}

	return synthetics
}

func getTestSyntheticsSSL() *datadog.SyntheticsTest {
	c := &datadog.SyntheticsConfig{
		Request: &datadog.SyntheticsRequest{
			Host: datadog.String("example.org"),
			Port: datadog.Int(443),
		},
		Assertions: []datadog.SyntheticsAssertion{{
			Type:     datadog.String("certificate"),
			Property: datadog.String("sslExpirationDate"),
			Operator: datadog.String("isInMoreThan"),
			Target:   float64(30),
		}},
	}
	o := &datadog.SyntheticsOptions{
		TickEvery:        datadog.Int(60),
		AcceptSelfSigned: datadog.Bool(true),
	}

	return &datadog.SyntheticsTest{
		Message:   datadog.String("Test message"),
		Name:      datadog.String("Test synthetics"),
		Config:    c,
		Options:   o,
		Locations: []string{"aws:eu-central-1"},
		Status:    datadog.String("live"),
		Type:      datadog.String("api"),
		Subtype:   datadog.String("ssl"),
		Tags:      []string{"tag1:value1", "tag2:value2"},
	}
}

func createTestSyntheticsSSL(t *testing.T) *datadog.SyntheticsTest {
	synthetics := getTestSyntheticsSSL()
	synthetics, err := client.CreateSyntheticsTest(synthetics)
	if err != nil {
		t.Fatalf("Creating a synthetics failed when it shouldn't: %s", err)
	}

	return synthetics
}

func cleanUpSynthetics(t *testing.T, publicId string) {
	if err := client.DeleteSyntheticsTests([]string{publicId}); err != nil {
		t.Fatalf("Deleting a synthetics failed when it shouldn't. Manual cleanup needed. (%s)", err)
	}

	deletedSynthetics, err := client.GetSyntheticsTest(publicId)
	if deletedSynthetics != nil {
		t.Fatal("Synthetics hasn't been deleted when it should have been. Manual cleanup needed.")
	}

	if err == nil {
		t.Fatal("Fetching deleted synthetics didn't lead to an error.")
	}
}