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"
|
2015-02-12 15:06:15 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-02-16 16:53:50 +00:00
|
|
|
|
|
|
|
"github.com/tus/tusd"
|
2015-02-12 15:06:15 +00:00
|
|
|
)
|
|
|
|
|
2015-02-28 13:49:52 +00:00
|
|
|
// Test interface implementation of Filestore
|
|
|
|
var _ tusd.DataStore = FileStore{}
|
|
|
|
|
2015-02-12 15:06:15 +00:00
|
|
|
func TestFilestore(t *testing.T) {
|
|
|
|
tmp, err := ioutil.TempDir("", "tusd-filestore-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
store := FileStore{tmp}
|
|
|
|
|
|
|
|
// Create new upload
|
2015-02-16 16:53:50 +00:00
|
|
|
id, err := store.NewUpload(tusd.FileInfo{
|
|
|
|
Size: 42,
|
|
|
|
MetaData: map[string]string{
|
|
|
|
"hello": "world",
|
|
|
|
},
|
2015-02-12 15:06:15 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
t.Errorf("id must not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check info without writing
|
|
|
|
info, err := store.GetInfo(id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if info.Size != 42 {
|
|
|
|
t.Errorf("expected size to be 42")
|
|
|
|
}
|
|
|
|
if info.Offset != 0 {
|
|
|
|
t.Errorf("expected offset to be 0")
|
|
|
|
}
|
|
|
|
if len(info.MetaData) != 1 || info.MetaData["hello"] != "world" {
|
|
|
|
t.Errorf("expected metadata to have one value")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write data to upload
|
2015-03-23 18:02:12 +00:00
|
|
|
bytesWritten, err := store.WriteChunk(id, 0, strings.NewReader("hello world"))
|
2015-02-12 15:06:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-23 18:02:12 +00:00
|
|
|
if bytesWritten != int64(len("hello world")) {
|
|
|
|
t.Errorf("expected 11 bytes to be written")
|
|
|
|
}
|
2015-02-12 15:06:15 +00:00
|
|
|
|
|
|
|
// Check new offset
|
|
|
|
info, err = store.GetInfo(id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if info.Size != 42 {
|
|
|
|
t.Errorf("expected size to be 42")
|
|
|
|
}
|
|
|
|
if info.Offset != int64(len("hello world")) {
|
|
|
|
t.Errorf("expected offset to be 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read content
|
|
|
|
reader, err := store.GetReader(id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
content, err := ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(content) != "hello world" {
|
|
|
|
t.Errorf("expected content to be 'hello world'")
|
|
|
|
}
|
2015-02-28 15:12:33 +00:00
|
|
|
reader.(io.Closer).Close()
|
2015-02-28 13:47:39 +00:00
|
|
|
|
|
|
|
// Terminate upload
|
|
|
|
if err := store.Terminate(id); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test if upload is deleted
|
|
|
|
if _, err := store.GetInfo(id); !os.IsNotExist(err) {
|
|
|
|
t.Fatal("expected os.ErrIsNotExist")
|
|
|
|
}
|
2015-02-12 15:06:15 +00:00
|
|
|
}
|
2015-12-26 20:23:09 +00:00
|
|
|
|
|
|
|
func TestFileLocker(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "tusd-file-locker")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var locker tusd.LockerDataStore
|
|
|
|
locker = FileStore{dir}
|
|
|
|
|
|
|
|
if err := locker.LockUpload("one"); err != nil {
|
|
|
|
t.Errorf("unexpected error when locking file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := locker.LockUpload("one"); err != tusd.ErrFileLocked {
|
|
|
|
t.Errorf("expected error when locking locked file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := locker.UnlockUpload("one"); err != nil {
|
|
|
|
t.Errorf("unexpected error when unlocking file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := locker.UnlockUpload("one"); err != nil {
|
|
|
|
t.Errorf("unexpected error when unlocking file again: %s", err)
|
|
|
|
}
|
|
|
|
}
|