config.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package basic
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/onsi/ginkgo/v2"
  6. "github.com/fatedier/frp/test/e2e/framework"
  7. "github.com/fatedier/frp/test/e2e/framework/consts"
  8. "github.com/fatedier/frp/test/e2e/pkg/port"
  9. )
  10. var _ = ginkgo.Describe("[Feature: Config]", func() {
  11. f := framework.NewDefaultFramework()
  12. ginkgo.Describe("Template", func() {
  13. ginkgo.It("render by env", func() {
  14. serverConf := consts.DefaultServerConfig
  15. clientConf := consts.DefaultClientConfig
  16. portName := port.GenName("TCP")
  17. serverConf += fmt.Sprintf(`
  18. auth.token = "{{ %s{{ .Envs.FRP_TOKEN }}%s }}"
  19. `, "`", "`")
  20. clientConf += fmt.Sprintf(`
  21. auth.token = "{{ %s{{ .Envs.FRP_TOKEN }}%s }}"
  22. [[proxies]]
  23. name = "tcp"
  24. type = "tcp"
  25. localPort = {{ .%s }}
  26. remotePort = {{ .%s }}
  27. `, "`", "`", framework.TCPEchoServerPort, portName)
  28. f.SetEnvs([]string{"FRP_TOKEN=123"})
  29. f.RunProcesses([]string{serverConf}, []string{clientConf})
  30. framework.NewRequestExpect(f).PortName(portName).Ensure()
  31. })
  32. ginkgo.It("Range ports mapping", func() {
  33. serverConf := consts.DefaultServerConfig
  34. clientConf := consts.DefaultClientConfig
  35. adminPort := f.AllocPort()
  36. localPortsRange := "13010-13012,13014"
  37. remotePortsRange := "23010-23012,23014"
  38. escapeTemplate := func(s string) string {
  39. return "{{ `" + s + "` }}"
  40. }
  41. clientConf += fmt.Sprintf(`
  42. webServer.port = %d
  43. %s
  44. [[proxies]]
  45. name = "tcp-%s"
  46. type = "tcp"
  47. localPort = %s
  48. remotePort = %s
  49. %s
  50. `, adminPort,
  51. escapeTemplate(fmt.Sprintf(`{{- range $_, $v := parseNumberRangePair "%s" "%s" }}`, localPortsRange, remotePortsRange)),
  52. escapeTemplate("{{ $v.First }}"),
  53. escapeTemplate("{{ $v.First }}"),
  54. escapeTemplate("{{ $v.Second }}"),
  55. escapeTemplate("{{- end }}"),
  56. )
  57. f.RunProcesses([]string{serverConf}, []string{clientConf})
  58. client := f.APIClientForFrpc(adminPort)
  59. checkProxyFn := func(name string, localPort, remotePort int) {
  60. status, err := client.GetProxyStatus(context.Background(), name)
  61. framework.ExpectNoError(err)
  62. framework.ExpectContainSubstring(status.LocalAddr, fmt.Sprintf(":%d", localPort))
  63. framework.ExpectContainSubstring(status.RemoteAddr, fmt.Sprintf(":%d", remotePort))
  64. }
  65. checkProxyFn("tcp-13010", 13010, 23010)
  66. checkProxyFn("tcp-13011", 13011, 23011)
  67. checkProxyFn("tcp-13012", 13012, 23012)
  68. checkProxyFn("tcp-13014", 13014, 23014)
  69. })
  70. })
  71. ginkgo.Describe("Includes", func() {
  72. ginkgo.It("split tcp proxies into different files", func() {
  73. serverPort := f.AllocPort()
  74. serverConfigPath := f.GenerateConfigFile(fmt.Sprintf(`
  75. bindAddr = "0.0.0.0"
  76. bindPort = %d
  77. `, serverPort))
  78. remotePort := f.AllocPort()
  79. proxyConfigPath := f.GenerateConfigFile(fmt.Sprintf(`
  80. [[proxies]]
  81. name = "tcp"
  82. type = "tcp"
  83. localPort = %d
  84. remotePort = %d
  85. `, f.PortByName(framework.TCPEchoServerPort), remotePort))
  86. remotePort2 := f.AllocPort()
  87. proxyConfigPath2 := f.GenerateConfigFile(fmt.Sprintf(`
  88. [[proxies]]
  89. name = "tcp2"
  90. type = "tcp"
  91. localPort = %d
  92. remotePort = %d
  93. `, f.PortByName(framework.TCPEchoServerPort), remotePort2))
  94. clientConfigPath := f.GenerateConfigFile(fmt.Sprintf(`
  95. serverPort = %d
  96. includes = ["%s","%s"]
  97. `, serverPort, proxyConfigPath, proxyConfigPath2))
  98. _, _, err := f.RunFrps("-c", serverConfigPath)
  99. framework.ExpectNoError(err)
  100. _, _, err = f.RunFrpc("-c", clientConfigPath)
  101. framework.ExpectNoError(err)
  102. framework.NewRequestExpect(f).Port(remotePort).Ensure()
  103. framework.NewRequestExpect(f).Port(remotePort2).Ensure()
  104. })
  105. })
  106. ginkgo.Describe("Support Formats", func() {
  107. ginkgo.It("YAML", func() {
  108. serverConf := fmt.Sprintf(`
  109. bindPort: {{ .%s }}
  110. log:
  111. level: trace
  112. `, port.GenName("Server"))
  113. remotePort := f.AllocPort()
  114. clientConf := fmt.Sprintf(`
  115. serverPort: {{ .%s }}
  116. log:
  117. level: trace
  118. proxies:
  119. - name: tcp
  120. type: tcp
  121. localPort: {{ .%s }}
  122. remotePort: %d
  123. `, port.GenName("Server"), framework.TCPEchoServerPort, remotePort)
  124. f.RunProcesses([]string{serverConf}, []string{clientConf})
  125. framework.NewRequestExpect(f).Port(remotePort).Ensure()
  126. })
  127. ginkgo.It("JSON", func() {
  128. serverConf := fmt.Sprintf(`{"bindPort": {{ .%s }}, "log": {"level": "trace"}}`, port.GenName("Server"))
  129. remotePort := f.AllocPort()
  130. clientConf := fmt.Sprintf(`{"serverPort": {{ .%s }}, "log": {"level": "trace"},
  131. "proxies": [{"name": "tcp", "type": "tcp", "localPort": {{ .%s }}, "remotePort": %d}]}`,
  132. port.GenName("Server"), framework.TCPEchoServerPort, remotePort)
  133. f.RunProcesses([]string{serverConf}, []string{clientConf})
  134. framework.NewRequestExpect(f).Port(remotePort).Ensure()
  135. })
  136. })
  137. })