新增通道服务相关测试用例
This commit is contained in:
128
pkg/testutil/remote.go
Normal file
128
pkg/testutil/remote.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"platform/pkg/remote"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// MockCloudClient 是CloudClient接口的测试实现
|
||||
type MockCloudClient struct {
|
||||
// 存储预期结果的字段
|
||||
EdgesMock func(param remote.CloudEdgesReq) (*remote.CloudEdgesResp, error)
|
||||
ConnectMock func(param remote.CloudConnectReq) error
|
||||
DisconnectMock func(param remote.CloudDisconnectReq) (int, error)
|
||||
AutoQueryMock func() (remote.CloudConnectResp, error)
|
||||
|
||||
// 记录调用历史
|
||||
EdgesCalls []remote.CloudEdgesReq
|
||||
ConnectCalls []remote.CloudConnectReq
|
||||
DisconnectCalls []remote.CloudDisconnectReq
|
||||
AutoQueryCalls int
|
||||
|
||||
// 用于并发安全
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// 确保MockCloudClient实现了CloudClient接口
|
||||
var _ remote.CloudClient = (*MockCloudClient)(nil)
|
||||
|
||||
func (m *MockCloudClient) CloudEdges(param remote.CloudEdgesReq) (*remote.CloudEdgesResp, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.EdgesCalls = append(m.EdgesCalls, param)
|
||||
if m.EdgesMock != nil {
|
||||
return m.EdgesMock(param)
|
||||
}
|
||||
return &remote.CloudEdgesResp{}, nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudConnect(param remote.CloudConnectReq) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ConnectCalls = append(m.ConnectCalls, param)
|
||||
if m.ConnectMock != nil {
|
||||
return m.ConnectMock(param)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudDisconnect(param remote.CloudDisconnectReq) (int, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.DisconnectCalls = append(m.DisconnectCalls, param)
|
||||
if m.DisconnectMock != nil {
|
||||
return m.DisconnectMock(param)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudAutoQuery() (remote.CloudConnectResp, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.AutoQueryCalls++
|
||||
if m.AutoQueryMock != nil {
|
||||
return m.AutoQueryMock()
|
||||
}
|
||||
return remote.CloudConnectResp{}, nil
|
||||
}
|
||||
|
||||
// MockGatewayClient 是GatewayClient接口的测试实现
|
||||
type MockGatewayClient struct {
|
||||
// 存储预期结果的字段
|
||||
PortConfigsMock func(params []remote.PortConfigsReq) error
|
||||
PortActiveMock func(param ...remote.PortActiveReq) (map[string]remote.PortData, error)
|
||||
|
||||
// 记录调用历史
|
||||
PortConfigsCalls [][]remote.PortConfigsReq
|
||||
PortActiveCalls [][]remote.PortActiveReq
|
||||
|
||||
// 用于并发安全
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// 确保MockGatewayClient实现了GatewayClient接口
|
||||
var _ remote.GatewayClient = (*MockGatewayClient)(nil)
|
||||
|
||||
func (m *MockGatewayClient) GatewayPortConfigs(params []remote.PortConfigsReq) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.PortConfigsCalls = append(m.PortConfigsCalls, params)
|
||||
if m.PortConfigsMock != nil {
|
||||
return m.PortConfigsMock(params)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockGatewayClient) GatewayPortActive(param ...remote.PortActiveReq) (map[string]remote.PortData, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.PortActiveCalls = append(m.PortActiveCalls, param)
|
||||
if m.PortActiveMock != nil {
|
||||
return m.PortActiveMock(param...)
|
||||
}
|
||||
return map[string]remote.PortData{}, nil
|
||||
}
|
||||
|
||||
// SetupCloudClientMock 替换全局CloudClient为测试实现并在测试完成后恢复
|
||||
func SetupCloudClientMock(t *testing.T) *MockCloudClient {
|
||||
mock := &MockCloudClient{}
|
||||
remote.Cloud = mock
|
||||
|
||||
return mock
|
||||
}
|
||||
|
||||
// SetupGatewayClientMock 创建一个MockGatewayClient并提供替换函数
|
||||
func SetupGatewayClientMock(t *testing.T) *MockGatewayClient {
|
||||
mock := &MockGatewayClient{}
|
||||
remote.GatewayInitializer = func(url, username, password string) remote.GatewayClient {
|
||||
return mock
|
||||
}
|
||||
return mock
|
||||
}
|
||||
|
||||
// NewMockGatewayClient 创建一个新的MockGatewayClient
|
||||
// 保留此函数以保持向后兼容性
|
||||
func NewMockGatewayClient() *MockGatewayClient {
|
||||
return &MockGatewayClient{}
|
||||
}
|
||||
Reference in New Issue
Block a user