重构代码结构,迁移Baiyin SDK相关逻辑至globals包,并添加支付宝客户端初始化
This commit is contained in:
43
pkg/env/env.go
vendored
43
pkg/env/env.go
vendored
@@ -182,6 +182,48 @@ func loadRemote() {
|
||||
|
||||
// endregion
|
||||
|
||||
// region alipay
|
||||
|
||||
var (
|
||||
AlipayAppId string
|
||||
AlipayAppPrivateKey string
|
||||
AlipayPublicKey string
|
||||
AlipayEncryptKey string
|
||||
AlipayProduction = false
|
||||
)
|
||||
|
||||
func loadAlipay() {
|
||||
AlipayAppId := os.Getenv("ALIPAY_APP_ID")
|
||||
if AlipayAppId == "" {
|
||||
panic("环境变量 ALIPAY_APP_ID 的值不能为空")
|
||||
}
|
||||
|
||||
AlipayAppPrivateKey := os.Getenv("ALIPAY_APP_PRIVATE_KEY")
|
||||
if AlipayAppPrivateKey == "" {
|
||||
panic("环境变量 ALIPAY_APP_PRIVATE_KEY 的值不能为空")
|
||||
}
|
||||
|
||||
AlipayPublicKey := os.Getenv("ALIPAY_PUBLIC_KEY")
|
||||
if AlipayPublicKey == "" {
|
||||
panic("环境变量 ALIPAY_PUBLIC_KEY 的值不能为空")
|
||||
}
|
||||
|
||||
AlipayEncryptKey := os.Getenv("ALIPAY_ENCRYPT_KEY")
|
||||
if AlipayEncryptKey == "" {
|
||||
panic("环境变量 ALIPAY_ENCRYPT_KEY 的值不能为空")
|
||||
}
|
||||
_AlipayProduction := os.Getenv("ALIPAY_PRODUCTION")
|
||||
if _AlipayProduction != "" {
|
||||
value, err := strconv.ParseBool(_AlipayProduction)
|
||||
if err != nil {
|
||||
panic("环境变量 ALIPAY_PRODUCTION 的值不是布尔值")
|
||||
}
|
||||
AlipayProduction = value
|
||||
}
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region debug
|
||||
|
||||
var (
|
||||
@@ -228,4 +270,5 @@ func Init() {
|
||||
loadLog()
|
||||
loadDebug()
|
||||
loadRemote()
|
||||
loadAlipay()
|
||||
}
|
||||
|
||||
@@ -1,554 +0,0 @@
|
||||
package baiyin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"platform/pkg/env"
|
||||
"platform/pkg/rds"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
var Cloud CloudClient
|
||||
|
||||
func Init() {
|
||||
Cloud = &cloud{
|
||||
url: env.BaiyinAddr,
|
||||
}
|
||||
}
|
||||
|
||||
type AutoConfig struct {
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Isp string `json:"isp"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
// region cloud:/edges
|
||||
|
||||
type CloudEdgesReq struct {
|
||||
Province string
|
||||
City string
|
||||
Isp string
|
||||
Offset int
|
||||
Limit int
|
||||
}
|
||||
|
||||
type CloudEdgesResp struct {
|
||||
Edges []Edge `json:"edges"`
|
||||
Total int `json:"total"`
|
||||
Offset int `json:"offset"`
|
||||
Limit int `json:"limit"`
|
||||
}
|
||||
|
||||
type Edge struct {
|
||||
EdgesId int `json:"edges_id"`
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Isp string `json:"isp"`
|
||||
Ip string `json:"ip"`
|
||||
Rtt int `json:"rtt"`
|
||||
PacketLoss int `json:"packet_loss"`
|
||||
}
|
||||
|
||||
func (c *cloud) CloudEdges(param CloudEdgesReq) (*CloudEdgesResp, error) {
|
||||
data := strings.Builder{}
|
||||
data.WriteString("province=")
|
||||
data.WriteString(param.Province)
|
||||
data.WriteString("&city=")
|
||||
data.WriteString(param.City)
|
||||
data.WriteString("&isp=")
|
||||
data.WriteString(param.Isp)
|
||||
data.WriteString("&offset=")
|
||||
data.WriteString(strconv.Itoa(param.Offset))
|
||||
data.WriteString("&limit=")
|
||||
data.WriteString(strconv.Itoa(param.Limit))
|
||||
|
||||
resp, err := c.requestCloud("GET", "/edges?"+data.String(), "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("failed to get edges")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result CloudEdgesResp
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region cloud:/connect
|
||||
|
||||
type CloudConnectReq struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Edge []string `json:"edge,omitempty"`
|
||||
AutoConfig []AutoConfig `json:"auto_config,omitempty"`
|
||||
}
|
||||
|
||||
func (c *cloud) CloudConnect(param CloudConnectReq) error {
|
||||
data, err := json.Marshal(param)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := c.requestCloud("POST", "/connect", string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.New("failed to connect")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var result map[string]any
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result["status"] == "error" {
|
||||
return errors.New(result["details"].(string))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region cloud:/disconnect
|
||||
|
||||
type CloudDisconnectReq struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Edge []string `json:"edge,omitempty"`
|
||||
Config []Config `json:"auto_config,omitempty"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Isp string `json:"isp"`
|
||||
Count int `json:"count"`
|
||||
Online bool `json:"online"`
|
||||
}
|
||||
|
||||
func (c *cloud) CloudDisconnect(param CloudDisconnectReq) (int, error) {
|
||||
data, err := json.Marshal(param)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
resp, err := c.requestCloud("POST", "/disconnect", string(data))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return 0, errors.New("failed to disconnect")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var result map[string]any
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if result["status"] == "error" {
|
||||
return 0, errors.New(result["details"].(string))
|
||||
}
|
||||
|
||||
return int(result["disconnected_edges"].(float64)), nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region cloud:/auto_query
|
||||
|
||||
type CloudConnectResp map[string][]AutoConfig
|
||||
|
||||
func (c *cloud) CloudAutoQuery() (CloudConnectResp, error) {
|
||||
resp, err := c.requestCloud("GET", "/auto_query", "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("failed to get auto_query")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result CloudConnectResp
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
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))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
var resp *http.Response
|
||||
for i := 0; i < 2; i++ {
|
||||
token, err := c.token(i == 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("token", token)
|
||||
|
||||
if env.DebugHttpDump {
|
||||
str, err := httputil.DumpRequest(req, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("==============================")
|
||||
fmt.Println(string(str))
|
||||
}
|
||||
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if env.DebugHttpDump {
|
||||
str, err := httputil.DumpResponse(resp, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("------------------------------")
|
||||
fmt.Println(string(str))
|
||||
}
|
||||
|
||||
if resp.StatusCode != 401 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *cloud) token(refresh bool) (string, error) {
|
||||
// redis 获取令牌
|
||||
if !refresh {
|
||||
token, err := rds.Client.Get(context.Background(), "remote:token").Result()
|
||||
if err == nil && token != "" {
|
||||
return token, nil
|
||||
}
|
||||
}
|
||||
|
||||
// redis 获取失败,重新获取
|
||||
resp, err := http.Get(env.BaiyinTokenUrl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
dump, err := httputil.DumpResponse(resp, true)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
fmt.Println(string(dump))
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", errors.New("failed to get token")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var result map[string]any
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if result["code"].(float64) != 1 {
|
||||
return "", errors.New("failed to get cloud token")
|
||||
}
|
||||
|
||||
// redis 设置令牌
|
||||
token := result["token"].(string)
|
||||
err = rds.Client.Set(context.Background(), "remote:token", token, 1*time.Hour).Err()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
type gateway struct {
|
||||
url string
|
||||
username string
|
||||
password string
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
type PortConfigsReq struct {
|
||||
Port int `json:"port"`
|
||||
Edge *[]string `json:"edge,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Time int `json:"time,omitempty"`
|
||||
Status bool `json:"status"`
|
||||
Rate int `json:"rate,omitempty"`
|
||||
Whitelist *[]string `json:"whitelist,omitempty"`
|
||||
Userpass *string `json:"userpass,omitempty"`
|
||||
AutoEdgeConfig *AutoEdgeConfig `json:"auto_edge_config,omitempty"`
|
||||
}
|
||||
|
||||
type AutoEdgeConfig struct {
|
||||
Province string `json:"province,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
Isp string `json:"isp,omitempty"`
|
||||
Count *int `json:"count,omitempty"`
|
||||
PacketLoss int `json:"packet_loss,omitempty"`
|
||||
}
|
||||
|
||||
func (c *gateway) GatewayPortConfigs(params []PortConfigsReq) error {
|
||||
if len(params) == 0 {
|
||||
return errors.New("params is empty")
|
||||
}
|
||||
|
||||
data, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := c.requestGateway("POST", "/port/configs", string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.New("failed to get port configs: " + string(body))
|
||||
}
|
||||
|
||||
var result map[string]any
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result["code"].(float64) != 0 {
|
||||
return errors.New("failed to configure port")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region gateway:/port/active
|
||||
|
||||
type PortActiveReq struct {
|
||||
Port string `json:"port"`
|
||||
Active *bool `json:"active"`
|
||||
Status *bool `json:"status"`
|
||||
}
|
||||
|
||||
type PortActiveResp struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data map[string]PortData `json:"data"`
|
||||
}
|
||||
|
||||
type PortData struct {
|
||||
Edge []string `json:"edge"`
|
||||
Type string `json:"type"`
|
||||
Status bool `json:"status"`
|
||||
Active bool `json:"active"`
|
||||
Time int `json:"time"`
|
||||
Whitelist []string `json:"whitelist"`
|
||||
Userpass string `json:"userpass"`
|
||||
}
|
||||
|
||||
func (c *gateway) GatewayPortActive(param ...PortActiveReq) (map[string]PortData, error) {
|
||||
_param := PortActiveReq{}
|
||||
if len(param) != 0 {
|
||||
_param = param[0]
|
||||
}
|
||||
|
||||
path := strings.Builder{}
|
||||
path.WriteString("/port/active")
|
||||
|
||||
if _param.Port != "" {
|
||||
path.WriteString("/")
|
||||
path.WriteString(_param.Port)
|
||||
}
|
||||
|
||||
values := url.Values{}
|
||||
if _param.Active != nil {
|
||||
values.Set("active", strconv.FormatBool(*_param.Active))
|
||||
}
|
||||
if _param.Status != nil {
|
||||
values.Set("status", strconv.FormatBool(*_param.Status))
|
||||
}
|
||||
|
||||
if len(values) > 0 {
|
||||
path.WriteString("?")
|
||||
path.WriteString(values.Encode())
|
||||
}
|
||||
|
||||
resp, err := c.requestGateway("GET", path.String(), "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("failed to get port active")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result PortActiveResp
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.Code != 0 {
|
||||
return nil, errors.New(result.Msg)
|
||||
}
|
||||
|
||||
return result.Data, nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
if env.DebugHttpDump {
|
||||
str, err := httputil.DumpRequest(req, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("==============================")
|
||||
fmt.Println(string(str))
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if env.DebugHttpDump {
|
||||
str, err := httputil.DumpResponse(resp, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("------------------------------")
|
||||
fmt.Println(string(str))
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"platform/pkg/sdks/baiyin"
|
||||
g "platform/web/globals"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
@@ -9,15 +9,15 @@ import (
|
||||
// MockCloudClient 是CloudClient接口的测试实现
|
||||
type MockCloudClient struct {
|
||||
// 存储预期结果的字段
|
||||
EdgesMock func(param baiyin.CloudEdgesReq) (*baiyin.CloudEdgesResp, error)
|
||||
ConnectMock func(param baiyin.CloudConnectReq) error
|
||||
DisconnectMock func(param baiyin.CloudDisconnectReq) (int, error)
|
||||
AutoQueryMock func() (baiyin.CloudConnectResp, error)
|
||||
EdgesMock func(param g.CloudEdgesReq) (*g.CloudEdgesResp, error)
|
||||
ConnectMock func(param g.CloudConnectReq) error
|
||||
DisconnectMock func(param g.CloudDisconnectReq) (int, error)
|
||||
AutoQueryMock func() (g.CloudConnectResp, error)
|
||||
|
||||
// 记录调用历史
|
||||
EdgesCalls []baiyin.CloudEdgesReq
|
||||
ConnectCalls []baiyin.CloudConnectReq
|
||||
DisconnectCalls []baiyin.CloudDisconnectReq
|
||||
EdgesCalls []g.CloudEdgesReq
|
||||
ConnectCalls []g.CloudConnectReq
|
||||
DisconnectCalls []g.CloudDisconnectReq
|
||||
AutoQueryCalls int
|
||||
|
||||
// 用于并发安全
|
||||
@@ -25,19 +25,19 @@ type MockCloudClient struct {
|
||||
}
|
||||
|
||||
// 确保MockCloudClient实现了CloudClient接口
|
||||
var _ baiyin.CloudClient = (*MockCloudClient)(nil)
|
||||
var _ g.CloudClient = (*MockCloudClient)(nil)
|
||||
|
||||
func (m *MockCloudClient) CloudEdges(param baiyin.CloudEdgesReq) (*baiyin.CloudEdgesResp, error) {
|
||||
func (m *MockCloudClient) CloudEdges(param g.CloudEdgesReq) (*g.CloudEdgesResp, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.EdgesCalls = append(m.EdgesCalls, param)
|
||||
if m.EdgesMock != nil {
|
||||
return m.EdgesMock(param)
|
||||
}
|
||||
return &baiyin.CloudEdgesResp{}, nil
|
||||
return &g.CloudEdgesResp{}, nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudConnect(param baiyin.CloudConnectReq) error {
|
||||
func (m *MockCloudClient) CloudConnect(param g.CloudConnectReq) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ConnectCalls = append(m.ConnectCalls, param)
|
||||
@@ -47,7 +47,7 @@ func (m *MockCloudClient) CloudConnect(param baiyin.CloudConnectReq) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudDisconnect(param baiyin.CloudDisconnectReq) (int, error) {
|
||||
func (m *MockCloudClient) CloudDisconnect(param g.CloudDisconnectReq) (int, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.DisconnectCalls = append(m.DisconnectCalls, param)
|
||||
@@ -57,33 +57,33 @@ func (m *MockCloudClient) CloudDisconnect(param baiyin.CloudDisconnectReq) (int,
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudAutoQuery() (baiyin.CloudConnectResp, error) {
|
||||
func (m *MockCloudClient) CloudAutoQuery() (g.CloudConnectResp, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.AutoQueryCalls++
|
||||
if m.AutoQueryMock != nil {
|
||||
return m.AutoQueryMock()
|
||||
}
|
||||
return baiyin.CloudConnectResp{}, nil
|
||||
return g.CloudConnectResp{}, nil
|
||||
}
|
||||
|
||||
// SetupCloudClientMock 替换全局CloudClient为测试实现并在测试完成后恢复
|
||||
func SetupCloudClientMock(t *testing.T) *MockCloudClient {
|
||||
mock := &MockCloudClient{
|
||||
EdgesMock: func(param baiyin.CloudEdgesReq) (*baiyin.CloudEdgesResp, error) {
|
||||
EdgesMock: func(param g.CloudEdgesReq) (*g.CloudEdgesResp, error) {
|
||||
panic("not implemented")
|
||||
},
|
||||
ConnectMock: func(param baiyin.CloudConnectReq) error {
|
||||
ConnectMock: func(param g.CloudConnectReq) error {
|
||||
panic("not implemented")
|
||||
},
|
||||
DisconnectMock: func(param baiyin.CloudDisconnectReq) (int, error) {
|
||||
DisconnectMock: func(param g.CloudDisconnectReq) (int, error) {
|
||||
panic("not implemented")
|
||||
},
|
||||
AutoQueryMock: func() (baiyin.CloudConnectResp, error) {
|
||||
AutoQueryMock: func() (g.CloudConnectResp, error) {
|
||||
panic("not implemented")
|
||||
},
|
||||
}
|
||||
baiyin.Cloud = mock
|
||||
g.Cloud = mock
|
||||
|
||||
return mock
|
||||
}
|
||||
@@ -94,9 +94,9 @@ type MockGatewayClient struct {
|
||||
}
|
||||
|
||||
// 确保MockGatewayClient实现了GatewayClient接口
|
||||
var _ baiyin.GatewayClient = (*MockGatewayClient)(nil)
|
||||
var _ g.GatewayClient = (*MockGatewayClient)(nil)
|
||||
|
||||
func (m *MockGatewayClient) GatewayPortConfigs(params []baiyin.PortConfigsReq) error {
|
||||
func (m *MockGatewayClient) GatewayPortConfigs(params []g.PortConfigsReq) error {
|
||||
testGatewayBase.mu.Lock()
|
||||
defer testGatewayBase.mu.Unlock()
|
||||
testGatewayBase.PortConfigsCalls = append(testGatewayBase.PortConfigsCalls, params)
|
||||
@@ -106,42 +106,42 @@ func (m *MockGatewayClient) GatewayPortConfigs(params []baiyin.PortConfigsReq) e
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockGatewayClient) GatewayPortActive(param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error) {
|
||||
func (m *MockGatewayClient) GatewayPortActive(param ...g.PortActiveReq) (map[string]g.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]baiyin.PortData{}, nil
|
||||
return map[string]g.PortData{}, nil
|
||||
}
|
||||
|
||||
type GatewayClientIns struct {
|
||||
|
||||
// 存储预期结果的字段
|
||||
PortConfigsMock func(c *MockGatewayClient, params []baiyin.PortConfigsReq) error
|
||||
PortActiveMock func(c *MockGatewayClient, param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error)
|
||||
PortConfigsMock func(c *MockGatewayClient, params []g.PortConfigsReq) error
|
||||
PortActiveMock func(c *MockGatewayClient, param ...g.PortActiveReq) (map[string]g.PortData, error)
|
||||
|
||||
// 记录调用历史
|
||||
PortConfigsCalls [][]baiyin.PortConfigsReq
|
||||
PortActiveCalls [][]baiyin.PortActiveReq
|
||||
PortConfigsCalls [][]g.PortConfigsReq
|
||||
PortActiveCalls [][]g.PortActiveReq
|
||||
|
||||
// 用于并发安全
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
var testGatewayBase = &GatewayClientIns{
|
||||
PortConfigsMock: func(c *MockGatewayClient, params []baiyin.PortConfigsReq) error {
|
||||
PortConfigsMock: func(c *MockGatewayClient, params []g.PortConfigsReq) error {
|
||||
panic("not implemented")
|
||||
},
|
||||
PortActiveMock: func(c *MockGatewayClient, param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error) {
|
||||
PortActiveMock: func(c *MockGatewayClient, param ...g.PortActiveReq) (map[string]g.PortData, error) {
|
||||
panic("not implemented")
|
||||
},
|
||||
}
|
||||
|
||||
// SetupGatewayClientMock 创建一个MockGatewayClient并提供替换函数
|
||||
func SetupGatewayClientMock(t *testing.T) *GatewayClientIns {
|
||||
baiyin.GatewayInitializer = func(url, username, password string) baiyin.GatewayClient {
|
||||
g.GatewayInitializer = func(url, username, password string) g.GatewayClient {
|
||||
return &MockGatewayClient{
|
||||
Host: url,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user