config.go 4.6 KB

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