14285: Merge branch 'master' into 14285-keep-balance-metrics
[arvados.git] / sdk / go / health / aggregator_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package health
6
7 import (
8         "encoding/json"
9         "net/http"
10         "net/http/httptest"
11         "strings"
12         "time"
13
14         "git.curoverse.com/arvados.git/sdk/go/arvados"
15         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
16         "gopkg.in/check.v1"
17 )
18
19 type AggregatorSuite struct {
20         handler *Aggregator
21         req     *http.Request
22         resp    *httptest.ResponseRecorder
23 }
24
25 // Gocheck boilerplate
26 var _ = check.Suite(&AggregatorSuite{})
27
28 func (s *AggregatorSuite) TestInterface(c *check.C) {
29         var _ http.Handler = &Aggregator{}
30 }
31
32 func (s *AggregatorSuite) SetUpTest(c *check.C) {
33         s.handler = &Aggregator{Config: &arvados.Config{
34                 Clusters: map[string]arvados.Cluster{
35                         "zzzzz": {
36                                 ManagementToken: arvadostest.ManagementToken,
37                                 NodeProfiles:    map[string]arvados.NodeProfile{},
38                         },
39                 },
40         }}
41         s.req = httptest.NewRequest("GET", "/_health/all", nil)
42         s.req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
43         s.resp = httptest.NewRecorder()
44 }
45
46 func (s *AggregatorSuite) TestNoAuth(c *check.C) {
47         s.req.Header.Del("Authorization")
48         s.handler.ServeHTTP(s.resp, s.req)
49         s.checkError(c)
50         c.Check(s.resp.Code, check.Equals, http.StatusUnauthorized)
51 }
52
53 func (s *AggregatorSuite) TestBadAuth(c *check.C) {
54         s.req.Header.Set("Authorization", "xyzzy")
55         s.handler.ServeHTTP(s.resp, s.req)
56         s.checkError(c)
57         c.Check(s.resp.Code, check.Equals, http.StatusUnauthorized)
58 }
59
60 func (s *AggregatorSuite) TestEmptyConfig(c *check.C) {
61         s.handler.ServeHTTP(s.resp, s.req)
62         s.checkOK(c)
63 }
64
65 func (s *AggregatorSuite) stubServer(handler http.Handler) (*httptest.Server, string) {
66         srv := httptest.NewServer(handler)
67         var port string
68         if parts := strings.Split(srv.URL, ":"); len(parts) < 3 {
69                 panic(srv.URL)
70         } else {
71                 port = parts[len(parts)-1]
72         }
73         return srv, ":" + port
74 }
75
76 type unhealthyHandler struct{}
77
78 func (*unhealthyHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
79         if req.URL.Path == "/_health/ping" {
80                 resp.Write([]byte(`{"health":"ERROR","error":"the bends"}`))
81         } else {
82                 http.Error(resp, "not found", http.StatusNotFound)
83         }
84 }
85
86 func (s *AggregatorSuite) TestUnhealthy(c *check.C) {
87         srv, listen := s.stubServer(&unhealthyHandler{})
88         defer srv.Close()
89         s.handler.Config.Clusters["zzzzz"].NodeProfiles["localhost"] = arvados.NodeProfile{
90                 Keepstore: arvados.SystemServiceInstance{Listen: listen},
91         }
92         s.handler.ServeHTTP(s.resp, s.req)
93         s.checkUnhealthy(c)
94 }
95
96 type healthyHandler struct{}
97
98 func (*healthyHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
99         if req.URL.Path == "/_health/ping" {
100                 resp.Write([]byte(`{"health":"OK"}`))
101         } else {
102                 http.Error(resp, "not found", http.StatusNotFound)
103         }
104 }
105
106 func (s *AggregatorSuite) TestHealthy(c *check.C) {
107         srv, listen := s.stubServer(&healthyHandler{})
108         defer srv.Close()
109         s.handler.Config.Clusters["zzzzz"].NodeProfiles["localhost"] = arvados.NodeProfile{
110                 Controller:  arvados.SystemServiceInstance{Listen: listen},
111                 Keepbalance: arvados.SystemServiceInstance{Listen: listen},
112                 Keepproxy:   arvados.SystemServiceInstance{Listen: listen},
113                 Keepstore:   arvados.SystemServiceInstance{Listen: listen},
114                 Keepweb:     arvados.SystemServiceInstance{Listen: listen},
115                 Nodemanager: arvados.SystemServiceInstance{Listen: listen},
116                 RailsAPI:    arvados.SystemServiceInstance{Listen: listen},
117                 Websocket:   arvados.SystemServiceInstance{Listen: listen},
118                 Workbench:   arvados.SystemServiceInstance{Listen: listen},
119         }
120         s.handler.ServeHTTP(s.resp, s.req)
121         resp := s.checkOK(c)
122         svc := "keepstore+http://localhost" + listen + "/_health/ping"
123         c.Logf("%#v", resp)
124         ep := resp.Checks[svc]
125         c.Check(ep.Health, check.Equals, "OK")
126         c.Check(ep.HTTPStatusCode, check.Equals, 200)
127 }
128
129 func (s *AggregatorSuite) TestHealthyAndUnhealthy(c *check.C) {
130         srvH, listenH := s.stubServer(&healthyHandler{})
131         defer srvH.Close()
132         srvU, listenU := s.stubServer(&unhealthyHandler{})
133         defer srvU.Close()
134         s.handler.Config.Clusters["zzzzz"].NodeProfiles["localhost"] = arvados.NodeProfile{
135                 Controller:  arvados.SystemServiceInstance{Listen: listenH},
136                 Keepbalance: arvados.SystemServiceInstance{Listen: listenH},
137                 Keepproxy:   arvados.SystemServiceInstance{Listen: listenH},
138                 Keepstore:   arvados.SystemServiceInstance{Listen: listenH},
139                 Keepweb:     arvados.SystemServiceInstance{Listen: listenH},
140                 Nodemanager: arvados.SystemServiceInstance{Listen: listenH},
141                 RailsAPI:    arvados.SystemServiceInstance{Listen: listenH},
142                 Websocket:   arvados.SystemServiceInstance{Listen: listenH},
143                 Workbench:   arvados.SystemServiceInstance{Listen: listenH},
144         }
145         s.handler.Config.Clusters["zzzzz"].NodeProfiles["127.0.0.1"] = arvados.NodeProfile{
146                 Keepstore: arvados.SystemServiceInstance{Listen: listenU},
147         }
148         s.handler.ServeHTTP(s.resp, s.req)
149         resp := s.checkUnhealthy(c)
150         ep := resp.Checks["keepstore+http://localhost"+listenH+"/_health/ping"]
151         c.Check(ep.Health, check.Equals, "OK")
152         c.Check(ep.HTTPStatusCode, check.Equals, 200)
153         ep = resp.Checks["keepstore+http://127.0.0.1"+listenU+"/_health/ping"]
154         c.Check(ep.Health, check.Equals, "ERROR")
155         c.Check(ep.HTTPStatusCode, check.Equals, 200)
156         c.Logf("%#v", ep)
157 }
158
159 func (s *AggregatorSuite) checkError(c *check.C) {
160         c.Check(s.resp.Code, check.Not(check.Equals), http.StatusOK)
161         var resp ClusterHealthResponse
162         err := json.NewDecoder(s.resp.Body).Decode(&resp)
163         c.Check(err, check.IsNil)
164         c.Check(resp.Health, check.Not(check.Equals), "OK")
165 }
166
167 func (s *AggregatorSuite) checkUnhealthy(c *check.C) ClusterHealthResponse {
168         return s.checkResult(c, "ERROR")
169 }
170
171 func (s *AggregatorSuite) checkOK(c *check.C) ClusterHealthResponse {
172         return s.checkResult(c, "OK")
173 }
174
175 func (s *AggregatorSuite) checkResult(c *check.C, health string) ClusterHealthResponse {
176         c.Check(s.resp.Code, check.Equals, http.StatusOK)
177         var resp ClusterHealthResponse
178         err := json.NewDecoder(s.resp.Body).Decode(&resp)
179         c.Check(err, check.IsNil)
180         c.Check(resp.Health, check.Equals, health)
181         return resp
182 }
183
184 type slowHandler struct{}
185
186 func (*slowHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
187         if req.URL.Path == "/_health/ping" {
188                 time.Sleep(3 * time.Second)
189                 resp.Write([]byte(`{"health":"OK"}`))
190         } else {
191                 http.Error(resp, "not found", http.StatusNotFound)
192         }
193 }
194
195 func (s *AggregatorSuite) TestPingTimeout(c *check.C) {
196         s.handler.timeout = arvados.Duration(100 * time.Millisecond)
197         srv, listen := s.stubServer(&slowHandler{})
198         defer srv.Close()
199         s.handler.Config.Clusters["zzzzz"].NodeProfiles["localhost"] = arvados.NodeProfile{
200                 Keepstore: arvados.SystemServiceInstance{Listen: listen},
201         }
202         s.handler.ServeHTTP(s.resp, s.req)
203         resp := s.checkUnhealthy(c)
204         ep := resp.Checks["keepstore+http://localhost"+listen+"/_health/ping"]
205         c.Check(ep.Health, check.Equals, "ERROR")
206         c.Check(ep.HTTPStatusCode, check.Equals, 0)
207         rt, err := ep.ResponseTime.Float64()
208         c.Check(err, check.IsNil)
209         c.Check(rt > 0.005, check.Equals, true)
210 }