David K. Bainbridge | e4572ee | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package probe |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "encoding/json" |
| 21 | "github.com/opencord/voltha-go/common/log" |
| 22 | "github.com/stretchr/testify/assert" |
| 23 | "io/ioutil" |
| 24 | "net/http" |
| 25 | "net/http/httptest" |
| 26 | "testing" |
| 27 | ) |
| 28 | |
| 29 | func init() { |
| 30 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 31 | } |
| 32 | |
| 33 | func 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 | |
| 42 | func AlwaysTrue(map[string]ServiceStatus) bool { |
| 43 | return true |
| 44 | } |
| 45 | |
| 46 | func AlwaysFalse(map[string]ServiceStatus) bool { |
| 47 | return false |
| 48 | } |
| 49 | |
| 50 | func 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 | |
| 59 | func 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 | |
| 67 | func 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 | |
| 75 | func 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 | |
| 86 | func 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 | |
| 103 | func 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 | |
| 122 | func 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 | |
| 135 | func 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 | |
| 150 | func 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 | |
| 160 | func 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 | |
| 176 | func 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 | |
| 195 | func 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 | |
| 205 | func TestReadzWithServices(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.StatusTeapot, resp.StatusCode, "invalid status code for no services") |
| 214 | } |
| 215 | |
| 216 | func TestReadzNpServicesDefault(t *testing.T) { |
| 217 | p := &Probe{} |
| 218 | |
| 219 | req := httptest.NewRequest("GET", "http://example.com/readz", nil) |
| 220 | w := httptest.NewRecorder() |
| 221 | p.readzFunc(w, req) |
| 222 | resp := w.Result() |
| 223 | assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code") |
| 224 | } |
| 225 | |
| 226 | func TestReadzWithServicesDefault(t *testing.T) { |
| 227 | p := &Probe{} |
| 228 | p.RegisterService("one", "two") |
| 229 | p.UpdateStatus("one", ServiceStatusRunning) |
| 230 | p.UpdateStatus("two", ServiceStatusRunning) |
| 231 | |
| 232 | req := httptest.NewRequest("GET", "http://example.com/readz", nil) |
| 233 | w := httptest.NewRecorder() |
| 234 | p.readzFunc(w, req) |
| 235 | resp := w.Result() |
| 236 | assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code") |
| 237 | } |
| 238 | |
| 239 | func TestReadzWithServicesDefaultOne(t *testing.T) { |
| 240 | p := &Probe{} |
| 241 | p.RegisterService("one", "two") |
| 242 | p.UpdateStatus("one", 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.StatusTeapot, resp.StatusCode, "invalid status code") |
| 249 | } |
| 250 | |
| 251 | func TestHealthzNoServices(t *testing.T) { |
| 252 | p := (&Probe{}).WithReadyFunc(AlwaysTrue) |
| 253 | req := httptest.NewRequest("GET", "http://example.com/healthz", nil) |
| 254 | w := httptest.NewRecorder() |
| 255 | p.healthzFunc(w, req) |
| 256 | resp := w.Result() |
| 257 | |
| 258 | assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for no services") |
| 259 | } |
| 260 | |
| 261 | func TestHealthzWithServices(t *testing.T) { |
| 262 | p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue) |
| 263 | p.RegisterService("one", "two") |
| 264 | |
| 265 | req := httptest.NewRequest("GET", "http://example.com/healthz", nil) |
| 266 | w := httptest.NewRecorder() |
| 267 | p.healthzFunc(w, req) |
| 268 | resp := w.Result() |
| 269 | assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for no services") |
| 270 | } |
| 271 | |
| 272 | func TestHealthzNoServicesDefault(t *testing.T) { |
| 273 | p := &Probe{} |
| 274 | |
| 275 | req := httptest.NewRequest("GET", "http://example.com/healthz", nil) |
| 276 | w := httptest.NewRecorder() |
| 277 | p.healthzFunc(w, req) |
| 278 | resp := w.Result() |
| 279 | assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code") |
| 280 | } |
| 281 | |
| 282 | func TestHealthzWithServicesDefault(t *testing.T) { |
| 283 | p := &Probe{} |
| 284 | p.RegisterService("one", "two") |
| 285 | p.UpdateStatus("one", ServiceStatusRunning) |
| 286 | p.UpdateStatus("two", ServiceStatusRunning) |
| 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") |
| 293 | } |
| 294 | |
| 295 | func TestHealthzWithServicesDefaultFailed(t *testing.T) { |
| 296 | p := &Probe{} |
| 297 | p.RegisterService("one", "two") |
| 298 | p.UpdateStatus("one", ServiceStatusFailed) |
| 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 | |
| 307 | func TestSetFuncsToNil(t *testing.T) { |
| 308 | p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse) |
| 309 | p.WithReadyFunc(nil).WithHealthFunc(nil) |
| 310 | assert.Nil(t, p.readyFunc, "ready func not reset to nil") |
| 311 | assert.Nil(t, p.healthFunc, "health func not reset to nil") |
| 312 | } |
| 313 | |
| 314 | func TestUpdateStatusFromContext(t *testing.T) { |
| 315 | p := &Probe{} |
| 316 | p.RegisterService("one") |
| 317 | ctx := context.WithValue(context.Background(), ProbeContextKey, p) |
| 318 | UpdateStatusFromContext(ctx, "one", ServiceStatusRunning) |
| 319 | |
| 320 | assert.Equal(t, 1, len(p.status), "wrong number of services") |
| 321 | _, ok := p.status["one"] |
| 322 | assert.True(t, ok, "unable to find registered service") |
| 323 | assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set correctly from context") |
| 324 | |
| 325 | } |
| 326 | |
| 327 | func TestUpdateStatusFromNilContext(t *testing.T) { |
| 328 | p := &Probe{} |
| 329 | p.RegisterService("one") |
| 330 | UpdateStatusFromContext(nil, "one", ServiceStatusRunning) |
| 331 | |
| 332 | assert.Equal(t, 1, len(p.status), "wrong number of services") |
| 333 | _, ok := p.status["one"] |
| 334 | assert.True(t, ok, "unable to find registered service") |
| 335 | assert.Equal(t, ServiceStatusUnknown, p.status["one"], "status not set correctly from context") |
| 336 | |
| 337 | } |
| 338 | |
| 339 | func TestUpdateStatusFromContextWithoutProbe(t *testing.T) { |
| 340 | p := &Probe{} |
| 341 | p.RegisterService("one") |
| 342 | ctx := context.Background() |
| 343 | UpdateStatusFromContext(ctx, "one", ServiceStatusRunning) |
| 344 | |
| 345 | assert.Equal(t, 1, len(p.status), "wrong number of services") |
| 346 | _, ok := p.status["one"] |
| 347 | assert.True(t, ok, "unable to find registered service") |
| 348 | assert.Equal(t, ServiceStatusUnknown, p.status["one"], "status not set correctly from context") |
| 349 | |
| 350 | } |
| 351 | |
| 352 | func TestUpdateStatusFromContextWrongType(t *testing.T) { |
| 353 | p := &Probe{} |
| 354 | p.RegisterService("one") |
| 355 | ctx := context.WithValue(context.Background(), ProbeContextKey, "Teapot") |
| 356 | UpdateStatusFromContext(ctx, "one", ServiceStatusRunning) |
| 357 | |
| 358 | assert.Equal(t, 1, len(p.status), "wrong number of services") |
| 359 | _, ok := p.status["one"] |
| 360 | assert.True(t, ok, "unable to find registered service") |
| 361 | assert.Equal(t, ServiceStatusUnknown, p.status["one"], "status not set correctly from context") |
| 362 | } |
| 363 | |
| 364 | func TestUpdateStatusNoRegistered(t *testing.T) { |
| 365 | p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse) |
| 366 | |
| 367 | p.UpdateStatus("one", ServiceStatusRunning) |
| 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, ServiceStatusRunning, p.status["one"], "status not set correctly from context") |
| 372 | } |