|
|
@ -8,6 +8,8 @@ import (
|
|
|
|
testMock "github.com/stretchr/testify/mock"
|
|
|
|
testMock "github.com/stretchr/testify/mock"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
|
@ -147,12 +149,24 @@ func (m MockRSC) Close() error {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var basepath string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
_, currentFile, _, _ := runtime.Caller(0)
|
|
|
|
|
|
|
|
basepath = filepath.Dir(currentFile)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func Path(rel string) string {
|
|
|
|
|
|
|
|
return filepath.Join(basepath, rel)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestFileSystem_Decompress(t *testing.T) {
|
|
|
|
func TestFileSystem_Decompress(t *testing.T) {
|
|
|
|
asserts := assert.New(t)
|
|
|
|
asserts := assert.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx := context.Background()
|
|
|
|
fs := FileSystem{
|
|
|
|
fs := FileSystem{
|
|
|
|
User: &model.User{Model: gorm.Model{ID: 1}},
|
|
|
|
User: &model.User{Model: gorm.Model{ID: 1}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
os.RemoveAll(util.RelativePath("tests/decompress"))
|
|
|
|
|
|
|
|
|
|
|
|
// 压缩文件不存在
|
|
|
|
// 压缩文件不存在
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -162,7 +176,7 @@ func TestFileSystem_Decompress(t *testing.T) {
|
|
|
|
// 查找压缩文件,未找到
|
|
|
|
// 查找压缩文件,未找到
|
|
|
|
mock.ExpectQuery("SELECT(.+)files(.+)").
|
|
|
|
mock.ExpectQuery("SELECT(.+)files(.+)").
|
|
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
|
|
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
|
|
|
|
err := fs.Decompress(ctx, "/1.zip", "/")
|
|
|
|
err := fs.Decompress(ctx, "/1.zip", "/", "")
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
asserts.Error(err)
|
|
|
|
asserts.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -174,7 +188,7 @@ func TestFileSystem_Decompress(t *testing.T) {
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockRSC{}, errors.New("error"))
|
|
|
|
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockRSC{}, errors.New("error"))
|
|
|
|
fs.Handler = testHandler
|
|
|
|
fs.Handler = testHandler
|
|
|
|
err := fs.Decompress(ctx, "/1.zip", "/")
|
|
|
|
err := fs.Decompress(ctx, "/1.zip", "/", "")
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
asserts.Error(err)
|
|
|
|
asserts.Error(err)
|
|
|
|
asserts.EqualError(err, "error")
|
|
|
|
asserts.EqualError(err, "error")
|
|
|
@ -188,7 +202,7 @@ func TestFileSystem_Decompress(t *testing.T) {
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockRSC{}, nil)
|
|
|
|
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockRSC{}, nil)
|
|
|
|
fs.Handler = testHandler
|
|
|
|
fs.Handler = testHandler
|
|
|
|
err := fs.Decompress(ctx, "/1.zip", "/")
|
|
|
|
err := fs.Decompress(ctx, "/1.zip", "/", "")
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
asserts.Error(err)
|
|
|
|
asserts.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -201,13 +215,13 @@ func TestFileSystem_Decompress(t *testing.T) {
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockNopRSC("1"), nil)
|
|
|
|
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockNopRSC("1"), nil)
|
|
|
|
fs.Handler = testHandler
|
|
|
|
fs.Handler = testHandler
|
|
|
|
err := fs.Decompress(ctx, "/1.zip", "/")
|
|
|
|
err := fs.Decompress(ctx, "/1.zip", "/", "")
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
asserts.Error(err)
|
|
|
|
asserts.Error(err)
|
|
|
|
asserts.EqualError(err, "read error")
|
|
|
|
asserts.Contains(err.Error(), "read error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 无效zip文件
|
|
|
|
// 无法重设上传策略
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cache.Set("setting_temp_path", "tests", 0)
|
|
|
|
cache.Set("setting_temp_path", "tests", 0)
|
|
|
|
fs.FileTarget = []model.File{{SourceName: "1.zip", Policy: model.Policy{Type: "mock"}}}
|
|
|
|
fs.FileTarget = []model.File{{SourceName: "1.zip", Policy: model.Policy{Type: "mock"}}}
|
|
|
@ -215,22 +229,7 @@ func TestFileSystem_Decompress(t *testing.T) {
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockRSC{rs: strings.NewReader("read")}, nil)
|
|
|
|
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockRSC{rs: strings.NewReader("read")}, nil)
|
|
|
|
fs.Handler = testHandler
|
|
|
|
fs.Handler = testHandler
|
|
|
|
err := fs.Decompress(ctx, "/1.zip", "/")
|
|
|
|
err := fs.Decompress(ctx, "/1.zip", "/", "")
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
|
|
|
asserts.EqualError(err, "zip: not a valid zip file")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 无法重设上传策略
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
zipFile, _ := os.Open(util.RelativePath("filesystem/tests/test.zip"))
|
|
|
|
|
|
|
|
fs.FileTarget = []model.File{{SourceName: "1.zip", Policy: model.Policy{Type: "mock"}}}
|
|
|
|
|
|
|
|
fs.FileTarget[0].Policy.ID = 1
|
|
|
|
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
|
|
|
|
testHandler.On("Get", testMock.Anything, "1.zip").Return(zipFile, nil)
|
|
|
|
|
|
|
|
fs.Handler = testHandler
|
|
|
|
|
|
|
|
err := fs.Decompress(ctx, "/1.zip", "/")
|
|
|
|
|
|
|
|
zipFile.Close()
|
|
|
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
asserts.Error(err)
|
|
|
|
asserts.Error(err)
|
|
|
|
asserts.True(util.IsEmpty(util.RelativePath("tests/decompress")))
|
|
|
|
asserts.True(util.IsEmpty(util.RelativePath("tests/decompress")))
|
|
|
@ -239,7 +238,7 @@ func TestFileSystem_Decompress(t *testing.T) {
|
|
|
|
// 无法上传,容量不足
|
|
|
|
// 无法上传,容量不足
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cache.Set("setting_max_parallel_transfer", "1", 0)
|
|
|
|
cache.Set("setting_max_parallel_transfer", "1", 0)
|
|
|
|
zipFile, _ := os.Open(util.RelativePath("filesystem/tests/test.zip"))
|
|
|
|
zipFile, _ := os.Open(Path("tests/test.zip"))
|
|
|
|
fs.FileTarget = []model.File{{SourceName: "1.zip", Policy: model.Policy{Type: "mock"}}}
|
|
|
|
fs.FileTarget = []model.File{{SourceName: "1.zip", Policy: model.Policy{Type: "mock"}}}
|
|
|
|
fs.FileTarget[0].Policy.ID = 1
|
|
|
|
fs.FileTarget[0].Policy.ID = 1
|
|
|
|
fs.User.Policy.Type = "mock"
|
|
|
|
fs.User.Policy.Type = "mock"
|
|
|
@ -247,7 +246,7 @@ func TestFileSystem_Decompress(t *testing.T) {
|
|
|
|
testHandler.On("Get", testMock.Anything, "1.zip").Return(zipFile, nil)
|
|
|
|
testHandler.On("Get", testMock.Anything, "1.zip").Return(zipFile, nil)
|
|
|
|
fs.Handler = testHandler
|
|
|
|
fs.Handler = testHandler
|
|
|
|
|
|
|
|
|
|
|
|
fs.Decompress(ctx, "/1.zip", "/")
|
|
|
|
fs.Decompress(ctx, "/1.zip", "/", "")
|
|
|
|
|
|
|
|
|
|
|
|
zipFile.Close()
|
|
|
|
zipFile.Close()
|
|
|
|
|
|
|
|
|
|
|
|