mirror of https://github.com/cloudreve/Cloudreve
Test: new changes in pkg: cache, cluster, conf
parent
1821923b74
commit
636ac52a3f
|
@ -37,7 +37,7 @@ func Init(path string) {
|
||||||
{
|
{
|
||||||
"both",
|
"both",
|
||||||
func() {
|
func() {
|
||||||
cache.Init()
|
cache.Init(conf.SystemConfig.Mode == "slave")
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
var Store Driver = NewMemoStore()
|
var Store Driver = NewMemoStore()
|
||||||
|
|
||||||
// Init 初始化缓存
|
// Init 初始化缓存
|
||||||
func Init() {
|
func Init(isSlave bool) {
|
||||||
if conf.RedisConfig.Server != "" && gin.Mode() != gin.TestMode {
|
if conf.RedisConfig.Server != "" && gin.Mode() != gin.TestMode {
|
||||||
Store = NewRedisStore(
|
Store = NewRedisStore(
|
||||||
10,
|
10,
|
||||||
|
@ -21,7 +21,7 @@ func Init() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.SystemConfig.Mode == "slave" {
|
if isSlave {
|
||||||
err := Store.Sets(conf.OptionOverwrite, "setting_")
|
err := Store.Sets(conf.OptionOverwrite, "setting_")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.Log().Warning("无法覆盖数据库设置: %s", err)
|
util.Log().Warning("无法覆盖数据库设置: %s", err)
|
||||||
|
|
|
@ -56,6 +56,10 @@ func TestInit(t *testing.T) {
|
||||||
asserts := assert.New(t)
|
asserts := assert.New(t)
|
||||||
|
|
||||||
asserts.NotPanics(func() {
|
asserts.NotPanics(func() {
|
||||||
Init()
|
Init(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
asserts.NotPanics(func() {
|
||||||
|
Init(true)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -413,7 +413,6 @@ func getAria2RequestBody(body *serializer.SlaveAria2Call) (io.Reader, error) {
|
||||||
return strings.NewReader(string(reqBodyEncoded)), nil
|
return strings.NewReader(string(reqBodyEncoded)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move to slave pkg
|
|
||||||
// RemoteCallback 发送远程存储策略上传回调请求
|
// RemoteCallback 发送远程存储策略上传回调请求
|
||||||
func RemoteCallback(url string, body serializer.UploadCallback) error {
|
func RemoteCallback(url string, body serializer.UploadCallback) error {
|
||||||
callbackBody, err := json.Marshal(struct {
|
callbackBody, err := json.Marshal(struct {
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
package cluster
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
|
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
|
||||||
|
"github.com/cloudreve/Cloudreve/v3/pkg/mocks/requestmock"
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -442,124 +446,114 @@ func TestSlaveCaller_DeleteTempFile(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//func TestRemoteCallback(t *testing.T) {
|
func TestRemoteCallback(t *testing.T) {
|
||||||
// asserts := assert.New(t)
|
asserts := assert.New(t)
|
||||||
//
|
|
||||||
// // 回调成功
|
// 回调成功
|
||||||
// {
|
{
|
||||||
// clientMock := request.ClientMock{}
|
clientMock := controllermock.RequestMock{}
|
||||||
// mockResp, _ := json.Marshal(serializer.Response{Code: 0})
|
mockResp, _ := json.Marshal(serializer.Response{Code: 0})
|
||||||
// clientMock.On(
|
clientMock.On(
|
||||||
// "Request",
|
"Request",
|
||||||
// "POST",
|
"POST",
|
||||||
// "http://test/test/url",
|
"http://test/test/url",
|
||||||
// testMock.Anything,
|
testMock.Anything,
|
||||||
// testMock.Anything,
|
testMock.Anything,
|
||||||
// ).Return(&request.Response{
|
).Return(&request.Response{
|
||||||
// Err: nil,
|
Err: nil,
|
||||||
// Response: &http.Response{
|
Response: &http.Response{
|
||||||
// StatusCode: 200,
|
StatusCode: 200,
|
||||||
// Body: ioutil.NopCloser(bytes.NewReader(mockResp)),
|
Body: ioutil.NopCloser(bytes.NewReader(mockResp)),
|
||||||
// },
|
},
|
||||||
// })
|
})
|
||||||
// request.GeneralClient = clientMock
|
request.GeneralClient = clientMock
|
||||||
// resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{})
|
||||||
// SourceName: "source",
|
asserts.NoError(resp)
|
||||||
// })
|
clientMock.AssertExpectations(t)
|
||||||
// asserts.NoError(resp)
|
}
|
||||||
// clientMock.AssertExpectations(t)
|
|
||||||
// }
|
// 服务端返回业务错误
|
||||||
//
|
{
|
||||||
// // 服务端返回业务错误
|
clientMock := controllermock.RequestMock{}
|
||||||
// {
|
mockResp, _ := json.Marshal(serializer.Response{Code: 401})
|
||||||
// clientMock := request.ClientMock{}
|
clientMock.On(
|
||||||
// mockResp, _ := json.Marshal(serializer.Response{Code: 401})
|
"Request",
|
||||||
// clientMock.On(
|
"POST",
|
||||||
// "Request",
|
"http://test/test/url",
|
||||||
// "POST",
|
testMock.Anything,
|
||||||
// "http://test/test/url",
|
testMock.Anything,
|
||||||
// testMock.Anything,
|
).Return(&request.Response{
|
||||||
// testMock.Anything,
|
Err: nil,
|
||||||
// ).Return(&request.Response{
|
Response: &http.Response{
|
||||||
// Err: nil,
|
StatusCode: 200,
|
||||||
// Response: &http.Response{
|
Body: ioutil.NopCloser(bytes.NewReader(mockResp)),
|
||||||
// StatusCode: 200,
|
},
|
||||||
// Body: ioutil.NopCloser(bytes.NewReader(mockResp)),
|
})
|
||||||
// },
|
request.GeneralClient = clientMock
|
||||||
// })
|
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{})
|
||||||
// request.GeneralClient = clientMock
|
asserts.EqualValues(401, resp.(serializer.AppError).Code)
|
||||||
// resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
clientMock.AssertExpectations(t)
|
||||||
// SourceName: "source",
|
}
|
||||||
// })
|
|
||||||
// asserts.EqualValues(401, resp.(serializer.AppError).Code)
|
// 无法解析回调响应
|
||||||
// clientMock.AssertExpectations(t)
|
{
|
||||||
// }
|
clientMock := controllermock.RequestMock{}
|
||||||
//
|
clientMock.On(
|
||||||
// // 无法解析回调响应
|
"Request",
|
||||||
// {
|
"POST",
|
||||||
// clientMock := request.ClientMock{}
|
"http://test/test/url",
|
||||||
// clientMock.On(
|
testMock.Anything,
|
||||||
// "Request",
|
testMock.Anything,
|
||||||
// "POST",
|
).Return(&request.Response{
|
||||||
// "http://test/test/url",
|
Err: nil,
|
||||||
// testMock.Anything,
|
Response: &http.Response{
|
||||||
// testMock.Anything,
|
StatusCode: 200,
|
||||||
// ).Return(&request.Response{
|
Body: ioutil.NopCloser(strings.NewReader("mockResp")),
|
||||||
// Err: nil,
|
},
|
||||||
// Response: &http.Response{
|
})
|
||||||
// StatusCode: 200,
|
request.GeneralClient = clientMock
|
||||||
// Body: ioutil.NopCloser(strings.NewReader("mockResp")),
|
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{})
|
||||||
// },
|
asserts.Error(resp)
|
||||||
// })
|
clientMock.AssertExpectations(t)
|
||||||
// request.GeneralClient = clientMock
|
}
|
||||||
// resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
|
||||||
// SourceName: "source",
|
// HTTP状态码非200
|
||||||
// })
|
{
|
||||||
// asserts.Error(resp)
|
clientMock := controllermock.RequestMock{}
|
||||||
// clientMock.AssertExpectations(t)
|
clientMock.On(
|
||||||
// }
|
"Request",
|
||||||
//
|
"POST",
|
||||||
// // HTTP状态码非200
|
"http://test/test/url",
|
||||||
// {
|
testMock.Anything,
|
||||||
// clientMock := request.ClientMock{}
|
testMock.Anything,
|
||||||
// clientMock.On(
|
).Return(&request.Response{
|
||||||
// "Request",
|
Err: nil,
|
||||||
// "POST",
|
Response: &http.Response{
|
||||||
// "http://test/test/url",
|
StatusCode: 404,
|
||||||
// testMock.Anything,
|
Body: ioutil.NopCloser(strings.NewReader("mockResp")),
|
||||||
// testMock.Anything,
|
},
|
||||||
// ).Return(&request.Response{
|
})
|
||||||
// Err: nil,
|
request.GeneralClient = clientMock
|
||||||
// Response: &http.Response{
|
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{})
|
||||||
// StatusCode: 404,
|
asserts.Error(resp)
|
||||||
// Body: ioutil.NopCloser(strings.NewReader("mockResp")),
|
clientMock.AssertExpectations(t)
|
||||||
// },
|
}
|
||||||
// })
|
|
||||||
// request.GeneralClient = clientMock
|
// 无法发起回调
|
||||||
// resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
{
|
||||||
// SourceName: "source",
|
clientMock := controllermock.RequestMock{}
|
||||||
// })
|
clientMock.On(
|
||||||
// asserts.Error(resp)
|
"Request",
|
||||||
// clientMock.AssertExpectations(t)
|
"POST",
|
||||||
// }
|
"http://test/test/url",
|
||||||
//
|
testMock.Anything,
|
||||||
// // 无法发起回调
|
testMock.Anything,
|
||||||
// {
|
).Return(&request.Response{
|
||||||
// clientMock := request.ClientMock{}
|
Err: errors.New("error"),
|
||||||
// clientMock.On(
|
})
|
||||||
// "Request",
|
request.GeneralClient = clientMock
|
||||||
// "POST",
|
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{})
|
||||||
// "http://test/test/url",
|
asserts.Error(resp)
|
||||||
// testMock.Anything,
|
clientMock.AssertExpectations(t)
|
||||||
// testMock.Anything,
|
}
|
||||||
// ).Return(&request.Response{
|
}
|
||||||
// Err: errors.New("error"),
|
|
||||||
// })
|
|
||||||
// request.GeneralClient = clientMock
|
|
||||||
// resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
|
||||||
// SourceName: "source",
|
|
||||||
// })
|
|
||||||
// asserts.Error(resp)
|
|
||||||
// clientMock.AssertExpectations(t)
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
|
@ -56,7 +56,11 @@ User = root
|
||||||
Password = root
|
Password = root
|
||||||
Host = 127.0.0.1:3306
|
Host = 127.0.0.1:3306
|
||||||
Name = v3
|
Name = v3
|
||||||
TablePrefix = v3_`
|
TablePrefix = v3_
|
||||||
|
|
||||||
|
[OptionOverwrite]
|
||||||
|
key=value
|
||||||
|
`
|
||||||
err := ioutil.WriteFile("testConf.ini", []byte(testCase), 0644)
|
err := ioutil.WriteFile("testConf.ini", []byte(testCase), 0644)
|
||||||
defer func() { err = os.Remove("testConf.ini") }()
|
defer func() { err = os.Remove("testConf.ini") }()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -65,6 +69,7 @@ TablePrefix = v3_`
|
||||||
asserts.NotPanics(func() {
|
asserts.NotPanics(func() {
|
||||||
Init("testConf.ini")
|
Init("testConf.ini")
|
||||||
})
|
})
|
||||||
|
asserts.Equal(OptionOverwrite["key"], "value")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMapSection(t *testing.T) {
|
func TestMapSection(t *testing.T) {
|
||||||
|
|
|
@ -7,11 +7,9 @@ import (
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/auth"
|
"github.com/cloudreve/Cloudreve/v3/pkg/auth"
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/balancer"
|
"github.com/cloudreve/Cloudreve/v3/pkg/balancer"
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/cluster"
|
"github.com/cloudreve/Cloudreve/v3/pkg/cluster"
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/task"
|
"github.com/cloudreve/Cloudreve/v3/pkg/task"
|
||||||
testMock "github.com/stretchr/testify/mock"
|
testMock "github.com/stretchr/testify/mock"
|
||||||
"io"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type NodePoolMock struct {
|
type NodePoolMock struct {
|
||||||
|
@ -151,11 +149,3 @@ func (t TaskPoolMock) Add(num int) {
|
||||||
func (t TaskPoolMock) Submit(job task.Job) {
|
func (t TaskPoolMock) Submit(job task.Job) {
|
||||||
t.Called(job)
|
t.Called(job)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestMock struct {
|
|
||||||
testMock.Mock
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r RequestMock) Request(method, target string, body io.Reader, opts ...request.Option) *request.Response {
|
|
||||||
return r.Called(method, target, body, opts).Get(0).(*request.Response)
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
package controllermock
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RequestMock struct {
|
||||||
|
mock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r RequestMock) Request(method, target string, body io.Reader, opts ...request.Option) *request.Response {
|
||||||
|
return r.Called(method, target, body, opts).Get(0).(*request.Response)
|
||||||
|
}
|
Loading…
Reference in New Issue