client.go 3.6 KB

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