| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package health |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"net/http" |
| 20 |
"net/http/httptest" |
| 21 |
"sync" |
| 22 |
"testing" |
| 23 |
) |
| 24 |
|
| 25 |
func TestNewHandler(t *testing.T) { |
| 26 |
s := httptest.NewServer(new(Handler)) |
| 27 |
defer s.Close() |
| 28 |
code, err := check(s) |
| 29 |
if err != nil { |
| 30 |
t.Fatalf("GET %s: %v", s.URL, err) |
| 31 |
} |
| 32 |
if code != http.StatusOK { |
| 33 |
t.Errorf("got HTTP status %d; want %d", code, http.StatusOK) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func TestChecker(t *testing.T) { |
| 38 |
c1 := &checker{err: errors.New("checker 1 down")} |
| 39 |
c2 := &checker{err: errors.New("checker 2 down")} |
| 40 |
h := new(Handler) |
| 41 |
h.Add(c1) |
| 42 |
h.Add(c2) |
| 43 |
s := httptest.NewServer(h) |
| 44 |
defer s.Close() |
| 45 |
|
| 46 |
t.Run("AllUnhealthy", func(t *testing.T) { |
| 47 |
code, err := check(s) |
| 48 |
if err != nil { |
| 49 |
t.Fatalf("GET %s: %v", s.URL, err) |
| 50 |
} |
| 51 |
if code != http.StatusInternalServerError { |
| 52 |
t.Errorf("got HTTP status %d; want %d", code, http.StatusInternalServerError) |
| 53 |
} |
| 54 |
}) |
| 55 |
c1.set(nil) |
| 56 |
t.Run("PartialHealthy", func(t *testing.T) { |
| 57 |
code, err := check(s) |
| 58 |
if err != nil { |
| 59 |
t.Fatalf("GET %s: %v", s.URL, err) |
| 60 |
} |
| 61 |
if code != http.StatusInternalServerError { |
| 62 |
t.Errorf("got HTTP status %d; want %d", code, http.StatusInternalServerError) |
| 63 |
} |
| 64 |
}) |
| 65 |
c2.set(nil) |
| 66 |
t.Run("AllHealthy", func(t *testing.T) { |
| 67 |
code, err := check(s) |
| 68 |
if err != nil { |
| 69 |
t.Fatalf("GET %s: %v", s.URL, err) |
| 70 |
} |
| 71 |
if code != http.StatusOK { |
| 72 |
t.Errorf("got HTTP status %d; want %d", code, http.StatusOK) |
| 73 |
} |
| 74 |
}) |
| 75 |
} |
| 76 |
|
| 77 |
func check(s *httptest.Server) (code int, err error) { |
| 78 |
resp, err := http.Get(s.URL) |
| 79 |
if err != nil { |
| 80 |
return 0, err |
| 81 |
} |
| 82 |
resp.Body.Close() |
| 83 |
return resp.StatusCode, nil |
| 84 |
} |
| 85 |
|
| 86 |
type checker struct { |
| 87 |
mu sync.Mutex |
| 88 |
err error |
| 89 |
} |
| 90 |
|
| 91 |
func (c *checker) CheckHealth() error { |
| 92 |
defer c.mu.Unlock() |
| 93 |
c.mu.Lock() |
| 94 |
return c.err |
| 95 |
} |
| 96 |
|
| 97 |
func (c *checker) set(e error) { |
| 98 |
defer c.mu.Unlock() |
| 99 |
c.mu.Lock() |
| 100 |
c.err = e |
| 101 |
} |