process.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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) {
  13. templates := make([]string, 0, len(serverTemplates)+len(clientTemplates))
  14. for _, t := range serverTemplates {
  15. templates = append(templates, t)
  16. }
  17. for _, t := range clientTemplates {
  18. templates = append(templates, t)
  19. }
  20. outs, ports, err := f.RenderTemplates(templates)
  21. ExpectNoError(err)
  22. ExpectTrue(len(templates) > 0)
  23. for name, port := range ports {
  24. f.usedPorts[name] = port
  25. }
  26. for i := range serverTemplates {
  27. path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-server-%d", i))
  28. err = os.WriteFile(path, []byte(outs[i]), 0666)
  29. ExpectNoError(err)
  30. flog.Trace("[%s] %s", path, outs[i])
  31. p := process.NewWithEnvs(TestContext.FRPServerPath, []string{"-c", path}, f.osEnvs)
  32. f.serverConfPaths = append(f.serverConfPaths, path)
  33. f.serverProcesses = append(f.serverProcesses, p)
  34. err = p.Start()
  35. ExpectNoError(err)
  36. }
  37. time.Sleep(time.Second)
  38. for i := range clientTemplates {
  39. index := i + len(serverTemplates)
  40. path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-client-%d", i))
  41. err = os.WriteFile(path, []byte(outs[index]), 0666)
  42. ExpectNoError(err)
  43. flog.Trace("[%s] %s", path, outs[index])
  44. p := process.NewWithEnvs(TestContext.FRPClientPath, []string{"-c", path}, f.osEnvs)
  45. f.clientConfPaths = append(f.clientConfPaths, path)
  46. f.clientProcesses = append(f.clientProcesses, p)
  47. err = p.Start()
  48. ExpectNoError(err)
  49. time.Sleep(500 * time.Millisecond)
  50. }
  51. time.Sleep(500 * time.Millisecond)
  52. }
  53. func (f *Framework) RunFrps(args ...string) (*process.Process, string, error) {
  54. p := process.NewWithEnvs(TestContext.FRPServerPath, args, f.osEnvs)
  55. f.serverProcesses = append(f.serverProcesses, p)
  56. err := p.Start()
  57. if err != nil {
  58. return p, p.StdOutput(), err
  59. }
  60. // sleep for a while to get std output
  61. time.Sleep(500 * time.Millisecond)
  62. return p, p.StdOutput(), nil
  63. }
  64. func (f *Framework) RunFrpc(args ...string) (*process.Process, string, error) {
  65. p := process.NewWithEnvs(TestContext.FRPClientPath, args, f.osEnvs)
  66. f.clientProcesses = append(f.clientProcesses, p)
  67. err := p.Start()
  68. if err != nil {
  69. return p, p.StdOutput(), err
  70. }
  71. time.Sleep(500 * time.Millisecond)
  72. return p, p.StdOutput(), nil
  73. }
  74. func (f *Framework) GenerateConfigFile(content string) string {
  75. f.configFileIndex++
  76. path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-config-%d", f.configFileIndex))
  77. err := os.WriteFile(path, []byte(content), 0666)
  78. ExpectNoError(err)
  79. return path
  80. }