10 "git.curoverse.com/arvados.git/sdk/go/arvados"
11 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15 type AggregatorSuite struct {
18 resp *httptest.ResponseRecorder
21 // Gocheck boilerplate
22 var _ = check.Suite(&AggregatorSuite{})
24 func (s *AggregatorSuite) TestInterface(c *check.C) {
25 var _ http.Handler = &Aggregator{}
28 func (s *AggregatorSuite) SetUpTest(c *check.C) {
29 s.handler = &Aggregator{Config: &arvados.Config{
30 Clusters: map[string]arvados.Cluster{
32 ManagementToken: arvadostest.ManagementToken,
33 SystemNodes: map[string]arvados.SystemNode{},
37 s.req = httptest.NewRequest("GET", "/_health/all", nil)
38 s.req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
39 s.resp = httptest.NewRecorder()
42 func (s *AggregatorSuite) TestNoAuth(c *check.C) {
43 s.req.Header.Del("Authorization")
44 s.handler.ServeHTTP(s.resp, s.req)
46 c.Check(s.resp.Code, check.Equals, http.StatusUnauthorized)
49 func (s *AggregatorSuite) TestBadAuth(c *check.C) {
50 s.req.Header.Set("Authorization", "xyzzy")
51 s.handler.ServeHTTP(s.resp, s.req)
53 c.Check(s.resp.Code, check.Equals, http.StatusUnauthorized)
56 func (s *AggregatorSuite) TestEmptyConfig(c *check.C) {
57 s.handler.ServeHTTP(s.resp, s.req)
61 func (s *AggregatorSuite) stubServer(handler http.Handler) (*httptest.Server, string) {
62 srv := httptest.NewServer(handler)
64 if parts := strings.Split(srv.URL, ":"); len(parts) < 3 {
67 port = parts[len(parts)-1]
69 return srv, ":" + port
72 type unhealthyHandler struct{}
74 func (*unhealthyHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
75 if req.URL.Path == "/_health/ping" {
76 resp.Write([]byte(`{"health":"ERROR","error":"the bends"}`))
78 http.Error(resp, "not found", http.StatusNotFound)
82 func (s *AggregatorSuite) TestUnhealthy(c *check.C) {
83 srv, listen := s.stubServer(&unhealthyHandler{})
85 s.handler.Config.Clusters["zzzzz"].SystemNodes["localhost"] = arvados.SystemNode{
86 Keepstore: arvados.Keepstore{Listen: listen},
88 s.handler.ServeHTTP(s.resp, s.req)
92 type healthyHandler struct{}
94 func (*healthyHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
95 if req.URL.Path == "/_health/ping" {
96 resp.Write([]byte(`{"health":"OK"}`))
98 http.Error(resp, "not found", http.StatusNotFound)
102 func (s *AggregatorSuite) TestHealthy(c *check.C) {
103 srv, listen := s.stubServer(&healthyHandler{})
105 s.handler.Config.Clusters["zzzzz"].SystemNodes["localhost"] = arvados.SystemNode{
106 Keepstore: arvados.Keepstore{Listen: listen},
108 s.handler.ServeHTTP(s.resp, s.req)
110 ep := resp.Checks["localhost/keepstore/_health/ping"]
111 c.Check(ep.Health, check.Equals, "OK")
112 c.Check(ep.Status, check.Equals, 200)
115 func (s *AggregatorSuite) TestHealthyAndUnhealthy(c *check.C) {
116 srvH, listenH := s.stubServer(&healthyHandler{})
118 srvU, listenU := s.stubServer(&unhealthyHandler{})
120 s.handler.Config.Clusters["zzzzz"].SystemNodes["localhost"] = arvados.SystemNode{
121 Keepstore: arvados.Keepstore{Listen: listenH},
123 s.handler.Config.Clusters["zzzzz"].SystemNodes["127.0.0.1"] = arvados.SystemNode{
124 Keepstore: arvados.Keepstore{Listen: listenU},
126 s.handler.ServeHTTP(s.resp, s.req)
127 resp := s.checkUnhealthy(c)
128 ep := resp.Checks["localhost/keepstore/_health/ping"]
129 c.Check(ep.Health, check.Equals, "OK")
130 c.Check(ep.Status, check.Equals, 200)
131 ep = resp.Checks["127.0.0.1/keepstore/_health/ping"]
132 c.Check(ep.Health, check.Equals, "ERROR")
133 c.Check(ep.Status, check.Equals, 200)
136 func (s *AggregatorSuite) checkError(c *check.C) {
137 c.Check(s.resp.Code, check.Not(check.Equals), http.StatusOK)
138 var resp ClusterHealthResponse
139 err := json.NewDecoder(s.resp.Body).Decode(&resp)
140 c.Check(err, check.IsNil)
141 c.Check(resp.Health, check.Not(check.Equals), "OK")
144 func (s *AggregatorSuite) checkUnhealthy(c *check.C) ClusterHealthResponse {
145 return s.checkResult(c, "ERROR")
148 func (s *AggregatorSuite) checkOK(c *check.C) ClusterHealthResponse {
149 return s.checkResult(c, "OK")
152 func (s *AggregatorSuite) checkResult(c *check.C, health string) ClusterHealthResponse {
153 c.Check(s.resp.Code, check.Equals, http.StatusOK)
154 var resp ClusterHealthResponse
155 err := json.NewDecoder(s.resp.Body).Decode(&resp)
156 c.Check(err, check.IsNil)
157 c.Check(resp.Health, check.Equals, health)
161 type slowHandler struct{}
163 func (*slowHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
164 if req.URL.Path == "/_health/ping" {
165 time.Sleep(3 * time.Second)
166 resp.Write([]byte(`{"health":"OK"}`))
168 http.Error(resp, "not found", http.StatusNotFound)
172 func (s *AggregatorSuite) TestPingTimeout(c *check.C) {
173 s.handler.timeout = arvados.Duration(100 * time.Millisecond)
174 srv, listen := s.stubServer(&slowHandler{})
176 s.handler.Config.Clusters["zzzzz"].SystemNodes["localhost"] = arvados.SystemNode{
177 Keepstore: arvados.Keepstore{Listen: listen},
179 s.handler.ServeHTTP(s.resp, s.req)
180 resp := s.checkUnhealthy(c)
181 ep := resp.Checks["localhost/keepstore/_health/ping"]
182 c.Check(ep.Health, check.Equals, "ERROR")
183 c.Check(ep.Status, check.Equals, 0)
184 rt, err := ep.ResponseTime.Float64()
185 c.Check(err, check.IsNil)
186 c.Check(rt > 0.005, check.Equals, true)