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