1
0

client.go 3.5 KB

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