-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_test.go
More file actions
501 lines (441 loc) · 15.4 KB
/
node_test.go
File metadata and controls
501 lines (441 loc) · 15.4 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package main
import (
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func Test_newNode(t *testing.T) {
n := newNode("testuuid", "http://localhost:1000", true)
if n.UUID != "testuuid" {
t.Error("node wasn't created properly")
}
}
func Test_DateFormatting(t *testing.T) {
n := newNode("testuuid", "http://localhost:1000", true)
n.LastSeen, _ = time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2006")
if n.LastSeenFormatted() != "2006-01-02 15:04:05" {
t.Error("wrong formatted date for LastSeen")
}
n.LastFailed, _ = time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2006")
if n.LastFailedFormatted() != "2006-01-02 15:04:05" {
t.Error("wrong formatted date for LastFailed")
}
}
func Test_HashKeys(t *testing.T) {
n := newNode("testuuid", "http://localhost:1000", true)
keys := n.HashKeys()
if len(keys) != replicas {
t.Error("wrong number of keys")
}
}
func Test_Unhealthy(t *testing.T) {
n := newNode("testuuid", "http://localhost:1000", true)
n.LastSeen = time.Now()
// LastFailed before LastSeen => Healthy
n.LastFailed = n.LastSeen.Add(-1 * time.Minute)
if n.Unhealthy() {
t.Error("node should be healthy")
}
// LastFailed after LastSeen => Unhealthy
n.LastFailed = n.LastSeen.Add(1 * time.Minute)
if !n.Unhealthy() {
t.Error("node should be unhealthy")
}
}
func Test_URLGenerators(t *testing.T) {
n := newNode("testuuid", "http://localhost:1000", true)
if n.AddFileURL() != "http://localhost:1000/local/" {
t.Errorf("AddFileURL mismatch: %s", n.AddFileURL())
}
k, err := keyFromString("sha1:0000000000000000000000000000000000000000")
if err != nil {
t.Fatalf("failed to create key: %v", err)
}
expected := "http://localhost:1000/local/sha1:0000000000000000000000000000000000000000/"
if n.retrieveURL(*k) != expected {
t.Errorf("retrieveURL mismatch: %s", n.retrieveURL(*k))
}
if n.retrieveInfoURL(*k) != expected {
t.Errorf("retrieveInfoURL mismatch: %s", n.retrieveInfoURL(*k))
}
}
func Test_AddFile(t *testing.T) {
// Success
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected POST, got %s", r.Method)
}
if r.URL.Path != "/local/" {
t.Errorf("Expected path /local/, got %s", r.URL.Path)
}
// Check secret
if r.Header.Get("X-Cask-Cluster-Secret") != "secret" {
t.Errorf("Expected secret header, got %s", r.Header.Get("X-Cask-Cluster-Secret"))
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709"))
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
// sha1 of empty string is da39a3ee5e6b4b0d3255bfef95601890afd80709
r := strings.NewReader("")
if !n.AddFile(*k, r, "secret") {
t.Error("AddFile returned false on success")
}
}
// Server Error
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
r := strings.NewReader("")
if n.AddFile(*k, r, "secret") {
t.Error("AddFile returned true on server error")
}
}
// Wrong key returned
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("sha1:otherkey"))
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
r := strings.NewReader("")
if n.AddFile(*k, r, "secret") {
t.Error("AddFile returned true when wrong key returned")
}
}
// Invalid URL
{
n := newNode("testuuid", ":::invalid-url:::", true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
r := strings.NewReader("")
if n.AddFile(*k, r, "secret") {
t.Error("AddFile returned true on invalid URL")
}
}
}
func Test_postFile(t *testing.T) {
// Success
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected POST, got %s", r.Method)
}
// Check multipart
err := r.ParseMultipartForm(1024)
if err != nil {
t.Errorf("failed to parse multipart form: %v", err)
}
file, _, err := r.FormFile("file")
if err != nil {
t.Errorf("failed to get file from form: %v", err)
}
defer file.Close()
content, _ := io.ReadAll(file)
if string(content) != "test content" {
t.Errorf("Expected 'test content', got '%s'", string(content))
}
if r.Header.Get("X-Cask-Cluster-Secret") != "secret" {
t.Errorf("Expected secret header, got %s", r.Header.Get("X-Cask-Cluster-Secret"))
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
r := strings.NewReader("test content")
resp, err := postFile(r, server.URL, "secret")
if err != nil {
t.Fatalf("postFile failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200, got %d", resp.StatusCode)
}
}
// Invalid URL
{
r := strings.NewReader("test content")
_, err := postFile(r, ":::invalid-url:::", "secret")
if err == nil {
t.Error("postFile should have failed with invalid URL")
}
}
}
func Test_Retrieve(t *testing.T) {
// Success
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Expected GET, got %s", r.Method)
}
if r.URL.Path != "/local/sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709/" {
t.Errorf("Expected path /local/sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709/, got %s", r.URL.Path)
}
// Check secret
if r.Header.Get("X-Cask-Cluster-Secret") != "secret" {
t.Errorf("Expected secret header, got %s", r.Header.Get("X-Cask-Cluster-Secret"))
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("test content"))
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
b, err := n.Retrieve(*k, "secret")
if err != nil {
t.Fatalf("Retrieve failed: %v", err)
}
if string(b) != "test content" {
t.Errorf("Expected 'test content', got '%s'", string(b))
}
}
// Server Error
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
_, err := n.Retrieve(*k, "secret")
if err == nil {
t.Error("Retrieve should have failed on server error")
}
}
// 404
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
_, err := n.Retrieve(*k, "secret")
if err == nil {
t.Error("Retrieve should have failed on 404")
}
if err.Error() != "404, probably" {
t.Errorf("Expected '404, probably', got '%v'", err)
}
}
}
func Test_RetrieveInfo(t *testing.T) {
// Success
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "HEAD" {
t.Errorf("Expected HEAD, got %s", r.Method)
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
ok, err := n.RetrieveInfo(*k, "secret")
if err != nil {
t.Fatalf("RetrieveInfo failed: %v", err)
}
if !ok {
t.Error("RetrieveInfo returned false on success")
}
}
// 404
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
ok, err := n.RetrieveInfo(*k, "secret")
if ok {
t.Error("RetrieveInfo returned true on 404")
}
if err == nil || err.Error() != "404, probably" {
t.Errorf("Expected '404, probably', got '%v'", err)
}
}
// Timeout (using timedHeadRequest directly to test with shorter timeout)
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
// We can't easily change the timeout in RetrieveInfo without changing code,
// but we can test timedHeadRequest directly.
_, err := timedHeadRequest(n.retrieveInfoURL(*k), 10*time.Millisecond, "secret")
if err == nil {
t.Error("timedHeadRequest should have timed out")
} else if err.Error() != "HEAD request timed out" {
t.Errorf("Expected 'HEAD request timed out', got '%v'", err)
}
}
}
func Test_CheckFile(t *testing.T) {
// Exists and Valid
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("test content"))
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
// sha1("test content") = 1eebdf4fdc9fc7bf283031b93f9aef3338de9052
k, _ := keyFromString("sha1:1eebdf4fdc9fc7bf283031b93f9aef3338de9052")
found, content, err := n.CheckFile(*k, "secret")
if !found {
t.Error("CheckFile returned false when file exists")
}
if err != nil {
t.Errorf("CheckFile returned error: %v", err)
}
if string(content) != "test content" {
t.Errorf("Expected 'test content', got '%s'", string(content))
}
}
// Exists but Corrupt
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("corrupt content"))
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
k, _ := keyFromString("sha1:1eebdf4fdc9fc7bf283031b93f9aef3338de9052")
found, _, err := n.CheckFile(*k, "secret")
if !found {
t.Error("CheckFile returned false when file exists (even if corrupt)")
}
if err == nil || err.Error() != "corrupt" {
t.Errorf("Expected 'corrupt' error, got '%v'", err)
}
}
// Not Found
{
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
n := newNode("testuuid", server.URL, true)
k, _ := keyFromString("sha1:1eebdf4fdc9fc7bf283031b93f9aef3338de9052")
found, _, err := n.CheckFile(*k, "secret")
if found {
t.Error("CheckFile returned true when file missing")
}
if err != nil {
t.Errorf("CheckFile returned error when file missing: %v", err)
}
}
}
func Test_doublecheckReplica(t *testing.T) {
k, _ := keyFromString("sha1:1eebdf4fdc9fc7bf283031b93f9aef3338de9052")
if !doublecheckReplica([]byte("test content"), *k) {
t.Error("doublecheckReplica failed for valid content")
}
if doublecheckReplica([]byte("wrong content"), *k) {
t.Error("doublecheckReplica succeeded for invalid content")
}
}
func Test_processRetrieveInfoResponse_Nil(t *testing.T) {
n := newNode("testuuid", "http://localhost:1000", true)
_, err := n.processRetrieveInfoResponse(nil)
if err == nil {
t.Error("processRetrieveInfoResponse should fail with nil response")
}
}
func Test_updateFreeSpaceStatus(t *testing.T) { n := newNode("testuuid", "http://localhost:1000", true)
// Case 1: Writeable, FreeSpace > Min => Stay Writeable
mb := MockBackend{freeSpace: 2000}
n.updateFreeSpaceStatus(1000, mb)
if !n.Writeable {
t.Error("Should be writeable")
}
// Case 2: Writeable, FreeSpace < Min => Become Unwriteable
mb.freeSpace = 500
n.updateFreeSpaceStatus(1000, mb)
if n.Writeable {
t.Error("Should be unwriteable")
}
// Case 3: Unwriteable, FreeSpace < Min => Stay Unwriteable
n.Writeable = false
mb.freeSpace = 500
n.updateFreeSpaceStatus(1000, mb)
if n.Writeable {
t.Error("Should be unwriteable")
}
// Case 4: Unwriteable, FreeSpace > Min => Become Writeable
mb.freeSpace = 2000
n.updateFreeSpaceStatus(1000, mb)
if !n.Writeable {
t.Error("Should be writeable")
}
}
func Test_Retrieve_Errors(t *testing.T) {
// NewRequest Error
{
// Control character in URL to trigger NewRequest error
n := newNode("testuuid", "http://loc\nalhost:1000", true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
_, err := n.Retrieve(*k, "secret")
if err == nil {
t.Error("Retrieve should have failed on invalid URL")
}
}
// Do Error
{
n := newNode("testuuid", "http://invalid-host-does-not-exist:12345", true)
k, _ := keyFromString("sha1:da39a3ee5e6b4b0d3255bfef95601890afd80709")
_, err := n.Retrieve(*k, "secret")
if err == nil {
t.Error("Retrieve should have failed on unreachable host")
}
}
}
func Test_timedHeadRequest_Errors(t *testing.T) {
// NewRequest Error
{
_, err := timedHeadRequest("http://loc\nalhost:1000", 1*time.Second, "secret")
if err == nil {
t.Error("timedHeadRequest should have failed on invalid URL")
}
}
// Do Error
{
_, err := timedHeadRequest("http://invalid-host-does-not-exist:12345", 1*time.Second, "secret")
if err == nil {
t.Error("timedHeadRequest should have failed on unreachable host")
}
}
}
func Test_postFile_Errors(t *testing.T) {
// Do Error
{
r := strings.NewReader("test content")
_, err := postFile(r, "http://invalid-host-does-not-exist:12345", "secret")
if err == nil {
t.Error("postFile should have failed on unreachable host")
}
}
}
type FailReader struct{}
func (f FailReader) Read(p []byte) (n int, err error) { return 0, errors.New("read failed") }
func Test_postFile_ReadError(t *testing.T) {
_, err := postFile(FailReader{}, "http://localhost:1000", "secret")
if err == nil {
t.Error("postFile should fail on read error")
}
}