frp/test/e2e/framework/process.go

101 lines
3.1 KiB
Go
Raw Normal View History

2020-06-02 16:48:55 +02:00
package framework
import (
"fmt"
"os"
2020-06-02 16:48:55 +02:00
"path/filepath"
"time"
2020-09-23 07:49:14 +02:00
flog "github.com/fatedier/frp/pkg/util/log"
2020-06-02 16:48:55 +02:00
"github.com/fatedier/frp/test/e2e/pkg/process"
)
// RunProcesses run multiple processes from templates.
// The first template should always be frps.
func (f *Framework) RunProcesses(serverTemplates []string, clientTemplates []string) ([]*process.Process, []*process.Process) {
2020-06-02 16:48:55 +02:00
templates := make([]string, 0, len(serverTemplates)+len(clientTemplates))
2022-08-28 19:02:53 +02:00
templates = append(templates, serverTemplates...)
templates = append(templates, clientTemplates...)
2020-06-02 16:48:55 +02:00
outs, ports, err := f.RenderTemplates(templates)
ExpectNoError(err)
ExpectTrue(len(templates) > 0)
for name, port := range ports {
f.usedPorts[name] = port
}
2020-06-02 16:48:55 +02:00
currentServerProcesses := make([]*process.Process, 0, len(serverTemplates))
2020-06-02 16:48:55 +02:00
for i := range serverTemplates {
path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-server-%d", i))
2022-08-28 19:02:53 +02:00
err = os.WriteFile(path, []byte(outs[i]), 0o666)
2020-06-02 16:48:55 +02:00
ExpectNoError(err)
2023-09-13 10:32:39 +02:00
if TestContext.Debug {
flog.Debug("[%s] %s", path, outs[i])
}
2021-06-18 10:48:36 +02:00
p := process.NewWithEnvs(TestContext.FRPServerPath, []string{"-c", path}, f.osEnvs)
2020-06-02 16:48:55 +02:00
f.serverConfPaths = append(f.serverConfPaths, path)
f.serverProcesses = append(f.serverProcesses, p)
currentServerProcesses = append(currentServerProcesses, p)
2020-06-02 16:48:55 +02:00
err = p.Start()
ExpectNoError(err)
time.Sleep(500 * time.Millisecond)
2020-06-02 16:48:55 +02:00
}
2023-12-21 15:46:08 +01:00
time.Sleep(2 * time.Second)
2020-06-02 16:48:55 +02:00
currentClientProcesses := make([]*process.Process, 0, len(clientTemplates))
2020-06-02 16:48:55 +02:00
for i := range clientTemplates {
index := i + len(serverTemplates)
path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-client-%d", i))
2022-08-28 19:02:53 +02:00
err = os.WriteFile(path, []byte(outs[index]), 0o666)
2020-06-02 16:48:55 +02:00
ExpectNoError(err)
2023-09-13 10:32:39 +02:00
if TestContext.Debug {
flog.Debug("[%s] %s", path, outs[index])
}
2021-06-18 10:48:36 +02:00
p := process.NewWithEnvs(TestContext.FRPClientPath, []string{"-c", path}, f.osEnvs)
2020-06-02 16:48:55 +02:00
f.clientConfPaths = append(f.clientConfPaths, path)
f.clientProcesses = append(f.clientProcesses, p)
currentClientProcesses = append(currentClientProcesses, p)
2020-06-02 16:48:55 +02:00
err = p.Start()
ExpectNoError(err)
time.Sleep(500 * time.Millisecond)
}
time.Sleep(3 * time.Second)
return currentServerProcesses, currentClientProcesses
2020-06-02 16:48:55 +02:00
}
2021-06-18 10:48:36 +02:00
func (f *Framework) RunFrps(args ...string) (*process.Process, string, error) {
p := process.NewWithEnvs(TestContext.FRPServerPath, args, f.osEnvs)
f.serverProcesses = append(f.serverProcesses, p)
err := p.Start()
if err != nil {
return p, p.StdOutput(), err
}
// sleep for a while to get std output
2023-12-21 15:46:08 +01:00
time.Sleep(2 * time.Second)
2021-06-18 10:48:36 +02:00
return p, p.StdOutput(), nil
}
func (f *Framework) RunFrpc(args ...string) (*process.Process, string, error) {
p := process.NewWithEnvs(TestContext.FRPClientPath, args, f.osEnvs)
f.clientProcesses = append(f.clientProcesses, p)
err := p.Start()
if err != nil {
return p, p.StdOutput(), err
}
2023-12-21 15:46:08 +01:00
time.Sleep(2 * time.Second)
2021-06-18 10:48:36 +02:00
return p, p.StdOutput(), nil
}
func (f *Framework) GenerateConfigFile(content string) string {
f.configFileIndex++
path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-config-%d", f.configFileIndex))
2022-08-28 19:02:53 +02:00
err := os.WriteFile(path, []byte(content), 0o666)
2021-06-18 10:48:36 +02:00
ExpectNoError(err)
return path
}