1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
14 "git.curoverse.com/arvados.git/sdk/go/arvados"
15 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
19 type AggregatorSuite struct {
22 resp *httptest.ResponseRecorder
25 // Gocheck boilerplate
26 var _ = check.Suite(&AggregatorSuite{})
28 func (s *AggregatorSuite) TestInterface(c *check.C) {
29 var _ http.Handler = &Aggregator{}
32 func (s *AggregatorSuite) SetUpTest(c *check.C) {
33 s.handler = &Aggregator{Config: &arvados.Config{
34 Clusters: map[string]arvados.Cluster{
36 ManagementToken: arvadostest.ManagementToken,
37 NodeProfiles: map[string]arvados.NodeProfile{},
41 s.req = httptest.NewRequest("GET", "/_health/all", nil)
42 s.req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
43 s.resp = httptest.NewRecorder()
46 func (s *AggregatorSuite) TestNoAuth(c *check.C) {
47 s.req.Header.Del("Authorization")
48 s.handler.ServeHTTP(s.resp, s.req)
50 c.Check(s.resp.Code, check.Equals, http.StatusUnauthorized)
53 func (s *AggregatorSuite) TestBadAuth(c *check.C) {
54 s.req.Header.Set("Authorization", "xyzzy")
55 s.handler.ServeHTTP(s.resp, s.req)
57 c.Check(s.resp.Code, check.Equals, http.StatusUnauthorized)
60 func (s *AggregatorSuite) TestEmptyConfig(c *check.C) {
61 s.handler.ServeHTTP(s.resp, s.req)
65 func (s *AggregatorSuite) stubServer(handler http.Handler) (*httptest.Server, string) {
66 srv := httptest.NewServer(handler)
68 if parts := strings.Split(srv.URL, ":"); len(parts) < 3 {
71 port = parts[len(parts)-1]
73 return srv, ":" + port
76 type unhealthyHandler struct{}
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"}`))
82 http.Error(resp, "not found", http.StatusNotFound)
86 func (s *AggregatorSuite) TestUnhealthy(c *check.C) {
87 srv, listen := s.stubServer(&unhealthyHandler{})
89 s.handler.Config.Clusters["zzzzz"].NodeProfiles["localhost"] = arvados.NodeProfile{
90 Keepstore: arvados.SystemServiceInstance{Listen: listen},
92 s.handler.ServeHTTP(s.resp, s.req)
96 type healthyHandler struct{}
98 func (*healthyHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
99 if req.URL.Path == "/_health/ping" {
100 resp.Write([]byte(`{"health":"OK"}`))
102 http.Error(resp, "not found", http.StatusNotFound)
106 func (s *AggregatorSuite) TestHealthy(c *check.C) {
107 srv, listen := s.stubServer(&healthyHandler{})
109 s.handler.Config.Clusters["zzzzz"].NodeProfiles["localhost"] = arvados.NodeProfile{
110 Controller: arvados.SystemServiceInstance{Listen: listen},
111 Keepproxy: arvados.SystemServiceInstance{Listen: listen},
112 Keepstore: arvados.SystemServiceInstance{Listen: listen},
113 Keepweb: arvados.SystemServiceInstance{Listen: listen},
114 Nodemanager: arvados.SystemServiceInstance{Listen: listen},
115 RailsAPI: arvados.SystemServiceInstance{Listen: listen},
116 Websocket: arvados.SystemServiceInstance{Listen: listen},
117 Workbench: arvados.SystemServiceInstance{Listen: listen},
119 s.handler.ServeHTTP(s.resp, s.req)
121 svc := "keepstore+http://localhost" + listen + "/_health/ping"
123 ep := resp.Checks[svc]
124 c.Check(ep.Health, check.Equals, "OK")
125 c.Check(ep.HTTPStatusCode, check.Equals, 200)
128 func (s *AggregatorSuite) TestHealthyAndUnhealthy(c *check.C) {
129 srvH, listenH := s.stubServer(&healthyHandler{})
131 srvU, listenU := s.stubServer(&unhealthyHandler{})
133 s.handler.Config.Clusters["zzzzz"].NodeProfiles["localhost"] = arvados.NodeProfile{
134 Controller: arvados.SystemServiceInstance{Listen: listenH},
135 Keepproxy: arvados.SystemServiceInstance{Listen: listenH},
136 Keepstore: arvados.SystemServiceInstance{Listen: listenH},
137 Keepweb: arvados.SystemServiceInstance{Listen: listenH},
138 Nodemanager: arvados.SystemServiceInstance{Listen: listenH},
139 RailsAPI: arvados.SystemServiceInstance{Listen: listenH},
140 Websocket: arvados.SystemServiceInstance{Listen: listenH},
141 Workbench: arvados.SystemServiceInstance{Listen: listenH},
143 s.handler.Config.Clusters["zzzzz"].NodeProfiles["127.0.0.1"] = arvados.NodeProfile{
144 Keepstore: arvados.SystemServiceInstance{Listen: listenU},
146 s.handler.ServeHTTP(s.resp, s.req)
147 resp := s.checkUnhealthy(c)
148 ep := resp.Checks["keepstore+http://localhost"+listenH+"/_health/ping"]
149 c.Check(ep.Health, check.Equals, "OK")
150 c.Check(ep.HTTPStatusCode, check.Equals, 200)
151 ep = resp.Checks["keepstore+http://127.0.0.1"+listenU+"/_health/ping"]
152 c.Check(ep.Health, check.Equals, "ERROR")
153 c.Check(ep.HTTPStatusCode, check.Equals, 200)
157 func (s *AggregatorSuite) checkError(c *check.C) {
158 c.Check(s.resp.Code, check.Not(check.Equals), http.StatusOK)
159 var resp ClusterHealthResponse
160 err := json.NewDecoder(s.resp.Body).Decode(&resp)
161 c.Check(err, check.IsNil)
162 c.Check(resp.Health, check.Not(check.Equals), "OK")
165 func (s *AggregatorSuite) checkUnhealthy(c *check.C) ClusterHealthResponse {
166 return s.checkResult(c, "ERROR")
169 func (s *AggregatorSuite) checkOK(c *check.C) ClusterHealthResponse {
170 return s.checkResult(c, "OK")
173 func (s *AggregatorSuite) checkResult(c *check.C, health string) ClusterHealthResponse {
174 c.Check(s.resp.Code, check.Equals, http.StatusOK)
175 var resp ClusterHealthResponse
176 err := json.NewDecoder(s.resp.Body).Decode(&resp)
177 c.Check(err, check.IsNil)
178 c.Check(resp.Health, check.Equals, health)
182 type slowHandler struct{}
184 func (*slowHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
185 if req.URL.Path == "/_health/ping" {
186 time.Sleep(3 * time.Second)
187 resp.Write([]byte(`{"health":"OK"}`))
189 http.Error(resp, "not found", http.StatusNotFound)
193 func (s *AggregatorSuite) TestPingTimeout(c *check.C) {
194 s.handler.timeout = arvados.Duration(100 * time.Millisecond)
195 srv, listen := s.stubServer(&slowHandler{})
197 s.handler.Config.Clusters["zzzzz"].NodeProfiles["localhost"] = arvados.NodeProfile{
198 Keepstore: arvados.SystemServiceInstance{Listen: listen},
200 s.handler.ServeHTTP(s.resp, s.req)
201 resp := s.checkUnhealthy(c)
202 ep := resp.Checks["keepstore+http://localhost"+listen+"/_health/ping"]
203 c.Check(ep.Health, check.Equals, "ERROR")
204 c.Check(ep.HTTPStatusCode, check.Equals, 0)
205 rt, err := ep.ResponseTime.Float64()
206 c.Check(err, check.IsNil)
207 c.Check(rt > 0.005, check.Equals, true)