frp/test/e2e/framework/process.go

100 lines
3.1 KiB
Go
Raw Normal View History

2020-06-02 22:48:55 +08:00
package framework
import (
"fmt"
"os"
2020-06-02 22:48:55 +08:00
"path/filepath"
"time"
2020-09-23 13:49:14 +08:00
flog "github.com/fatedier/frp/pkg/util/log"
2020-06-02 22:48:55 +08: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 22:48:55 +08:00
templates := make([]string, 0, len(serverTemplates)+len(clientTemplates))
2022-08-29 01:02:53 +08:00
templates = append(templates, serverTemplates...)
templates = append(templates, clientTemplates...)
2020-06-02 22:48:55 +08: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 22:48:55 +08:00
currentServerProcesses := make([]*process.Process, 0, len(serverTemplates))
2020-06-02 22:48:55 +08:00
for i := range serverTemplates {
path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-server-%d", i))
2022-08-29 01:02:53 +08:00
err = os.WriteFile(path, []byte(outs[i]), 0o666)
2020-06-02 22:48:55 +08:00
ExpectNoError(err)
2023-09-13 16:32:39 +08:00
if TestContext.Debug {
flog.Debug("[%s] %s", path, outs[i])
}
2021-06-18 16:48:36 +08:00
p := process.NewWithEnvs(TestContext.FRPServerPath, []string{"-c", path}, f.osEnvs)
2020-06-02 22:48:55 +08:00
f.serverConfPaths = append(f.serverConfPaths, path)
f.serverProcesses = append(f.serverProcesses, p)
currentServerProcesses = append(currentServerProcesses, p)
2020-06-02 22:48:55 +08:00
err = p.Start()
ExpectNoError(err)
}
2023-05-29 00:27:27 +08:00
time.Sleep(1 * time.Second)
2020-06-02 22:48:55 +08:00
currentClientProcesses := make([]*process.Process, 0, len(clientTemplates))
2020-06-02 22:48:55 +08:00
for i := range clientTemplates {
index := i + len(serverTemplates)
path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-client-%d", i))
2022-08-29 01:02:53 +08:00
err = os.WriteFile(path, []byte(outs[index]), 0o666)
2020-06-02 22:48:55 +08:00
ExpectNoError(err)
2023-09-13 16:32:39 +08:00
if TestContext.Debug {
flog.Debug("[%s] %s", path, outs[index])
}
2021-06-18 16:48:36 +08:00
p := process.NewWithEnvs(TestContext.FRPClientPath, []string{"-c", path}, f.osEnvs)
2020-06-02 22:48:55 +08:00
f.clientConfPaths = append(f.clientConfPaths, path)
f.clientProcesses = append(f.clientProcesses, p)
currentClientProcesses = append(currentClientProcesses, p)
2020-06-02 22:48:55 +08:00
err = p.Start()
ExpectNoError(err)
time.Sleep(500 * time.Millisecond)
}
time.Sleep(3 * time.Second)
return currentServerProcesses, currentClientProcesses
2020-06-02 22:48:55 +08:00
}
2021-06-18 16:48:36 +08: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
time.Sleep(time.Second)
2021-06-18 16:48:36 +08: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
}
time.Sleep(time.Second)
2021-06-18 16:48:36 +08: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-29 01:02:53 +08:00
err := os.WriteFile(path, []byte(content), 0o666)
2021-06-18 16:48:36 +08:00
ExpectNoError(err)
return path
}