-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaws_content_store_test.go
220 lines (191 loc) · 5.45 KB
/
aws_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
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/s3"
)
var awsContentStore *AwsContentStore
func TestAwsContentStorePut(t *testing.T) {
setupAwsTest()
defer teardownAwsTest()
if testing.Short() {
t.Skip("skipping test in short mode.")
}
m := &MetaObject{
Oid: "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72",
Size: 12,
}
b := bytes.NewBuffer([]byte("test content"))
if err := awsContentStore.Put(m, b); err != nil {
t.Fatalf("expected put to succeed, got: %s", err)
}
if err := awsContentStore.Exists(m); !err {
t.Fatalf("expected content to exist after putting")
}
}
func TestAwsContentStorePutHashMismatch(t *testing.T) {
setupAwsTest()
defer teardownAwsTest()
if testing.Short() {
t.Skip("skipping test in short mode.")
}
m := &MetaObject{
Oid: "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72",
Size: 12,
}
b := bytes.NewBuffer([]byte("bogus content"))
if err := awsContentStore.Put(m, b); err == nil {
t.Fatal("expected put with bogus content to fail")
}
}
func TestAwsContentStorePutSizeMismatch(t *testing.T) {
setupAwsTest()
defer teardownAwsTest()
if testing.Short() {
t.Skip("skipping test in short mode.")
}
m := &MetaObject{
Oid: "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72",
Size: 14,
}
b := bytes.NewBuffer([]byte("test content"))
if err := awsContentStore.Put(m, b); err == nil {
t.Fatal("expected put with bogus size to fail")
}
}
func TestAwsContentStoreGet(t *testing.T) {
setupAwsTest()
defer teardownAwsTest()
if testing.Short() {
t.Skip("skipping test in short mode.")
}
m := &MetaObject{
Oid: "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72",
Size: 12,
}
b := bytes.NewBuffer([]byte("test content"))
if err := awsContentStore.Put(m, b); err != nil {
t.Fatalf("expected put to succeed, got: %s", err)
}
r, err := awsContentStore.Get(m)
if err != nil {
t.Fatalf("expected get to succeed, got: %s", err)
}
by, _ := ioutil.ReadAll(r)
if string(by) != "test content" {
t.Fatalf("expected to read content, got: %s", string(by))
}
}
func TestAwsContentStoreGetNonExisting(t *testing.T) {
setupAwsTest()
defer teardownAwsTest()
if testing.Short() {
t.Skip("skipping test in short mode.")
}
_, err := awsContentStore.Get(&MetaObject{Oid: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"})
if err == nil {
t.Fatalf("expected to get an error, but content existed")
}
}
func TestAwsContentStoreExists(t *testing.T) {
setupAwsTest()
defer teardownAwsTest()
if testing.Short() {
t.Skip("skipping test in short mode.")
}
m := &MetaObject{
Oid: "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72",
Size: 12,
}
b := bytes.NewBuffer([]byte("test content"))
if awsContentStore.Exists(m) {
t.Fatalf("expected content to not exist yet")
}
if err := awsContentStore.Put(m, b); err != nil {
t.Fatalf("expected put to succeed, got: %s", err)
}
if !awsContentStore.Exists(m) {
t.Fatalf("expected content to exist")
}
}
func TestAwsSettings(t *testing.T) {
setupAwsTest()
defer teardownAwsTest()
Config.Aws.BucketAcl = "private"
awsContentStore.setAcl()
if awsContentStore.acl != s3.Private {
t.Fatalf("Should have been set to private, but got %s", awsContentStore.acl)
}
Config.Aws.BucketAcl = "public-read"
awsContentStore.setAcl()
if awsContentStore.acl != s3.PublicRead {
t.Fatalf("Should have been set to public-read, but got %s", awsContentStore.acl)
}
Config.Aws.BucketAcl = "public-read-write"
awsContentStore.setAcl()
if awsContentStore.acl != s3.PublicReadWrite {
t.Fatalf("Should have been set to public-read-write, but got %s", awsContentStore.acl)
}
Config.Aws.BucketAcl = "authenticated-read"
awsContentStore.setAcl()
if awsContentStore.acl != s3.AuthenticatedRead {
t.Fatalf("Should have been set to authenticated-read, but got %s", awsContentStore.acl)
}
Config.Aws.BucketAcl = "bucket-owner-read"
awsContentStore.setAcl()
if awsContentStore.acl != s3.BucketOwnerRead {
t.Fatalf("Should have been set to bucket-owner-read, but got %s", awsContentStore.acl)
}
Config.Aws.BucketAcl = "bucket-owner-full-control"
awsContentStore.setAcl()
if awsContentStore.acl != s3.BucketOwnerFull {
t.Fatalf("Should have been set to bucket-owner-full-control, but got %s", awsContentStore.acl)
}
}
func awsConnectForTest() *s3.Bucket {
os.Setenv("AWS_ACCESS_KEY_ID", Config.Aws.AccessKeyId)
os.Setenv("AWS_SECRET_ACCESS_KEY", Config.Aws.SecretAccessKey)
auth, err := aws.EnvAuth()
perror(err)
return s3.New(auth, aws.Regions[Config.Aws.Region]).Bucket(Config.Aws.BucketName)
}
func setupAwsTest() {
bucket := awsConnectForTest()
bucket.PutBucket(s3.Private)
store, err := NewAwsContentStore()
if err != nil {
fmt.Printf("error initializing content store: %s\n", err)
os.Exit(1)
}
awsContentStore = store
}
func teardownAwsTest() {
bucket := awsConnectForTest()
// remove all bucket contents
items, err := bucket.List("", "", "", 1000)
if err != nil {
return
}
var delItems []string
if len(items.Contents) > 0 {
for _, item := range items.Contents {
if len(item.Key) < 1 {
continue
}
if strings.Contains(item.Key, "a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72") {
delItems = append(delItems, item.Key)
}
}
}
if len(delItems) > 0 {
oops := bucket.MultiDel(delItems)
if oops != nil {
fmt.Println("Oops", oops)
}
}
}