Fix error in memorystore
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Torjus Håkestad 2022-01-24 23:18:13 +01:00
parent ee761d4006
commit 150ffc3400
4 changed files with 135 additions and 79 deletions

View File

@ -7,20 +7,23 @@ import (
) )
func TestFSFileStore(t *testing.T) { func TestFSFileStore(t *testing.T) {
dir := t.TempDir() newFunc := func() files.FileStore {
s, err := files.NewFSFileStore(dir) dir := t.TempDir()
if err != nil { s, err := files.NewFSFileStore(dir)
t.Fatalf("Error creating store: %s", err) if err != nil {
t.Fatalf("Error creating store: %s", err)
}
return s
} }
RunFilestoreTest(s, t) RunFilestoreTest(newFunc, t)
persistentDir := t.TempDir() persistentDir := t.TempDir()
newFunc := func() files.FileStore { persistentFunc := func() files.FileStore {
s, err := files.NewFSFileStore(persistentDir) s, err := files.NewFSFileStore(persistentDir)
if err != nil { if err != nil {
t.Fatalf("Error creating store: %s", err) t.Fatalf("Error creating store: %s", err)
} }
return s return s
} }
RunPersistentFilestoreTest(newFunc, t) RunPersistentFilestoreTest(persistentFunc, t)
} }

View File

@ -57,11 +57,16 @@ func (s *MemoryFileStore) Get(id string) (*File, error) {
return nil, fmt.Errorf("no such item") return nil, fmt.Errorf("no such item")
} }
body := new(bytes.Buffer)
if _, err := body.Write(fd.Body.Bytes()); err != nil {
return nil, err
}
f := &File{ f := &File{
ID: fd.ID, ID: fd.ID,
MaxViews: fd.MaxViews, MaxViews: fd.MaxViews,
ExpiresOn: fd.ExpiresOn, ExpiresOn: fd.ExpiresOn,
Body: io.NopCloser(&fd.Body), Body: io.NopCloser(body),
FileSize: fd.FileSize, FileSize: fd.FileSize,
} }

View File

@ -7,7 +7,9 @@ import (
) )
func TestMemoryFileStore(t *testing.T) { func TestMemoryFileStore(t *testing.T) {
s := files.NewMemoryFileStore() newFunc := func() files.FileStore {
return files.NewMemoryFileStore()
}
RunFilestoreTest(s, t) RunFilestoreTest(newFunc, t)
} }

View File

