client.go 3.5 KB

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