client.go 3.4 KB

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