-
Notifications
You must be signed in to change notification settings - Fork 163
/
content_store_test.go
177 lines (138 loc) · 3.87 KB
/
content_store_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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"
)
var contentStore *ContentStore
func TestContentStorePut(t *testing.T) {
setup()
defer teardown()
m := &MetaObject{
Oid: "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72",
Size: 12,
}
b := bytes.NewBuffer([]byte("test content"))
if err := contentStore.Put(m, b); err != nil {
t.Fatalf("expected put to succeed, got: %s", err)
}
path := "content-store-test/6a/e8/a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72"
if _, err := os.Stat(path); os.IsNotExist(err) {
t.Fatalf("expected content to exist after putting")
}
}
func TestContentStorePutHashMismatch(t *testing.T) {
setup()
defer teardown()
m := &MetaObject{
Oid: "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72",
Size: 12,
}
b := bytes.NewBuffer([]byte("bogus content"))
if err := contentStore.Put(m, b); err == nil {
t.Fatal("expected put with bogus content to fail")
}
path := "content-store-test/6a/e8/a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72"
if _, err := os.Stat(path); err == nil {
t.Fatalf("expected content to not exist after putting bogus content")
}
}
func TestContentStorePutSizeMismatch(t *testing.T) {
setup()
defer teardown()
m := &MetaObject{
Oid: "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72",
Size: 14,
}
b := bytes.NewBuffer([]byte("test content"))
if err := contentStore.Put(m, b); err == nil {
t.Fatal("expected put with bogus size to fail")
}
path := "content-store-test/6a/e8/a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72"
if _, err := os.Stat(path); err == nil {
t.Fatalf("expected content to not exist after putting bogus size")
}
}
func TestContentStoreGet(t *testing.T) {
setup()
defer teardown()
m := &MetaObject{
Oid: "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72",
Size: 12,
}
b := bytes.NewBuffer([]byte("test content"))
if err := contentStore.Put(m, b); err != nil {
t.Fatalf("expected put to succeed, got: %s", err)
}
r, err := contentStore.Get(m, 0)
if err != nil {
t.Fatalf("expected get to succeed, got: %s", err)
} else {
defer r.Close()
}
by, _ := ioutil.ReadAll(r)
if string(by) != "test content" {
t.Fatalf("expected to read content, got: %s", string(by))
}
}
func TestContentStoreGetWithRange(t *testing.T) {
setup()
defer teardown()
m := &MetaObject{
Oid: "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72",
Size: 12,
}
b := bytes.NewBuffer([]byte("test content"))
if err := contentStore.Put(m, b); err != nil {
t.Fatalf("expected put to succeed, got: %s", err)
}
r, err := contentStore.Get(m, 5)
if err != nil {
t.Fatalf("expected get to succeed, got: %s", err)
} else {
defer r.Close()
}
by, _ := ioutil.ReadAll(r)
if string(by) != "content" {
t.Fatalf("expected to read content, got: %s", string(by))
}
}
func TestContenStoreGetNonExisting(t *testing.T) {
setup()
defer teardown()
_, err := contentStore.Get(&MetaObject{Oid: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, 0)
if err == nil {
t.Fatalf("expected to get an error, but content existed")
}
}
func TestContentStoreExists(t *testing.T) {
setup()
defer teardown()
m := &MetaObject{
Oid: "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72",
Size: 12,
}
b := bytes.NewBuffer([]byte("test content"))
if contentStore.Exists(m) {
t.Fatalf("expected content to not exist yet")
}
if err := contentStore.Put(m, b); err != nil {
t.Fatalf("expected put to succeed, got: %s", err)
}
if !contentStore.Exists(m) {
t.Fatalf("expected content to exist")
}
}
func setup() {
store, err := NewContentStore("content-store-test")
if err != nil {
fmt.Printf("error initializing content store: %s\n", err)
os.Exit(1)
}
contentStore = store
}
func teardown() {
os.RemoveAll("content-store-test")
}