| 9 |
| 10 | "github.com/onsi/ginkgo/v2" |
| 11 |
| 12 | "github.com/fatedier/frp/pkg/transport" |
| 13 | "github.com/fatedier/frp/test/e2e/framework" |
| 14 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 15 | "github.com/fatedier/frp/test/e2e/mock/server/httpserver" |
| 16 | "github.com/fatedier/frp/test/e2e/mock/server/streamserver" |
| 17 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 18 | ) |
| 19 |
| 20 | var _ = ginkgo.Describe("[Feature: Group]", func() { |
| 21 | f := framework.NewDefaultFramework() |
| 22 |
| 23 | newHTTPServer := func(port int, respContent string) *httpserver.Server { |
| 24 | return httpserver.New( |
| 25 | httpserver.WithBindPort(port), |
| 26 | httpserver.WithHandler(framework.SpecifiedHTTPBodyHandler([]byte(respContent))), |
| 27 | ) |
| 28 | } |
| 29 |
| 30 | validateFooBarResponse := func(resp *request.Response) bool { |
| 31 | if string(resp.Content) == "foo" || string(resp.Content) == "bar" { |
| 32 | return true |
| 33 | } |
| 34 | return false |
| 35 | } |
| 36 |
| 37 | doFooBarHTTPRequest := func(vhostPort int, host string) []string { |
| 38 | results := []string{} |
| 39 | var wait sync.WaitGroup |
| 40 | var mu sync.Mutex |
| 41 | expectFn := func() { |
| 42 | framework.NewRequestExpect(f).Port(vhostPort). |
| 43 | RequestModify(func(r *request.Request) { |
| 44 | r.HTTP().HTTPHost(host) |
| 45 | }). |
| 46 | Ensure(validateFooBarResponse, func(resp *request.Response) bool { |
| 47 | mu.Lock() |
| 48 | defer mu.Unlock() |
| 49 | results = append(results, string(resp.Content)) |
| 50 | return true |
| 51 | }) |
| 52 | } |
| 53 | for range 10 { |
| 54 | wait.Go(func() { |
| 55 | expectFn() |
| 56 | }) |
| 57 | } |
| 58 |
| 59 | wait.Wait() |
| 60 | return results |
| 61 | } |
| 62 |
| 63 | ginkgo.Describe("Load Balancing", func() { |
| 64 | ginkgo.It("TCP", func() { |
| 65 | serverConf := consts.DefaultServerConfig |
| 66 | clientConf := consts.DefaultClientConfig |
| 67 |
| 68 | fooPort := f.AllocPort() |
| 69 | fooServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(fooPort), streamserver.WithRespContent([]byte("foo"))) |
| 70 | f.RunServer("", fooServer) |
| 71 |
| 72 | barPort := f.AllocPort() |
| 73 | barServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(barPort), streamserver.WithRespContent([]byte("bar"))) |
| 74 | f.RunServer("", barServer) |
| 75 |
| 76 | remotePort := f.AllocPort() |
| 77 | clientConf += fmt.Sprintf(` |
| 78 | [[proxies]] |
| 79 | name = "foo" |
| 80 | type = "tcp" |
| 81 | localPort = %d |
| 82 | remotePort = %d |
| 83 | loadBalancer.group = "test" |
| 84 | loadBalancer.groupKey = "123" |
| 85 |
| 86 | [[proxies]] |
| 87 | name = "bar" |
| 88 | type = "tcp" |
| 89 | localPort = %d |
| 90 | remotePort = %d |
| 91 | loadBalancer.group = "test" |
| 92 | loadBalancer.groupKey = "123" |
| 93 | `, fooPort, remotePort, barPort, remotePort) |
| 94 |
| 95 | f.RunProcesses(serverConf, []string{clientConf}) |
| 96 |
| 97 | fooCount := 0 |
| 98 | barCount := 0 |
| 99 | for i := range 10 { |
| 100 | framework.NewRequestExpect(f).Explain("times " + strconv.Itoa(i)).Port(remotePort).Ensure(func(resp *request.Response) bool { |
| 101 | switch string(resp.Content) { |
| 102 | case "foo": |
| 103 | fooCount++ |
| 104 | case "bar": |
| 105 | barCount++ |
| 106 | default: |
| 107 | return false |
| 108 | } |
| 109 | return true |
| 110 | }) |
| 111 | } |
| 112 |
| 113 | framework.ExpectTrue(fooCount > 1 && barCount > 1, "fooCount: %d, barCount: %d", fooCount, barCount) |
| 114 | }) |
| 115 |
| 116 | ginkgo.It("HTTPS", func() { |
| 117 | vhostHTTPSPort := f.AllocPort() |
| 118 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 119 | vhostHTTPSPort = %d |
| 120 | `, vhostHTTPSPort) |
| 121 | clientConf := consts.DefaultClientConfig |
| 122 |
| 123 | tlsConfig, err := transport.NewServerTLSConfig("", "", "") |
| 124 | framework.ExpectNoError(err) |
| 125 |
| 126 | fooPort := f.AllocPort() |
| 127 | fooServer := httpserver.New( |
| 128 | httpserver.WithBindPort(fooPort), |
| 129 | httpserver.WithHandler(framework.SpecifiedHTTPBodyHandler([]byte("foo"))), |
| 130 | httpserver.WithTLSConfig(tlsConfig), |
| 131 | ) |
| 132 | f.RunServer("", fooServer) |
| 133 |
| 134 | barPort := f.AllocPort() |
| 135 | barServer := httpserver.New( |
| 136 | httpserver.WithBindPort(barPort), |
| 137 | httpserver.WithHandler(framework.SpecifiedHTTPBodyHandler([]byte("bar"))), |
| 138 | httpserver.WithTLSConfig(tlsConfig), |
| 139 | ) |
| 140 | f.RunServer("", barServer) |
| 141 |
| 142 | clientConf += fmt.Sprintf(` |
| 143 | [[proxies]] |
| 144 | name = "foo" |
| 145 | type = "https" |
| 146 | localPort = %d |
| 147 | customDomains = ["example.com"] |
| 148 | loadBalancer.group = "test" |
| 149 | loadBalancer.groupKey = "123" |
| 150 |
| 151 | [[proxies]] |
| 152 | name = "bar" |
| 153 | type = "https" |
| 154 | localPort = %d |
| 155 | customDomains = ["example.com"] |
| 156 | loadBalancer.group = "test" |
| 157 | loadBalancer.groupKey = "123" |
| 158 | `, fooPort, barPort) |
| 159 |
| 160 | f.RunProcesses(serverConf, []string{clientConf}) |
| 161 |
| 162 | fooCount := 0 |
| 163 | barCount := 0 |
| 164 | for i := range 10 { |
| 165 | framework.NewRequestExpect(f). |
| 166 | Explain("times " + strconv.Itoa(i)). |
| 167 | Port(vhostHTTPSPort). |
| 168 | RequestModify(func(r *request.Request) { |
| 169 | r.HTTPS().HTTPHost("example.com").TLSConfig(&tls.Config{ |
| 170 | ServerName: "example.com", |
| 171 | InsecureSkipVerify: true, |
| 172 | }) |
| 173 | }). |
| 174 | Ensure(func(resp *request.Response) bool { |
| 175 | switch string(resp.Content) { |
| 176 | case "foo": |
| 177 | fooCount++ |
| 178 | case "bar": |
| 179 | barCount++ |
| 180 | default: |
| 181 | return false |
| 182 | } |
| 183 | return true |
| 184 | }) |
| 185 | } |
| 186 |
| 187 | framework.ExpectTrue(fooCount > 1 && barCount > 1, "fooCount: %d, barCount: %d", fooCount, barCount) |
| 188 | }) |
| 189 |
| 190 | ginkgo.It("TCPMux httpconnect", func() { |
| 191 | vhostPort := f.AllocPort() |
| 192 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 193 | tcpmuxHTTPConnectPort = %d |
| 194 | `, vhostPort) |
| 195 | clientConf := consts.DefaultClientConfig |
| 196 |
| 197 | fooPort := f.AllocPort() |
| 198 | fooServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(fooPort), streamserver.WithRespContent([]byte("foo"))) |
| 199 | f.RunServer("", fooServer) |
| 200 |
| 201 | barPort := f.AllocPort() |
| 202 | barServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(barPort), streamserver.WithRespContent([]byte("bar"))) |
| 203 | f.RunServer("", barServer) |
| 204 |
| 205 | clientConf += fmt.Sprintf(` |
| 206 | [[proxies]] |
| 207 | name = "foo" |
| 208 | type = "tcpmux" |
| 209 | multiplexer = "httpconnect" |
| 210 | localPort = %d |
| 211 | customDomains = ["tcpmux-group.example.com"] |
| 212 | loadBalancer.group = "test" |
| 213 | loadBalancer.groupKey = "123" |
| 214 |
| 215 | [[proxies]] |
| 216 | name = "bar" |
| 217 | type = "tcpmux" |
| 218 | multiplexer = "httpconnect" |
| 219 | localPort = %d |
| 220 | customDomains = ["tcpmux-group.example.com"] |
| 221 | loadBalancer.group = "test" |
| 222 | loadBalancer.groupKey = "123" |
| 223 | `, fooPort, barPort) |
| 224 |
| 225 | f.RunProcesses(serverConf, []string{clientConf}) |
| 226 |
| 227 | proxyURL := fmt.Sprintf("http://127.0.0.1:%d", vhostPort) |
| 228 | fooCount := 0 |
| 229 | barCount := 0 |
| 230 | for i := range 10 { |
| 231 | framework.NewRequestExpect(f). |
| 232 | Explain("times " + strconv.Itoa(i)). |
| 233 | RequestModify(func(r *request.Request) { |
| 234 | r.Addr("tcpmux-group.example.com").Proxy(proxyURL) |
| 235 | }). |
| 236 | Ensure(func(resp *request.Response) bool { |
| 237 | switch string(resp.Content) { |
| 238 | case "foo": |
| 239 | fooCount++ |
| 240 | case "bar": |
| 241 | barCount++ |
| 242 | default: |
| 243 | return false |
| 244 | } |
| 245 | return true |
| 246 | }) |
| 247 | } |
| 248 |
| 249 | framework.ExpectTrue(fooCount > 1 && barCount > 1, "fooCount: %d, barCount: %d", fooCount, barCount) |
| 250 | }) |
| 251 | }) |
| 252 |
| 253 | ginkgo.Describe("Health Check", func() { |
| 254 | ginkgo.It("TCP", func() { |
| 255 | serverConf := consts.DefaultServerConfig |
| 256 | clientConf := consts.DefaultClientConfig |
| 257 |
| 258 | fooPort := f.AllocPort() |
| 259 | fooServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(fooPort), streamserver.WithRespContent([]byte("foo"))) |
| 260 | f.RunServer("", fooServer) |
| 261 |
| 262 | barPort := f.AllocPort() |
| 263 | barServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(barPort), streamserver.WithRespContent([]byte("bar"))) |
| 264 | f.RunServer("", barServer) |
| 265 |
| 266 | remotePort := f.AllocPort() |
| 267 | clientConf += fmt.Sprintf(` |
| 268 | [[proxies]] |
| 269 | name = "foo" |
| 270 | type = "tcp" |
| 271 | localPort = %d |
| 272 | remotePort = %d |
| 273 | loadBalancer.group = "test" |
| 274 | loadBalancer.groupKey = "123" |
| 275 | healthCheck.type = "tcp" |
| 276 | healthCheck.intervalSeconds = 1 |
| 277 |
| 278 | [[proxies]] |
| 279 | name = "bar" |
| 280 | type = "tcp" |
| 281 | localPort = %d |
| 282 | remotePort = %d |
| 283 | loadBalancer.group = "test" |
| 284 | loadBalancer.groupKey = "123" |
| 285 | healthCheck.type = "tcp" |
| 286 | healthCheck.intervalSeconds = 1 |
| 287 | `, fooPort, remotePort, barPort, remotePort) |
| 288 |
| 289 | _, clientProcesses := f.RunProcesses(serverConf, []string{clientConf}) |
| 290 |
| 291 | // check foo and bar is ok |
| 292 | results := []string{} |
| 293 | for range 10 { |
| 294 | framework.NewRequestExpect(f).Port(remotePort).Ensure(validateFooBarResponse, func(resp *request.Response) bool { |
| 295 | results = append(results, string(resp.Content)) |
| 296 | return true |
| 297 | }) |
| 298 | } |
| 299 | framework.ExpectContainElements(results, []string{"foo", "bar"}) |
| 300 |
| 301 | // close bar server, check foo is ok |
| 302 | failedCount := clientProcesses[0].CountOutput("[bar] health check failed") |
| 303 | barServer.Close() |
| 304 | framework.ExpectNoError(clientProcesses[0].WaitForOutput("[bar] health check failed", failedCount+1, 5*time.Second)) |
| 305 | for range 10 { |
| 306 | framework.NewRequestExpect(f).Port(remotePort).ExpectResp([]byte("foo")).Ensure() |
| 307 | } |
| 308 |
| 309 | // resume bar server, check foo and bar is ok |
| 310 | successCount := clientProcesses[0].CountOutput("[bar] health check success") |
| 311 | f.RunServer("", barServer) |
| 312 | framework.ExpectNoError(clientProcesses[0].WaitForOutput("[bar] health check success", successCount+1, 5*time.Second)) |
| 313 | results = []string{} |
| 314 | for range 10 { |
| 315 | framework.NewRequestExpect(f).Port(remotePort).Ensure(validateFooBarResponse, func(resp *request.Response) bool { |
| 316 | results = append(results, string(resp.Content)) |
| 317 | return true |
| 318 | }) |
| 319 | } |
| 320 | framework.ExpectContainElements(results, []string{"foo", "bar"}) |
| 321 | }) |
| 322 |
| 323 | ginkgo.It("HTTP", func() { |
| 324 | vhostPort := f.AllocPort() |
| 325 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 326 | vhostHTTPPort = %d |
| 327 | `, vhostPort) |
| 328 | clientConf := consts.DefaultClientConfig |
| 329 |
| 330 | fooPort := f.AllocPort() |
| 331 | fooServer := newHTTPServer(fooPort, "foo") |
| 332 | f.RunServer("", fooServer) |
| 333 |
| 334 | barPort := f.AllocPort() |
| 335 | barServer := newHTTPServer(barPort, "bar") |
| 336 | f.RunServer("", barServer) |
| 337 |
| 338 | clientConf += fmt.Sprintf(` |
| 339 | [[proxies]] |
| 340 | name = "foo" |
| 341 | type = "http" |
| 342 | localPort = %d |
| 343 | customDomains = ["example.com"] |
| 344 | loadBalancer.group = "test" |
| 345 | loadBalancer.groupKey = "123" |
| 346 | healthCheck.type = "http" |
| 347 | healthCheck.intervalSeconds = 1 |
| 348 | healthCheck.path = "/healthz" |
| 349 |
| 350 | [[proxies]] |
| 351 | name = "bar" |
| 352 | type = "http" |
| 353 | localPort = %d |
| 354 | customDomains = ["example.com"] |
| 355 | loadBalancer.group = "test" |
| 356 | loadBalancer.groupKey = "123" |
| 357 | healthCheck.type = "http" |
| 358 | healthCheck.intervalSeconds = 1 |
| 359 | healthCheck.path = "/healthz" |
| 360 | `, fooPort, barPort) |
| 361 |
| 362 | _, clientProcesses := f.RunProcesses(serverConf, []string{clientConf}) |
| 363 |
| 364 | // send first HTTP request |
| 365 | var contents []string |
| 366 | framework.NewRequestExpect(f).Port(vhostPort). |
| 367 | RequestModify(func(r *request.Request) { |
| 368 | r.HTTP().HTTPHost("example.com") |
| 369 | }). |
| 370 | Ensure(func(resp *request.Response) bool { |
| 371 | contents = append(contents, string(resp.Content)) |
| 372 | return true |
| 373 | }) |
| 374 |
| 375 | // send second HTTP request, should be forwarded to another service |
| 376 | framework.NewRequestExpect(f).Port(vhostPort). |
| 377 | RequestModify(func(r *request.Request) { |
| 378 | r.HTTP().HTTPHost("example.com") |
| 379 | }). |
| 380 | Ensure(func(resp *request.Response) bool { |
| 381 | contents = append(contents, string(resp.Content)) |
| 382 | return true |
| 383 | }) |
| 384 |
| 385 | framework.ExpectContainElements(contents, []string{"foo", "bar"}) |
| 386 |
| 387 | // check foo and bar is ok |
| 388 | results := doFooBarHTTPRequest(vhostPort, "example.com") |
| 389 | framework.ExpectContainElements(results, []string{"foo", "bar"}) |
| 390 |
| 391 | // close bar server, check foo is ok |
| 392 | failedCount := clientProcesses[0].CountOutput("[bar] health check failed") |
| 393 | barServer.Close() |
| 394 | framework.ExpectNoError(clientProcesses[0].WaitForOutput("[bar] health check failed", failedCount+1, 5*time.Second)) |
| 395 | results = doFooBarHTTPRequest(vhostPort, "example.com") |
| 396 | framework.ExpectContainElements(results, []string{"foo"}) |
| 397 | framework.ExpectNotContainElements(results, []string{"bar"}) |
| 398 |
| 399 | // resume bar server, check foo and bar is ok |
| 400 | successCount := clientProcesses[0].CountOutput("[bar] health check success") |
| 401 | f.RunServer("", barServer) |
| 402 | framework.ExpectNoError(clientProcesses[0].WaitForOutput("[bar] health check success", successCount+1, 5*time.Second)) |
| 403 | results = doFooBarHTTPRequest(vhostPort, "example.com") |
| 404 | framework.ExpectContainElements(results, []string{"foo", "bar"}) |
| 405 | }) |
| 406 | }) |
| 407 | }) |