process.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package framework
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. flog "github.com/fatedier/frp/pkg/util/log"
  8. "github.com/fatedier/frp/test/e2e/pkg/process"
  9. )
  10. // RunProcesses run multiple processes from templates.
  11. // The first template should always be frps.
  12. func (f *Framework) RunProcesses(serverTemplates []string, clientTemplates []string) ([]*process.Process, []*process.Process) {
  13. templates := make([]string, 0, len(serverTemplates)+len(clientTemplates))
  14. templates = append(templates, serverTemplates...)
  15. templates = append(templates, clientTemplates...)
  16. outs, ports, err := f.RenderTemplates(templates)
  17. ExpectNoError(err)
  18. ExpectTrue(len(templates) > 0)
  19. for name, port := range ports {
  20. f.usedPorts[name] = port
  21. }
  22. currentServerProcesses := make([]*process.Process, 0, len(serverTemplates))
  23. for i := range serverTemplates {
  24. path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-server-%d", i))
  25. err = os.WriteFile(path, []byte(outs[i]), 0o600)
  26. ExpectNoError(err)
  27. if TestContext.Debug {
  28. flog.Debugf("[%s] %s", path, outs[i])
  29. }
  30. p := process.NewWithEnvs(TestContext.FRPServerPath, []string{"-c", path}, f.osEnvs)
  31. f.serverConfPaths = append(f.serverConfPaths, path)
  32. f.serverProcesses = append(f.serverProcesses, p)
  33. currentServerProcesses = append(currentServerProcesses, p)
  34. err = p.Start()
  35. ExpectNoError(err)
  36. time.Sleep(500 * time.Millisecond)
  37. }
  38. time.Sleep(2 * time.Second)
  39. currentClientProcesses := make([]*process.Process, 0, len(clientTemplates))
  40. for i := range clientTemplates {
  41. index := i + len(serverTemplates)
  42. path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-client-%d", i))
  43. err = os.WriteFile(path, []byte(outs[index]), 0o600)
  44. ExpectNoError(err)
  45. if TestContext.Debug {
  46. flog.Debugf("[%s] %s", path, outs[index])
  47. }
  48. p := process.NewWithEnvs(TestContext.FRPClientPath, []string{"-c", path}, f.osEnvs)
  49. f.clientConfPaths = append(f.clientConfPaths, path)
  50. f.clientProcesses = append(f.clientProcesses, p)
  51. currentClientProcesses = append(currentClientProcesses, p)
  52. err = p.Start()
  53. ExpectNoError(err)
  54. time.Sleep(500 * time.Millisecond)
  55. }
  56. time.Sleep(3 * time.Second)
  57. return currentServerProcesses, currentClientProcesses
  58. }
  59. func (f *Framework) RunFrps(args ...string) (*process.Process, string, error) {
  60. p := process.NewWithEnvs(TestContext.FRPServerPath, args, f.osEnvs)
  61. f.serverProcesses = append(f.serverProcesses, p)
  62. err := p.Start()
  63. if err != nil {
  64. return p, p.StdOutput(), err
  65. }
  66. // sleep for a while to get std output
  67. time.Sleep(2 * time.Second)
  68. return p, p.StdOutput(), nil
  69. }
  70. func (f *Framework) RunFrpc(args ...string) (*process.Process, string, error) {
  71. p := process.NewWithEnvs(TestContext.FRPClientPath, args, f.osEnvs)
  72. f.clientProcesses = append(f.clientProcesses, p)
  73. err := p.Start()
  74. if err != nil {
  75. return p, p.StdOutput(), err
  76. }
  77. time.Sleep(2 * time.Second)
  78. return p, p.StdOutput(), nil
  79. }
  80. func (f *Framework) GenerateConfigFile(content string) string {
  81. f.configFileIndex++
  82. path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-config-%d", f.configFileIndex))
  83. err := os.WriteFile(path, []byte(content), 0o600)
  84. ExpectNoError(err)
  85. return path
  86. }