frp/test/e2e/framework/mockservers.go

87 lines
2.5 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"
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), streamserver.WithEchoMode(true))
s.udpEchoServer = streamserver.New(streamserver.UDP, streamserver.WithBindPort(udpPort), streamserver.WithEchoMode(true))
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)
2021-06-18 16:48:36 +08:00
s.udsEchoServer = streamserver.New(streamserver.Unix, streamserver.WithBindAddr(udsAddr), streamserver.WithEchoMode(true))
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
}
2021-06-18 16:48:36 +08:00
if err := m.httpSimpleServer.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()
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
}