-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_dashboard_test.go
executable file
·231 lines (218 loc) · 7.03 KB
/
monitor_dashboard_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
package main
import (
"fmt"
"time"
"github.com/gorilla/mux"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("ServiceMonitor", func() {
child4 := &Status{
ID: "child4",
Probe: ProbeRef{
Type: "NewRelic",
Data: map[string]string{
MonitorNameKey: "monitor4",
},
},
}
child3 := &Status{
ID: "child3",
Status: "good",
Probe: ProbeRef{
Type: "NewRelic",
Data: map[string]string{
MonitorNameKey: "monitor3",
},
},
}
child2 := &Status{
ID: "child2",
Children: []*Status{child3, child4},
}
child1 := &Status{
ID: "child1",
Probe: ProbeRef{
Type: "NewRelic",
Data: map[string]string{},
},
}
parent := &Status{
ID: "parent",
Children: []*Status{child1, child2},
}
statuses := []*Status{parent}
Describe("Monitor", func() {
Describe("Given a tree of statuses", func() {
Context("When trying to find a nested status", func() {
It("Then it should be found", func() {
status, err := FindStatus("parent#child2#child3", statuses)
Expect(err).To(BeNil())
Expect(status).To(Equal(child3))
status, err = FindStatus("parent#child1", statuses)
Expect(err).To(BeNil())
Expect(status).To(Equal(child1))
})
})
Context("When trying to find a parent status with children", func() {
It("Then it should be found along with the children", func() {
status, err := FindStatus("parent", statuses)
Expect(err).To(BeNil())
Expect(status).To(Equal(parent))
status, err = FindStatus("parent#child2", statuses)
Expect(err).To(BeNil())
Expect(status).To(Equal(child2))
})
})
Context("When trying to find a status that doesn't exist", func() {
It("Then it should not be found", func() {
_, err := FindStatus("unknown", statuses)
Expect(err).ToNot(BeNil())
_, err = FindStatus("parent#child3", statuses)
Expect(err).ToNot(BeNil())
})
})
})
Describe("Given a Monitor", func() {
mc := &MonitorConfig{
Router: mux.NewRouter(),
Statuses: statuses,
}
m := NewMonitor(mc)
Context("When a Status exist", func() {
It("Then it can be updated", func() {
child3.Status = "bad"
err := m.UpdateStatusByID(StatusUpdate{
ID: "parent#child2#child3",
Status: "good",
})
Expect(err).To(BeNil())
Expect(child3.Status).To(Equal("good"))
})
})
Context("When a Status does not exist", func() {
It("Then it will error when updated", func() {
err := m.UpdateStatusByID(StatusUpdate{
ID: "unknown",
Status: "good",
})
Expect(err).ToNot(BeNil())
})
})
})
})
Describe("Probe", func() {
Describe("Given creating status cache for New Relic Probe", func() {
config := &NewRelicProbeConfig{}
nr := NewNewRelicProbe(config)
Context("When the probe type and monitor name exist", func() {
It("Then it should be added to the cache correctly", func() {
err := nr.Initialize([]*Status{child3})
Expect(err).To(BeNil())
Expect(nr.statuses).To(HaveKey("monitor3"))
Expect(*nr.statuses["monitor3"]).To(Equal(StatusUpdate{
ID: child3.ID,
Status: child3.Status,
lastUpdateMillis: 0,
},
))
})
})
Context("When the probe type and monitor name exist in children", func() {
It("Then they should be added to the cache correctly", func() {
err := nr.Initialize([]*Status{child2})
Expect(err).To(BeNil())
Expect(nr.statuses).To(HaveKey("monitor3"))
Expect(*nr.statuses["monitor3"]).To(Equal(StatusUpdate{
ID: child2.ID + IdDelimiter + child3.ID,
Status: child3.Status,
lastUpdateMillis: 0,
},
))
Expect(nr.statuses).To(HaveKey("monitor4"))
Expect(*nr.statuses["monitor4"]).To(Equal(StatusUpdate{
ID: child2.ID + IdDelimiter + child4.ID,
Status: child4.Status,
lastUpdateMillis: 0,
},
))
})
})
Context("When the monitor name does not exist", func() {
It("Then we should error", func() {
err := nr.Initialize([]*Status{child1})
Expect(err).ToNot(BeNil())
})
})
})
Describe("Given creating New Relic query", func() {
monitorNames := []string{"name1", "name2"}
Context("When cache is empty", func() {
It("Then query should not have monitor names", func() {
monitorNames := []string{}
interval, _ := time.ParseDuration("5m")
query := createNRQLQuery(monitorNames, interval)
expected := fmt.Sprintf(NewRelicNRQLBase, "", 8)
Expect(query).To(Equal(expected))
})
})
Context("When cache has monitor names", func() {
It("Then query should have monitor names correctly delimited and time in the query should have that time plus buffer time", func() {
interval, _ := time.ParseDuration("5m")
query := createNRQLQuery(monitorNames, interval)
expected := fmt.Sprintf(NewRelicNRQLBase, "'name1','name2'", 8)
Expect(query).To(Equal(expected))
})
})
Context("When time interval is given not in minutes", func() {
It("Then time in the query should be properly converted (seconds)", func() {
interval, _ := time.ParseDuration("30s")
query := createNRQLQuery(monitorNames, interval)
expected := fmt.Sprintf(NewRelicNRQLBase, "'name1','name2'", 4)
Expect(query).To(Equal(expected))
})
It("Then time in the query should be properly converted (hours)", func() {
interval, _ := time.ParseDuration("2h")
query := createNRQLQuery(monitorNames, interval)
expected := fmt.Sprintf(NewRelicNRQLBase, "'name1','name2'", 123)
Expect(query).To(Equal(expected))
})
})
})
Describe("Given a built StatusUpdate cache", func() {
Context("When the cache has content", func() {
It("Then the monitor names should be returned", func() {
monitorNames := []string{"name1", "name2"}
cache := make(map[string]*StatusUpdate)
for _, name := range monitorNames {
cache[name] = &StatusUpdate{}
}
names := monitorNamesFromCache(cache)
Expect(names).To(ConsistOf(monitorNames))
})
})
Context("When the cache has no content", func() {
It("Then an empty array should be returned", func() {
cache := make(map[string]*StatusUpdate)
names := monitorNamesFromCache(cache)
Expect(names).To(BeEmpty())
})
})
})
Describe("Given creating the New Relic requets URL", func() {
Context("When the query is given", func() {
It("Then the query should be url encoded and append to base URL", func() {
monitorNames := []string{"name1", "name2"}
cache := make(map[string]*StatusUpdate)
for _, name := range monitorNames {
cache[name] = &StatusUpdate{}
}
interval, _ := time.ParseDuration("5m")
query := createRequestURL(cache, interval)
expected := "https://insights-api.newrelic.com/v1/accounts/918250/query?nrql=SELECT%20monitorName%2C%20result%20FROM%20SyntheticCheck%20WHERE%20monitorName%20IN%20%28%27name1%27%2C%27name2%27%29%20SINCE%208%20minutes%20ago%20LIMIT%20100"
Expect(query).To(Equal(expected))
})
})
})
})
})