Test: pkg/task

pull/247/head
HFO4 5 years ago
parent 2b853dddd3
commit fc5b7d42c8

@ -75,10 +75,13 @@ func GetPolicyByID(ID interface{}) (Policy, error) {
// AfterFind 找到存储策略后的钩子
func (policy *Policy) AfterFind() (err error) {
// 解析存储策略设置到OptionsSerialized
err = json.Unmarshal([]byte(policy.Options), &policy.OptionsSerialized)
if policy.OptionsSerialized.FileType == nil {
policy.OptionsSerialized.FileType = []string{}
if policy.Options != "" {
err = json.Unmarshal([]byte(policy.Options), &policy.OptionsSerialized)
if policy.OptionsSerialized.FileType == nil {
policy.OptionsSerialized.FileType = []string{}
}
}
return err
}

@ -26,13 +26,13 @@ import (
func (fs *FileSystem) Compress(ctx context.Context, folderIDs, fileIDs []uint, isArchive bool) (string, error) {
// 查找待压缩目录
folders, err := model.GetFoldersByIDs(folderIDs, fs.User.ID)
if err != nil && len(folders) != 0 {
if err != nil && len(folderIDs) != 0 {
return "", ErrDBListObjects
}
// 查找待压缩文件
files, err := model.GetFilesByIDs(fileIDs, fs.User.ID)
if err != nil && len(files) != 0 {
if err != nil && len(fileIDs) != 0 {
return "", ErrDBListObjects
}

@ -44,6 +44,9 @@ func (fs *FileSystem) ValidateLegalName(ctx context.Context, name string) bool {
// ValidateFileSize 验证上传的文件大小是否超出限制
func (fs *FileSystem) ValidateFileSize(ctx context.Context, size uint64) bool {
if fs.User.Policy.MaxSize == 0 {
return true
}
return size <= fs.User.Policy.MaxSize
}

@ -0,0 +1,196 @@
package task
import (
"errors"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/cache"
"github.com/HFO4/cloudreve/pkg/util"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"testing"
)
func TestCompressTask_Props(t *testing.T) {
asserts := assert.New(t)
task := &CompressTask{
User: &model.User{},
}
asserts.NotEmpty(task.Props())
asserts.Equal(CompressTaskType, task.Type())
asserts.EqualValues(0, task.Creator())
asserts.Nil(task.Model())
}
func TestCompressTask_SetStatus(t *testing.T) {
asserts := assert.New(t)
task := &CompressTask{
User: &model.User{},
TaskModel: &model.Task{
Model: gorm.Model{ID: 1},
},
}
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
task.SetStatus(3)
asserts.NoError(mock.ExpectationsWereMet())
}
func TestCompressTask_SetError(t *testing.T) {
asserts := assert.New(t)
task := &CompressTask{
User: &model.User{},
TaskModel: &model.Task{
Model: gorm.Model{ID: 1},
},
zipPath: "test/TestCompressTask_SetError",
}
zipFile, _ := util.CreatNestedFile("test/TestCompressTask_SetError")
zipFile.Close()
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
task.SetErrorMsg("error")
asserts.NoError(mock.ExpectationsWereMet())
asserts.False(util.Exists("test/TestCompressTask_SetError"))
asserts.Equal("error", task.GetError().Msg)
}
func TestCompressTask_Do(t *testing.T) {
asserts := assert.New(t)
task := &CompressTask{
TaskModel: &model.Task{
Model: gorm.Model{ID: 1},
},
}
// 无法创建文件系统
{
task.User = &model.User{
Policy: model.Policy{
Type: "unknown",
},
}
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
1))
mock.ExpectCommit()
task.Do()
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotEmpty(task.GetError().Msg)
}
// 压缩出错
{
task.User = &model.User{
Policy: model.Policy{
Type: "mock",
},
}
task.TaskProps.Dirs = []uint{1}
// 更新进度
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
1))
mock.ExpectCommit()
// 查找目录
mock.ExpectQuery("SELECT(.+)").WillReturnError(errors.New("error"))
// 更新错误
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
1))
mock.ExpectCommit()
task.Do()
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotEmpty(task.GetError().Msg)
}
// 上传出错
{
task.User = &model.User{
Policy: model.Policy{
Type: "mock",
MaxSize: 1,
},
}
task.TaskProps.Dirs = []uint{1}
cache.Set("setting_temp_path", "test", 0)
// 更新进度
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
1))
mock.ExpectCommit()
// 查找目录
mock.ExpectQuery("SELECT(.+)folders").
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
// 查找文件
mock.ExpectQuery("SELECT(.+)files").
WillReturnRows(sqlmock.NewRows([]string{"id"}))
// 查找子文件
mock.ExpectQuery("SELECT(.+)files").
WillReturnRows(sqlmock.NewRows([]string{"id"}))
// 查找子目录
mock.ExpectQuery("SELECT(.+)folders").
WillReturnRows(sqlmock.NewRows([]string{"id"}))
// 更新错误
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
1))
mock.ExpectCommit()
task.Do()
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotEmpty(task.GetError().Msg)
asserts.True(util.IsEmpty("test/compress"))
}
}
func TestNewCompressTask(t *testing.T) {
asserts := assert.New(t)
// 成功
{
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
job, err := NewCompressTask(&model.User{}, "/", []uint{12}, []uint{})
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotNil(job)
asserts.NoError(err)
}
// 失败
{
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
mock.ExpectRollback()
job, err := NewCompressTask(&model.User{}, "/", []uint{12}, []uint{})
asserts.NoError(mock.ExpectationsWereMet())
asserts.Nil(job)
asserts.Error(err)
}
}
func TestNewCompressTaskFromModel(t *testing.T) {
asserts := assert.New(t)
// 成功
{
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
job, err := NewCompressTaskFromModel(&model.Task{Props: "{}"})
asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(err)
asserts.NotNil(job)
}
// JSON解析失败
{
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
job, err := NewCompressTaskFromModel(&model.Task{Props: ""})
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.Nil(job)
}
}

