Repository navigation
Expand file tree
/
Copy pathserver_test.go
More file actions
82 lines (72 loc) · 2.47 KB
/
Copy pathserver_test.go
File metadata and controls
82 lines (72 loc) · 2.47 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
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/girish/storage/store"
)
func TestHTTPPutStoresFile(t *testing.T) {
root := t.TempDir()
localStore := store.NewStore(store.StoreOpts{
Root: root,
PathTransformFunc: store.CASPathTransfromFunc,
})
server := NewFileServer(FileServerOpts{Store: localStore})
const key = "curl-upload-test"
expected := []byte("uploaded over HTTP")
req := httptest.NewRequest(http.MethodPut, "/files/"+key, bytes.NewReader(expected))
response := httptest.NewRecorder()
server.httpHandler().ServeHTTP(response, req)
if response.Code != http.StatusCreated {
t.Fatalf("expected status %d, got %d: %s", http.StatusCreated, response.Code, response.Body.String())
}
pathKey := store.CASPathTransfromFunc(key)
filePath := filepath.Join(root, filepath.FromSlash(pathKey.FullPath()))
actual, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("failed to read stored file: %v", err)
}
if !bytes.Equal(actual, expected) {
t.Fatalf("stored content = %q, want %q", actual, expected)
}
}
func TestHTTPPutReplicatesToPeerWithoutLooping(t *testing.T) {
newNode := func(t *testing.T) *FileServer {
t.Helper()
localStore := store.NewStore(store.StoreOpts{
Root: t.TempDir(),
PathTransformFunc: store.CASPathTransfromFunc,
})
return NewFileServer(FileServerOpts{Store: localStore})
}
first := newNode(t)
second := newNode(t)
firstHTTP := httptest.NewServer(first.httpHandler())
defer firstHTTP.Close()
secondHTTP := httptest.NewServer(second.httpHandler())
defer secondHTTP.Close()
first.Peers = []string{secondHTTP.URL}
second.Peers = []string{firstHTTP.URL}
const key = "replication-test"
expected := []byte("replicated between nodes")
req := httptest.NewRequest(http.MethodPut, "/files/"+key, bytes.NewReader(expected))
response := httptest.NewRecorder()
first.httpHandler().ServeHTTP(response, req)
if response.Code != http.StatusCreated {
t.Fatalf("expected status %d, got %d: %s", http.StatusCreated, response.Code, response.Body.String())
}
for _, node := range []*FileServer{first, second} {
pathKey := store.CASPathTransfromFunc(key)
filePath := filepath.Join(node.Store.Root, filepath.FromSlash(pathKey.FullPath()))
actual, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("failed to read replicated file: %v", err)
}
if !bytes.Equal(actual, expected) {
t.Fatalf("stored content = %q, want %q", actual, expected)
}
}
}