|
| 1 | +/* |
| 2 | + * Copyright 2018 The Trickster Authors |
| 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 | + |
| 17 | +package health |
| 18 | + |
| 19 | +import ( |
| 20 | + "maps" |
| 21 | + "net/http" |
| 22 | + "net/http/httptest" |
| 23 | + "net/url" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + "github.com/trickstercache/trickster/v2/pkg/backends" |
| 29 | + "github.com/trickstercache/trickster/v2/pkg/backends/healthcheck" |
| 30 | + ho "github.com/trickstercache/trickster/v2/pkg/backends/healthcheck/options" |
| 31 | + bo "github.com/trickstercache/trickster/v2/pkg/backends/options" |
| 32 | + "github.com/trickstercache/trickster/v2/pkg/cache" |
| 33 | + "github.com/trickstercache/trickster/v2/pkg/proxy/handlers" |
| 34 | + po "github.com/trickstercache/trickster/v2/pkg/proxy/paths/options" |
| 35 | +) |
| 36 | + |
| 37 | +var _ healthcheck.HealthChecker = (*mockHealthChecker)(nil) |
| 38 | + |
| 39 | +type mockTarget struct { |
| 40 | + description string |
| 41 | + options *ho.Options |
| 42 | + client *http.Client |
| 43 | + status *healthcheck.Status |
| 44 | +} |
| 45 | + |
| 46 | +type mockHealthChecker struct { |
| 47 | + // map of registered targets, with configurable status |
| 48 | + targets map[string]mockTarget |
| 49 | + // if configured, return status for all targets |
| 50 | + globalStatus *healthcheck.Status |
| 51 | + // list of subscribers to notify of activity |
| 52 | + subscribers []chan bool |
| 53 | +} |
| 54 | + |
| 55 | +func newMockHealthChecker() *mockHealthChecker { |
| 56 | + return &mockHealthChecker{ |
| 57 | + targets: make(map[string]mockTarget), |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func (m *mockHealthChecker) Register(name string, description string, options *ho.Options, client *http.Client) (*healthcheck.Status, error) { |
| 62 | + target := mockTarget{ |
| 63 | + description: description, |
| 64 | + options: options, |
| 65 | + client: client, |
| 66 | + } |
| 67 | + if m.globalStatus != nil { |
| 68 | + target.status = m.globalStatus |
| 69 | + } else { |
| 70 | + target.status = healthcheck.NewStatus(name, description, "initializing", 0, time.Time{}, nil) |
| 71 | + } |
| 72 | + m.targets[name] = target |
| 73 | + return &healthcheck.Status{}, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (m *mockHealthChecker) Unregister(name string) { |
| 77 | + delete(m.targets, name) |
| 78 | +} |
| 79 | + |
| 80 | +func (m *mockHealthChecker) Status(name string) *healthcheck.Status { |
| 81 | + if m.globalStatus != nil { |
| 82 | + return m.globalStatus |
| 83 | + } |
| 84 | + |
| 85 | + if t, ok := m.targets[name]; ok { |
| 86 | + return t.status |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func (m *mockHealthChecker) Subscribe(ch chan bool) { |
| 92 | + m.subscribers = append(m.subscribers, ch) |
| 93 | +} |
| 94 | + |
| 95 | +// no-op for mock |
| 96 | +func (m *mockHealthChecker) Statuses() healthcheck.StatusLookup { |
| 97 | + lookup := make(healthcheck.StatusLookup, len(m.targets)) |
| 98 | + for name, t := range m.targets { |
| 99 | + status := t.status |
| 100 | + if t.status == nil { |
| 101 | + if m.globalStatus == nil { |
| 102 | + continue // no status or global status, skip |
| 103 | + } |
| 104 | + t.status = m.globalStatus |
| 105 | + } |
| 106 | + lookup[name] = status |
| 107 | + } |
| 108 | + return lookup |
| 109 | +} |
| 110 | + |
| 111 | +func (m *mockHealthChecker) notify(status bool) { |
| 112 | + for _, ch := range m.subscribers { |
| 113 | + ch <- status |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func (m *mockHealthChecker) Shutdown() { |
| 118 | + m.notify(true) |
| 119 | + // no-op for mock, no background processes to stop |
| 120 | +} |
| 121 | + |
| 122 | +type mockBackend struct { |
| 123 | + name string |
| 124 | + handlers handlers.Lookup |
| 125 | +} |
| 126 | + |
| 127 | +func (m *mockBackend) RegisterHandlers(hl handlers.Lookup) { |
| 128 | + maps.Copy(m.handlers, hl) |
| 129 | +} |
| 130 | + |
| 131 | +func (m *mockBackend) Handlers() handlers.Lookup { |
| 132 | + return m.handlers |
| 133 | +} |
| 134 | + |
| 135 | +func (m *mockBackend) DefaultPathConfigs(*bo.Options) po.List { |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func (m *mockBackend) Configuration() *bo.Options { |
| 140 | + return nil |
| 141 | +} |
| 142 | +func (m *mockBackend) Name() string { |
| 143 | + return m.name |
| 144 | +} |
| 145 | +func (m *mockBackend) HTTPClient() *http.Client { |
| 146 | + return nil |
| 147 | +} |
| 148 | +func (m *mockBackend) SetCache(cache.Cache) {} |
| 149 | +func (m *mockBackend) Router() http.Handler { |
| 150 | + return nil |
| 151 | +} |
| 152 | +func (m *mockBackend) Cache() cache.Cache { |
| 153 | + return nil |
| 154 | +} |
| 155 | +func (m *mockBackend) BaseUpstreamURL() *url.URL { |
| 156 | + return nil |
| 157 | +} |
| 158 | +func (m *mockBackend) SetHealthCheckProbe(healthcheck.DemandProbe) {} |
| 159 | +func (m *mockBackend) HealthHandler(http.ResponseWriter, *http.Request) {} |
| 160 | +func (m *mockBackend) DefaultHealthCheckConfig() *ho.Options { |
| 161 | + return nil |
| 162 | +} |
| 163 | +func (m *mockBackend) HealthCheckHTTPClient() *http.Client { |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func TestStatusHandler(t *testing.T) { |
| 168 | + const ( |
| 169 | + name = "mock-backend" |
| 170 | + ) |
| 171 | + |
| 172 | + hc := newMockHealthChecker() |
| 173 | + hc.globalStatus = healthcheck.NewStatus(name, "mock health checker", "mock detail", 1, time.Time{}, nil) |
| 174 | + backends := make(backends.Backends) |
| 175 | + backends[name] = &mockBackend{ |
| 176 | + name: name, |
| 177 | + } |
| 178 | + status, err := hc.Register(name, "test backend", &ho.Options{}, &http.Client{}) |
| 179 | + require.NoError(t, err) |
| 180 | + require.NotNil(t, status) |
| 181 | + |
| 182 | + sh := StatusHandler(func() time.Time { |
| 183 | + return time.Time{} |
| 184 | + }, hc, backends) |
| 185 | + require.NotNil(t, sh) |
| 186 | + |
| 187 | + req, err := http.NewRequest("GET", "/", nil) |
| 188 | + req.Header.Set("Accept", "application/json") |
| 189 | + require.NoError(t, err) |
| 190 | + |
| 191 | + w := httptest.NewRecorder() |
| 192 | + |
| 193 | + sh.ServeHTTP(w, req) |
| 194 | + require.Equal(t, http.StatusOK, w.Result().StatusCode) |
| 195 | + expected := `{"title":"Trickster Backend Health Status","updateTime":"0001-01-01 00:00:00 UTC","available":[{"name":"mock-backend","provider":"mock health checker"}]}` |
| 196 | + require.Equal(t, expected, w.Body.String()) |
| 197 | +} |
0 commit comments