mockservers.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package framework
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "github.com/fatedier/frp/test/e2e/framework/consts"
  7. "github.com/fatedier/frp/test/e2e/mock/server"
  8. "github.com/fatedier/frp/test/e2e/mock/server/httpserver"
  9. "github.com/fatedier/frp/test/e2e/mock/server/streamserver"
  10. "github.com/fatedier/frp/test/e2e/pkg/port"
  11. )
  12. const (
  13. TCPEchoServerPort = "TCPEchoServerPort"
  14. UDPEchoServerPort = "UDPEchoServerPort"
  15. UDSEchoServerAddr = "UDSEchoServerAddr"
  16. HTTPSimpleServerPort = "HTTPSimpleServerPort"
  17. )
  18. type MockServers struct {
  19. tcpEchoServer server.Server
  20. udpEchoServer server.Server
  21. udsEchoServer server.Server
  22. httpSimpleServer server.Server
  23. }
  24. func NewMockServers(portAllocator *port.Allocator) *MockServers {
  25. s := &MockServers{}
  26. tcpPort := portAllocator.Get()
  27. udpPort := portAllocator.Get()
  28. httpPort := portAllocator.Get()
  29. s.tcpEchoServer = streamserver.New(streamserver.TCP, streamserver.WithBindPort(tcpPort))
  30. s.udpEchoServer = streamserver.New(streamserver.UDP, streamserver.WithBindPort(udpPort))
  31. s.httpSimpleServer = httpserver.New(httpserver.WithBindPort(httpPort), httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  32. w.Write([]byte(consts.TestString))
  33. })))
  34. udsIndex := portAllocator.Get()
  35. udsAddr := fmt.Sprintf("%s/frp_echo_server_%d.sock", os.TempDir(), udsIndex)
  36. os.Remove(udsAddr)
  37. s.udsEchoServer = streamserver.New(streamserver.Unix, streamserver.WithBindAddr(udsAddr))
  38. return s
  39. }
  40. func (m *MockServers) Run() error {
  41. if err := m.tcpEchoServer.Run(); err != nil {
  42. return err
  43. }
  44. if err := m.udpEchoServer.Run(); err != nil {
  45. return err
  46. }
  47. if err := m.udsEchoServer.Run(); err != nil {
  48. return err
  49. }
  50. if err := m.httpSimpleServer.Run(); err != nil {
  51. return err
  52. }
  53. return nil
  54. }
  55. func (m *MockServers) Close() {
  56. m.tcpEchoServer.Close()
  57. m.udpEchoServer.Close()
  58. m.udsEchoServer.Close()
  59. m.httpSimpleServer.Close()
  60. os.Remove(m.udsEchoServer.BindAddr())
  61. }
  62. func (m *MockServers) GetTemplateParams() map[string]interface{} {
  63. ret := make(map[string]interface{})
  64. ret[TCPEchoServerPort] = m.tcpEchoServer.BindPort()
  65. ret[UDPEchoServerPort] = m.udpEchoServer.BindPort()
  66. ret[UDSEchoServerAddr] = m.udsEchoServer.BindAddr()
  67. ret[HTTPSimpleServerPort] = m.httpSimpleServer.BindPort()
  68. return ret
  69. }
  70. func (m *MockServers) GetParam(key string) interface{} {
  71. params := m.GetTemplateParams()
  72. if v, ok := params[key]; ok {
  73. return v
  74. }
  75. return nil
  76. }