blob: 2a797d695d7018bef52b5d27779fd6546aeea6f6 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package probe
17
18import (
19 "context"
20 "encoding/json"
Scott Bakerce767002019-10-23 13:30:24 -070021 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Baker2c1c4822019-10-16 11:02:41 -070022 "github.com/stretchr/testify/assert"
23 "io/ioutil"
24 "net/http"
25 "net/http/httptest"
26 "testing"
27)
28
29func init() {
30 log.AddPackage(log.JSON, log.WarnLevel, nil)
31}
32
33func TestServiceStatusString(t *testing.T) {
34 assert.Equal(t, "Unknown", ServiceStatusUnknown.String(), "ServiceStatusUnknown")
35 assert.Equal(t, "Preparing", ServiceStatusPreparing.String(), "ServiceStatusPreparing")
36 assert.Equal(t, "Prepared", ServiceStatusPrepared.String(), "ServiceStatusPrepared")
37 assert.Equal(t, "Running", ServiceStatusRunning.String(), "ServiceStatusRunning")
38 assert.Equal(t, "Stopped", ServiceStatusStopped.String(), "ServiceStatusStopped")
39 assert.Equal(t, "Failed", ServiceStatusFailed.String(), "ServiceStatusFailed")
40}
41
42func AlwaysTrue(map[string]ServiceStatus) bool {
43 return true
44}
45
46func AlwaysFalse(map[string]ServiceStatus) bool {
47 return false
48}
49
50func TestWithFuncs(t *testing.T) {
51 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
52
53 assert.NotNil(t, p.readyFunc, "ready func not set")
54 assert.True(t, p.readyFunc(nil), "ready func not set correctly")
55 assert.NotNil(t, p.healthFunc, "health func not set")
56 assert.False(t, p.healthFunc(nil), "health func not set correctly")
57}
58
59func TestWithReadyFuncOnly(t *testing.T) {
60 p := (&Probe{}).WithReadyFunc(AlwaysTrue)
61
62 assert.NotNil(t, p.readyFunc, "ready func not set")
63 assert.True(t, p.readyFunc(nil), "ready func not set correctly")
64 assert.Nil(t, p.healthFunc, "health func set")
65}
66
67func TestWithHealthFuncOnly(t *testing.T) {
68 p := (&Probe{}).WithHealthFunc(AlwaysTrue)
69
70 assert.Nil(t, p.readyFunc, "ready func set")
71 assert.NotNil(t, p.healthFunc, "health func not set")
72 assert.True(t, p.healthFunc(nil), "health func not set correctly")
73}
74
75func TestRegisterOneService(t *testing.T) {
76 p := &Probe{}
77
78 p.RegisterService("one")
79
80 assert.Equal(t, 1, len(p.status), "wrong number of services")
81
82 _, ok := p.status["one"]
83 assert.True(t, ok, "service not found")
84}
85
86func TestRegisterMultipleServices(t *testing.T) {
87 p := &Probe{}
88
89 p.RegisterService("one", "two", "three", "four")
90
91 assert.Equal(t, 4, len(p.status), "wrong number of services")
92
93 _, ok := p.status["one"]
94 assert.True(t, ok, "service one not found")
95 _, ok = p.status["two"]
96 assert.True(t, ok, "service two not found")
97 _, ok = p.status["three"]
98 assert.True(t, ok, "service three not found")
99 _, ok = p.status["four"]
100 assert.True(t, ok, "service four not found")
101}
102
103func TestRegisterMultipleServicesIncremental(t *testing.T) {
104 p := &Probe{}
105
106 p.RegisterService("one")
107 p.RegisterService("two")
108 p.RegisterService("three", "four")
109
110 assert.Equal(t, 4, len(p.status), "wrong number of services")
111
112 _, ok := p.status["one"]
113 assert.True(t, ok, "service one not found")
114 _, ok = p.status["two"]
115 assert.True(t, ok, "service two not found")
116 _, ok = p.status["three"]
117 assert.True(t, ok, "service three not found")
118 _, ok = p.status["four"]
119 assert.True(t, ok, "service four not found")
120}
121
122func TestRegisterMultipleServicesDuplicates(t *testing.T) {
123 p := &Probe{}
124
125 p.RegisterService("one", "one", "one", "two")
126
127 assert.Equal(t, 2, len(p.status), "wrong number of services")
128
129 _, ok := p.status["one"]
130 assert.True(t, ok, "service one not found")
131 _, ok = p.status["two"]
132 assert.True(t, ok, "service two not found")
133}
134
135func TestRegisterMultipleServicesDuplicatesIncremental(t *testing.T) {
136 p := &Probe{}
137
138 p.RegisterService("one")
139 p.RegisterService("one")
140 p.RegisterService("one", "two")
141
142 assert.Equal(t, 2, len(p.status), "wrong number of services")
143
144 _, ok := p.status["one"]
145 assert.True(t, ok, "service one not found")
146 _, ok = p.status["two"]
147 assert.True(t, ok, "service two not found")
148}
149
150func TestUpdateStatus(t *testing.T) {
151 p := &Probe{}
152
153 p.RegisterService("one", "two")
154 p.UpdateStatus("one", ServiceStatusRunning)
155
156 assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set")
157 assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
158}
159
160func TestRegisterOverwriteStatus(t *testing.T) {
161 p := &Probe{}
162
163 p.RegisterService("one", "two")
164 p.UpdateStatus("one", ServiceStatusRunning)
165
166 assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set")
167 assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
168
169 p.RegisterService("one", "three")
170 assert.Equal(t, 3, len(p.status), "wrong number of services")
171 assert.Equal(t, ServiceStatusRunning, p.status["one"], "status overridden")
172 assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
173 assert.Equal(t, ServiceStatusUnknown, p.status["three"], "status set")
174}
175
176func TestDetailzWithServies(t *testing.T) {
177 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
178 p.RegisterService("one", "two")
179
180 req := httptest.NewRequest("GET", "http://example.com/detailz", nil)
181 w := httptest.NewRecorder()
182 p.detailzFunc(w, req)
183 resp := w.Result()
184 body, _ := ioutil.ReadAll(resp.Body)
185
186 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code for no services")
187 assert.Equal(t, "application/json", resp.Header.Get("Content-Type"), "wrong content type")
188 var vals map[string]string
189 err := json.Unmarshal(body, &vals)
190 assert.Nil(t, err, "unable to unmarshal values")
191 assert.Equal(t, "Unknown", vals["one"], "wrong value")
192 assert.Equal(t, "Unknown", vals["two"], "wrong value")
193}
194
195func TestReadzNoServices(t *testing.T) {
196 p := (&Probe{}).WithReadyFunc(AlwaysTrue)
197 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
198 w := httptest.NewRecorder()
199 p.readzFunc(w, req)
200 resp := w.Result()
201
202 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for no services")
203}
204
205func TestReadzWithServicesWithTrue(t *testing.T) {
206 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
207 p.RegisterService("one", "two")
208
209 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
210 w := httptest.NewRecorder()
211 p.readzFunc(w, req)
212 resp := w.Result()
213 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code for registered only services")
214}
215
216func TestReadzWithServicesWithDefault(t *testing.T) {
217 p := &Probe{}
218 p.RegisterService("one", "two")
219
220 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
221 w := httptest.NewRecorder()
222 p.readzFunc(w, req)
223 resp := w.Result()
224 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for registered only services")
225}
226
227func TestReadzNpServicesDefault(t *testing.T) {
228 p := &Probe{}
229
230 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
231 w := httptest.NewRecorder()
232 p.readzFunc(w, req)
233 resp := w.Result()
234 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code")
235}
236
237func TestReadzWithServicesDefault(t *testing.T) {
238 p := &Probe{}
239 p.RegisterService("one", "two")
240 p.UpdateStatus("one", ServiceStatusRunning)
241 p.UpdateStatus("two", ServiceStatusRunning)
242
243 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
244 w := httptest.NewRecorder()
245 p.readzFunc(w, req)
246 resp := w.Result()
247 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code")
248}
249
250func TestReadzWithServicesDefaultOne(t *testing.T) {
251 p := &Probe{}
252 p.RegisterService("one", "two")
253 p.UpdateStatus("one", ServiceStatusRunning)
254
255 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
256 w := httptest.NewRecorder()
257 p.readzFunc(w, req)
258 resp := w.Result()
259 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code")
260}
261
262func TestHealthzNoServices(t *testing.T) {
263 p := (&Probe{}).WithReadyFunc(AlwaysTrue)
264 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
265 w := httptest.NewRecorder()
266 p.healthzFunc(w, req)
267 resp := w.Result()
268
269 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for no services")
270}
271
272func TestHealthzWithServicesWithTrue(t *testing.T) {
273 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
274 p.RegisterService("one", "two")
275
276 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
277 w := httptest.NewRecorder()
278 p.healthzFunc(w, req)
279 resp := w.Result()
280 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code for registered only services")
281}
282
283func TestHealthzWithServicesWithDefault(t *testing.T) {
284 p := &Probe{}
285 p.RegisterService("one", "two")
286
287 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
288 w := httptest.NewRecorder()
289 p.healthzFunc(w, req)
290 resp := w.Result()
291 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code for registered only services")
292}
293
294func TestHealthzNoServicesDefault(t *testing.T) {
295 p := &Probe{}
296
297 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
298 w := httptest.NewRecorder()
299 p.healthzFunc(w, req)
300 resp := w.Result()
301 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code")
302}
303
304func TestHealthzWithServicesDefault(t *testing.T) {
305 p := &Probe{}
306 p.RegisterService("one", "two")
307 p.UpdateStatus("one", ServiceStatusRunning)
308 p.UpdateStatus("two", ServiceStatusRunning)
309
310 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
311 w := httptest.NewRecorder()
312 p.healthzFunc(w, req)
313 resp := w.Result()
314 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code")
315}
316
317func TestHealthzWithServicesDefaultFailed(t *testing.T) {
318 p := &Probe{}
319 p.RegisterService("one", "two")
320 p.UpdateStatus("one", ServiceStatusFailed)
321
322 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
323 w := httptest.NewRecorder()
324 p.healthzFunc(w, req)
325 resp := w.Result()
326 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code")
327}
328
329func TestSetFuncsToNil(t *testing.T) {
330 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
331 p.WithReadyFunc(nil).WithHealthFunc(nil)
332 assert.Nil(t, p.readyFunc, "ready func not reset to nil")
333 assert.Nil(t, p.healthFunc, "health func not reset to nil")
334}
335
336func TestUpdateStatusFromContext(t *testing.T) {
337 p := &Probe{}
338 p.RegisterService("one")
339 ctx := context.WithValue(context.Background(), ProbeContextKey, p)
340 UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
341
342 assert.Equal(t, 1, len(p.status), "wrong number of services")
343 _, ok := p.status["one"]
344 assert.True(t, ok, "unable to find registered service")
345 assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set correctly from context")
346
347}
348
349func TestUpdateStatusFromNilContext(t *testing.T) {
350 p := &Probe{}
351 p.RegisterService("one")
352 UpdateStatusFromContext(nil, "one", ServiceStatusRunning)
353
354 assert.Equal(t, 1, len(p.status), "wrong number of services")
355 _, ok := p.status["one"]
356 assert.True(t, ok, "unable to find registered service")
357 assert.Equal(t, ServiceStatusUnknown, p.status["one"], "status not set correctly from context")
358
359}
360
361func TestUpdateStatusFromContextWithoutProbe(t *testing.T) {
362 p := &Probe{}
363 p.RegisterService("one")
364 ctx := context.Background()
365 UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
366
367 assert.Equal(t, 1, len(p.status), "wrong number of services")
368 _, ok := p.status["one"]
369 assert.True(t, ok, "unable to find registered service")
370 assert.Equal(t, ServiceStatusUnknown, p.status["one"], "status not set correctly from context")
371
372}
373
374func TestUpdateStatusFromContextWrongType(t *testing.T) {
375 p := &Probe{}
376 p.RegisterService("one")
377 ctx := context.WithValue(context.Background(), ProbeContextKey, "Teapot")
378 UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
379
380 assert.Equal(t, 1, len(p.status), "wrong number of services")
381 _, ok := p.status["one"]
382 assert.True(t, ok, "unable to find registered service")
383 assert.Equal(t, ServiceStatusUnknown, p.status["one"], "status not set correctly from context")
384}
385
386func TestUpdateStatusNoRegistered(t *testing.T) {
387 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
388
389 p.UpdateStatus("one", ServiceStatusRunning)
390 assert.Equal(t, 1, len(p.status), "wrong number of services")
391 _, ok := p.status["one"]
392 assert.True(t, ok, "unable to find registered service")
393 assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set correctly from context")
394}