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