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