新增通道服务相关测试用例
This commit is contained in:
@@ -13,15 +13,29 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type client struct {
|
||||
// CloudClient 定义云服务接口
|
||||
type CloudClient interface {
|
||||
CloudEdges(param CloudEdgesReq) (*CloudEdgesResp, error)
|
||||
CloudConnect(param CloudConnectReq) error
|
||||
CloudDisconnect(param CloudDisconnectReq) (int, error)
|
||||
CloudAutoQuery() (CloudConnectResp, error)
|
||||
}
|
||||
|
||||
// GatewayClient 定义网关接口
|
||||
type GatewayClient interface {
|
||||
GatewayPortConfigs(params []PortConfigsReq) error
|
||||
GatewayPortActive(param ...PortActiveReq) (map[string]PortData, error)
|
||||
}
|
||||
|
||||
type cloud struct {
|
||||
url string
|
||||
token string
|
||||
}
|
||||
|
||||
var Client client
|
||||
var Cloud CloudClient
|
||||
|
||||
func Init() {
|
||||
Client = client{
|
||||
Cloud = &cloud{
|
||||
url: env.RemoteAddr,
|
||||
token: env.RemoteToken,
|
||||
}
|
||||
@@ -61,7 +75,7 @@ type Edge struct {
|
||||
PacketLoss int `json:"packet_loss"`
|
||||
}
|
||||
|
||||
func (c *client) CloudEdges(param CloudEdgesReq) (*CloudEdgesResp, error) {
|
||||
func (c *cloud) CloudEdges(param CloudEdgesReq) (*CloudEdgesResp, error) {
|
||||
data := strings.Builder{}
|
||||
data.WriteString("province=")
|
||||
data.WriteString(param.Province)
|
||||
@@ -110,7 +124,7 @@ type CloudConnectReq struct {
|
||||
AutoConfig []AutoConfig `json:"auto_config,omitempty"`
|
||||
}
|
||||
|
||||
func (c *client) CloudConnect(param CloudConnectReq) error {
|
||||
func (c *cloud) CloudConnect(param CloudConnectReq) error {
|
||||
data, err := json.Marshal(param)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -165,7 +179,7 @@ type Config struct {
|
||||
Online bool `json:"online"`
|
||||
}
|
||||
|
||||
func (c *client) CloudDisconnect(param CloudDisconnectReq) (int, error) {
|
||||
func (c *cloud) CloudDisconnect(param CloudDisconnectReq) (int, error) {
|
||||
data, err := json.Marshal(param)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -208,7 +222,7 @@ func (c *client) CloudDisconnect(param CloudDisconnectReq) (int, error) {
|
||||
|
||||
type CloudConnectResp map[string][]AutoConfig
|
||||
|
||||
func (c *client) CloudAutoQuery() (CloudConnectResp, error) {
|
||||
func (c *cloud) CloudAutoQuery() (CloudConnectResp, error) {
|
||||
resp, err := c.requestCloud("GET", "/auto_query", "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -237,7 +251,7 @@ func (c *client) CloudAutoQuery() (CloudConnectResp, error) {
|
||||
|
||||
// endregion
|
||||
|
||||
func (c *client) requestCloud(method string, url string, data string) (*http.Response, error) {
|
||||
func (c *cloud) requestCloud(method string, url string, data string) (*http.Response, error) {
|
||||
|
||||
url = fmt.Sprintf("%s/api%s", c.url, url)
|
||||
req, err := http.NewRequest(method, url, strings.NewReader(data))
|
||||
@@ -274,14 +288,22 @@ func (c *client) requestCloud(method string, url string, data string) (*http.Res
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
type Gateway struct {
|
||||
type gateway struct {
|
||||
url string
|
||||
username string
|
||||
password string
|
||||
}
|
||||
|
||||
func InitGateway(url, username, password string) *Gateway {
|
||||
return &Gateway{url, username, password}
|
||||
var GatewayInitializer = func(url, username, password string) GatewayClient {
|
||||
return &gateway{
|
||||
url: url,
|
||||
username: username,
|
||||
password: password,
|
||||
}
|
||||
}
|
||||
|
||||
func NewGateway(url, username, password string) GatewayClient {
|
||||
return GatewayInitializer(url, username, password)
|
||||
}
|
||||
|
||||
// region gateway:/port/configs
|
||||
@@ -306,7 +328,7 @@ type AutoEdgeConfig struct {
|
||||
PacketLoss int `json:"packet_loss,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Gateway) GatewayPortConfigs(params []PortConfigsReq) error {
|
||||
func (c *gateway) GatewayPortConfigs(params []PortConfigsReq) error {
|
||||
if len(params) == 0 {
|
||||
return errors.New("params is empty")
|
||||
}
|
||||
@@ -372,7 +394,7 @@ type PortData struct {
|
||||
Userpass string `json:"userpass"`
|
||||
}
|
||||
|
||||
func (c *Gateway) GatewayPortActive(param ...PortActiveReq) (map[string]PortData, error) {
|
||||
func (c *gateway) GatewayPortActive(param ...PortActiveReq) (map[string]PortData, error) {
|
||||
_param := PortActiveReq{}
|
||||
if len(param) != 0 {
|
||||
_param = param[0]
|
||||
@@ -431,7 +453,7 @@ func (c *Gateway) GatewayPortActive(param ...PortActiveReq) (map[string]PortData
|
||||
|
||||
// endregion
|
||||
|
||||
func (c *Gateway) requestGateway(method string, url string, data string) (*http.Response, error) {
|
||||
func (c *gateway) requestGateway(method string, url string, data string) (*http.Response, error) {
|
||||
url = fmt.Sprintf("http://%s:%s@%s:9990%s", c.username, c.password, c.url, url)
|
||||
req, err := http.NewRequest(method, url, strings.NewReader(data))
|
||||
if err != nil {
|
||||
|
||||
40
pkg/testutil/db.go
Normal file
40
pkg/testutil/db.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"platform/pkg/orm"
|
||||
q "platform/web/queries"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// SetupDBTest 创建一个带有 sqlmock 的 GORM 数据库连接
|
||||
func SetupDBTest(t *testing.T) sqlmock.Sqlmock {
|
||||
|
||||
// 创建 sqlmock
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("创建sqlmock失败: %v", err)
|
||||
}
|
||||
|
||||
// 配置 gorm 连接
|
||||
gormDB, err := gorm.Open(postgres.New(postgres.Config{
|
||||
Conn: db,
|
||||
PreferSimpleProtocol: true, // 禁用 prepared statement 缓存
|
||||
}), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("gorm 打开数据库连接失败: %v", err)
|
||||
}
|
||||
|
||||
q.SetDefault(gormDB)
|
||||
orm.DB = gormDB
|
||||
|
||||
// 使用 t.Cleanup 确保测试结束后关闭数据库连接
|
||||
t.Cleanup(func() {
|
||||
db.Close()
|
||||
})
|
||||
|
||||
return mock
|
||||
}
|
||||
30
pkg/testutil/redis.go
Normal file
30
pkg/testutil/redis.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"platform/pkg/rds"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// SetupRedisTest 创建一个测试用的Redis实例
|
||||
// 返回miniredis实例,使用t.Cleanup自动清理资源
|
||||
func SetupRedisTest(t *testing.T) *miniredis.Miniredis {
|
||||
mr, err := miniredis.Run()
|
||||
if err != nil {
|
||||
t.Fatalf("设置 miniredis 失败: %v", err)
|
||||
}
|
||||
|
||||
// 替换 Redis 客户端为测试客户端
|
||||
rds.Client = redis.NewClient(&redis.Options{
|
||||
Addr: mr.Addr(),
|
||||
})
|
||||
|
||||
// 使用t.Cleanup确保测试结束后恢复原始客户端并关闭miniredis
|
||||
t.Cleanup(func() {
|
||||
mr.Close()
|
||||
})
|
||||
|
||||
return mr
|
||||
}
|
||||
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