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")
90 resp := httptest.NewRecorder()
91 h.ServeHTTP(resp, s.request("/ping", goodToken))
92 s.checkHealthy(c, resp)
94 resp = httptest.NewRecorder()
95 h.ServeHTTP(resp, s.request("/ping", goodToken))
96 s.checkUnhealthy(c, resp)
99 func (s *Suite) TestZeroValueIsDisabled(c *check.C) {
100 resp := httptest.NewRecorder()
101 (&Handler{}).ServeHTTP(resp, s.request("/ping", goodToken))
102 c.Check(resp.Code, check.Equals, http.StatusNotFound)
104 resp = httptest.NewRecorder()
105 (&Handler{}).ServeHTTP(resp, s.request("/ping", ""))
106 c.Check(resp.Code, check.Equals, http.StatusNotFound)
109 func (s *Suite) request(path, token string) *http.Request {
110 u, _ := url.Parse("http://foo.local" + path)
111 req := &http.Request{
115 RequestURI: u.RequestURI(),
118 req.Header = http.Header{
119 "Authorization": {"Bearer " + token},
125 func (s *Suite) checkHealthy(c *check.C, resp *httptest.ResponseRecorder) {
126 c.Check(resp.Code, check.Equals, http.StatusOK)
127 c.Check(resp.Body.String(), check.Equals, `{"health":"OK"}`+"\n")
130 func (s *Suite) checkUnhealthy(c *check.C, resp *httptest.ResponseRecorder) {
131 c.Check(resp.Code, check.Equals, http.StatusOK)
132 var result map[string]interface{}
133 err := json.Unmarshal(resp.Body.Bytes(), &result)
134 c.Assert(err, check.IsNil)
135 c.Check(result["health"], check.Equals, "ERROR")
136 c.Check(result["error"].(string), check.Not(check.Equals), "")