cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2019.7.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

origin/cloudflare_status_page_test.go

386lines · modecode

1package origin
2
3import (
4 "testing"
5 "time"
6
7 "github.com/cloudflare/golibs/lrucache"
8 "github.com/stretchr/testify/assert"
9)
10
11func TestParseStatusPage(t *testing.T) {
12 testCases := []struct {
13 input []byte
14 output *StatusPage
15 fail bool
16 }{
17 {
18 input: []byte(`<html>
19 <head><title>504 Gateway Time-out</title></head>
20 <body><center><h1>504 Gateway Time-out</h1></center></body>
21 </html>`),
22 output: nil,
23 fail: true,
24 },
25 {
26 input: []byte(`{
27 "page": {
28 "id": "yh6f0r4529hb",
29 "name": "Cloudflare",
30 "url": "https://www.cloudflarestatus.com",
31 "time_zone": "Etc/UTC",
32 "updated_at": "2019-01-10T20:11:38.750Z"
33 },
34 "incidents": [
35 {
36 "name": "Cloudflare API service issues",
37 "status": "resolved",
38 "created_at": "2018-09-17T19:29:21.132Z",
39 "updated_at": "2018-09-18T07:45:41.313Z",
40 "monitoring_at": "2018-09-17T21:35:06.492Z",
41 "resolved_at": "2018-09-18T07:45:41.290Z",
42 "shortlink": "http://stspg.io/7f079791e",
43 "id": "q746ybtyb6q0",
44 "page_id": "yh6f0r4529hb",
45 "incident_updates": [
46 {
47 "status": "resolved",
48 "body": "Cloudflare has resolved the issue and the service have resumed normal operation.",
49 "created_at": "2018-09-18T07:45:41.290Z",
50 "updated_at": "2018-09-18T07:45:41.290Z",
51 "display_at": "2018-09-18T07:45:41.290Z",
52 "affected_components": [
53 {
54 "code": "g4tb35rs9yw7",
55 "name": "Cloudflare customer dashboard and APIs - Cloudflare APIs",
56 "old_status": "operational",
57 "new_status": "operational"
58 }
59 ],
60 "deliver_notifications": true,
61 "tweet_id": null,
62 "id": "zl5g2pl5zhfs",
63 "incident_id": "q746ybtyb6q0",
64 "custom_tweet": null
65 },
66 {
67 "status": "monitoring",
68 "body": "Cloudflare has implemented a fix for this issue and is currently monitoring the results.\r\n\r\nWe will update the status once the issue is resolved.",
69 "created_at": "2018-09-17T21:35:06.492Z",
70 "updated_at": "2018-09-17T21:35:06.492Z",
71 "display_at": "2018-09-17T21:35:06.492Z",
72 "affected_components": [
73 {
74 "code": "g4tb35rs9yw7",
75 "name": "Cloudflare customer dashboard and APIs - Cloudflare APIs",
76 "old_status": "degraded_performance",
77 "new_status": "operational"
78 }
79 ],
80 "deliver_notifications": false,
81 "tweet_id": null,
82 "id": "0001sv3chdnx",
83 "incident_id": "q746ybtyb6q0",
84 "custom_tweet": null
85 },
86 {
87 "status": "investigating",
88 "body": "We are continuing to investigate this issue.",
89 "created_at": "2018-09-17T19:30:08.049Z",
90 "updated_at": "2018-09-17T19:30:08.049Z",
91 "display_at": "2018-09-17T19:30:08.049Z",
92 "affected_components": [
93 {
94 "code": "g4tb35rs9yw7",
95 "name": "Cloudflare customer dashboard and APIs - Cloudflare APIs",
96 "old_status": "operational",
97 "new_status": "degraded_performance"
98 }
99 ],
100 "deliver_notifications": false,
101 "tweet_id": null,
102 "id": "qdr164tfpq7m",
103 "incident_id": "q746ybtyb6q0",
104 "custom_tweet": null
105 },
106 {
107 "status": "investigating",
108 "body": "Cloudflare is investigating issues with APIs and Page Rule delays for Page Rule updates. Cloudflare Page Rule service delivery is unaffected and is operating normally. Also, these issues do not affect the Cloudflare CDN and therefore, do not impact customer websites.",
109 "created_at": "2018-09-17T19:29:21.201Z",
110 "updated_at": "2018-09-17T19:29:21.201Z",
111 "display_at": "2018-09-17T19:29:21.201Z",
112 "affected_components": [
113 {
114 "code": "g4tb35rs9yw7",
115 "name": "Cloudflare customer dashboard and APIs - Cloudflare APIs",
116 "old_status": "operational",
117 "new_status": "operational"
118 }
119 ],
120 "deliver_notifications": false,
121 "tweet_id": null,
122 "id": "qzl2n0q8tskg",
123 "incident_id": "q746ybtyb6q0",
124 "custom_tweet": null
125 }
126 ],
127 "components": [
128 {
129 "status": "operational",
130 "name": "Cloudflare APIs",
131 "created_at": "2014-10-09T03:32:07.158Z",
132 "updated_at": "2019-01-01T22:58:30.846Z",
133 "position": 2,
134 "description": null,
135 "showcase": false,
136 "id": "g4tb35rs9yw7",
137 "page_id": "yh6f0r4529hb",
138 "group_id": "1km35smx8p41",
139 "group": false,
140 "only_show_if_degraded": false,
141 "automation_email": "component+g4tb35rs9yw7@notifications.statuspage.io"
142 }
143 ],
144 "impact": "minor"
145 },
146 {
147 "name": "Web Analytics Delays",
148 "status": "resolved",
149 "created_at": "2018-09-17T18:05:39.907Z",
150 "updated_at": "2018-09-17T22:53:05.078Z",
151 "monitoring_at": null,
152 "resolved_at": "2018-09-17T22:53:05.057Z",
153 "shortlink": "http://stspg.io/cb208928c",
154 "id": "wqfk9mzs5qt1",
155 "page_id": "yh6f0r4529hb",
156 "incident_updates": [
157 {
158 "status": "resolved",
159 "body": "Cloudflare has resolved the issue and Web Analytics have resumed normal operation.",
160 "created_at": "2018-09-17T22:53:05.057Z",
161 "updated_at": "2018-09-17T22:53:05.057Z",
162 "display_at": "2018-09-17T22:53:05.057Z",
163 "affected_components": [
164 {
165 "code": "4c231tkdlpcl",
166 "name": "Cloudflare customer dashboard and APIs - Analytics",
167 "old_status": "degraded_performance",
168 "new_status": "operational"
169 }
170 ],
171 "deliver_notifications": false,
172 "tweet_id": null,
173 "id": "93y1w00yqzk4",
174 "incident_id": "wqfk9mzs5qt1",
175 "custom_tweet": null
176 },
177 {
178 "status": "investigating",
179 "body": "There is a delay in processing Cloudflare Web Analytics. This affects timely delivery of customer data.\n\nThese delays do not impact analytics for DNS and Rate Limiting.",
180 "created_at": "2018-09-17T18:05:40.033Z",
181 "updated_at": "2018-09-17T18:05:40.033Z",
182 "display_at": "2018-09-17T18:05:40.033Z",
183 "affected_components": [
184 {
185 "code": "4c231tkdlpcl",
186 "name": "Cloudflare customer dashboard and APIs - Analytics",
187 "old_status": "operational",
188 "new_status": "degraded_performance"
189 }
190 ],
191 "deliver_notifications": false,
192 "tweet_id": null,
193 "id": "362t6lv0vrpk",
194 "incident_id": "wqfk9mzs5qt1",
195 "custom_tweet": null
196 }
197 ],
198 "components": [
199 {
200 "status": "operational",
201 "name": "Analytics",
202 "created_at": "2014-11-13T11:54:10.191Z",
203 "updated_at": "2018-12-31T08:20:52.349Z",
204 "position": 3,
205 "description": "Customer data",
206 "showcase": false,
207 "id": "4c231tkdlpcl",
208 "page_id": "yh6f0r4529hb",
209 "group_id": "1km35smx8p41",
210 "group": false,
211 "only_show_if_degraded": false,
212 "automation_email": "component+4c231tkdlpcl@notifications.statuspage.io"
213 }
214 ],
215 "impact": "minor"
216 }
217 ]
218 }`),
219 output: &StatusPage{
220 Incidents: []Incident{
221 Incident{
222 Name: "Cloudflare API service issues",
223 ID: "q746ybtyb6q0",
224 Updates: []IncidentUpdate{
225 IncidentUpdate{
226 Body: "Cloudflare has resolved the issue and the service have resumed normal operation.",
227 },
228 IncidentUpdate{
229 Body: "Cloudflare has implemented a fix for this issue and is currently monitoring the results.\r\n\r\nWe will update the status once the issue is resolved.",
230 },
231 IncidentUpdate{
232 Body: "We are continuing to investigate this issue.",
233 },
234 IncidentUpdate{
235 Body: "Cloudflare is investigating issues with APIs and Page Rule delays for Page Rule updates. Cloudflare Page Rule service delivery is unaffected and is operating normally. Also, these issues do not affect the Cloudflare CDN and therefore, do not impact customer websites.",
236 },
237 },
238 },
239 Incident{
240 Name: "Web Analytics Delays",
241 ID: "wqfk9mzs5qt1",
242 Updates: []IncidentUpdate{
243 IncidentUpdate{
244 Body: "Cloudflare has resolved the issue and Web Analytics have resumed normal operation.",
245 },
246 IncidentUpdate{
247 Body: "There is a delay in processing Cloudflare Web Analytics. This affects timely delivery of customer data.\n\nThese delays do not impact analytics for DNS and Rate Limiting.",
248 },
249 },
250 },
251 },
252 },
253 fail: false,
254 },
255 }
256
257 for _, testCase := range testCases {
258 output, err := parseStatusPage(testCase.input)
259 if testCase.fail {
260 assert.Error(t, err)
261 } else {
262 assert.Nil(t, err)
263 assert.Equal(t, testCase.output, output)
264 }
265 }
266}
267
268func TestIsArgoTunnelIncident(t *testing.T) {
269 testCases := []struct {
270 input Incident
271 output bool
272 }{
273 {
274 input: Incident{},
275 output: false,
276 },
277 {
278 input: Incident{Name: "An Argo Tunnel incident"},
279 output: true,
280 },
281 {
282 input: Incident{Name: "an argo tunnel incident"},
283 output: true,
284 },
285 {
286 input: Incident{Name: "an aRgO TuNnEl incident"},
287 output: true,
288 },
289 {
290 input: Incident{Name: "an argotunnel incident"},
291 output: false,
292 },
293 {
294 input: Incident{Name: "irrelevant"},
295 output: false,
296 },
297 {
298 input: Incident{
299 Name: "irrelevant",
300 Updates: []IncidentUpdate{
301 IncidentUpdate{Body: "irrelevant"},
302 IncidentUpdate{Body: "an Argo Tunnel incident"},
303 IncidentUpdate{Body: "irrelevant"},
304 },
305 },
306 output: true,
307 },
308 {
309 input: Incident{
310 Name: "an Argo Tunnel incident",
311 Updates: []IncidentUpdate{
312 IncidentUpdate{Body: "irrelevant"},
313 IncidentUpdate{Body: "irrelevant"},
314 IncidentUpdate{Body: "irrelevant"},
315 },
316 },
317 output: true,
318 },
319 }
320 for _, testCase := range testCases {
321 actual := isArgoTunnelIncident(testCase.input)
322 assert.Equal(t, testCase.output, actual, "Test case failed: %v", testCase.input)
323 }
324}
325
326func TestIncidentURL(t *testing.T) {
327 incident := Incident{
328 ID: "s6k0dnn5347b",
329 }
330 assert.Equal(t, "https://www.cloudflarestatus.com/incidents/s6k0dnn5347b", incident.URL())
331}
332
333func TestNewCachedIncidentLookup(t *testing.T) {
334 c := newCachedIncidentLookup(func() []Incident { return nil })
335 assert.Equal(t, time.Minute, c.ttl)
336 assert.Equal(t, 1, c.cache.Capacity())
337}
338
339func TestCachedIncidentLookup(t *testing.T) {
340 expected := []Incident{
341 Incident{
342 Name: "An incident",
343 ID: "incidentID",
344 },
345 }
346
347 var shouldCallUncachedLookup bool
348 c := &cachedIncidentLookup{
349 cache: lrucache.NewLRUCache(1),
350 ttl: 50 * time.Millisecond,
351 uncachedLookup: func() []Incident {
352 if !shouldCallUncachedLookup {
353 t.Fatal("uncachedLookup shouldn't have been called")
354 }
355 return expected
356 },
357 }
358
359 shouldCallUncachedLookup = true
360 assert.Equal(t, expected, c.ActiveIncidents())
361
362 shouldCallUncachedLookup = false
363 assert.Equal(t, expected, c.ActiveIncidents())
364 assert.Equal(t, expected, c.ActiveIncidents())
365
366 time.Sleep(50 * time.Millisecond)
367 shouldCallUncachedLookup = true
368 assert.Equal(t, expected, c.ActiveIncidents())
369}
370
371func TestCachedIncidentLookupDoesntPanic(t *testing.T) {
372 expected := []Incident{
373 Incident{
374 Name: "An incident",
375 ID: "incidentID",
376 },
377 }
378 c := &cachedIncidentLookup{
379 cache: lrucache.NewLRUCache(1),
380 ttl: 50 * time.Millisecond,
381 uncachedLookup: func() []Incident { return expected },
382 }
383 c.cache.Set(cacheKey, 42, time.Now().Add(30*time.Minute))
384 actual := c.ActiveIncidents()
385 assert.Equal(t, expected, actual)
386}