@ -12,8 +12,11 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
func RunFilestoreTest(s files.FileStore, t *testing.T) { var ignoreBody = cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
func RunFilestoreTest(newStoreFunc func() files.FileStore, t *testing.T) {
t.Run("Basic", func(t *testing.T) { t.Run("Basic", func(t *testing.T) {
s := newStoreFunc()
// Create // Create
dataString := "TEST_LOL_OMG" dataString := "TEST_LOL_OMG"
id := uuid.Must(uuid.NewRandom()).String() id := uuid.Must(uuid.NewRandom()).String()
@ -58,7 +61,6 @@ func RunFilestoreTest(s files.FileStore, t *testing.T) {
FileSize: int64(len(dataString)), FileSize: int64(len(dataString)),
} }
ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
if diff := cmp.Diff(retrieved, expected, ignoreBody); diff != "" { if diff := cmp.Diff(retrieved, expected, ignoreBody); diff != "" {
t.Errorf("File comparison failed: %s", diff) t.Errorf("File comparison failed: %s", diff)
} }
@ -88,84 +90,128 @@ func RunFilestoreTest(s files.FileStore, t *testing.T) {
t.Fatalf("List after delete has wrong length: %d", len(ids)) t.Fatalf("List after delete has wrong length: %d", len(ids))
} }
}) })
t.Run("MultipleGet", func(t *testing.T) {
s := newStoreFunc()
fileContents := "multiple get test !"
body := io.NopCloser(strings.NewReader(fileContents))
file := &files.File{
ID: uuid.NewString(),
OriginalFilename: "multiple.txt",
MaxViews: 999,
ExpiresOn: time.Now().Add(1 * time.Hour),
Body: body,
FileSize: int64(len(fileContents)),
}
if err := s.Store(file); err != nil {
t.Fatalf("Error storing file: %s", err)
}
first, err := s.Get(file.ID)
if err != nil {
t.Errorf("Error retrieving first file: %s", err)
}
firstBody := new(bytes.Buffer)
io.Copy(firstBody, first.Body)
first.Body.Close()
if diff := cmp.Diff(firstBody.String(), fileContents); diff != "" {
t.Fatalf("File contents mismatch: %s", diff)
}
second, err := s.Get(file.ID)
if err != nil {
t.Errorf("Error retrieving first file: %s", err)
}
secondBody := new(bytes.Buffer)
io.Copy(secondBody, second.Body)
first.Body.Close()
if diff := cmp.Diff(secondBody.String(), fileContents); diff != "" {
t.Fatalf("File contents mismatch: %s", diff)
}
})
} }
func RunPersistentFilestoreTest(newStoreFunc func() files.FileStore, t *testing.T) { func RunPersistentFilestoreTest(newStoreFunc func() files.FileStore, t *testing.T) {
s := newStoreFunc() t.Run("Basics", func(t *testing.T) {
s := newStoreFunc()
files := []struct { files := []struct {
File *files.File File *files.File
ExpectedData string ExpectedData string
}{ }{
{ {
File: &files.File{ File: &files.File{
ID: uuid.NewString(), ID: uuid.NewString(),
OriginalFilename: "testfile.txt", OriginalFilename: "testfile.txt",
MaxViews: 5, MaxViews: 5,
ExpiresOn: time.Now().Add(10 * time.Minute), ExpiresOn: time.Now().Add(10 * time.Minute),
Body: io.NopCloser(strings.NewReader("cocks!")), Body: io.NopCloser(strings.NewReader("cocks!")),
FileSize: 6, FileSize: 6,
},
ExpectedData: "cocks!",
}, },
ExpectedData: "cocks!", {
}, File: &files.File{
{ ID: uuid.NewString(),
File: &files.File{ OriginalFilename: "testfile2.txt",
ID: uuid.NewString(), MaxViews: 5,
OriginalFilename: "testfile2.txt", ExpiresOn: time.Now().Add(10 * time.Minute),
MaxViews: 5, Body: io.NopCloser(strings.NewReader("derps!")),
ExpiresOn: time.Now().Add(10 * time.Minute), FileSize: 6,
Body: io.NopCloser(strings.NewReader("derps!")), },
FileSize: 6, ExpectedData: "derps!",
}, },
ExpectedData: "derps!",
},
}
for _, f := range files {
err := s.Store(f.File)
if err != nil {
t.Fatalf("Error storing file: %s", err)
}
}
for _, f := range files {
retrieved, err := s.Get(f.File.ID)
if err != nil {
t.Fatalf("Unable to retrieve file: %s", err)
} }
ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore()) for _, f := range files {
if !cmp.Equal(retrieved, f.File, ignoreBody) { err := s.Store(f.File)
t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File)) if err != nil {
t.Fatalf("Error storing file: %s", err)
}
} }
buf := new(strings.Builder) for _, f := range files {
if _, err := io.Copy(buf, retrieved.Body); err != nil { retrieved, err := s.Get(f.File.ID)
t.Fatalf("Error reading from body: %s", err) if err != nil {
} t.Fatalf("Unable to retrieve file: %s", err)
retrieved.Body.Close() }
if buf.String() != f.ExpectedData {
t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
}
}
// Reopen store, and fetch again if !cmp.Equal(retrieved, f.File, ignoreBody) {
s = newStoreFunc() t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File))
for _, f := range files { }
retrieved, err := s.Get(f.File.ID) buf := new(strings.Builder)
if err != nil { if _, err := io.Copy(buf, retrieved.Body); err != nil {
t.Fatalf("Unable to retrieve file: %s", err) t.Fatalf("Error reading from body: %s", err)
}
retrieved.Body.Close()
if buf.String() != f.ExpectedData {
t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
}
} }
ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore()) // Reopen store, and fetch again
if !cmp.Equal(retrieved, f.File, ignoreBody) { s = newStoreFunc()
t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File)) for _, f := range files {
retrieved, err := s.Get(f.File.ID)
if err != nil {
t.Fatalf("Unable to retrieve file: %s", err)
}
if !cmp.Equal(retrieved, f.File, ignoreBody) {
t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File))
}
buf := new(strings.Builder)
if _, err := io.Copy(buf, retrieved.Body); err != nil {
t.Fatalf("Error reading from body: %s", err)
}
retrieved.Body.Close()
if buf.String() != f.ExpectedData {
t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
}
} }
buf := new(strings.Builder) })
if _, err := io.Copy(buf, retrieved.Body); err != nil {
t.Fatalf("Error reading from body: %s", err)
}
retrieved.Body.Close()
if buf.String() != f.ExpectedData {
t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
}
}
} }