mirror of
https://github.com/fatedier/frp.git
synced 2025-07-27 07:35:07 +00:00
new e2e framework (#1835)
This commit is contained in:
57
test/e2e/pkg/port/port.go
Normal file
57
test/e2e/pkg/port/port.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package port
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
)
|
||||
|
||||
type Allocator struct {
|
||||
reserved sets.Int
|
||||
used sets.Int
|
||||
}
|
||||
|
||||
// NewAllocator return a port allocator for testing.
|
||||
// Example: from: 10, to: 20, mod 4, index 1
|
||||
// Reserved ports: 13, 17
|
||||
func NewAllocator(from int, to int, mod int, index int) *Allocator {
|
||||
pa := &Allocator{
|
||||
reserved: sets.NewInt(),
|
||||
used: sets.NewInt(),
|
||||
}
|
||||
for i := from; i <= to; i++ {
|
||||
if i%mod == index {
|
||||
pa.reserved.Insert(i)
|
||||
}
|
||||
}
|
||||
return pa
|
||||
}
|
||||
|
||||
func (pa *Allocator) Get() int {
|
||||
for i := 0; i < 10; i++ {
|
||||
port, _ := pa.reserved.PopAny()
|
||||
if port == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// TODO: Distinguish between TCP and UDP
|
||||
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
||||
if err != nil {
|
||||
// Maybe not controlled by us, mark it used.
|
||||
pa.used.Insert(port)
|
||||
continue
|
||||
}
|
||||
l.Close()
|
||||
pa.used.Insert(port)
|
||||
return port
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (pa *Allocator) Release(port int) {
|
||||
if pa.used.Has(port) {
|
||||
pa.used.Delete(port)
|
||||
pa.reserved.Insert(port)
|
||||
}
|
||||
}
|
47
test/e2e/pkg/process/process.go
Normal file
47
test/e2e/pkg/process/process.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package process
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type Process struct {
|
||||
cmd *exec.Cmd
|
||||
cancel context.CancelFunc
|
||||
errorOutput *bytes.Buffer
|
||||
|
||||
beforeStopHandler func()
|
||||
}
|
||||
|
||||
func New(path string, params []string) *Process {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cmd := exec.CommandContext(ctx, path, params...)
|
||||
p := &Process{
|
||||
cmd: cmd,
|
||||
cancel: cancel,
|
||||
}
|
||||
p.errorOutput = bytes.NewBufferString("")
|
||||
cmd.Stderr = p.errorOutput
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Process) Start() error {
|
||||
return p.cmd.Start()
|
||||
}
|
||||
|
||||
func (p *Process) Stop() error {
|
||||
if p.beforeStopHandler != nil {
|
||||
p.beforeStopHandler()
|
||||
}
|
||||
p.cancel()
|
||||
return p.cmd.Wait()
|
||||
}
|
||||
|
||||
func (p *Process) ErrorOutput() string {
|
||||
return p.errorOutput.String()
|
||||
}
|
||||
|
||||
func (p *Process) SetBeforeStopHandler(fn func()) {
|
||||
p.beforeStopHandler = fn
|
||||
}
|
31
test/e2e/pkg/request/request.go
Normal file
31
test/e2e/pkg/request/request.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
func SendTCPRequest(port int, content []byte, timeout time.Duration) (res string, err error) {
|
||||
c, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
||||
if err != nil {
|
||||
err = fmt.Errorf("connect to tcp server error: %v", err)
|
||||
return
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
c.SetDeadline(time.Now().Add(timeout))
|
||||
return sendTCPRequestByConn(c, content)
|
||||
}
|
||||
|
||||
func sendTCPRequestByConn(c net.Conn, content []byte) (res string, err error) {
|
||||
c.Write(content)
|
||||
|
||||
buf := make([]byte, 2048)
|
||||
n, errRet := c.Read(buf)
|
||||
if errRet != nil {
|
||||
err = fmt.Errorf("read from tcp error: %v", errRet)
|
||||
return
|
||||
}
|
||||
return string(buf[:n]), nil
|
||||
}
|
Reference in New Issue
Block a user