frp/test/e2e/framework/process.go

101 lines
3.1 KiB
Go
Raw Permalink 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))
err = os.WriteFile(path, []byte(outs[i]), 0o600)
2020-06-02 22:48:55 +08:00
ExpectNoError(err)
2023-09-13 16:32:39 +08:00
if TestContext.Debug {
2024-03-12 13:58:53 +08:00
flog.Debugf("[%s] %s", path, outs[i])
2023-09-13 16:32:39 +08:00
}
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)
time.Sleep(500 * time.Millisecond)
2020-06-02 22:48:55 +08:00
}
2023-12-21 22:46:08 +08:00
time.Sleep(2 * 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))
err = os.WriteFile(path, []byte(outs[index]), 0o600)
2020-06-02 22:48:55 +08:00
ExpectNoError(err)
2023-09-13 16:32:39 +08:00
if TestContext.Debug {
2024-03-12 13:58:53 +08:00
flog.Debugf("[%s] %s", path, outs[index])
2023-09-13 16:32:39 +08:00
}
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
2023-12-21 22:46:08 +08:00
time.Sleep(2 * 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
}
2023-12-21 22:46:08 +08:00
time.Sleep(2 * 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))
err := os.WriteFile(path, []byte(content), 0o600)
2021-06-18 16:48:36 +08:00
ExpectNoError(err)
return path
}