2020-06-02 22:48:55 +08:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-09-29 10:33:57 +08:00
|
|
|
"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.
|
2022-03-08 15:08:09 +08:00
|
|
|
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)
|
|
|
|
|
2021-06-02 23:54:22 +08:00
|
|
|
for name, port := range ports {
|
|
|
|
f.usedPorts[name] = port
|
|
|
|
}
|
2020-06-02 22:48:55 +08:00
|
|
|
|
2022-03-08 15:08:09 +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)
|
2022-03-08 15:08:09 +08:00
|
|
|
currentServerProcesses = append(currentServerProcesses, p)
|
2020-06-02 22:48:55 +08:00
|
|
|
err = p.Start()
|
|
|
|
ExpectNoError(err)
|
2023-10-11 16:53:03 +08:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2020-06-02 22:48:55 +08:00
|
|
|
}
|
2023-05-29 00:27:27 +08:00
|
|
|
time.Sleep(1 * time.Second)
|
2020-06-02 22:48:55 +08:00
|
|
|
|
2022-03-08 15:08:09 +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)
|
2022-03-08 15:08:09 +08:00
|
|
|
currentClientProcesses = append(currentClientProcesses, p)
|
2020-06-02 22:48:55 +08:00
|
|
|
err = p.Start()
|
|
|
|
ExpectNoError(err)
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
2023-06-02 16:06:29 +08:00
|
|
|
time.Sleep(3 * time.Second)
|
2022-03-08 15:08:09 +08:00
|
|
|
|
|
|
|
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-02-09 01:11:00 +08:00
|
|
|
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
|
|
|
|
}
|
2023-02-09 01:11:00 +08:00
|
|
|
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
|
|
|
|
}
|