2015-02-12 15:06:15 +00:00
|
|
|
package filestore
|
|
|
|
|
|
|
|
import (
|
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"
|
2019-08-19 07:29:56 +00:00
|
|
|
"path/filepath"
|
2015-02-12 15:06:15 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-02-16 16:53:50 +00:00
|
|
|
|
2016-01-20 15:40:13 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-06-11 16:23:20 +00:00
|
|
|
"github.com/tus/tusd/pkg/handler"
|
2015-02-12 15:06:15 +00:00
|
|
|
)
|
|
|
|
|
2015-02-28 13:49:52 +00:00
|
|
|
// Test interface implementation of Filestore
|
2019-06-11 16:23:20 +00:00
|
|
|
var _ handler.DataStore = FileStore{}
|
|
|
|
var _ handler.TerminaterDataStore = FileStore{}
|
|
|
|
var _ handler.ConcaterDataStore = FileStore{}
|
|
|
|
var _ handler.LengthDeferrerDataStore = FileStore{}
|
2015-02-28 13:49:52 +00:00
|
|
|
|
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}
|
|
|
|
|
|
|
|
// Create new upload
|
2019-08-26 09:41:52 +00:00
|
|
|
upload, err := store.NewUpload(handler.FileInfo{
|
2015-02-16 16:53:50 +00:00
|
|
|
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
|
2019-08-26 09:41:52 +00:00
|
|
|
info, err := upload.GetInfo()
|
2016-01-20 15:40:13 +00:00
|
|
|
a.NoError(err)
|
|
|
|
a.EqualValues(42, info.Size)
|
|
|
|
a.EqualValues(0, info.Offset)
|
2019-06-11 16:23:20 +00:00
|
|
|
a.Equal(handler.MetaData{"hello": "world"}, info.MetaData)
|
2019-08-19 07:29:56 +00:00
|
|
|
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
|
2019-08-26 09:41:52 +00:00
|
|
|
bytesWritten, err := upload.WriteChunk(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
|
2019-08-26 09:41:52 +00:00
|
|
|
info, err = upload.GetInfo()
|
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
|
2019-08-26 09:41:52 +00:00
|
|
|
reader, err := upload.GetReader()
|
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
|
2019-08-26 09:41:52 +00:00
|
|
|
a.NoError(store.AsTerminatableUpload(upload).Terminate())
|
2015-02-28 13:47:39 +00:00
|
|
|
|
|
|
|
// Test if upload is deleted
|
2019-08-26 09:41:52 +00:00
|
|
|
upload, err = store.GetUpload(info.ID)
|
|
|
|
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
|
|
|
}
|
2015-12-26 20:23:09 +00:00
|
|
|
|
2017-01-26 20:46:06 +00:00
|
|
|
func TestMissingPath(t *testing.T) {
|
|
|
|
a := assert.New(t)
|
|
|
|
|
|
|
|
store := FileStore{"./path-that-does-not-exist"}
|
|
|
|
|
2019-08-26 09:41:52 +00:00
|
|
|
upload, err := store.NewUpload(handler.FileInfo{})
|
2017-01-26 20:46:06 +00:00
|
|
|
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)
|
2015-12-26 20:23:09 +00:00
|
|
|
}
|
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}
|
|
|
|
|
|
|
|
// Create new upload to hold concatenated upload
|
2019-08-26 09:41:52 +00:00
|
|
|
finUpload, err := store.NewUpload(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()
|
|
|
|
a.NoError(err)
|
|
|
|
finId := finInfo.ID
|
2016-01-23 21:55:08 +00:00
|
|
|
|
|
|
|
// Create three uploads for concatenating
|
|
|
|
ids := make([]string, 3)
|
|
|
|
contents := []string{
|
|
|
|
"abc",
|
|
|
|
"def",
|
|
|
|
"ghi",
|
|
|
|
}
|
|
|
|
for i := 0; i < 3; i++ {
|
2019-08-26 09:41:52 +00:00
|
|
|
upload, err := store.NewUpload(handler.FileInfo{Size: 3})
|
2016-01-23 21:55:08 +00:00
|
|
|
a.NoError(err)
|
|
|
|
|
2019-08-26 09:41:52 +00:00
|
|
|
n, err := upload.WriteChunk(0, strings.NewReader(contents[i]))
|
2016-01-23 21:55:08 +00:00
|
|
|
a.NoError(err)
|
|
|
|
a.EqualValues(3, n)
|
|
|
|
|
2019-08-26 09:41:52 +00:00
|
|
|
info, err := upload.GetInfo()
|
|
|
|
a.NoError(err)
|
|
|
|
|
|
|
|
ids[i] = info.ID
|
2016-01-23 21:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = store.ConcatUploads(finId, ids)
|
|
|
|
a.NoError(err)
|
|
|
|
|
|
|
|
// Check offset
|
2019-08-26 09:41:52 +00:00
|
|
|
finUpload, err = store.GetUpload(finId)
|
|
|
|
a.NoError(err)
|
|
|
|
|
|
|
|
info, err := finUpload.GetInfo()
|
2016-01-23 21:55:08 +00:00
|
|
|
a.NoError(err)
|
|
|
|
a.EqualValues(9, info.Size)
|
|
|
|
a.EqualValues(9, info.Offset)
|
|
|
|
|
|
|
|
// Read content
|
2019-08-26 09:41:52 +00:00
|
|
|
reader, err := finUpload.GetReader()
|
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()
|
|
|
|
}
|
2018-05-13 14:28:02 +00:00
|
|
|
|
|
|
|
func TestDeclareLength(t *testing.T) {
|
|
|
|
a := assert.New(t)
|
|
|
|
|
|
|
|
tmp, err := ioutil.TempDir("", "tusd-filestore-declare-length-")
|
|
|
|
a.NoError(err)
|
|
|
|
|
|
|
|
store := FileStore{tmp}
|
|
|
|
|
2019-08-26 09:41:52 +00:00
|
|
|
upload, err := store.NewUpload(handler.FileInfo{
|
|
|
|
Size: 0,
|
|
|
|
SizeIsDeferred: true,
|
|
|
|
})
|
2018-05-13 14:28:02 +00:00
|
|
|
a.NoError(err)
|
2019-08-26 09:41:52 +00:00
|
|
|
a.NotEqual(nil, upload)
|
2018-05-13 14:28:02 +00:00
|
|
|
|
2019-08-26 09:41:52 +00:00
|
|
|
info, err := upload.GetInfo()
|
|
|
|
a.NoError(err)
|
|
|
|
a.EqualValues(0, info.Size)
|
|
|
|
a.Equal(true, info.SizeIsDeferred)
|
2018-05-13 14:28:02 +00:00
|
|
|
|
2019-08-26 09:41:52 +00:00
|
|
|
err = store.AsLengthDeclarableUpload(upload).DeclareLength(100)
|
2018-05-13 14:28:02 +00:00
|
|
|
a.NoError(err)
|
|
|
|
|
2019-08-26 09:41:52 +00:00
|
|
|
updatedInfo, err := upload.GetInfo()
|
|
|
|
a.NoError(err)
|
|
|
|
a.EqualValues(100, updatedInfo.Size)
|
|
|
|
a.Equal(false, updatedInfo.SizeIsDeferred)
|
2018-05-13 14:28:02 +00:00
|
|
|
}
|