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