frp/test/e2e/framework/mockservers.go

86 lines
2.4 KiB
Go
Raw Permalink Normal View History

2020-06-02 22:48:55 +08:00
package framework
import (
2020-09-07 14:57:23 +08:00
"fmt"
2021-06-18 16:48:36 +08:00
"net/http"
2020-09-07 14:57:23 +08:00
"os"
2021-06-18 16:48:36 +08:00
"github.com/fatedier/frp/test/e2e/framework/consts"
"github.com/fatedier/frp/test/e2e/mock/server"
2021-06-18 16:48:36 +08:00
"github.com/fatedier/frp/test/e2e/mock/server/httpserver"
"github.com/fatedier/frp/test/e2e/mock/server/streamserver"
2020-06-02 22:48:55 +08:00
"github.com/fatedier/frp/test/e2e/pkg/port"
)
const (
2021-06-18 16:48:36 +08:00
TCPEchoServerPort = "TCPEchoServerPort"
UDPEchoServerPort = "UDPEchoServerPort"
UDSEchoServerAddr = "UDSEchoServerAddr"
HTTPSimpleServerPort = "HTTPSimpleServerPort"
2020-06-02 22:48:55 +08:00
)
type MockServers struct {
2021-06-18 16:48:36 +08:00
tcpEchoServer server.Server
udpEchoServer server.Server
udsEchoServer server.Server
httpSimpleServer server.Server
2020-06-02 22:48:55 +08:00
}
func NewMockServers(portAllocator *port.Allocator) *MockServers {
s := &MockServers{}
tcpPort := portAllocator.Get()
udpPort := portAllocator.Get()
2021-06-18 16:48:36 +08:00
httpPort := portAllocator.Get()
s.tcpEchoServer = streamserver.New(streamserver.TCP, streamserver.WithBindPort(tcpPort))
s.udpEchoServer = streamserver.New(streamserver.UDP, streamserver.WithBindPort(udpPort))
2022-08-29 01:02:53 +08:00
s.httpSimpleServer = httpserver.New(httpserver.WithBindPort(httpPort),
httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, _ = w.Write([]byte(consts.TestString))
})),
)
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 = streamserver.New(streamserver.Unix, streamserver.WithBindAddr(udsAddr))
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
}
2023-07-21 10:30:46 +08:00
return m.httpSimpleServer.Run()
2020-06-02 22:48:55 +08:00
}
2020-09-07 14:57:23 +08:00
func (m *MockServers) Close() {
m.tcpEchoServer.Close()
m.udpEchoServer.Close()
m.udsEchoServer.Close()
2021-06-18 16:48:36 +08:00
m.httpSimpleServer.Close()
os.Remove(m.udsEchoServer.BindAddr())
2020-09-07 14:57:23 +08:00
}
2020-06-02 22:48:55 +08:00
func (m *MockServers) GetTemplateParams() map[string]interface{} {
ret := make(map[string]interface{})
ret[TCPEchoServerPort] = m.tcpEchoServer.BindPort()
ret[UDPEchoServerPort] = m.udpEchoServer.BindPort()
ret[UDSEchoServerAddr] = m.udsEchoServer.BindAddr()
2021-06-18 16:48:36 +08:00
ret[HTTPSimpleServerPort] = m.httpSimpleServer.BindPort()
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
}