blob: 37e801383b1cb9595041504d2fb91814f6916670 [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"
serkant.uluderyab38671c2019-11-01 09:35:38 -070021 "github.com/opencord/voltha-lib-go/v3/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")
Scott Baker104b67d2019-10-29 15:56:27 -070040 assert.Equal(t, "NotReady", ServiceStatusNotReady.String(), "ServiceStatusNotReady")
Scott Baker2c1c4822019-10-16 11:02:41 -070041}
42
43func AlwaysTrue(map[string]ServiceStatus) bool {
44 return true
45}
46
47func AlwaysFalse(map[string]ServiceStatus) bool {
48 return false
49}
50
51func TestWithFuncs(t *testing.T) {
52 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
53
54 assert.NotNil(t, p.readyFunc, "ready func not set")
55 assert.True(t, p.readyFunc(nil), "ready func not set correctly")
56 assert.NotNil(t, p.healthFunc, "health func not set")
57 assert.False(t, p.healthFunc(nil), "health func not set correctly")
58}
59
60func TestWithReadyFuncOnly(t *testing.T) {
61 p := (&Probe{}).WithReadyFunc(AlwaysTrue)
62
63 assert.NotNil(t, p.readyFunc, "ready func not set")
64 assert.True(t, p.readyFunc(nil), "ready func not set correctly")
65 assert.Nil(t, p.healthFunc, "health func set")
66}
67
68func TestWithHealthFuncOnly(t *testing.T) {
69 p := (&Probe{}).WithHealthFunc(AlwaysTrue)
70
71 assert.Nil(t, p.readyFunc, "ready func set")
72 assert.NotNil(t, p.healthFunc, "health func not set")
73 assert.True(t, p.healthFunc(nil), "health func not set correctly")
74}
75
76func TestRegisterOneService(t *testing.T) {
77 p := &Probe{}
78
79 p.RegisterService("one")
80
81 assert.Equal(t, 1, len(p.status), "wrong number of services")
82
83 _, ok := p.status["one"]
84 assert.True(t, ok, "service not found")
85}
86
87func TestRegisterMultipleServices(t *testing.T) {
88 p := &Probe{}
89
90 p.RegisterService("one", "two", "three", "four")
91
92 assert.Equal(t, 4, len(p.status), "wrong number of services")
93
94 _, ok := p.status["one"]
95 assert.True(t, ok, "service one not found")
96 _, ok = p.status["two"]
97 assert.True(t, ok, "service two not found")
98 _, ok = p.status["three"]
99 assert.True(t, ok, "service three not found")
100 _, ok = p.status["four"]
101 assert.True(t, ok, "service four not found")
102}
103
104func TestRegisterMultipleServicesIncremental(t *testing.T) {
105 p := &Probe{}
106
107 p.RegisterService("one")
108 p.RegisterService("two")
109 p.RegisterService("three", "four")
110
111 assert.Equal(t, 4, len(p.status), "wrong number of services")
112
113 _, ok := p.status["one"]
114 assert.True(t, ok, "service one not found")
115 _, ok = p.status["two"]
116 assert.True(t, ok, "service two not found")
117 _, ok = p.status["three"]
118 assert.True(t, ok, "service three not found")
119 _, ok = p.status["four"]
120 assert.True(t, ok, "service four not found")
121}
122
123func TestRegisterMultipleServicesDuplicates(t *testing.T) {
124 p := &Probe{}
125
126 p.RegisterService("one", "one", "one", "two")
127
128 assert.Equal(t, 2, len(p.status), "wrong number of services")
129
130 _, ok := p.status["one"]
131 assert.True(t, ok, "service one not found")
132 _, ok = p.status["two"]
133 assert.True(t, ok, "service two not found")
134}
135
136func TestRegisterMultipleServicesDuplicatesIncremental(t *testing.T) {
137 p := &Probe{}
138
139 p.RegisterService("one")
140 p.RegisterService("one")
141 p.RegisterService("one", "two")
142
143 assert.Equal(t, 2, len(p.status), "wrong number of services")
144
145 _, ok := p.status["one"]
146 assert.True(t, ok, "service one not found")
147 _, ok = p.status["two"]
148 assert.True(t, ok, "service two not found")
149}
150
151func TestUpdateStatus(t *testing.T) {
152 p := &Probe{}
153
154 p.RegisterService("one", "two")
155 p.UpdateStatus("one", ServiceStatusRunning)
156
157 assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set")
158 assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
159}
160
161func TestRegisterOverwriteStatus(t *testing.T) {
162 p := &Probe{}
163
164 p.RegisterService("one", "two")
165 p.UpdateStatus("one", ServiceStatusRunning)
166
167 assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set")
168 assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
169
170 p.RegisterService("one", "three")
171 assert.Equal(t, 3, len(p.status), "wrong number of services")
172 assert.Equal(t, ServiceStatusRunning, p.status["one"], "status overridden")
173 assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
174 assert.Equal(t, ServiceStatusUnknown, p.status["three"], "status set")
175}
176
177func TestDetailzWithServies(t *testing.T) {
178 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
179 p.RegisterService("one", "two")
180
181 req := httptest.NewRequest("GET", "http://example.com/detailz", nil)
182 w := httptest.NewRecorder()
183 p.detailzFunc(w, req)
184 resp := w.Result()
185 body, _ := ioutil.ReadAll(resp.Body)
186
187 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code for no services")
188 assert.Equal(t, "application/json", resp.Header.Get("Content-Type"), "wrong content type")
189 var vals map[string]string
190 err := json.Unmarshal(body, &vals)
191 assert.Nil(t, err, "unable to unmarshal values")
192 assert.Equal(t, "Unknown", vals["one"], "wrong value")
193 assert.Equal(t, "Unknown", vals["two"], "wrong value")
194}
195
196func TestReadzNoServices(t *testing.T) {
197 p := (&Probe{}).WithReadyFunc(AlwaysTrue)
198 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
199 w := httptest.NewRecorder()
200 p.readzFunc(w, req)
201 resp := w.Result()
202
203 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for no services")
204}
205
206func TestReadzWithServicesWithTrue(t *testing.T) {
207 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
208 p.RegisterService("one", "two")
209
210 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
211 w := httptest.NewRecorder()
212 p.readzFunc(w, req)
213 resp := w.Result()
214 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code for registered only services")
215}
216
217func TestReadzWithServicesWithDefault(t *testing.T) {
218 p := &Probe{}
219 p.RegisterService("one", "two")
220
221 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
222 w := httptest.NewRecorder()
223 p.readzFunc(w, req)
224 resp := w.Result()
225 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for registered only services")
226}
227
228func TestReadzNpServicesDefault(t *testing.T) {
229 p := &Probe{}
230
231 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
232 w := httptest.NewRecorder()
233 p.readzFunc(w, req)
234 resp := w.Result()
235 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code")
236}
237
238func TestReadzWithServicesDefault(t *testing.T) {
239 p := &Probe{}
240 p.RegisterService("one", "two")
241 p.UpdateStatus("one", ServiceStatusRunning)
242 p.UpdateStatus("two", ServiceStatusRunning)
243
244 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
245 w := httptest.NewRecorder()
246 p.readzFunc(w, req)
247 resp := w.Result()
248 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code")
249}
250
251func TestReadzWithServicesDefaultOne(t *testing.T) {
252 p := &Probe{}
253 p.RegisterService("one", "two")
254 p.UpdateStatus("one", ServiceStatusRunning)
255
256 req := httptest.NewRequest("GET", "http://example.com/readz", nil)
257 w := httptest.NewRecorder()
258 p.readzFunc(w, req)
259 resp := w.Result()
260 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code")
261}
262
263func TestHealthzNoServices(t *testing.T) {
264 p := (&Probe{}).WithReadyFunc(AlwaysTrue)
265 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
266 w := httptest.NewRecorder()
267 p.healthzFunc(w, req)
268 resp := w.Result()
269
270 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for no services")
271}
272
273func TestHealthzWithServicesWithTrue(t *testing.T) {
274 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
275 p.RegisterService("one", "two")
276
277 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
278 w := httptest.NewRecorder()
279 p.healthzFunc(w, req)
280 resp := w.Result()
281 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code for registered only services")
282}
283
284func TestHealthzWithServicesWithDefault(t *testing.T) {
285 p := &Probe{}
286 p.RegisterService("one", "two")
287
288 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
289 w := httptest.NewRecorder()
290 p.healthzFunc(w, req)
291 resp := w.Result()
292 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code for registered only services")
293}
294
295func TestHealthzNoServicesDefault(t *testing.T) {
296 p := &Probe{}
297
298 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
299 w := httptest.NewRecorder()
300 p.healthzFunc(w, req)
301 resp := w.Result()
302 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code")
303}
304
305func TestHealthzWithServicesDefault(t *testing.T) {
306 p := &Probe{}
307 p.RegisterService("one", "two")
308 p.UpdateStatus("one", ServiceStatusRunning)
309 p.UpdateStatus("two", ServiceStatusRunning)
310
311 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
312 w := httptest.NewRecorder()
313 p.healthzFunc(w, req)
314 resp := w.Result()
315 assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code")
316}
317
318func TestHealthzWithServicesDefaultFailed(t *testing.T) {
319 p := &Probe{}
320 p.RegisterService("one", "two")
321 p.UpdateStatus("one", ServiceStatusFailed)
322
323 req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
324 w := httptest.NewRecorder()
325 p.healthzFunc(w, req)
326 resp := w.Result()
327 assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code")
328}
329
330func TestSetFuncsToNil(t *testing.T) {
331 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
332 p.WithReadyFunc(nil).WithHealthFunc(nil)
333 assert.Nil(t, p.readyFunc, "ready func not reset to nil")
334 assert.Nil(t, p.healthFunc, "health func not reset to nil")
335}
336
Scott Baker104b67d2019-10-29 15:56:27 -0700337func TestGetProbeFromContext(t *testing.T) {
338 p := &Probe{}
339 p.RegisterService("one")
340 ctx := context.WithValue(context.Background(), ProbeContextKey, p)
341 pc := GetProbeFromContext(ctx)
342 assert.Equal(t, p, pc, "Probe from context was not identical to original probe")
343}
344
345func TestGetProbeFromContextMssing(t *testing.T) {
346 ctx := context.Background()
347 pc := GetProbeFromContext(ctx)
348 assert.Nil(t, pc, "Context had a non-nil probe when it should have been nil")
349}
350
Scott Baker2c1c4822019-10-16 11:02:41 -0700351func TestUpdateStatusFromContext(t *testing.T) {
352 p := &Probe{}
353 p.RegisterService("one")
354 ctx := context.WithValue(context.Background(), ProbeContextKey, p)
355 UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
356
357 assert.Equal(t, 1, len(p.status), "wrong number of services")
358 _, ok := p.status["one"]
359 assert.True(t, ok, "unable to find registered service")
360 assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set correctly from context")
Scott Baker2c1c4822019-10-16 11:02:41 -0700361}
362
363func TestUpdateStatusFromNilContext(t *testing.T) {
364 p := &Probe{}
365 p.RegisterService("one")
366 UpdateStatusFromContext(nil, "one", ServiceStatusRunning)
367
368 assert.Equal(t, 1, len(p.status), "wrong number of services")
369 _, ok := p.status["one"]
370 assert.True(t, ok, "unable to find registered service")
371 assert.Equal(t, ServiceStatusUnknown, p.status["one"], "status not set correctly from context")
372
373}
374
375func TestUpdateStatusFromContextWithoutProbe(t *testing.T) {
376 p := &Probe{}
377 p.RegisterService("one")
378 ctx := context.Background()
379 UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
380
381 assert.Equal(t, 1, len(p.status), "wrong number of services")
382 _, ok := p.status["one"]
383 assert.True(t, ok, "unable to find registered service")
384 assert.Equal(t, ServiceStatusUnknown, p.status["one"], "status not set correctly from context")
385
386}
387
388func TestUpdateStatusFromContextWrongType(t *testing.T) {
389 p := &Probe{}
390 p.RegisterService("one")
391 ctx := context.WithValue(context.Background(), ProbeContextKey, "Teapot")
392 UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
393
394 assert.Equal(t, 1, len(p.status), "wrong number of services")
395 _, ok := p.status["one"]
396 assert.True(t, ok, "unable to find registered service")
397 assert.Equal(t, ServiceStatusUnknown, p.status["one"], "status not set correctly from context")
398}
399
400func TestUpdateStatusNoRegistered(t *testing.T) {
401 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
402
403 p.UpdateStatus("one", ServiceStatusRunning)
404 assert.Equal(t, 1, len(p.status), "wrong number of services")
405 _, ok := p.status["one"]
406 assert.True(t, ok, "unable to find registered service")
407 assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set correctly from context")
408}
Scott Baker104b67d2019-10-29 15:56:27 -0700409
410func TestIsReadyTrue(t *testing.T) {
411 p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
412
413 p.RegisterService("SomeService")
414
415 assert.True(t, p.IsReady(), "IsReady should have been true")
416}
417
418func TestIsReadyFalse(t *testing.T) {
419 p := (&Probe{}).WithReadyFunc(AlwaysFalse).WithHealthFunc(AlwaysFalse)
420
421 p.RegisterService("SomeService")
422
423 assert.False(t, p.IsReady(), "IsReady should have been false")
424}
425
426func TestGetStatus(t *testing.T) {
427 p := &Probe{}
428
429 p.RegisterService("one", "two")
430 p.UpdateStatus("one", ServiceStatusRunning)
431
432 ss := p.GetStatus("one")
433 assert.Equal(t, ServiceStatusRunning, ss, "Service status should have been ServiceStatusRunning")
434}
435
436func TestGetStatusMissingService(t *testing.T) {
437 p := &Probe{}
438
439 p.RegisterService("one", "two")
440
441 ss := p.GetStatus("three")
442 assert.Equal(t, ServiceStatusUnknown, ss, "Service status should have been ServiceStatusUnknown")
443}