| 9 |
| 10 | "github.com/onsi/ginkgo/v2" |
| 11 |
| 12 | "github.com/fatedier/frp/test/e2e/framework" |
| 13 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 14 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 15 | ) |
| 16 |
| 17 | var _ = ginkgo.Describe("[Feature: ClientManage]", func() { |
| 18 | f := framework.NewDefaultFramework() |
| 19 |
| 20 | ginkgo.It("Update && Reload API", func() { |
| 21 | serverConf := consts.DefaultServerConfig |
| 22 |
| 23 | adminPort := f.AllocPort() |
| 24 |
| 25 | p1Port := f.AllocPort() |
| 26 | p2Port := f.AllocPort() |
| 27 | p3Port := f.AllocPort() |
| 28 |
| 29 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 30 | webServer.port = %d |
| 31 |
| 32 | [[proxies]] |
| 33 | name = "p1" |
| 34 | type = "tcp" |
| 35 | localPort = {{ .%s }} |
| 36 | remotePort = %d |
| 37 |
| 38 | [[proxies]] |
| 39 | name = "p2" |
| 40 | type = "tcp" |
| 41 | localPort = {{ .%s }} |
| 42 | remotePort = %d |
| 43 |
| 44 | [[proxies]] |
| 45 | name = "p3" |
| 46 | type = "tcp" |
| 47 | localPort = {{ .%s }} |
| 48 | remotePort = %d |
| 49 | `, adminPort, |
| 50 | framework.TCPEchoServerPort, p1Port, |
| 51 | framework.TCPEchoServerPort, p2Port, |
| 52 | framework.TCPEchoServerPort, p3Port) |
| 53 |
| 54 | f.RunProcesses(serverConf, []string{clientConf}) |
| 55 |
| 56 | framework.NewRequestExpect(f).Port(p1Port).Ensure() |
| 57 | framework.NewRequestExpect(f).Port(p2Port).Ensure() |
| 58 | framework.NewRequestExpect(f).Port(p3Port).Ensure() |
| 59 |
| 60 | client := f.APIClientForFrpc(adminPort) |
| 61 | conf, err := client.GetConfig(context.Background()) |
| 62 | framework.ExpectNoError(err) |
| 63 |
| 64 | newP2Port := f.AllocPort() |
| 65 | // change p2 port and remove p3 proxy |
| 66 | newClientConf := strings.ReplaceAll(conf, strconv.Itoa(p2Port), strconv.Itoa(newP2Port)) |
| 67 | p3Index := strings.LastIndex(newClientConf, "[[proxies]]") |
| 68 | if p3Index >= 0 { |
| 69 | newClientConf = newClientConf[:p3Index] |
| 70 | } |
| 71 |
| 72 | err = client.UpdateConfig(context.Background(), newClientConf) |
| 73 | framework.ExpectNoError(err) |
| 74 |
| 75 | err = client.Reload(context.Background(), true) |
| 76 | framework.ExpectNoError(err) |
| 77 | time.Sleep(time.Second) |
| 78 |
| 79 | framework.NewRequestExpect(f).Port(p1Port).Explain("p1 port").Ensure() |
| 80 | framework.NewRequestExpect(f).Port(p2Port).Explain("original p2 port").ExpectError(true).Ensure() |
| 81 | framework.NewRequestExpect(f).Port(newP2Port).Explain("new p2 port").Ensure() |
| 82 | framework.NewRequestExpect(f).Port(p3Port).Explain("p3 port").ExpectError(true).Ensure() |
| 83 | }) |
| 84 |
| 85 | ginkgo.It("healthz", func() { |
| 86 | serverConf := consts.DefaultServerConfig |
| 87 |
| 88 | dashboardPort := f.AllocPort() |
| 89 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 90 | webServer.addr = "0.0.0.0" |
| 91 | webServer.port = %d |
| 92 | webServer.user = "admin" |
| 93 | webServer.password = "admin" |
| 94 | `, dashboardPort) |
| 95 |
| 96 | f.RunProcesses(serverConf, []string{clientConf}) |
| 97 |
| 98 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 99 | r.HTTP().HTTPPath("/healthz") |
| 100 | }).Port(dashboardPort).ExpectResp([]byte("")).Ensure() |
| 101 |
| 102 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 103 | r.HTTP().HTTPPath("/") |
| 104 | }).Port(dashboardPort). |
| 105 | Ensure(framework.ExpectResponseCode(401)) |
| 106 | }) |
| 107 |
| 108 | ginkgo.It("stop", func() { |
| 109 | serverConf := consts.DefaultServerConfig |
| 110 |
| 111 | adminPort := f.AllocPort() |
| 112 | testPort := f.AllocPort() |
| 113 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 114 | webServer.port = %d |
| 115 |
| 116 | [[proxies]] |
| 117 | name = "test" |
| 118 | type = "tcp" |
| 119 | localPort = {{ .%s }} |
| 120 | remotePort = %d |
| 121 | `, adminPort, framework.TCPEchoServerPort, testPort) |
| 122 |
| 123 | f.RunProcesses(serverConf, []string{clientConf}) |
| 124 |
| 125 | framework.NewRequestExpect(f).Port(testPort).Ensure() |
| 126 |
| 127 | client := f.APIClientForFrpc(adminPort) |
| 128 | err := client.Stop(context.Background()) |
| 129 | framework.ExpectNoError(err) |
| 130 |
| 131 | time.Sleep(3 * time.Second) |
| 132 |
| 133 | // frpc stopped so the port is not listened, expect error |
| 134 | framework.NewRequestExpect(f).Port(testPort).ExpectError(true).Ensure() |
| 135 | }) |
| 136 | }) |