完善 channel remove 测试用例
This commit is contained in:
@@ -67,11 +67,47 @@ func (m *MockCloudClient) CloudAutoQuery() (remote.CloudConnectResp, error) {
|
||||
return remote.CloudConnectResp{}, nil
|
||||
}
|
||||
|
||||
// SetupCloudClientMock 替换全局CloudClient为测试实现并在测试完成后恢复
|
||||
func SetupCloudClientMock(t *testing.T) *MockCloudClient {
|
||||
mock := &MockCloudClient{}
|
||||
remote.Cloud = mock
|
||||
|
||||
return mock
|
||||
}
|
||||
|
||||
// MockGatewayClient 是GatewayClient接口的测试实现
|
||||
type MockGatewayClient struct {
|
||||
Host string
|
||||
}
|
||||
|
||||
// 确保MockGatewayClient实现了GatewayClient接口
|
||||
var _ remote.GatewayClient = (*MockGatewayClient)(nil)
|
||||
|
||||
func (m *MockGatewayClient) GatewayPortConfigs(params []remote.PortConfigsReq) error {
|
||||
testGatewayBase.mu.Lock()
|
||||
defer testGatewayBase.mu.Unlock()
|
||||
testGatewayBase.PortConfigsCalls = append(testGatewayBase.PortConfigsCalls, params)
|
||||
if testGatewayBase.PortConfigsMock != nil {
|
||||
return testGatewayBase.PortConfigsMock(m, params)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockGatewayClient) GatewayPortActive(param ...remote.PortActiveReq) (map[string]remote.PortData, error) {
|
||||
testGatewayBase.mu.Lock()
|
||||
defer testGatewayBase.mu.Unlock()
|
||||
testGatewayBase.PortActiveCalls = append(testGatewayBase.PortActiveCalls, param)
|
||||
if testGatewayBase.PortActiveMock != nil {
|
||||
return testGatewayBase.PortActiveMock(m, param...)
|
||||
}
|
||||
return map[string]remote.PortData{}, nil
|
||||
}
|
||||
|
||||
type GatewayClientIns struct {
|
||||
|
||||
// 存储预期结果的字段
|
||||
PortConfigsMock func(params []remote.PortConfigsReq) error
|
||||
PortActiveMock func(param ...remote.PortActiveReq) (map[string]remote.PortData, error)
|
||||
PortConfigsMock func(c *MockGatewayClient, params []remote.PortConfigsReq) error
|
||||
PortActiveMock func(c *MockGatewayClient, param ...remote.PortActiveReq) (map[string]remote.PortData, error)
|
||||
|
||||
// 记录调用历史
|
||||
PortConfigsCalls [][]remote.PortConfigsReq
|
||||
@@ -81,48 +117,14 @@ type MockGatewayClient struct {
|
||||
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
|
||||
}
|
||||
var testGatewayBase = &GatewayClientIns{}
|
||||
|
||||
// SetupGatewayClientMock 创建一个MockGatewayClient并提供替换函数
|
||||
func SetupGatewayClientMock(t *testing.T) *MockGatewayClient {
|
||||
mock := &MockGatewayClient{}
|
||||
func SetupGatewayClientMock(t *testing.T) *GatewayClientIns {
|
||||
remote.GatewayInitializer = func(url, username, password string) remote.GatewayClient {
|
||||
return mock
|
||||
return &MockGatewayClient{
|
||||
Host: url,
|
||||
}
|
||||
}
|
||||
return mock
|
||||
}
|
||||
|
||||
// NewMockGatewayClient 创建一个新的MockGatewayClient
|
||||
// 保留此函数以保持向后兼容性
|
||||
func NewMockGatewayClient() *MockGatewayClient {
|
||||
return &MockGatewayClient{}
|
||||
return testGatewayBase
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user