File: health_test.go

package info (click to toggle)
golang-github-coreos-pkg 4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 340 kB
  • sloc: sh: 30; makefile: 3
file content (198 lines) | stat: -rw-r--r-- 4,240 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package health

import (
	"encoding/json"
	"errors"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/coreos/pkg/httputil"
)

type boolChecker bool

func (b boolChecker) Healthy() error {
	if b {
		return nil
	}
	return errors.New("Unhealthy")
}

func errString(err error) string {
	if err == nil {
		return ""
	}
	return err.Error()
}

func TestCheck(t *testing.T) {
	for i, test := range []struct {
		checks   []Checkable
		expected string
	}{
		{[]Checkable{}, ""},

		{[]Checkable{boolChecker(true)}, ""},

		{[]Checkable{boolChecker(true), boolChecker(true)}, ""},

		{[]Checkable{boolChecker(true), boolChecker(false)}, "Unhealthy"},

		{[]Checkable{boolChecker(true), boolChecker(false), boolChecker(false)}, "multiple health check failure: [Unhealthy Unhealthy]"},
	} {
		err := Check(test.checks)

		if errString(err) != test.expected {
			t.Errorf("case %d: want %v, got %v", i, test.expected, errString(err))
		}
	}
}

func TestHandlerFunc(t *testing.T) {
	for i, test := range []struct {
		checker         Checker
		method          string
		expectedStatus  string
		expectedCode    int
		expectedMessage string
	}{
		{
			Checker{
				Checks: []Checkable{
					boolChecker(true),
				},
			},
			"GET",
			"ok",
			http.StatusOK,
			"",
		},

		// Wrong method.
		{
			Checker{
				Checks: []Checkable{
					boolChecker(true),
				},
			},
			"POST",
			"",
			http.StatusMethodNotAllowed,
			"GET only acceptable method",
		},

		// Health check fails.
		{
			Checker{
				Checks: []Checkable{
					boolChecker(false),
				},
			},
			"GET",
			"error",
			http.StatusInternalServerError,
			"Unhealthy",
		},

		// Health check fails, with overridden ErrorHandler.
		{
			Checker{
				Checks: []Checkable{
					boolChecker(false),
				},
				UnhealthyHandler: func(w http.ResponseWriter, r *http.Request, err error) {
					httputil.WriteJSONResponse(w,
						http.StatusInternalServerError, StatusResponse{
							Status: "error",
							Details: &StatusResponseDetails{
								Code:    http.StatusInternalServerError,
								Message: "Override!",
							},
						})
				},
			},
			"GET",
			"error",
			http.StatusInternalServerError,
			"Override!",
		},

		// Health check succeeds, with overridden SuccessHandler.
		{
			Checker{
				Checks: []Checkable{
					boolChecker(true),
				},
				HealthyHandler: func(w http.ResponseWriter, r *http.Request) {
					httputil.WriteJSONResponse(w,
						http.StatusOK, StatusResponse{
							Status: "okey-dokey",
						})
				},
			},
			"GET",
			"okey-dokey",
			http.StatusOK,
			"",
		},
	} {
		w := httptest.NewRecorder()
		r := &http.Request{}
		r.Method = test.method
		test.checker.ServeHTTP(w, r)
		if w.Code != test.expectedCode {
			t.Errorf("case %d: w.code == %v, want %v", i, w.Code, test.expectedCode)
		}

		if test.expectedStatus == "" {
			// This is to handle the wrong-method case, when the
			// body of the response is empty.
			continue
		}

		statusMap := make(map[string]interface{})
		err := json.Unmarshal(w.Body.Bytes(), &statusMap)
		if err != nil {
			t.Fatalf("case %d: failed to Unmarshal response body: %v", i, err)
		}

		status, ok := statusMap["status"].(string)
		if !ok {
			t.Errorf("case %d: status not present or not a string in json: %q", i, w.Body.Bytes())
		}
		if status != test.expectedStatus {
			t.Errorf("case %d: status == %v, want %v", i, status, test.expectedStatus)
		}

		detailMap, ok := statusMap["details"].(map[string]interface{})
		if test.expectedMessage != "" {
			if !ok {
				t.Fatalf("case %d: could not find/unmarshal detailMap", i)
			}
			message, ok := detailMap["message"].(string)
			if !ok {
				t.Fatalf("case %d: message not present or not a string in json: %q",
					i, w.Body.Bytes())
			}
			if message != test.expectedMessage {
				t.Errorf("case %d: message == %v, want %v", i, message, test.expectedMessage)
			}

			code, ok := detailMap["code"].(float64)
			if !ok {
				t.Fatalf("case %d: code not present or not an int in json: %q",
					i, w.Body.Bytes())
			}
			if int(code) != test.expectedCode {
				t.Errorf("case %d: code == %v, want %v", i, code, test.expectedCode)
			}

		} else {
			if ok {
				t.Errorf("case %d: unwanted detailMap present: %q", i, detailMap)
			}
		}

	}
}