-
Notifications
You must be signed in to change notification settings - Fork 3
/
server_test.go
243 lines (213 loc) · 6.7 KB
/
server_test.go
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package keel_test
import (
"bytes"
"context"
"io"
"net/http"
"strings"
"syscall"
"testing"
"time"
"github.com/foomo/keel/service"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
"github.com/foomo/keel"
)
type KeelTestSuite struct {
suite.Suite
l *zap.Logger
svr *keel.Server
mux *http.ServeMux
cancel context.CancelFunc
}
// SetupSuite hook
func (s *KeelTestSuite) SetupSuite() {
s.l = zaptest.NewLogger(s.T())
}
// BeforeTest hook
func (s *KeelTestSuite) BeforeTest(suiteName, testName string) {
s.l = zaptest.NewLogger(s.T())
s.mux = http.NewServeMux()
s.mux.HandleFunc("/ok", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
s.mux.HandleFunc("/sleep", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Second * 1)
w.WriteHeader(http.StatusOK)
})
s.mux.HandleFunc("/panic", func(w http.ResponseWriter, r *http.Request) {
panic("foobar")
})
s.mux.HandleFunc("/log/info", func(w http.ResponseWriter, r *http.Request) {
s.l.Info("logging info")
})
s.mux.HandleFunc("/log/debug", func(w http.ResponseWriter, r *http.Request) {
s.l.Debug("logging debug")
})
s.mux.HandleFunc("/log/warn", func(w http.ResponseWriter, r *http.Request) {
s.l.Warn("logging warn")
})
s.mux.HandleFunc("/log/error", func(w http.ResponseWriter, r *http.Request) {
s.l.Error("logging error")
})
ctx, cancel := context.WithCancel(context.Background())
s.svr = keel.NewServer(
keel.WithContext(ctx),
keel.WithLogger(s.l),
)
s.cancel = cancel
}
// AfterTest hook
func (s *KeelTestSuite) AfterTest(suiteName, testName string) {
s.cancel()
time.Sleep(time.Second * 3)
}
// TearDownSuite hook
func (s *KeelTestSuite) TearDownSuite() {}
func (s *KeelTestSuite) TestServiceHTTP() {
s.svr.AddServices(
service.NewHTTP(s.l, "test", "localhost:55000", s.mux),
)
s.runServer()
if statusCode, _, err := s.httpGet("http://localhost:55000/ok"); s.NoError(err) {
s.Equal(http.StatusOK, statusCode)
}
}
func (s *KeelTestSuite) TestServiceHTTPZap() {
s.svr.AddServices(
service.NewHTTPZap(s.l, "zap", "localhost:9100", "/log"),
service.NewHTTP(s.l, "test", "localhost:55000", s.mux),
)
s.runServer()
s.Run("default", func() {
if statusCode, body, err := s.httpGet("http://localhost:9100/log"); s.NoError(err) {
s.Equal(http.StatusOK, statusCode)
s.Equal(`{"level":"info","disableCaller":true,"disableStacktrace":true}`, body)
}
if statusCode, _, err := s.httpGet("http://localhost:55000/log/info"); s.NoError(err) {
s.Equal(http.StatusOK, statusCode)
}
if statusCode, _, err := s.httpGet("http://localhost:55000/log/debug"); s.NoError(err) {
s.Equal(http.StatusOK, statusCode)
}
})
s.Run("set debug level", func() {
if statusCode, body, err := s.httpPut("http://localhost:9100/log", `{"level":"debug"}`); s.NoError(err) {
s.Equal(http.StatusOK, statusCode)
s.Equal(`{"level":"debug","disableCaller":true,"disableStacktrace":true}`, body)
}
if statusCode, _, err := s.httpGet("http://localhost:55000/log/info"); s.NoError(err) {
s.Equal(http.StatusOK, statusCode)
}
if statusCode, _, err := s.httpGet("http://localhost:55000/log/debug"); s.NoError(err) {
s.Equal(http.StatusOK, statusCode)
}
})
s.Run("enable caller", func() {
if statusCode, body, err := s.httpPut("http://localhost:9100/log", `{"disableCaller":false}`); s.NoError(err) {
s.Equal(http.StatusOK, statusCode)
s.Equal(`{"level":"debug","disableCaller":false,"disableStacktrace":true}`, body)
}
if statusCode, _, err := s.httpGet("http://localhost:55000/log/error"); s.NoError(err) {
s.Equal(http.StatusOK, statusCode)
}
})
s.Run("enable stacktrace", func() {
if statusCode, body, err := s.httpPut("http://localhost:9100/log", `{"disableStacktrace":false}`); s.NoError(err) {
s.Equal(http.StatusOK, statusCode)
s.Equal(`{"level":"debug","disableCaller":false,"disableStacktrace":false}`, body)
}
if statusCode, _, err := s.httpGet("http://localhost:55000/log/error"); s.NoError(err) {
s.Equal(http.StatusOK, statusCode)
}
})
}
func (s *KeelTestSuite) TestGraceful() {
s.svr.AddServices(
service.NewHTTP(s.l, "test", "localhost:55000", s.mux),
)
s.runServer()
{ // check that we're up
if statusCode, _, err := s.httpGet("http://localhost:55000/ok"); s.NoError(err) {
s.l.Info("received response from /ok")
s.Equal(http.StatusOK, statusCode)
}
}
{ // start long running call in separate process
waitChan := make(chan string)
go func(waitChan chan string) {
waitChan <- "ok"
s.l.Info("rending request to /sleep")
if statusCode, _, err := s.httpGet("http://localhost:55000/sleep"); s.NoError(err) {
s.l.Info("received response from /sleep")
s.Equal(http.StatusOK, statusCode)
}
}(waitChan)
s.l.Info("waiting for ")
<-waitChan
}
{
waitChan := make(chan string)
go func(waitChan chan string) {
waitChan <- "ok"
time.Sleep(time.Second)
if s.NoError(syscall.Kill(syscall.Getpid(), syscall.SIGINT)) {
s.l.Info("killed myself")
}
}(waitChan)
<-waitChan
}
time.Sleep(time.Second * 3)
{ // check that server is down
_, _, err := s.httpGet("http://localhost:55000/ok")
s.Require().Error(err)
}
s.l.Info("done")
}
// runServer helper
func (s *KeelTestSuite) runServer() {
l := s.svr.Logger()
waitChan := make(chan string)
go func(waitChan chan string) {
waitChan <- "finished"
s.svr.Run()
}(waitChan)
l.Debug("waiting for server process to start")
<-waitChan
time.Sleep(time.Second)
l.Debug("continuing test")
}
// httpGet helper
func (s *KeelTestSuite) httpGet(url string) (int, string, error) {
if req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil); err != nil {
return 0, "", err
} else if resp, err := http.DefaultClient.Do(req); err != nil {
return 0, "", err
} else if body, err := io.ReadAll(resp.Body); err != nil {
return 0, "", err
} else if err := resp.Body.Close(); err != nil {
return 0, "", err
} else {
return resp.StatusCode, string(bytes.TrimSpace(body)), nil
}
}
// httpPut helper
func (s *KeelTestSuite) httpPut(url, data string) (int, string, error) {
if req, err := http.NewRequestWithContext(context.Background(), http.MethodPut, url, strings.NewReader(data)); err != nil {
return 0, "", err
} else if resp, err := http.DefaultClient.Do(req); err != nil {
return 0, "", err
} else if body, err := io.ReadAll(resp.Body); err != nil {
return 0, "", err
} else if err := resp.Body.Close(); err != nil {
return 0, "", err
} else {
return resp.StatusCode, string(bytes.TrimSpace(body)), nil
}
}
// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestKeelTestSuite(t *testing.T) {
suite.Run(t, new(KeelTestSuite))
}