tusd/pkg/filestore/filestore_test.go

179 lines
4.1 KiB
Go
Raw Permalink Normal View History

2015-02-12 15:06:15 +00:00
package filestore
import (
"context"
2015-02-28 15:12:33 +00:00
"io"
2015-02-12 15:06:15 +00:00
"io/ioutil"
2015-02-28 13:47:39 +00:00
"os"
"path/filepath"
2015-02-12 15:06:15 +00:00
"strings"
"testing"
2016-01-20 15:40:13 +00:00
"github.com/stretchr/testify/assert"
"github.com/tus/tusd/pkg/handler"
2015-02-12 15:06:15 +00:00
)
// Test interface implementation of Filestore
var _ handler.DataStore = FileStore{}
var _ handler.TerminaterDataStore = FileStore{}
var _ handler.ConcaterDataStore = FileStore{}
var _ handler.LengthDeferrerDataStore = FileStore{}
2015-02-12 15:06:15 +00:00
func TestFilestore(t *testing.T) {
2016-01-20 15:40:13 +00:00
a := assert.New(t)
2015-02-12 15:06:15 +00:00
tmp, err := ioutil.TempDir("", "tusd-filestore-")
2016-01-20 15:40:13 +00:00
a.NoError(err)
2015-02-12 15:06:15 +00:00
store := FileStore{tmp}
ctx := context.Background()
2015-02-12 15:06:15 +00:00
// Create new upload
upload, err := store.NewUpload(ctx, handler.FileInfo{
Size: 42,
MetaData: map[string]string{
"hello": "world",
},
2015-02-12 15:06:15 +00:00
})
2016-01-20 15:40:13 +00:00
a.NoError(err)
2019-08-26 09:41:52 +00:00
a.NotEqual(nil, upload)
2015-02-12 15:06:15 +00:00
// Check info without writing
info, err := upload.GetInfo(ctx)
2016-01-20 15:40:13 +00:00
a.NoError(err)
a.EqualValues(42, info.Size)
a.EqualValues(0, info.Offset)
a.Equal(handler.MetaData{"hello": "world"}, info.MetaData)
a.Equal(2, len(info.Storage))
a.Equal("filestore", info.Storage["Type"])
2019-08-26 09:41:52 +00:00
a.Equal(filepath.Join(tmp, info.ID), info.Storage["Path"])
2015-02-12 15:06:15 +00:00
// Write data to upload
bytesWritten, err := upload.WriteChunk(ctx, 0, strings.NewReader("hello world"))
2016-01-20 15:40:13 +00:00
a.NoError(err)
a.EqualValues(len("hello world"), bytesWritten)
2015-02-12 15:06:15 +00:00
// Check new offset
info, err = upload.GetInfo(ctx)
2016-01-20 15:40:13 +00:00
a.NoError(err)
a.EqualValues(42, info.Size)
a.EqualValues(11, info.Offset)
2015-02-12 15:06:15 +00:00
// Read content
reader, err := upload.GetReader(ctx)
2016-01-20 15:40:13 +00:00
a.NoError(err)
2015-02-12 15:06:15 +00:00
content, err := ioutil.ReadAll(reader)
2016-01-20 15:40:13 +00:00
a.NoError(err)
a.Equal("hello world", string(content))
2015-02-28 15:12:33 +00:00
reader.(io.Closer).Close()
2015-02-28 13:47:39 +00:00
// Terminate upload
a.NoError(store.AsTerminatableUpload(upload).Terminate(ctx))
2015-02-28 13:47:39 +00:00
// Test if upload is deleted
upload, err = store.GetUpload(ctx, info.ID)
2019-08-26 09:41:52 +00:00
a.Equal(nil, upload)
2016-01-20 15:40:13 +00:00
a.True(os.IsNotExist(err))
2015-02-12 15:06:15 +00:00
}
func TestMissingPath(t *testing.T) {
a := assert.New(t)
store := FileStore{"./path-that-does-not-exist"}
ctx := context.Background()
upload, err := store.NewUpload(ctx, handler.FileInfo{})
a.Error(err)
2019-08-26 09:41:52 +00:00
a.Equal("upload directory does not exist: ./path-that-does-not-exist", err.Error())
a.Equal(nil, upload)
}
2016-01-23 21:55:08 +00:00
func TestConcatUploads(t *testing.T) {
a := assert.New(t)
tmp, err := ioutil.TempDir("", "tusd-filestore-concat-")
a.NoError(err)
store := FileStore{tmp}
ctx := context.Background()
2016-01-23 21:55:08 +00:00
// Create new upload to hold concatenated upload
finUpload, err := store.NewUpload(ctx, handler.FileInfo{Size: 9})
2016-01-23 21:55:08 +00:00
a.NoError(err)
2019-08-26 09:41:52 +00:00
a.NotEqual(nil, finUpload)
finInfo, err := finUpload.GetInfo(ctx)
2019-08-26 09:41:52 +00:00
a.NoError(err)
finId := finInfo.ID
2016-01-23 21:55:08 +00:00
// Create three uploads for concatenating
partialUploads := make([]handler.Upload, 3)
2016-01-23 21:55:08 +00:00
contents := []string{
"abc",
"def",
"ghi",
}
for i := 0; i < 3; i++ {
upload, err := store.NewUpload(ctx, handler.FileInfo{Size: 3})
2016-01-23 21:55:08 +00:00
a.NoError(err)
n, err := upload.WriteChunk(ctx, 0, strings.NewReader(contents[i]))
2016-01-23 21:55:08 +00:00
a.NoError(err)
a.EqualValues(3, n)
partialUploads[i] = upload
2016-01-23 21:55:08 +00:00
}
err = store.AsConcatableUpload(finUpload).ConcatUploads(ctx, partialUploads)
2016-01-23 21:55:08 +00:00
a.NoError(err)
// Check offset
finUpload, err = store.GetUpload(ctx, finId)
2019-08-26 09:41:52 +00:00
a.NoError(err)
info, err := finUpload.GetInfo(ctx)
2016-01-23 21:55:08 +00:00
a.NoError(err)
a.EqualValues(9, info.Size)
a.EqualValues(9, info.Offset)
// Read content
reader, err := finUpload.GetReader(ctx)
2016-01-23 21:55:08 +00:00
a.NoError(err)
content, err := ioutil.ReadAll(reader)
a.NoError(err)
a.Equal("abcdefghi", string(content))
reader.(io.Closer).Close()
}
func TestDeclareLength(t *testing.T) {
a := assert.New(t)
tmp, err := ioutil.TempDir("", "tusd-filestore-declare-length-")
a.NoError(err)
store := FileStore{tmp}
ctx := context.Background()
upload, err := store.NewUpload(ctx, handler.FileInfo{
2019-08-26 09:41:52 +00:00
Size: 0,
SizeIsDeferred: true,
})
a.NoError(err)
2019-08-26 09:41:52 +00:00
a.NotEqual(nil, upload)
info, err := upload.GetInfo(ctx)
2019-08-26 09:41:52 +00:00
a.NoError(err)
a.EqualValues(0, info.Size)
a.Equal(true, info.SizeIsDeferred)
err = store.AsLengthDeclarableUpload(upload).DeclareLength(ctx, 100)
a.NoError(err)
updatedInfo, err := upload.GetInfo(ctx)
2019-08-26 09:41:52 +00:00
a.NoError(err)
a.EqualValues(100, updatedInfo.Size)
a.Equal(false, updatedInfo.SizeIsDeferred)
}