1
0

group.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package features
  2. import (
  3. "fmt"
  4. "strconv"
  5. "sync"
  6. "time"
  7. "github.com/onsi/ginkgo/v2"
  8. "github.com/fatedier/frp/test/e2e/framework"
  9. "github.com/fatedier/frp/test/e2e/framework/consts"
  10. "github.com/fatedier/frp/test/e2e/mock/server/httpserver"
  11. "github.com/fatedier/frp/test/e2e/mock/server/streamserver"
  12. "github.com/fatedier/frp/test/e2e/pkg/request"
  13. )
  14. var _ = ginkgo.Describe("[Feature: Group]", func() {
  15. f := framework.NewDefaultFramework()
  16. newHTTPServer := func(port int, respContent string) *httpserver.Server {
  17. return httpserver.New(
  18. httpserver.WithBindPort(port),
  19. httpserver.WithHandler(framework.SpecifiedHTTPBodyHandler([]byte(respContent))),
  20. )
  21. }
  22. validateFooBarResponse := func(resp *request.Response) bool {
  23. if string(resp.Content) == "foo" || string(resp.Content) == "bar" {
  24. return true
  25. }
  26. return false
  27. }
  28. doFooBarHTTPRequest := func(vhostPort int, host string) []string {
  29. results := []string{}
  30. var wait sync.WaitGroup
  31. var mu sync.Mutex
  32. expectFn := func() {
  33. framework.NewRequestExpect(f).Port(vhostPort).
  34. RequestModify(func(r *request.Request) {
  35. r.HTTP().HTTPHost(host)
  36. }).
  37. Ensure(validateFooBarResponse, func(resp *request.Response) bool {
  38. mu.Lock()
  39. defer mu.Unlock()
  40. results = append(results, string(resp.Content))
  41. return true
  42. })
  43. }
  44. for i := 0; i < 10; i++ {
  45. wait.Add(1)
  46. go func() {
  47. defer wait.Done()
  48. expectFn()
  49. }()
  50. }
  51. wait.Wait()
  52. return results
  53. }
  54. ginkgo.Describe("Load Balancing", func() {
  55. ginkgo.It("TCP", func() {
  56. serverConf := consts.DefaultServerConfig
  57. clientConf := consts.DefaultClientConfig
  58. fooPort := f.AllocPort()
  59. fooServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(fooPort), streamserver.WithRespContent([]byte("foo")))
  60. f.RunServer("", fooServer)
  61. barPort := f.AllocPort()
  62. barServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(barPort), streamserver.WithRespContent([]byte("bar")))
  63. f.RunServer("", barServer)
  64. remotePort := f.AllocPort()
  65. clientConf += fmt.Sprintf(`
  66. [[proxies]]
  67. name = "foo"
  68. type = "tcp"
  69. localPort = %d
  70. remotePort = %d
  71. loadBalancer.group = "test"
  72. loadBalancer.groupKey = "123"
  73. [[proxies]]
  74. name = "bar"
  75. type = "tcp"
  76. localPort = %d
  77. remotePort = %d
  78. loadBalancer.group = "test"
  79. loadBalancer.groupKey = "123"
  80. `, fooPort, remotePort, barPort, remotePort)
  81. f.RunProcesses([]string{serverConf}, []string{clientConf})
  82. fooCount := 0
  83. barCount := 0
  84. for i := 0; i < 10; i++ {
  85. framework.NewRequestExpect(f).Explain("times " + strconv.Itoa(i)).Port(remotePort).Ensure(func(resp *request.Response) bool {
  86. switch string(resp.Content) {
  87. case "foo":
  88. fooCount++
  89. case "bar":
  90. barCount++
  91. default:
  92. return false
  93. }
  94. return true
  95. })
  96. }
  97. framework.ExpectTrue(fooCount > 1 && barCount > 1, "fooCount: %d, barCount: %d", fooCount, barCount)
  98. })
  99. })
  100. ginkgo.Describe("Health Check", func() {
  101. ginkgo.It("TCP", func() {
  102. serverConf := consts.DefaultServerConfig
  103. clientConf := consts.DefaultClientConfig
  104. fooPort := f.AllocPort()
  105. fooServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(fooPort), streamserver.WithRespContent([]byte("foo")))
  106. f.RunServer("", fooServer)
  107. barPort := f.AllocPort()
  108. barServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(barPort), streamserver.WithRespContent([]byte("bar")))
  109. f.RunServer("", barServer)
  110. remotePort := f.AllocPort()
  111. clientConf += fmt.Sprintf(`
  112. [[proxies]]
  113. name = "foo"
  114. type = "tcp"
  115. localPort = %d
  116. remotePort = %d
  117. loadBalancer.group = "test"
  118. loadBalancer.groupKey = "123"
  119. healthCheck.type = "tcp"
  120. healthCheck.intervalSeconds = 1
  121. [[proxies]]
  122. name = "bar"
  123. type = "tcp"
  124. localPort = %d
  125. remotePort = %d
  126. loadBalancer.group = "test"
  127. loadBalancer.groupKey = "123"
  128. healthCheck.type = "tcp"
  129. healthCheck.intervalSeconds = 1
  130. `, fooPort, remotePort, barPort, remotePort)
  131. f.RunProcesses([]string{serverConf}, []string{clientConf})
  132. // check foo and bar is ok
  133. results := []string{}
  134. for i := 0; i < 10; i++ {
  135. framework.NewRequestExpect(f).Port(remotePort).Ensure(validateFooBarResponse, func(resp *request.Response) bool {
  136. results = append(results, string(resp.Content))
  137. return true
  138. })
  139. }
  140. framework.ExpectContainElements(results, []string{"foo", "bar"})
  141. // close bar server, check foo is ok
  142. barServer.Close()
  143. time.Sleep(2 * time.Second)
  144. for i := 0; i < 10; i++ {
  145. framework.NewRequestExpect(f).Port(remotePort).ExpectResp([]byte("foo")).Ensure()
  146. }
  147. // resume bar server, check foo and bar is ok
  148. f.RunServer("", barServer)
  149. time.Sleep(2 * time.Second)
  150. results = []string{}
  151. for i := 0; i < 10; i++ {
  152. framework.NewRequestExpect(f).Port(remotePort).Ensure(validateFooBarResponse, func(resp *request.Response) bool {
  153. results = append(results, string(resp.Content))
  154. return true
  155. })
  156. }
  157. framework.ExpectContainElements(results, []string{"foo", "bar"})
  158. })
  159. ginkgo.It("HTTP", func() {
  160. vhostPort := f.AllocPort()
  161. serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
  162. vhostHTTPPort = %d
  163. `, vhostPort)
  164. clientConf := consts.DefaultClientConfig
  165. fooPort := f.AllocPort()
  166. fooServer := newHTTPServer(fooPort, "foo")
  167. f.RunServer("", fooServer)
  168. barPort := f.AllocPort()
  169. barServer := newHTTPServer(barPort, "bar")
  170. f.RunServer("", barServer)
  171. clientConf += fmt.Sprintf(`
  172. [[proxies]]
  173. name = "foo"
  174. type = "http"
  175. localPort = %d
  176. customDomains = ["example.com"]
  177. loadBalancer.group = "test"
  178. loadBalancer.groupKey = "123"
  179. healthCheck.type = "http"
  180. healthCheck.intervalSeconds = 1
  181. healthCheck.path = "/healthz"
  182. [[proxies]]
  183. name = "bar"
  184. type = "http"
  185. localPort = %d
  186. customDomains = ["example.com"]
  187. loadBalancer.group = "test"
  188. loadBalancer.groupKey = "123"
  189. healthCheck.type = "http"
  190. healthCheck.intervalSeconds = 1
  191. healthCheck.path = "/healthz"
  192. `, fooPort, barPort)
  193. f.RunProcesses([]string{serverConf}, []string{clientConf})
  194. // send first HTTP request
  195. var contents []string
  196. framework.NewRequestExpect(f).Port(vhostPort).
  197. RequestModify(func(r *request.Request) {
  198. r.HTTP().HTTPHost("example.com")
  199. }).
  200. Ensure(func(resp *request.Response) bool {
  201. contents = append(contents, string(resp.Content))
  202. return true
  203. })
  204. // send second HTTP request, should be forwarded to another service
  205. framework.NewRequestExpect(f).Port(vhostPort).
  206. RequestModify(func(r *request.Request) {
  207. r.HTTP().HTTPHost("example.com")
  208. }).
  209. Ensure(func(resp *request.Response) bool {
  210. contents = append(contents, string(resp.Content))
  211. return true
  212. })
  213. framework.ExpectContainElements(contents, []string{"foo", "bar"})
  214. // check foo and bar is ok
  215. results := doFooBarHTTPRequest(vhostPort, "example.com")
  216. framework.ExpectContainElements(results, []string{"foo", "bar"})
  217. // close bar server, check foo is ok
  218. barServer.Close()
  219. time.Sleep(2 * time.Second)
  220. results = doFooBarHTTPRequest(vhostPort, "example.com")
  221. framework.ExpectContainElements(results, []string{"foo"})
  222. framework.ExpectNotContainElements(results, []string{"bar"})
  223. // resume bar server, check foo and bar is ok
  224. f.RunServer("", barServer)
  225. time.Sleep(2 * time.Second)
  226. results = doFooBarHTTPRequest(vhostPort, "example.com")
  227. framework.ExpectContainElements(results, []string{"foo", "bar"})
  228. })
  229. })
  230. })