| 9 | "strings" |
| 10 | "text/template" |
| 11 |
| 12 | "github.com/onsi/ginkgo/v2" |
| 13 |
| 14 | "github.com/fatedier/frp/test/e2e/mock/server" |
| 15 | "github.com/fatedier/frp/test/e2e/pkg/port" |
| 16 | "github.com/fatedier/frp/test/e2e/pkg/process" |
| 17 | ) |
| 18 |
| 19 | type Options struct { |
| 20 | TotalParallelNode int |
| 21 | CurrentNodeIndex int |
| 22 | FromPortIndex int |
| 23 | ToPortIndex int |
| 24 | } |
| 25 |
| 26 | type Framework struct { |
| 27 | TempDirectory string |
| 28 |
| 29 | // ports used in this framework indexed by port name. |
| 30 | usedPorts map[string]int |
| 31 |
| 32 | // record ports allocated by this framework and release them after each test |
| 33 | allocatedPorts []int |
| 34 |
| 35 | // portAllocator to alloc port for this test case. |
| 36 | portAllocator *port.Allocator |
| 37 |
| 38 | // Multiple default mock servers used for e2e testing. |
| 39 | mockServers *MockServers |
| 40 |
| 41 | // beforeEachStarted indicates that BeforeEach has started |
| 42 | beforeEachStarted bool |
| 43 |
| 44 | serverConfPaths []string |
| 45 | serverProcesses []*process.Process |
| 46 | clientConfPaths []string |
| 47 | clientProcesses []*process.Process |
| 48 |
| 49 | // Manual registered mock servers. |
| 50 | servers []server.Server |
| 51 |
| 52 | // used to generate unique config file name. |
| 53 | configFileIndex int64 |
| 54 |
| 55 | // envs used to start processes, the form is `key=value`. |
| 56 | osEnvs []string |
| 57 | } |
| 58 |
| 59 | func NewDefaultFramework() *Framework { |
| 60 | suiteConfig, _ := ginkgo.GinkgoConfiguration() |
| 61 | options := Options{ |
| 62 | TotalParallelNode: suiteConfig.ParallelTotal, |
| 63 | CurrentNodeIndex: suiteConfig.ParallelProcess, |
| 64 | FromPortIndex: 10000, |
| 65 | ToPortIndex: 30000, |
| 66 | } |
| 67 | return NewFramework(options) |
| 68 | } |
| 69 |
| 70 | func NewFramework(opt Options) *Framework { |
| 71 | f := &Framework{ |
| 72 | portAllocator: port.NewAllocator(opt.FromPortIndex, opt.ToPortIndex, opt.TotalParallelNode, opt.CurrentNodeIndex-1), |
| 73 | usedPorts: make(map[string]int), |
| 74 | } |
| 75 |
| 76 | ginkgo.BeforeEach(f.BeforeEach) |
| 77 | ginkgo.AfterEach(f.AfterEach) |
| 78 | return f |
| 79 | } |
| 80 |
| 81 | // BeforeEach create a temp directory. |
| 82 | func (f *Framework) BeforeEach() { |
| 83 | f.beforeEachStarted = true |
| 84 |
| 85 | dir, err := os.MkdirTemp(os.TempDir(), "frp-e2e-test-*") |
| 86 | ExpectNoError(err) |
| 87 | f.TempDirectory = dir |
| 88 |
| 89 | f.mockServers = NewMockServers(f.portAllocator) |
| 90 | if err := f.mockServers.Run(); err != nil { |
| 91 | Failf("%v", err) |
| 92 | } |
| 93 |
| 94 | params := f.mockServers.GetTemplateParams() |
| 95 | for k, v := range params { |
| 96 | switch t := v.(type) { |
| 97 | case int: |
| 98 | f.usedPorts[k] = t |
| 99 | default: |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 |
| 104 | func (f *Framework) AfterEach() { |
| 105 | if !f.beforeEachStarted { |
| 106 | return |
| 107 | } |
| 108 |
| 109 | // stop processor |
| 110 | for _, p := range f.serverProcesses { |
| 111 | _ = p.Stop() |
| 112 | if TestContext.Debug || ginkgo.CurrentSpecReport().Failed() { |
| 113 | fmt.Println(p.ErrorOutput()) |
| 114 | fmt.Println(p.StdOutput()) |
| 115 | } |
| 116 | } |
| 117 | for _, p := range f.clientProcesses { |
| 118 | _ = p.Stop() |
| 119 | if TestContext.Debug || ginkgo.CurrentSpecReport().Failed() { |
| 120 | fmt.Println(p.ErrorOutput()) |
| 121 | fmt.Println(p.StdOutput()) |
| 122 | } |
| 123 | } |
| 124 | f.serverProcesses = nil |
| 125 | f.clientProcesses = nil |
| 126 |
| 127 | // close default mock servers |
| 128 | f.mockServers.Close() |
| 129 |
| 130 | // close manual registered mock servers |
| 131 | for _, s := range f.servers { |
| 132 | s.Close() |
| 133 | } |
| 134 |
| 135 | // clean directory |
| 136 | os.RemoveAll(f.TempDirectory) |
| 137 | f.TempDirectory = "" |
| 138 | f.serverConfPaths = []string{} |
| 139 | f.clientConfPaths = []string{} |
| 140 |
| 141 | // release used ports |
| 142 | for _, port := range f.usedPorts { |
| 143 | f.portAllocator.Release(port) |
| 144 | } |
| 145 | f.usedPorts = make(map[string]int) |
| 146 |
| 147 | // release allocated ports |
| 148 | for _, port := range f.allocatedPorts { |
| 149 | f.portAllocator.Release(port) |
| 150 | } |
| 151 | f.allocatedPorts = make([]int, 0) |
| 152 |
| 153 | // clear os envs |
| 154 | f.osEnvs = make([]string, 0) |
| 155 | } |
| 156 |
| 157 | var portRegex = regexp.MustCompile(`{{ \.Port.*? }}`) |
| 158 |
| 159 | // RenderPortsTemplate render templates with ports. |
| 160 | // |
| 161 | // Local: {{ .Port1 }} |
| 162 | // Target: {{ .Port2 }} |
| 163 | // |
| 164 | // return rendered content and all allocated ports. |
| 165 | func (f *Framework) genPortsFromTemplates(templates []string) (ports map[string]int, err error) { |
| 166 | ports = make(map[string]int) |
| 167 | for _, t := range templates { |
| 168 | arrs := portRegex.FindAllString(t, -1) |
| 169 | for _, str := range arrs { |
| 170 | str = strings.TrimPrefix(str, "{{ .") |
| 171 | str = strings.TrimSuffix(str, " }}") |
| 172 | str = strings.TrimSpace(str) |
| 173 | ports[str] = 0 |
| 174 | } |
| 175 | } |
| 176 | defer func() { |
| 177 | if err != nil { |
| 178 | for _, port := range ports { |
| 179 | f.portAllocator.Release(port) |
| 180 | } |
| 181 | } |
| 182 | }() |
| 183 |
| 184 | for name := range ports { |
| 185 | port := f.portAllocator.GetByName(name) |
| 186 | if port <= 0 { |
| 187 | return nil, fmt.Errorf("can't allocate port") |
| 188 | } |
| 189 | ports[name] = port |
| 190 | } |
| 191 | return |
| 192 | } |
| 193 |
| 194 | // RenderTemplates alloc all ports for port names placeholder. |
| 195 | func (f *Framework) RenderTemplates(templates []string) (outs []string, ports map[string]int, err error) { |
| 196 | ports, err = f.genPortsFromTemplates(templates) |
| 197 | if err != nil { |
| 198 | return |
| 199 | } |
| 200 |
| 201 | params := f.mockServers.GetTemplateParams() |
| 202 | for name, port := range ports { |
| 203 | params[name] = port |
| 204 | } |
| 205 |
| 206 | for name, port := range f.usedPorts { |
| 207 | params[name] = port |
| 208 | } |
| 209 |
| 210 | for _, t := range templates { |
| 211 | tmpl, err := template.New("frp-e2e").Parse(t) |
| 212 | if err != nil { |
| 213 | return nil, nil, err |
| 214 | } |
| 215 | buffer := bytes.NewBuffer(nil) |
| 216 | if err = tmpl.Execute(buffer, params); err != nil { |
| 217 | return nil, nil, err |
| 218 | } |
| 219 | outs = append(outs, buffer.String()) |
| 220 | } |
| 221 | return |
| 222 | } |
| 223 |
| 224 | func (f *Framework) PortByName(name string) int { |
| 225 | return f.usedPorts[name] |
| 226 | } |
| 227 |
| 228 | func (f *Framework) AllocPort() int { |
| 229 | port := f.portAllocator.Get() |
| 230 | ExpectTrue(port > 0, "alloc port failed") |
| 231 | f.allocatedPorts = append(f.allocatedPorts, port) |
| 232 | return port |
| 233 | } |
| 234 |
| 235 | func (f *Framework) AllocPortExcludingRanges(ranges ...[2]int) int { |
| 236 | for range 1000 { |
| 237 | port := f.portAllocator.Get() |
| 238 | ExpectTrue(port > 0, "alloc port failed") |
| 239 |
| 240 | inExcludedRange := false |
| 241 | for _, portRange := range ranges { |
| 242 | if port >= portRange[0] && port <= portRange[1] { |
| 243 | inExcludedRange = true |
| 244 | break |
| 245 | } |
| 246 | } |
| 247 | if inExcludedRange { |
| 248 | f.portAllocator.Release(port) |
| 249 | continue |
| 250 | } |
| 251 |
| 252 | f.allocatedPorts = append(f.allocatedPorts, port) |
| 253 | return port |
| 254 | } |
| 255 |
| 256 | Failf("alloc port outside excluded ranges failed") |
| 257 | return 0 |
| 258 | } |
| 259 |
| 260 | func (f *Framework) RunServer(portName string, s server.Server) { |
| 261 | f.servers = append(f.servers, s) |
| 262 | if s.BindPort() > 0 && portName != "" { |
| 263 | f.usedPorts[portName] = s.BindPort() |
| 264 | } |
| 265 | err := s.Run() |
| 266 | ExpectNoError(err, "RunServer: with PortName %s", portName) |
| 267 | } |
| 268 |
| 269 | func (f *Framework) SetEnvs(envs []string) { |
| 270 | f.osEnvs = envs |
| 271 | } |
| 272 |
| 273 | func (f *Framework) WriteTempFile(name string, content string) string { |
| 274 | filePath := filepath.Join(f.TempDirectory, name) |
| 275 | err := os.WriteFile(filePath, []byte(content), 0o600) |
| 276 | ExpectNoError(err) |
| 277 | return filePath |
| 278 | } |