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