frp/test/e2e/framework/mockservers.go

86 lines
2.0 KiB
Go
Raw Normal View History

2020-06-02 22:48:55 +08:00
package framework
import (
2020-09-07 14:57:23 +08:00
"fmt"
"os"
2020-06-02 22:48:55 +08:00
"github.com/fatedier/frp/test/e2e/mock/echoserver"
"github.com/fatedier/frp/test/e2e/pkg/port"
)
const (
TCPEchoServerPort = "TCPEchoServerPort"
UDPEchoServerPort = "UDPEchoServerPort"
2020-09-07 14:57:23 +08:00
UDSEchoServerAddr = "UDSEchoServerAddr"
2020-06-02 22:48:55 +08:00
)
type MockServers struct {
tcpEchoServer *echoserver.Server
udpEchoServer *echoserver.Server
2020-09-07 14:57:23 +08:00
udsEchoServer *echoserver.Server
2020-06-02 22:48:55 +08:00
}
func NewMockServers(portAllocator *port.Allocator) *MockServers {
s := &MockServers{}
tcpPort := portAllocator.Get()
udpPort := portAllocator.Get()
s.tcpEchoServer = echoserver.New(echoserver.Options{
Type: echoserver.TCP,
BindAddr: "127.0.0.1",
BindPort: int32(tcpPort),
RepeatNum: 1,
})
s.udpEchoServer = echoserver.New(echoserver.Options{
Type: echoserver.UDP,
BindAddr: "127.0.0.1",
BindPort: int32(udpPort),
RepeatNum: 1,
})
2020-09-07 14:57:23 +08:00
udsIndex := portAllocator.Get()
udsAddr := fmt.Sprintf("%s/frp_echo_server_%d.sock", os.TempDir(), udsIndex)
os.Remove(udsAddr)
s.udsEchoServer = echoserver.New(echoserver.Options{
Type: echoserver.Unix,
BindAddr: udsAddr,
RepeatNum: 1,
})
2020-06-02 22:48:55 +08:00
return s
}
func (m *MockServers) Run() error {
if err := m.tcpEchoServer.Run(); err != nil {
return err
}
if err := m.udpEchoServer.Run(); err != nil {
return err
}
2020-09-07 14:57:23 +08:00
if err := m.udsEchoServer.Run(); err != nil {
return err
}
2020-06-02 22:48:55 +08:00
return nil
}
2020-09-07 14:57:23 +08:00
func (m *MockServers) Close() {
m.tcpEchoServer.Close()
m.udpEchoServer.Close()
m.udsEchoServer.Close()
os.Remove(m.udsEchoServer.GetOptions().BindAddr)
}
2020-06-02 22:48:55 +08:00
func (m *MockServers) GetTemplateParams() map[string]interface{} {
ret := make(map[string]interface{})
ret[TCPEchoServerPort] = m.tcpEchoServer.GetOptions().BindPort
ret[UDPEchoServerPort] = m.udpEchoServer.GetOptions().BindPort
2020-09-07 14:57:23 +08:00
ret[UDSEchoServerAddr] = m.udsEchoServer.GetOptions().BindAddr
2020-06-02 22:48:55 +08:00
return ret
}
func (m *MockServers) GetParam(key string) interface{} {
params := m.GetTemplateParams()
if v, ok := params[key]; ok {
return v
}
return nil
}