Skip to content

Commit

Permalink
Merge pull request #112 from discentem/pr112
Browse files Browse the repository at this point in the history
add test for s3 cleanup()
  • Loading branch information
discentem authored May 31, 2023
2 parents c90f933 + 9facc85 commit 77a9fab
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
1 change: 1 addition & 0 deletions internal/stores/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ go_test(
srcs = [
"gcs_test.go",
"s3_test.go",
"stores_test.go",
],
embed = [":stores"],
deps = [
Expand Down
62 changes: 60 additions & 2 deletions internal/stores/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package stores

import (
"context"
"errors"
"fmt"
"io"
"os"
"testing"
"time"

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

b, _ := afero.ReadFile(*memfs, "test.cfile")
assert.Equal(t, string(b), `{
assert.Equal(t, `{
"name": "test",
"checksum": "4df3c3f68fcc83b27e9d42c90431a72499f17875c81a599b566c9889b9696703",
"date_modified": "2014-11-12T11:45:26.371Z"
}`)
}`, string(b))
}

type s3FailServer struct{}

func (s s3FailServer) Download(
ctx context.Context,
w io.WriterAt,
input *s3.GetObjectInput,
options ...func(*s3manager.Downloader)) (n int64, err error) {
return 0, errors.New("s3FailServer download failed")
}

func (s s3FailServer) Upload(ctx context.Context,
input *s3.PutObjectInput,
opts ...func(*s3manager.Uploader)) (
*s3manager.UploadOutput, error,
) {
return nil, errors.New("s3FailServer upload failed")
}

func TestS3StoreUploadFailCleanup(t *testing.T) {
mTime, _ := time.Parse("2006-01-02T15:04:05.000Z", "2014-11-12T11:45:26.371Z")
memfs, err := testutils.MemMapFsWith(map[string]testutils.MapFile{
"test": {
Content: []byte("bla"),
ModTime: &mTime,
},
// create .cfile to ensure when Upload fails that this gets cleaned up
"test.cfile": {
Content: []byte("bla"),
ModTime: &mTime,
},
})
assert.NoError(t, err)

// intentionally broken Uploads
S3ServerFail := s3FailServer{}

store := S3Store{
Options: Options{
BackendAddress: "s3://test",
MetadataFileExtension: "cfile",
},
fsys: *memfs,
awsRegion: "us-east-1",
s3Uploader: S3ServerFail,
s3Downloader: S3ServerFail,
}

// Upload expected to failed
err = store.Upload(context.Background(), "test")
assert.Error(t, err)

// Ensure this file doesn't exist to confirm cleanup() inside store.Upload succeeded
_, err = afero.ReadFile(*memfs, "test.cfile")
assert.Equal(t, true, os.IsNotExist(err))
}

func TestS3StoreRetrieve(t *testing.T) {
Expand Down
57 changes: 57 additions & 0 deletions internal/stores/stores_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package stores

import (
"context"
"testing"
"time"

"github.com/discentem/cavorite/internal/testutils"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)

type simpleStore struct {
fsys afero.Fs
}

func (s simpleStore) Upload(ctx context.Context, objects ...string) error {
return nil
}
func (s simpleStore) Retrieve(ctx context.Context, objects ...string) error {
return nil
}
func (s simpleStore) GetOptions() Options {
return Options{
MetadataFileExtension: "cfile",
}
}
func (s simpleStore) GetFsys() afero.Fs {
return s.fsys
}

func TestWriteMetadataToFsys(t *testing.T) {
mTime, _ := time.Parse("2006-01-02T15:04:05.000Z", "2014-11-12T11:45:26.371Z")
memfs, _ := testutils.MemMapFsWith(map[string]testutils.MapFile{
"thing/a/whatever": {
Content: []byte(`blah`),
ModTime: &mTime,
},
})
store := simpleStore{
fsys: *memfs,
}
fs := *memfs
fi, err := fs.Open("thing/a/whatever")
assert.NoError(t, err)

_, err = WriteMetadataToFsys(store, "thing/a/whatever", fi)
assert.NoError(t, err)

b, _ := afero.ReadFile(*memfs, "thing/a/whatever.cfile")
assert.Equal(t, string(b), `{
"name": "whatever",
"checksum": "8b7df143d91c716ecfa5fc1730022f6b421b05cedee8fd52b1fc65a96030ad52",
"date_modified": "2014-11-12T11:45:26.371Z"
}`)

}
6 changes: 6 additions & 0 deletions internal/testutils/memfs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package testutils

import (
"os"
"path/filepath"
"time"

"github.com/spf13/afero"
Expand All @@ -15,6 +17,10 @@ type MapFile struct {
func MemMapFsWith(files map[string]MapFile) (*afero.Fs, error) {
memfsys := afero.NewMemMapFs()
for fname, mfile := range files {
err := memfsys.MkdirAll(filepath.Dir(fname), os.ModeDir)
if err != nil {
return nil, err
}
afile, err := memfsys.Create(fname)
if err != nil {
return nil, err
Expand Down

0 comments on commit 77a9fab

Please sign in to comment.