1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
14 "git.arvados.org/arvados.git/sdk/go/arvados"
15 "git.arvados.org/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{Cluster: &arvados.Cluster{
34 ManagementToken: arvadostest.ManagementToken,
36 s.req = httptest.NewRequest("GET", "/_health/all", nil)
37 s.req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
38 s.resp = httptest.NewRecorder()
41 func (s *AggregatorSuite) TestNoAuth(c *check.C) {
42 s.req.Header.Del("Authorization")
43 s.handler.ServeHTTP(s.resp, s.req)
45 c.Check(s.resp.Code, check.Equals, http.StatusUnauthorized)
48 func (s *AggregatorSuite) TestBadAuth(c *check.C) {
49 s.req.Header.Set("Authorization", "xyzzy")
50 s.handler.ServeHTTP(s.resp, s.req)
52 c.Check(s.resp.Code, check.Equals, http.StatusUnauthorized)
55 func (s *AggregatorSuite) TestNoServicesConfigured(c *check.C) {
56 s.handler.ServeHTTP(s.resp, s.req)
60 func (s *AggregatorSuite) stubServer(handler http.Handler) (*httptest.Server, string) {
61 srv := httptest.NewServer(handler)
63 if parts := strings.Split(srv.URL, ":"); len(parts) < 3 {
66 port = parts[len(parts)-1]
68 return srv, ":" + port
71 func (s *AggregatorSuite) TestUnhealthy(c *check.C) {
72 srv, listen := s.stubServer(&unhealthyHandler{})
74 arvadostest.SetServiceURL(&s.handler.Cluster.Services.Keepstore, "http://localhost"+listen+"/")
75 s.handler.ServeHTTP(s.resp, s.req)
79 func (s *AggregatorSuite) TestHealthy(c *check.C) {
80 srv, listen := s.stubServer(&healthyHandler{})
82 s.setAllServiceURLs(listen)
83 s.handler.ServeHTTP(s.resp, s.req)
85 svc := "keepstore+http://localhost" + listen + "/_health/ping"
87 ep := resp.Checks[svc]
88 c.Check(ep.Health, check.Equals, "OK")
89 c.Check(ep.HTTPStatusCode, check.Equals, 200)
92 func (s *AggregatorSuite) TestHealthyAndUnhealthy(c *check.C) {
93 srvH, listenH := s.stubServer(&healthyHandler{})
95 srvU, listenU := s.stubServer(&unhealthyHandler{})
97 s.setAllServiceURLs(listenH)
98 arvadostest.SetServiceURL(&s.handler.Cluster.Services.Keepstore, "http://localhost"+listenH+"/", "http://127.0.0.1"+listenU+"/")
99 s.handler.ServeHTTP(s.resp, s.req)
100 resp := s.checkUnhealthy(c)
101 ep := resp.Checks["keepstore+http://localhost"+listenH+"/_health/ping"]
102 c.Check(ep.Health, check.Equals, "OK")
103 c.Check(ep.HTTPStatusCode, check.Equals, 200)
104 ep = resp.Checks["keepstore+http://127.0.0.1"+listenU+"/_health/ping"]
105 c.Check(ep.Health, check.Equals, "ERROR")
106 c.Check(ep.HTTPStatusCode, check.Equals, 200)
110 func (s *AggregatorSuite) TestPingTimeout(c *check.C) {
111 s.handler.timeout = arvados.Duration(100 * time.Millisecond)
112 srv, listen := s.stubServer(&slowHandler{})
114 arvadostest.SetServiceURL(&s.handler.Cluster.Services.Keepstore, "http://localhost"+listen+"/")
115 s.handler.ServeHTTP(s.resp, s.req)
116 resp := s.checkUnhealthy(c)
117 ep := resp.Checks["keepstore+http://localhost"+listen+"/_health/ping"]
118 c.Check(ep.Health, check.Equals, "ERROR")
119 c.Check(ep.HTTPStatusCode, check.Equals, 0)
120 rt, err := ep.ResponseTime.Float64()
121 c.Check(err, check.IsNil)
122 c.Check(rt > 0.005, check.Equals, true)
125 func (s *AggregatorSuite) checkError(c *check.C) {
126 c.Check(s.resp.Code, check.Not(check.Equals), http.StatusOK)
127 var resp ClusterHealthResponse
128 err := json.Unmarshal(s.resp.Body.Bytes(), &resp)
129 c.Check(err, check.IsNil)
130 c.Check(resp.Health, check.Not(check.Equals), "OK")
133 func (s *AggregatorSuite) checkUnhealthy(c *check.C) ClusterHealthResponse {
134 return s.checkResult(c, "ERROR")
137 func (s *AggregatorSuite) checkOK(c *check.C) ClusterHealthResponse {
138 return s.checkResult(c, "OK")
141 func (s *AggregatorSuite) checkResult(c *check.C, health string) ClusterHealthResponse {
142 c.Check(s.resp.Code, check.Equals, http.StatusOK)
143 var resp ClusterHealthResponse
144 c.Log(s.resp.Body.String())
145 err := json.Unmarshal(s.resp.Body.Bytes(), &resp)
146 c.Check(err, check.IsNil)
147 c.Check(resp.Health, check.Equals, health)
151 func (s *AggregatorSuite) setAllServiceURLs(listen string) {
152 svcs := &s.handler.Cluster.Services
153 for _, svc := range []*arvados.Service{
166 arvadostest.SetServiceURL(svc, "http://localhost"+listen+"/")
170 type unhealthyHandler struct{}
172 func (*unhealthyHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
173 if req.URL.Path == "/_health/ping" {
174 resp.Write([]byte(`{"health":"ERROR","error":"the bends"}`))
176 http.Error(resp, "not found", http.StatusNotFound)
180 type healthyHandler struct{}
182 func (*healthyHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
183 if req.URL.Path == "/_health/ping" {
184 resp.Write([]byte(`{"health":"OK"}`))
186 http.Error(resp, "not found", http.StatusNotFound)
190 type slowHandler struct{}
192 func (*slowHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
193 if req.URL.Path == "/_health/ping" {
194 time.Sleep(3 * time.Second)
195 resp.Write([]byte(`{"health":"OK"}`))
197 http.Error(resp, "not found", http.StatusNotFound)