This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
api_test.go
311 lines (231 loc) · 8.33 KB
/
api_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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright 2017 Square Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package keysync
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"testing"
"time"
"github.com/square/keysync/backup"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func randomPort() uint16 {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
panicOnError(err)
listener, err := net.ListenTCP("tcp", addr)
panicOnError(err)
port := listener.Addr().(*net.TCPAddr).Port
listener.Close()
return uint16(port)
}
func waitForServer(t *testing.T, port uint16) {
for i := 0; i < 10; i++ {
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/status", port), nil)
if err != nil {
t.Fatal("error building request?")
}
res, err := http.DefaultClient.Do(req)
if err != nil || res.StatusCode != http.StatusOK {
time.Sleep(1 * time.Second)
continue
}
}
}
func TestApiSyncAllAndSyncClientSuccess(t *testing.T) {
if testing.Short() {
t.Skip("Skipping API test in short mode.")
}
port := randomPort()
server := createDefaultServerWithDeletionRace()
defer server.Close()
// Load a test config
syncer, err := createNewSyncer("fixtures/configs/test-config.yaml", server)
require.Nil(t, err)
NewAPIServer(syncer, nil, port, logrus.NewEntry(logrus.New()), metricsForTest())
waitForServer(t, port)
// Test SyncAll success
req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/sync", port), nil)
require.Nil(t, err)
res, err := http.DefaultClient.Do(req)
require.Nil(t, err)
data, _ := ioutil.ReadAll(res.Body)
require.Nil(t, err)
status := StatusResponse{}
err = json.Unmarshal(data, &status)
require.Nil(t, err)
require.True(t, status.Ok)
// Test SyncClientsuccess
req, err = http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/sync/client1", port), nil)
require.Nil(t, err)
res, err = http.DefaultClient.Do(req)
require.Nil(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
data, _ = ioutil.ReadAll(res.Body)
require.Nil(t, err)
status = StatusResponse{}
err = json.Unmarshal(data, &status)
require.Nil(t, err)
require.True(t, status.Ok)
// Test SyncClient failure on nonexistent client
req, err = http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/sync/non-existent", port), nil)
require.Nil(t, err)
res, err = http.DefaultClient.Do(req)
require.Nil(t, err)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
data, _ = ioutil.ReadAll(res.Body)
require.Nil(t, err)
status = StatusResponse{}
err = json.Unmarshal(data, &status)
require.Nil(t, err)
require.False(t, status.Ok)
}
func TestApiSyncOneError(t *testing.T) {
if testing.Short() {
t.Skip("Skipping API test in short mode.")
}
port := randomPort()
config, err := LoadConfig("fixtures/configs/errorconfigs/nonexistent-client-dir-config.yaml")
require.Nil(t, err)
syncer, err := NewSyncer(config, OutputDirCollection{}, logrus.NewEntry(logrus.New()), metricsForTest())
require.Nil(t, err)
_, err = syncer.LoadClients()
assert.NotNil(t, err)
NewAPIServer(syncer, nil, port, logrus.NewEntry(logrus.New()), metricsForTest())
waitForServer(t, port)
// Test error loading clients when syncing single client
req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/sync/client1", port), nil)
require.Nil(t, err)
res, err := http.DefaultClient.Do(req)
require.Nil(t, err)
assert.Equal(t, http.StatusInternalServerError, res.StatusCode)
// Test error loading clients when syncing all clients
req, err = http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/sync", port), nil)
require.Nil(t, err)
res, err = http.DefaultClient.Do(req)
require.Nil(t, err)
assert.Equal(t, http.StatusInternalServerError, res.StatusCode)
}
// Ensure the /backup path returns an error if no backup is configured
func TestNoBackup(t *testing.T) {
if testing.Short() {
t.Skip("Skipping API test in short mode.")
}
port := randomPort()
server := createDefaultServerWithDeletionRace()
defer server.Close()
// Load a test config
syncer, err := createNewSyncer("fixtures/configs/test-config.yaml", server)
require.Nil(t, err)
NewAPIServer(syncer, nil, port, logrus.NewEntry(logrus.New()), metricsForTest())
waitForServer(t, port)
// Call the /backup API, which should return an error
req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/backup", port), nil)
require.Nil(t, err)
res, err := http.DefaultClient.Do(req)
require.Nil(t, err)
require.EqualValues(t, http.StatusServiceUnavailable, res.StatusCode)
}
// stubBackup is used to verify the API server calls the backup object
type stubBackup struct {
backupCalls int
}
var _ backup.Backup = &stubBackup{}
func (b *stubBackup) Backup() error {
b.backupCalls++
return nil
}
func (b *stubBackup) Restore([]byte) error {
return nil
}
func TestBackup(t *testing.T) {
if testing.Short() {
t.Skip("Skipping API test in short mode.")
}
port := randomPort()
server := createDefaultServerWithDeletionRace()
defer server.Close()
// Load a test config
syncer, err := createNewSyncer("fixtures/configs/test-config.yaml", server)
require.Nil(t, err)
stub := stubBackup{}
NewAPIServer(syncer, &stub, port, logrus.NewEntry(logrus.New()), metricsForTest())
waitForServer(t, port)
// Call the /backup API, which should call the Backup() method
req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/backup", port), nil)
require.Nil(t, err)
res, err := http.DefaultClient.Do(req)
require.Nil(t, err)
require.EqualValues(t, http.StatusOK, res.StatusCode)
// The backup object should have been called exactly once
require.EqualValues(t, 1, stub.backupCalls)
}
func TestHealthCheck(t *testing.T) {
if testing.Short() {
t.Skip("Skipping API test in short mode.")
}
port := randomPort()
config, err := LoadConfig("fixtures/configs/errorconfigs/nonexistent-client-dir-config.yaml")
require.Nil(t, err)
syncer, err := NewSyncer(config, OutputDirCollection{}, logrus.NewEntry(logrus.New()), metricsForTest())
require.Nil(t, err)
_, err = syncer.LoadClients()
assert.NotNil(t, err)
NewAPIServer(syncer, nil, port, logrus.NewEntry(logrus.New()), metricsForTest())
waitForServer(t, port)
// 1. Check that health check returns false if we've never had a success
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/status", port), nil)
require.Nil(t, err)
res, err := http.DefaultClient.Do(req)
require.Nil(t, err)
assert.Equal(t, http.StatusServiceUnavailable, res.StatusCode)
// 2. Check health is true under good conditions (make it look like there was a successful sync)
syncer.updateSuccessTimestamp()
req, err = http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/status", port), nil)
require.Nil(t, err)
res, err = http.DefaultClient.Do(req)
require.Nil(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
}
func TestMetricsReporting(t *testing.T) {
if testing.Short() {
t.Skip("Skipping API test in short mode.")
}
port := randomPort()
config, err := LoadConfig("fixtures/configs/errorconfigs/nonexistent-client-dir-config.yaml")
require.Nil(t, err)
syncer, err := NewSyncer(config, OutputDirCollection{}, logrus.NewEntry(logrus.New()), metricsForTest())
require.Nil(t, err)
_, err = syncer.LoadClients()
assert.NotNil(t, err)
NewAPIServer(syncer, nil, port, logrus.NewEntry(logrus.New()), metricsForTest())
waitForServer(t, port)
// Check health under good conditions
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/metrics", port), nil)
require.Nil(t, err)
res, err := http.DefaultClient.Do(req)
require.Nil(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
// Check that metrics is valid JSON (should be an array)
body, _ := ioutil.ReadAll(res.Body)
var parsed []interface{}
err = json.Unmarshal(body, &parsed)
if err != nil {
t.Errorf("output from /metrics is not valid JSON, though it should be: %s", err)
}
}