| 9 | "github.com/onsi/ginkgo/v2" |
| 10 |
| 11 | "github.com/fatedier/frp/test/e2e/framework" |
| 12 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 13 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 14 | ) |
| 15 |
| 16 | var _ = ginkgo.Describe("[Feature: Store]", func() { |
| 17 | f := framework.NewDefaultFramework() |
| 18 |
| 19 | ginkgo.Describe("Store API", func() { |
| 20 | ginkgo.It("create proxy via API and verify connection", func() { |
| 21 | adminPort := f.AllocPort() |
| 22 | remotePort := f.AllocPort() |
| 23 |
| 24 | serverConf := consts.DefaultServerConfig |
| 25 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 26 | webServer.addr = "127.0.0.1" |
| 27 | webServer.port = %d |
| 28 |
| 29 | [store] |
| 30 | path = "%s/store.json" |
| 31 | `, adminPort, f.TempDirectory) |
| 32 |
| 33 | f.RunProcesses(serverConf, []string{clientConf}) |
| 34 | framework.ExpectNoError(framework.WaitForTCPReady(fmt.Sprintf("127.0.0.1:%d", adminPort), 5*time.Second)) |
| 35 |
| 36 | proxyConfig := map[string]any{ |
| 37 | "name": "test-tcp", |
| 38 | "type": "tcp", |
| 39 | "tcp": map[string]any{ |
| 40 | "localIP": "127.0.0.1", |
| 41 | "localPort": f.PortByName(framework.TCPEchoServerPort), |
| 42 | "remotePort": remotePort, |
| 43 | }, |
| 44 | } |
| 45 | proxyBody, _ := json.Marshal(proxyConfig) |
| 46 |
| 47 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 48 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies").HTTPParams("POST", "", "/api/store/proxies", map[string]string{ |
| 49 | "Content-Type": "application/json", |
| 50 | }).Body(proxyBody) |
| 51 | }).Ensure(func(resp *request.Response) bool { |
| 52 | return resp.Code == 200 |
| 53 | }) |
| 54 |
| 55 | framework.ExpectNoError(framework.WaitForTCPReady(fmt.Sprintf("127.0.0.1:%d", remotePort), 5*time.Second)) |
| 56 |
| 57 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 58 | }) |
| 59 |
| 60 | ginkgo.It("update proxy via API", func() { |
| 61 | adminPort := f.AllocPort() |
| 62 | remotePort1 := f.AllocPort() |
| 63 | remotePort2 := f.AllocPort() |
| 64 |
| 65 | serverConf := consts.DefaultServerConfig |
| 66 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 67 | webServer.addr = "127.0.0.1" |
| 68 | webServer.port = %d |
| 69 |
| 70 | [store] |
| 71 | path = "%s/store.json" |
| 72 | `, adminPort, f.TempDirectory) |
| 73 |
| 74 | f.RunProcesses(serverConf, []string{clientConf}) |
| 75 | framework.ExpectNoError(framework.WaitForTCPReady(fmt.Sprintf("127.0.0.1:%d", adminPort), 5*time.Second)) |
| 76 |
| 77 | proxyConfig := map[string]any{ |
| 78 | "name": "test-tcp", |
| 79 | "type": "tcp", |
| 80 | "tcp": map[string]any{ |
| 81 | "localIP": "127.0.0.1", |
| 82 | "localPort": f.PortByName(framework.TCPEchoServerPort), |
| 83 | "remotePort": remotePort1, |
| 84 | }, |
| 85 | } |
| 86 | proxyBody, _ := json.Marshal(proxyConfig) |
| 87 |
| 88 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 89 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies").HTTPParams("POST", "", "/api/store/proxies", map[string]string{ |
| 90 | "Content-Type": "application/json", |
| 91 | }).Body(proxyBody) |
| 92 | }).Ensure(func(resp *request.Response) bool { |
| 93 | return resp.Code == 200 |
| 94 | }) |
| 95 |
| 96 | framework.ExpectNoError(framework.WaitForTCPReady(fmt.Sprintf("127.0.0.1:%d", remotePort1), 5*time.Second)) |
| 97 | framework.NewRequestExpect(f).Port(remotePort1).Ensure() |
| 98 |
| 99 | proxyConfig["tcp"].(map[string]any)["remotePort"] = remotePort2 |
| 100 | proxyBody, _ = json.Marshal(proxyConfig) |
| 101 |
| 102 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 103 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies/test-tcp").HTTPParams("PUT", "", "/api/store/proxies/test-tcp", map[string]string{ |
| 104 | "Content-Type": "application/json", |
| 105 | }).Body(proxyBody) |
| 106 | }).Ensure(func(resp *request.Response) bool { |
| 107 | return resp.Code == 200 |
| 108 | }) |
| 109 |
| 110 | framework.ExpectNoError(framework.WaitForTCPReady(fmt.Sprintf("127.0.0.1:%d", remotePort2), 5*time.Second)) |
| 111 | framework.ExpectNoError(framework.WaitForTCPUnreachable(fmt.Sprintf("127.0.0.1:%d", remotePort1), 100*time.Millisecond, 5*time.Second)) |
| 112 | framework.NewRequestExpect(f).Port(remotePort2).Ensure() |
| 113 | framework.NewRequestExpect(f).Port(remotePort1).ExpectError(true).Ensure() |
| 114 | }) |
| 115 |
| 116 | ginkgo.It("delete proxy via API", func() { |
| 117 | adminPort := f.AllocPort() |
| 118 | remotePort := f.AllocPort() |
| 119 |
| 120 | serverConf := consts.DefaultServerConfig |
| 121 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 122 | webServer.addr = "127.0.0.1" |
| 123 | webServer.port = %d |
| 124 |
| 125 | [store] |
| 126 | path = "%s/store.json" |
| 127 | `, adminPort, f.TempDirectory) |
| 128 |
| 129 | f.RunProcesses(serverConf, []string{clientConf}) |
| 130 | framework.ExpectNoError(framework.WaitForTCPReady(fmt.Sprintf("127.0.0.1:%d", adminPort), 5*time.Second)) |
| 131 |
| 132 | proxyConfig := map[string]any{ |
| 133 | "name": "test-tcp", |
| 134 | "type": "tcp", |
| 135 | "tcp": map[string]any{ |
| 136 | "localIP": "127.0.0.1", |
| 137 | "localPort": f.PortByName(framework.TCPEchoServerPort), |
| 138 | "remotePort": remotePort, |
| 139 | }, |
| 140 | } |
| 141 | proxyBody, _ := json.Marshal(proxyConfig) |
| 142 |
| 143 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 144 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies").HTTPParams("POST", "", "/api/store/proxies", map[string]string{ |
| 145 | "Content-Type": "application/json", |
| 146 | }).Body(proxyBody) |
| 147 | }).Ensure(func(resp *request.Response) bool { |
| 148 | return resp.Code == 200 |
| 149 | }) |
| 150 |
| 151 | framework.ExpectNoError(framework.WaitForTCPReady(fmt.Sprintf("127.0.0.1:%d", remotePort), 5*time.Second)) |
| 152 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 153 |
| 154 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 155 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies/test-tcp").HTTPParams("DELETE", "", "/api/store/proxies/test-tcp", nil) |
| 156 | }).Ensure(func(resp *request.Response) bool { |
| 157 | return resp.Code == 200 |
| 158 | }) |
| 159 |
| 160 | framework.ExpectNoError(framework.WaitForTCPUnreachable(fmt.Sprintf("127.0.0.1:%d", remotePort), 100*time.Millisecond, 5*time.Second)) |
| 161 | framework.NewRequestExpect(f).Port(remotePort).ExpectError(true).Ensure() |
| 162 | }) |
| 163 |
| 164 | ginkgo.It("list and get proxy via API", func() { |
| 165 | adminPort := f.AllocPort() |
| 166 | remotePort := f.AllocPort() |
| 167 |
| 168 | serverConf := consts.DefaultServerConfig |
| 169 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 170 | webServer.addr = "127.0.0.1" |
| 171 | webServer.port = %d |
| 172 |
| 173 | [store] |
| 174 | path = "%s/store.json" |
| 175 | `, adminPort, f.TempDirectory) |
| 176 |
| 177 | f.RunProcesses(serverConf, []string{clientConf}) |
| 178 | framework.ExpectNoError(framework.WaitForTCPReady(fmt.Sprintf("127.0.0.1:%d", adminPort), 5*time.Second)) |
| 179 |
| 180 | proxyConfig := map[string]any{ |
| 181 | "name": "test-tcp", |
| 182 | "type": "tcp", |
| 183 | "tcp": map[string]any{ |
| 184 | "localIP": "127.0.0.1", |
| 185 | "localPort": f.PortByName(framework.TCPEchoServerPort), |
| 186 | "remotePort": remotePort, |
| 187 | }, |
| 188 | } |
| 189 | proxyBody, _ := json.Marshal(proxyConfig) |
| 190 |
| 191 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 192 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies").HTTPParams("POST", "", "/api/store/proxies", map[string]string{ |
| 193 | "Content-Type": "application/json", |
| 194 | }).Body(proxyBody) |
| 195 | }).Ensure(func(resp *request.Response) bool { |
| 196 | return resp.Code == 200 |
| 197 | }) |
| 198 |
| 199 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 200 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies") |
| 201 | }).Ensure(func(resp *request.Response) bool { |
| 202 | return resp.Code == 200 && strings.Contains(string(resp.Content), "test-tcp") |
| 203 | }) |
| 204 |
| 205 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 206 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies/test-tcp") |
| 207 | }).Ensure(func(resp *request.Response) bool { |
| 208 | return resp.Code == 200 && strings.Contains(string(resp.Content), "test-tcp") |
| 209 | }) |
| 210 |
| 211 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 212 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies/nonexistent") |
| 213 | }).Ensure(func(resp *request.Response) bool { |
| 214 | return resp.Code == 404 |
| 215 | }) |
| 216 | }) |
| 217 |
| 218 | ginkgo.It("store disabled returns 404", func() { |
| 219 | adminPort := f.AllocPort() |
| 220 |
| 221 | serverConf := consts.DefaultServerConfig |
| 222 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 223 | webServer.addr = "127.0.0.1" |
| 224 | webServer.port = %d |
| 225 | `, adminPort) |
| 226 |
| 227 | f.RunProcesses(serverConf, []string{clientConf}) |
| 228 | framework.ExpectNoError(framework.WaitForTCPReady(fmt.Sprintf("127.0.0.1:%d", adminPort), 5*time.Second)) |
| 229 |
| 230 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 231 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies") |
| 232 | }).Ensure(func(resp *request.Response) bool { |
| 233 | return resp.Code == 404 |
| 234 | }) |
| 235 | }) |
| 236 |
| 237 | ginkgo.It("rejects mismatched type block", func() { |
| 238 | adminPort := f.AllocPort() |
| 239 |
| 240 | serverConf := consts.DefaultServerConfig |
| 241 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 242 | webServer.addr = "127.0.0.1" |
| 243 | webServer.port = %d |
| 244 |
| 245 | [store] |
| 246 | path = "%s/store.json" |
| 247 | `, adminPort, f.TempDirectory) |
| 248 |
| 249 | f.RunProcesses(serverConf, []string{clientConf}) |
| 250 | framework.ExpectNoError(framework.WaitForTCPReady(fmt.Sprintf("127.0.0.1:%d", adminPort), 5*time.Second)) |
| 251 |
| 252 | invalidBody, _ := json.Marshal(map[string]any{ |
| 253 | "name": "bad-proxy", |
| 254 | "type": "tcp", |
| 255 | "udp": map[string]any{ |
| 256 | "localPort": 1234, |
| 257 | }, |
| 258 | }) |
| 259 |
| 260 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 261 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies").HTTPParams("POST", "", "/api/store/proxies", map[string]string{ |
| 262 | "Content-Type": "application/json", |
| 263 | }).Body(invalidBody) |
| 264 | }).Ensure(func(resp *request.Response) bool { |
| 265 | return resp.Code == 400 |
| 266 | }) |
| 267 | }) |
| 268 |
| 269 | ginkgo.It("rejects path/body name mismatch on update", func() { |
| 270 | adminPort := f.AllocPort() |
| 271 | remotePort := f.AllocPort() |
| 272 |
| 273 | serverConf := consts.DefaultServerConfig |
| 274 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 275 | webServer.addr = "127.0.0.1" |
| 276 | webServer.port = %d |
| 277 |
| 278 | [store] |
| 279 | path = "%s/store.json" |
| 280 | `, adminPort, f.TempDirectory) |
| 281 |
| 282 | f.RunProcesses(serverConf, []string{clientConf}) |
| 283 | framework.ExpectNoError(framework.WaitForTCPReady(fmt.Sprintf("127.0.0.1:%d", adminPort), 5*time.Second)) |
| 284 |
| 285 | createBody, _ := json.Marshal(map[string]any{ |
| 286 | "name": "proxy-a", |
| 287 | "type": "tcp", |
| 288 | "tcp": map[string]any{ |
| 289 | "localIP": "127.0.0.1", |
| 290 | "localPort": f.PortByName(framework.TCPEchoServerPort), |
| 291 | "remotePort": remotePort, |
| 292 | }, |
| 293 | }) |
| 294 |
| 295 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 296 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies").HTTPParams("POST", "", "/api/store/proxies", map[string]string{ |
| 297 | "Content-Type": "application/json", |
| 298 | }).Body(createBody) |
| 299 | }).Ensure(func(resp *request.Response) bool { |
| 300 | return resp.Code == 200 |
| 301 | }) |
| 302 |
| 303 | updateBody, _ := json.Marshal(map[string]any{ |
| 304 | "name": "proxy-b", |
| 305 | "type": "tcp", |
| 306 | "tcp": map[string]any{ |
| 307 | "localIP": "127.0.0.1", |
| 308 | "localPort": f.PortByName(framework.TCPEchoServerPort), |
| 309 | "remotePort": remotePort, |
| 310 | }, |
| 311 | }) |
| 312 |
| 313 | framework.NewRequestExpect(f).RequestModify(func(r *request.Request) { |
| 314 | r.HTTP().Port(adminPort).HTTPPath("/api/store/proxies/proxy-a").HTTPParams("PUT", "", "/api/store/proxies/proxy-a", map[string]string{ |
| 315 | "Content-Type": "application/json", |
| 316 | }).Body(updateBody) |
| 317 | }).Ensure(func(resp *request.Response) bool { |
| 318 | return resp.Code == 400 |
| 319 | }) |
| 320 | }) |
| 321 | }) |
| 322 | }) |