Skip to content

Commit 77a9fab

Browse files
authored
Merge pull request #112 from discentem/pr112
add test for s3 cleanup()
2 parents c90f933 + 9facc85 commit 77a9fab

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed

internal/stores/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ go_test(
2929
srcs = [
3030
"gcs_test.go",
3131
"s3_test.go",
32+
"stores_test.go",
3233
],
3334
embed = [":stores"],
3435
deps = [

internal/stores/s3_test.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package stores
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
8+
"os"
79
"testing"
810
"time"
911

@@ -115,11 +117,67 @@ func TestS3StoreUpload(t *testing.T) {
115117
assert.NoError(t, err)
116118

117119
b, _ := afero.ReadFile(*memfs, "test.cfile")
118-
assert.Equal(t, string(b), `{
120+
assert.Equal(t, `{
119121
"name": "test",
120122
"checksum": "4df3c3f68fcc83b27e9d42c90431a72499f17875c81a599b566c9889b9696703",
121123
"date_modified": "2014-11-12T11:45:26.371Z"
122-
}`)
124+
}`, string(b))
125+
}
126+
127+
type s3FailServer struct{}
128+
129+
func (s s3FailServer) Download(
130+
ctx context.Context,
131+
w io.WriterAt,
132+
input *s3.GetObjectInput,
133+
options ...func(*s3manager.Downloader)) (n int64, err error) {
134+
return 0, errors.New("s3FailServer download failed")
135+
}
136+
137+
func (s s3FailServer) Upload(ctx context.Context,
138+
input *s3.PutObjectInput,
139+
opts ...func(*s3manager.Uploader)) (
140+
*s3manager.UploadOutput, error,
141+
) {
142+
return nil, errors.New("s3FailServer upload failed")
143+
}
144+
145+
func TestS3StoreUploadFailCleanup(t *testing.T) {
146+
mTime, _ := time.Parse("2006-01-02T15:04:05.000Z", "2014-11-12T11:45:26.371Z")
147+
memfs, err := testutils.MemMapFsWith(map[string]testutils.MapFile{
148+
"test": {
149+
Content: []byte("bla"),
150+
ModTime: &mTime,
151+
},
152+
// create .cfile to ensure when Upload fails that this gets cleaned up
153+
"test.cfile": {
154+
Content: []byte("bla"),
155+
ModTime: &mTime,
156+
},
157+
})
158+
assert.NoError(t, err)
159+
160+
// intentionally broken Uploads
161+
S3ServerFail := s3FailServer{}
162+
163+
store := S3Store{
164+
Options: Options{
165+
BackendAddress: "s3://test",
166+
MetadataFileExtension: "cfile",
167+
},
168+
fsys: *memfs,
169+
awsRegion: "us-east-1",
170+
s3Uploader: S3ServerFail,
171+
s3Downloader: S3ServerFail,
172+
}
173+
174+
// Upload expected to failed
175+
err = store.Upload(context.Background(), "test")
176+
assert.Error(t, err)
177+
178+
// Ensure this file doesn't exist to confirm cleanup() inside store.Upload succeeded
179+
_, err = afero.ReadFile(*memfs, "test.cfile")
180+
assert.Equal(t, true, os.IsNotExist(err))
123181
}
124182

125183
func TestS3StoreRetrieve(t *testing.T) {

internal/stores/stores_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package stores
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/discentem/cavorite/internal/testutils"
9+
"github.com/spf13/afero"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
type simpleStore struct {
14+
fsys afero.Fs
15+
}
16+
17+
func (s simpleStore) Upload(ctx context.Context, objects ...string) error {
18+
return nil
19+
}
20+
func (s simpleStore) Retrieve(ctx context.Context, objects ...string) error {
21+
return nil
22+
}
23+
func (s simpleStore) GetOptions() Options {
24+
return Options{
25+
MetadataFileExtension: "cfile",
26+
}
27+
}
28+
func (s simpleStore) GetFsys() afero.Fs {
29+
return s.fsys
30+
}
31+
32+
func TestWriteMetadataToFsys(t *testing.T) {
33+
mTime, _ := time.Parse("2006-01-02T15:04:05.000Z", "2014-11-12T11:45:26.371Z")
34+
memfs, _ := testutils.MemMapFsWith(map[string]testutils.MapFile{
35+
"thing/a/whatever": {
36+
Content: []byte(`blah`),
37+
ModTime: &mTime,
38+
},
39+
})
40+
store := simpleStore{
41+
fsys: *memfs,
42+
}
43+
fs := *memfs
44+
fi, err := fs.Open("thing/a/whatever")
45+
assert.NoError(t, err)
46+
47+
_, err = WriteMetadataToFsys(store, "thing/a/whatever", fi)
48+
assert.NoError(t, err)
49+
50+
b, _ := afero.ReadFile(*memfs, "thing/a/whatever.cfile")
51+
assert.Equal(t, string(b), `{
52+
"name": "whatever",
53+
"checksum": "8b7df143d91c716ecfa5fc1730022f6b421b05cedee8fd52b1fc65a96030ad52",
54+
"date_modified": "2014-11-12T11:45:26.371Z"
55+
}`)
56+
57+
}

internal/testutils/memfs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package testutils
22

33
import (
4+
"os"
5+
"path/filepath"
46
"time"
57

68
"github.com/spf13/afero"
@@ -15,6 +17,10 @@ type MapFile struct {
1517
func MemMapFsWith(files map[string]MapFile) (*afero.Fs, error) {
1618
memfsys := afero.NewMemMapFs()
1719
for fname, mfile := range files {
20+
err := memfsys.MkdirAll(filepath.Dir(fname), os.ModeDir)
21+
if err != nil {
22+
return nil, err
23+
}
1824
afile, err := memfsys.Create(fname)
1925
if err != nil {
2026
return nil, err

0 commit comments

Comments
 (0)