@ -0,0 +1,139 @@
package task
import (
"errors"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"testing"
)
func TestDecompressTask_Props(t *testing.T) {
asserts := assert.New(t)
task := &DecompressTask{
User: &model.User{},
}
asserts.NotEmpty(task.Props())
asserts.Equal(DecompressTaskType, task.Type())
asserts.EqualValues(0, task.Creator())
asserts.Nil(task.Model())
}
func TestDecompressTask_SetStatus(t *testing.T) {
asserts := assert.New(t)
task := &DecompressTask{
User: &model.User{},
TaskModel: &model.Task{
Model: gorm.Model{ID: 1},
},
}
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
task.SetStatus(3)
asserts.NoError(mock.ExpectationsWereMet())
}
func TestDecompressTask_SetError(t *testing.T) {
asserts := assert.New(t)
task := &DecompressTask{
User: &model.User{},
TaskModel: &model.Task{
Model: gorm.Model{ID: 1},
},
zipPath: "test/TestCompressTask_SetError",
}
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
task.SetErrorMsg("error", nil)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Equal("error", task.GetError().Msg)
}
func TestDecompressTask_Do(t *testing.T) {
asserts := assert.New(t)
task := &DecompressTask{
TaskModel: &model.Task{
Model: gorm.Model{ID: 1},
},
}
// 无法创建文件系统
{
task.User = &model.User{
Policy: model.Policy{
Type: "unknown",
},
}
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
1))
mock.ExpectCommit()
task.Do()
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotEmpty(task.GetError().Msg)
}
// 压缩文件不存在
{
task.User = &model.User{
Policy: model.Policy{
Type: "mock",
},
}
task.TaskProps.Src = "test"
task.Do()
asserts.NotEmpty(task.GetError().Msg)
}
}
func TestNewDecompressTask(t *testing.T) {
asserts := assert.New(t)
// 成功
{
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
job, err := NewDecompressTask(&model.User{}, "/", "/")
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotNil(job)
asserts.NoError(err)
}
// 失败
{
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
mock.ExpectRollback()
job, err := NewDecompressTask(&model.User{}, "/", "/")
asserts.NoError(mock.ExpectationsWereMet())
asserts.Nil(job)
asserts.Error(err)
}
}
func TestNewDecompressTaskFromModel(t *testing.T) {
asserts := assert.New(t)
// 成功
{
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
job, err := NewDecompressTaskFromModel(&model.Task{Props: "{}"})
asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(err)
asserts.NotNil(job)
}
// JSON解析失败
{
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
job, err := NewDecompressTaskFromModel(&model.Task{Props: ""})
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.Nil(job)
}
}

