mirror of
https://github.com/fatedier/frp.git
synced 2025-07-27 07:35:07 +00:00
more e2e tests (#1845)
This commit is contained in:
@@ -3,3 +3,19 @@ package consts
|
||||
const (
|
||||
TestString = "frp is a fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet."
|
||||
)
|
||||
|
||||
const (
|
||||
PortServerName = "PortServer"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultServerConfig = `
|
||||
[common]
|
||||
bind_port = {{ .PortServer }}
|
||||
`
|
||||
|
||||
DefaultClientConfig = `
|
||||
[common]
|
||||
server_port = {{ .PortServer }}
|
||||
`
|
||||
)
|
||||
|
@@ -60,10 +60,6 @@ func NewFramework(opt Options) *Framework {
|
||||
f := &Framework{
|
||||
portAllocator: port.NewAllocator(opt.FromPortIndex, opt.ToPortIndex, opt.TotalParallelNode, opt.CurrentNodeIndex-1),
|
||||
}
|
||||
f.mockServers = NewMockServers(f.portAllocator)
|
||||
if err := f.mockServers.Run(); err != nil {
|
||||
Failf("%v", err)
|
||||
}
|
||||
|
||||
ginkgo.BeforeEach(f.BeforeEach)
|
||||
ginkgo.AfterEach(f.AfterEach)
|
||||
@@ -79,6 +75,11 @@ func (f *Framework) BeforeEach() {
|
||||
dir, err := ioutil.TempDir(os.TempDir(), "frpe2e-test-*")
|
||||
ExpectNoError(err)
|
||||
f.TempDirectory = dir
|
||||
|
||||
f.mockServers = NewMockServers(f.portAllocator)
|
||||
if err := f.mockServers.Run(); err != nil {
|
||||
Failf("%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Framework) AfterEach() {
|
||||
@@ -88,19 +89,38 @@ func (f *Framework) AfterEach() {
|
||||
|
||||
RemoveCleanupAction(f.cleanupHandle)
|
||||
|
||||
os.RemoveAll(f.TempDirectory)
|
||||
f.TempDirectory = ""
|
||||
f.UsedPorts = nil
|
||||
f.serverConfPaths = nil
|
||||
f.clientConfPaths = nil
|
||||
// stop processor
|
||||
for _, p := range f.serverProcesses {
|
||||
p.Stop()
|
||||
if TestContext.Debug {
|
||||
fmt.Println(p.ErrorOutput())
|
||||
fmt.Println(p.StdOutput())
|
||||
}
|
||||
}
|
||||
for _, p := range f.clientProcesses {
|
||||
p.Stop()
|
||||
if TestContext.Debug {
|
||||
fmt.Println(p.ErrorOutput())
|
||||
fmt.Println(p.StdOutput())
|
||||
}
|
||||
}
|
||||
f.serverProcesses = nil
|
||||
f.clientProcesses = nil
|
||||
|
||||
// close mock servers
|
||||
f.mockServers.Close()
|
||||
|
||||
// clean directory
|
||||
os.RemoveAll(f.TempDirectory)
|
||||
f.TempDirectory = ""
|
||||
f.serverConfPaths = nil
|
||||
f.clientConfPaths = nil
|
||||
|
||||
// release used ports
|
||||
for _, port := range f.UsedPorts {
|
||||
f.portAllocator.Release(port)
|
||||
}
|
||||
f.UsedPorts = nil
|
||||
}
|
||||
|
||||
var portRegex = regexp.MustCompile(`{{ \.Port.*? }}`)
|
||||
|
@@ -1,6 +1,9 @@
|
||||
package framework
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/fatedier/frp/test/e2e/mock/echoserver"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/port"
|
||||
)
|
||||
@@ -8,11 +11,13 @@ import (
|
||||
const (
|
||||
TCPEchoServerPort = "TCPEchoServerPort"
|
||||
UDPEchoServerPort = "UDPEchoServerPort"
|
||||
UDSEchoServerAddr = "UDSEchoServerAddr"
|
||||
)
|
||||
|
||||
type MockServers struct {
|
||||
tcpEchoServer *echoserver.Server
|
||||
udpEchoServer *echoserver.Server
|
||||
udsEchoServer *echoserver.Server
|
||||
}
|
||||
|
||||
func NewMockServers(portAllocator *port.Allocator) *MockServers {
|
||||
@@ -31,6 +36,15 @@ func NewMockServers(portAllocator *port.Allocator) *MockServers {
|
||||
BindPort: int32(udpPort),
|
||||
RepeatNum: 1,
|
||||
})
|
||||
|
||||
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,
|
||||
})
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -41,13 +55,24 @@ func (m *MockServers) Run() error {
|
||||
if err := m.udpEchoServer.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.udsEchoServer.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockServers) Close() {
|
||||
m.tcpEchoServer.Close()
|
||||
m.udpEchoServer.Close()
|
||||
m.udsEchoServer.Close()
|
||||
os.Remove(m.udsEchoServer.GetOptions().BindAddr)
|
||||
}
|
||||
|
||||
func (m *MockServers) GetTemplateParams() map[string]interface{} {
|
||||
ret := make(map[string]interface{})
|
||||
ret[TCPEchoServerPort] = m.tcpEchoServer.GetOptions().BindPort
|
||||
ret[UDPEchoServerPort] = m.udpEchoServer.GetOptions().BindPort
|
||||
ret[UDSEchoServerAddr] = m.udsEchoServer.GetOptions().BindAddr
|
||||
return ret
|
||||
}
|
||||
|
||||
|
@@ -6,8 +6,46 @@ import (
|
||||
"github.com/fatedier/frp/test/e2e/pkg/request"
|
||||
)
|
||||
|
||||
func ExpectTCPReuqest(port int, in, out []byte, timeout time.Duration) {
|
||||
res, err := request.SendTCPRequest(port, in, timeout)
|
||||
ExpectNoError(err)
|
||||
ExpectEqual(string(out), res)
|
||||
func ExpectRequest(protocol string, port int, in, out []byte, timeout time.Duration, explain ...interface{}) {
|
||||
switch protocol {
|
||||
case "tcp":
|
||||
ExpectTCPRequest(port, in, out, timeout, explain...)
|
||||
case "udp":
|
||||
ExpectUDPRequest(port, in, out, timeout, explain...)
|
||||
default:
|
||||
Failf("ExpectRequest not support protocol: %s", protocol)
|
||||
}
|
||||
}
|
||||
|
||||
func ExpectRequestError(protocol string, port int, in []byte, timeout time.Duration, explain ...interface{}) {
|
||||
switch protocol {
|
||||
case "tcp":
|
||||
ExpectTCPRequestError(port, in, timeout, explain...)
|
||||
case "udp":
|
||||
ExpectUDPRequestError(port, in, timeout, explain...)
|
||||
default:
|
||||
Failf("ExpectRequestError not support protocol: %s", protocol)
|
||||
}
|
||||
}
|
||||
|
||||
func ExpectTCPRequest(port int, in, out []byte, timeout time.Duration, explain ...interface{}) {
|
||||
res, err := request.SendTCPRequest(port, in, timeout)
|
||||
ExpectNoError(err, explain...)
|
||||
ExpectEqual(string(out), res, explain...)
|
||||
}
|
||||
|
||||
func ExpectTCPRequestError(port int, in []byte, timeout time.Duration, explain ...interface{}) {
|
||||
_, err := request.SendTCPRequest(port, in, timeout)
|
||||
ExpectError(err, explain...)
|
||||
}
|
||||
|
||||
func ExpectUDPRequest(port int, in, out []byte, timeout time.Duration, explain ...interface{}) {
|
||||
res, err := request.SendUDPRequest(port, in, timeout)
|
||||
ExpectNoError(err, explain...)
|
||||
ExpectEqual(string(out), res, explain...)
|
||||
}
|
||||
|
||||
func ExpectUDPRequestError(port int, in []byte, timeout time.Duration, explain ...interface{}) {
|
||||
_, err := request.SendUDPRequest(port, in, timeout)
|
||||
ExpectError(err, explain...)
|
||||
}
|
||||
|
@@ -4,12 +4,15 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/onsi/ginkgo/config"
|
||||
)
|
||||
|
||||
type TestContextType struct {
|
||||
FRPClientPath string
|
||||
FRPServerPath string
|
||||
LogLevel string
|
||||
Debug bool
|
||||
}
|
||||
|
||||
var TestContext TestContextType
|
||||
@@ -23,9 +26,16 @@ var TestContext TestContextType
|
||||
// regardless whether the test is actually in the test suite.
|
||||
//
|
||||
func RegisterCommonFlags(flags *flag.FlagSet) {
|
||||
// Turn on EmitSpecProgress to get spec progress (especially on interrupt)
|
||||
config.GinkgoConfig.EmitSpecProgress = true
|
||||
|
||||
// Randomize specs as well as suites
|
||||
config.GinkgoConfig.RandomizeAllSpecs = true
|
||||
|
||||
flags.StringVar(&TestContext.FRPClientPath, "frpc-path", "../../bin/frpc", "The frp client binary to use.")
|
||||
flags.StringVar(&TestContext.FRPServerPath, "frps-path", "../../bin/frps", "The frp server binary to use.")
|
||||
flags.StringVar(&TestContext.LogLevel, "log-level", "debug", "Log level.")
|
||||
flags.BoolVar(&TestContext.Debug, "debug", false, "Enable debug mode to print detail info.")
|
||||
}
|
||||
|
||||
func ValidateTestContext(t *TestContextType) error {
|
||||
|
@@ -12,3 +12,7 @@ func init() {
|
||||
uuid, _ := uuid.NewUUID()
|
||||
RunID = uuid.String()
|
||||
}
|
||||
|
||||
func GenPortName(name string) string {
|
||||
return "Port" + name
|
||||
}
|
||||
|
Reference in New Issue
Block a user