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 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},
120 s.handler.ServeHTTP(s.resp, s.req)
122 svc := "keepstore+http://localhost" + listen + "/_health/ping"
124 ep := resp.Checks[svc]
125 c.Check(ep.Health, check.Equals, "OK")
126 c.Check(ep.HTTPStatusCode, check.Equals, 200)
129 func (s *AggregatorSuite) TestHealthyAndUnhealthy(c *check.C) {
130 srvH, listenH := s.stubServer(&healthyHandler{})
132 srvU, listenU := s.stubServer(&unhealthyHandler{})
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},
145 s.handler.Config.Clusters["zzzzz"].NodeProfiles["127.0.0.1"] = arvados.NodeProfile{
146 Keepstore: arvados.SystemServiceInstance{Listen: listenU},
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)
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")
167 func (s *AggregatorSuite) checkUnhealthy(c *check.C) ClusterHealthResponse {
168 return s.checkResult(c, "ERROR")
171 func (s *AggregatorSuite) checkOK(c *check.C) ClusterHealthResponse {
172 return s.checkResult(c, "OK")
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)
184 type slowHandler struct{}
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"}`))
191 http.Error(resp, "not found", http.StatusNotFound)
195 func (s *AggregatorSuite) TestPingTimeout(c *check.C) {
196 s.handler.timeout = arvados.Duration(100 * time.Millisecond)
197 srv, listen := s.stubServer(&slowHandler{})
199 s.handler.Config.Clusters["zzzzz"].NodeProfiles["localhost"] = arvados.NodeProfile{
200 Keepstore: arvados.SystemServiceInstance{Listen: listen},
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)