@ -0,0 +1,73 @@
package task
import (
"errors"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models"
"github.com/stretchr/testify/assert"
"testing"
)
func TestRecord(t *testing.T) {
asserts := assert.New(t)
job := &TransferTask{
User: &model.User{Policy: model.Policy{Type: "unknown"}},
}
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
_, err := Record(job)
asserts.NoError(err)
}
func TestResume(t *testing.T) {
asserts := assert.New(t)
// 没有任务
{
mock.ExpectQuery("SELECT(.+)").WithArgs(Queued).WillReturnRows(sqlmock.NewRows([]string{"type"}))
Resume()
asserts.NoError(mock.ExpectationsWereMet())
}
}
func TestGetJobFromModel(t *testing.T) {
asserts := assert.New(t)
// CompressTaskType
{
task := &model.Task{
Status: 0,
Type: CompressTaskType,
}
mock.ExpectQuery("SELECT(.+)users(.+)").WillReturnError(errors.New("error"))
job, err := GetJobFromModel(task)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Nil(job)
asserts.Error(err)
}
// DecompressTaskType
{
task := &model.Task{
Status: 0,
Type: DecompressTaskType,
}
mock.ExpectQuery("SELECT(.+)users(.+)").WillReturnError(errors.New("error"))
job, err := GetJobFromModel(task)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Nil(job)
asserts.Error(err)
}
// TransferTaskType
{
task := &model.Task{
Status: 0,
Type: TransferTaskType,
}
mock.ExpectQuery("SELECT(.+)users(.+)").WillReturnError(errors.New("error"))
job, err := GetJobFromModel(task)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Nil(job)
asserts.Error(err)
}
}

@ -0,0 +1,51 @@
package task
import (
"database/sql"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/cache"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"testing"
)
var mock sqlmock.Sqlmock
// TestMain 初始化数据库Mock
func TestMain(m *testing.M) {
var db *sql.DB
var err error
db, mock, err = sqlmock.New()
if err != nil {
panic("An error was not expected when opening a stub database connection")
}
model.DB, _ = gorm.Open("mysql", db)
defer db.Close()
m.Run()
}
func TestInit(t *testing.T) {
asserts := assert.New(t)
cache.Set("setting_max_worker_num", "10", 0)
mock.ExpectQuery("SELECT(.+)").WithArgs(Queued).WillReturnRows(sqlmock.NewRows([]string{"type"}).AddRow(-1))
Init()
asserts.NoError(mock.ExpectationsWereMet())
asserts.Len(TaskPoll.idleWorker, 10)
}
func TestPool_Submit(t *testing.T) {
asserts := assert.New(t)
pool := &Pool{
idleWorker: make(chan int, 1),
}
pool.Add(1)
job := &MockJob{
DoFunc: func() {
},
}
asserts.NotPanics(func() {
pool.Submit(job)
})
}

