|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package probe |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "errors" |
| 23 | + "net/http" |
| 24 | + "net/http/httptest" |
| 25 | + "testing" |
| 26 | +) |
| 27 | + |
| 28 | +func TestHandlersSuccess(t *testing.T) { |
| 29 | + resetProbeState() |
| 30 | + |
| 31 | + RegisterLiveness("ok", func(context.Context) error { return nil }) |
| 32 | + RegisterReadiness("ok", func(context.Context) error { return nil }) |
| 33 | + RegisterStartup("ok", func(context.Context) error { return nil }) |
| 34 | + |
| 35 | + req := httptest.NewRequest(http.MethodGet, "/live", nil) |
| 36 | + rec := httptest.NewRecorder() |
| 37 | + livenessHandler(rec, req) |
| 38 | + if rec.Code != http.StatusOK { |
| 39 | + t.Fatalf("expected 200 for liveness, got %d", rec.Code) |
| 40 | + } |
| 41 | + |
| 42 | + req = httptest.NewRequest(http.MethodGet, "/ready", nil) |
| 43 | + rec = httptest.NewRecorder() |
| 44 | + readinessHandler(rec, req) |
| 45 | + if rec.Code != http.StatusOK { |
| 46 | + t.Fatalf("expected 200 for readiness, got %d", rec.Code) |
| 47 | + } |
| 48 | + |
| 49 | + req = httptest.NewRequest(http.MethodGet, "/startup", nil) |
| 50 | + rec = httptest.NewRecorder() |
| 51 | + startupHandler(rec, req) |
| 52 | + if rec.Code != http.StatusOK { |
| 53 | + t.Fatalf("expected 200 for startup, got %d", rec.Code) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestHandlersFailure(t *testing.T) { |
| 58 | + resetProbeState() |
| 59 | + |
| 60 | + RegisterLiveness("fail", func(context.Context) error { return errors.New("bad") }) |
| 61 | + RegisterReadiness("fail", func(context.Context) error { return errors.New("bad") }) |
| 62 | + RegisterStartup("fail", func(context.Context) error { return errors.New("bad") }) |
| 63 | + |
| 64 | + req := httptest.NewRequest(http.MethodGet, "/live", nil) |
| 65 | + rec := httptest.NewRecorder() |
| 66 | + livenessHandler(rec, req) |
| 67 | + if rec.Code != http.StatusServiceUnavailable { |
| 68 | + t.Fatalf("expected 503 for liveness, got %d", rec.Code) |
| 69 | + } |
| 70 | + |
| 71 | + req = httptest.NewRequest(http.MethodGet, "/ready", nil) |
| 72 | + rec = httptest.NewRecorder() |
| 73 | + readinessHandler(rec, req) |
| 74 | + if rec.Code != http.StatusServiceUnavailable { |
| 75 | + t.Fatalf("expected 503 for readiness, got %d", rec.Code) |
| 76 | + } |
| 77 | + |
| 78 | + req = httptest.NewRequest(http.MethodGet, "/startup", nil) |
| 79 | + rec = httptest.NewRecorder() |
| 80 | + startupHandler(rec, req) |
| 81 | + if rec.Code != http.StatusServiceUnavailable { |
| 82 | + t.Fatalf("expected 503 for startup, got %d", rec.Code) |
| 83 | + } |
| 84 | +} |
0 commit comments