1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
15 check "gopkg.in/check.v1"
18 // Gocheck boilerplate
19 var _ = check.Suite(&Suite{})
21 func Test(t *testing.T) {
28 goodToken = "supersecret"
32 func (s *Suite) TestPassFailRefuse(c *check.C) {
37 "success": func() error { return nil },
38 "miracle": func() error { return errors.New("unimplemented") },
42 resp := httptest.NewRecorder()
43 h.ServeHTTP(resp, s.request("/_health/ping", goodToken))
44 s.checkHealthy(c, resp)
46 resp = httptest.NewRecorder()
47 h.ServeHTTP(resp, s.request("/_health/success", goodToken))
48 s.checkHealthy(c, resp)
50 resp = httptest.NewRecorder()
51 h.ServeHTTP(resp, s.request("/_health/miracle", goodToken))
52 s.checkUnhealthy(c, resp)
54 resp = httptest.NewRecorder()
55 h.ServeHTTP(resp, s.request("/_health/miracle", badToken))
56 c.Check(resp.Code, check.Equals, http.StatusForbidden)
58 resp = httptest.NewRecorder()
59 h.ServeHTTP(resp, s.request("/_health/miracle", ""))
60 c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
62 resp = httptest.NewRecorder()
63 h.ServeHTTP(resp, s.request("/_health/theperthcountyconspiracy", ""))
64 c.Check(resp.Code, check.Equals, http.StatusNotFound)
66 resp = httptest.NewRecorder()
67 h.ServeHTTP(resp, s.request("/x/miracle", ""))
68 c.Check(resp.Code, check.Equals, http.StatusNotFound)
70 resp = httptest.NewRecorder()
71 h.ServeHTTP(resp, s.request("/miracle", ""))
72 c.Check(resp.Code, check.Equals, http.StatusNotFound)
75 func (s *Suite) TestPingOverride(c *check.C) {
80 "ping": func() error {
85 return errors.New("good error")
89 resp := httptest.NewRecorder()
90 h.ServeHTTP(resp, s.request("/ping", goodToken))
91 s.checkHealthy(c, resp)
93 resp = httptest.NewRecorder()
94 h.ServeHTTP(resp, s.request("/ping", goodToken))
95 s.checkUnhealthy(c, resp)
98 func (s *Suite) TestZeroValueIsDisabled(c *check.C) {
99 resp := httptest.NewRecorder()
100 (&Handler{}).ServeHTTP(resp, s.request("/ping", goodToken))
101 c.Check(resp.Code, check.Equals, http.StatusNotFound)
103 resp = httptest.NewRecorder()
104 (&Handler{}).ServeHTTP(resp, s.request("/ping", ""))
105 c.Check(resp.Code, check.Equals, http.StatusNotFound)
108 func (s *Suite) request(path, token string) *http.Request {
109 u, _ := url.Parse("http://foo.local" + path)
110 req := &http.Request{
114 RequestURI: u.RequestURI(),
117 req.Header = http.Header{
118 "Authorization": {"Bearer " + token},
124 func (s *Suite) checkHealthy(c *check.C, resp *httptest.ResponseRecorder) {
125 c.Check(resp.Code, check.Equals, http.StatusOK)
126 c.Check(resp.Body.String(), check.Equals, `{"health":"OK"}`+"\n")
129 func (s *Suite) checkUnhealthy(c *check.C, resp *httptest.ResponseRecorder) {
130 c.Check(resp.Code, check.Equals, http.StatusOK)
131 var result map[string]interface{}
132 err := json.Unmarshal(resp.Body.Bytes(), &result)
133 c.Assert(err, check.IsNil)
134 c.Check(result["health"], check.Equals, "ERROR")
135 c.Check(result["error"].(string), check.Not(check.Equals), "")