mirror of
https://github.com/fatedier/frp.git
synced 2026-01-11 22:23:12 +00:00
add e2e tests (#2334)
This commit is contained in:
@@ -1,21 +1,38 @@
|
||||
package consts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/fatedier/frp/test/e2e/pkg/port"
|
||||
)
|
||||
|
||||
const (
|
||||
TestString = "frp is a fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet."
|
||||
|
||||
DefaultTimeout = 2 * time.Second
|
||||
)
|
||||
|
||||
const (
|
||||
PortServerName = "PortServer"
|
||||
)
|
||||
var (
|
||||
PortServerName string
|
||||
PortClientAdmin string
|
||||
|
||||
const (
|
||||
DefaultServerConfig = `
|
||||
[common]
|
||||
bind_port = {{ .PortServer }}
|
||||
bind_port = {{ .%s }}
|
||||
log_level = trace
|
||||
`
|
||||
|
||||
DefaultClientConfig = `
|
||||
[common]
|
||||
server_port = {{ .PortServer }}
|
||||
server_port = {{ .%s }}
|
||||
log_level = trace
|
||||
`
|
||||
)
|
||||
|
||||
func init() {
|
||||
PortServerName = port.GenName("Server")
|
||||
PortClientAdmin = port.GenName("ClientAdmin")
|
||||
DefaultServerConfig = fmt.Sprintf(DefaultServerConfig, port.GenName("Server"))
|
||||
DefaultClientConfig = fmt.Sprintf(DefaultClientConfig, port.GenName("Server"))
|
||||
}
|
||||
|
||||
@@ -25,8 +25,11 @@ type Options struct {
|
||||
|
||||
type Framework struct {
|
||||
TempDirectory string
|
||||
UsedPorts map[string]int
|
||||
|
||||
// ports used in this framework indexed by port name.
|
||||
usedPorts map[string]int
|
||||
|
||||
// portAllocator to alloc port for this test case.
|
||||
portAllocator *port.Allocator
|
||||
|
||||
// Multiple mock servers used for e2e testing.
|
||||
@@ -117,10 +120,10 @@ func (f *Framework) AfterEach() {
|
||||
f.clientConfPaths = nil
|
||||
|
||||
// release used ports
|
||||
for _, port := range f.UsedPorts {
|
||||
for _, port := range f.usedPorts {
|
||||
f.portAllocator.Release(port)
|
||||
}
|
||||
f.UsedPorts = nil
|
||||
f.usedPorts = nil
|
||||
}
|
||||
|
||||
var portRegex = regexp.MustCompile(`{{ \.Port.*? }}`)
|
||||
@@ -151,7 +154,7 @@ func (f *Framework) genPortsFromTemplates(templates []string) (ports map[string]
|
||||
}()
|
||||
|
||||
for name := range ports {
|
||||
port := f.portAllocator.Get()
|
||||
port := f.portAllocator.GetByName(name)
|
||||
if port <= 0 {
|
||||
return nil, fmt.Errorf("can't allocate port")
|
||||
}
|
||||
@@ -161,6 +164,7 @@ func (f *Framework) genPortsFromTemplates(templates []string) (ports map[string]
|
||||
|
||||
}
|
||||
|
||||
// RenderTemplates alloc all ports for port names placeholder.
|
||||
func (f *Framework) RenderTemplates(templates []string) (outs []string, ports map[string]int, err error) {
|
||||
ports, err = f.genPortsFromTemplates(templates)
|
||||
if err != nil {
|
||||
@@ -185,3 +189,7 @@ func (f *Framework) RenderTemplates(templates []string) (outs []string, ports ma
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (f *Framework) PortByName(name string) int {
|
||||
return f.usedPorts[name]
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func (f *Framework) RunProcesses(serverTemplates []string, clientTemplates []str
|
||||
ExpectNoError(err)
|
||||
ExpectTrue(len(templates) > 0)
|
||||
|
||||
f.UsedPorts = ports
|
||||
f.usedPorts = ports
|
||||
|
||||
for i := range serverTemplates {
|
||||
path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-server-%d", i))
|
||||
@@ -40,8 +40,8 @@ func (f *Framework) RunProcesses(serverTemplates []string, clientTemplates []str
|
||||
f.serverProcesses = append(f.serverProcesses, p)
|
||||
err = p.Start()
|
||||
ExpectNoError(err)
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
|
||||
for i := range clientTemplates {
|
||||
index := i + len(serverTemplates)
|
||||
@@ -56,4 +56,5 @@ func (f *Framework) RunProcesses(serverTemplates []string, clientTemplates []str
|
||||
ExpectNoError(err)
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
|
||||
@@ -1,51 +1,85 @@
|
||||
package framework
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/fatedier/frp/test/e2e/framework/consts"
|
||||
"github.com/fatedier/frp/test/e2e/pkg/request"
|
||||
)
|
||||
|
||||
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 SetRequestProtocol(protocol string) func(*request.Request) {
|
||||
return func(r *request.Request) {
|
||||
r.Protocol(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 SetRequestPort(port int) func(*request.Request) {
|
||||
return func(r *request.Request) {
|
||||
r.Port(port)
|
||||
}
|
||||
}
|
||||
|
||||
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...)
|
||||
// NewRequest return a default TCP request with default timeout and content.
|
||||
func NewRequest() *request.Request {
|
||||
return request.New().
|
||||
Timeout(consts.DefaultTimeout).
|
||||
Body([]byte(consts.TestString))
|
||||
}
|
||||
|
||||
func ExpectTCPRequestError(port int, in []byte, timeout time.Duration, explain ...interface{}) {
|
||||
_, err := request.SendTCPRequest(port, in, timeout)
|
||||
func ExpectResponse(req *request.Request, expectResp []byte, explain ...interface{}) {
|
||||
ret, err := req.Do()
|
||||
ExpectNoError(err, explain...)
|
||||
ExpectEqualValues(expectResp, ret, explain...)
|
||||
}
|
||||
|
||||
func ExpectResponseError(req *request.Request, explain ...interface{}) {
|
||||
_, err := req.Do()
|
||||
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...)
|
||||
type RequestExpect struct {
|
||||
req *request.Request
|
||||
|
||||
f *Framework
|
||||
expectResp []byte
|
||||
expectError bool
|
||||
explain []interface{}
|
||||
}
|
||||
|
||||
func ExpectUDPRequestError(port int, in []byte, timeout time.Duration, explain ...interface{}) {
|
||||
_, err := request.SendUDPRequest(port, in, timeout)
|
||||
ExpectError(err, explain...)
|
||||
func NewRequestExpect(f *Framework) *RequestExpect {
|
||||
return &RequestExpect{
|
||||
req: NewRequest(),
|
||||
f: f,
|
||||
expectResp: []byte(consts.TestString),
|
||||
expectError: false,
|
||||
explain: make([]interface{}, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (e *RequestExpect) Request(f func(r *request.Request)) *RequestExpect {
|
||||
f(e.req)
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *RequestExpect) PortName(name string) *RequestExpect {
|
||||
if e.f != nil {
|
||||
e.req.Port(e.f.PortByName(name))
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *RequestExpect) ExpectError(expectErr bool) *RequestExpect {
|
||||
e.expectError = expectErr
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *RequestExpect) Explain(explain ...interface{}) *RequestExpect {
|
||||
e.explain = explain
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *RequestExpect) Ensure() {
|
||||
if e.expectError {
|
||||
ExpectResponseError(e.req, e.explain...)
|
||||
} else {
|
||||
ExpectResponse(e.req, e.expectResp, e.explain...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,3 @@ func init() {
|
||||
uuid, _ := uuid.NewUUID()
|
||||
RunID = uuid.String()
|
||||
}
|
||||
|
||||
func GenPortName(name string) string {
|
||||
return "Port" + name
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user