@ -88,7 +88,8 @@ func (job *TransferTask) Do() {
}
defer fs.Recycle()
for _, file := range job.TaskProps.Src {
for index, file := range job.TaskProps.Src {
job.TaskModel.SetProgress(index)
err = fs.UploadFromPath(context.Background(), file, path.Join(job.TaskProps.Dst, filepath.Base(file)))
if err != nil {
job.SetErrorMsg("文件转存失败", err)

@ -0,0 +1,154 @@
package task
import (
"errors"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"testing"
)
func TestTransferTask_Props(t *testing.T) {
asserts := assert.New(t)
task := &TransferTask{
User: &model.User{},
}
asserts.NotEmpty(task.Props())
asserts.Equal(TransferTaskType, task.Type())
asserts.EqualValues(0, task.Creator())
asserts.Nil(task.Model())
}
func TestTransferTask_SetStatus(t *testing.T) {
asserts := assert.New(t)
task := &TransferTask{
User: &model.User{},
TaskModel: &model.Task{
Model: gorm.Model{ID: 1},
},
}
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
task.SetStatus(3)
asserts.NoError(mock.ExpectationsWereMet())
}
func TestTransferTask_SetError(t *testing.T) {
asserts := assert.New(t)
task := &TransferTask{
User: &model.User{},
TaskModel: &model.Task{
Model: gorm.Model{ID: 1},
},
}
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
task.SetErrorMsg("error", nil)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Equal("error", task.GetError().Msg)
}
func TestTransferTask_Do(t *testing.T) {
asserts := assert.New(t)
task := &TransferTask{
TaskModel: &model.Task{
Model: gorm.Model{ID: 1},
},
}
// 无法创建文件系统
{
task.TaskProps.Parent = "test/not_exist"
task.User = &model.User{
Policy: model.Policy{
Type: "unknown",
},
}
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
1))
mock.ExpectCommit()
task.Do()
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotEmpty(task.GetError().Msg)
}
// 上传出错
{
task.User = &model.User{
Policy: model.Policy{
Type: "mock",
},
}
task.TaskProps.Src = []string{"test/not_exist"}
task.TaskProps.Parent = "test/not_exist"
// 更新进度
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
1))
mock.ExpectCommit()
// 更新错误
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
1))
mock.ExpectCommit()
task.Do()
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotEmpty(task.GetError().Msg)
}
}
func TestNewTransferTask(t *testing.T) {
asserts := assert.New(t)
// 成功
{
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
job, err := NewTransferTask(1, []string{}, "/", "/")
asserts.NoError(mock.ExpectationsWereMet())
asserts.NotNil(job)
asserts.NoError(err)
}
// 失败
{
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
mock.ExpectRollback()
job, err := NewTransferTask(1, []string{}, "/", "/")
asserts.NoError(mock.ExpectationsWereMet())
asserts.Nil(job)
asserts.Error(err)
}
}
func TestNewTransferTaskFromModel(t *testing.T) {
asserts := assert.New(t)
// 成功
{
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
job, err := NewTransferTaskFromModel(&model.Task{Props: "{}"})
asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(err)
asserts.NotNil(job)
}
// JSON解析失败
{
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
job, err := NewTransferTaskFromModel(&model.Task{Props: "?"})
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.Nil(job)
}
}

@ -19,7 +19,7 @@ func (worker *GeneralWorker) Do(job Job) {
defer func() {
// 致命错误捕获
if err := recover(); err != nil {
util.Log().Debug("任务执行出错,panic")
util.Log().Debug("任务执行出错,%s", err)
job.SetError(&JobError{Msg: "致命错误"})
job.SetStatus(Error)
}

@ -0,0 +1,80 @@
package task
import (
model "github.com/HFO4/cloudreve/models"
"github.com/stretchr/testify/assert"
"testing"
)
type MockJob struct {
Err *JobError
Status int
DoFunc func()
}
func (job *MockJob) Type() int {
panic("implement me")
}
func (job *MockJob) Creator() uint {
panic("implement me")
}
func (job *MockJob) Props() string {
panic("implement me")
}
func (job *MockJob) Model() *model.Task {
panic("implement me")
}
func (job *MockJob) SetStatus(status int) {
job.Status = status
}
func (job *MockJob) Do() {
job.DoFunc()
}
func (job *MockJob) SetError(*JobError) {
}
func (job *MockJob) GetError() *JobError {
return job.Err
}
func TestGeneralWorker_Do(t *testing.T) {
asserts := assert.New(t)
worker := &GeneralWorker{}
job := &MockJob{}
// 正常
{
job.DoFunc = func() {
}
worker.Do(job)
asserts.Equal(Complete, job.Status)
}
// 有错误
{
job.DoFunc = func() {
}
job.Status = Queued
job.Err = &JobError{Msg: "error"}
worker.Do(job)
asserts.Equal(Error, job.Status)
}
// 有致命错误
{
job.DoFunc = func() {
panic("mock fatal error")
}
job.Status = Queued
job.Err = nil
worker.Do(job)
asserts.Equal(Error, job.Status)
}
}
Loading…
Cancel
Save