|
1 | 1 | package portforward |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/loft-sh/devspace/cmd" |
| 7 | + "github.com/loft-sh/devspace/cmd/flags" |
| 8 | + "github.com/loft-sh/devspace/e2e/framework" |
| 9 | + "github.com/loft-sh/devspace/e2e/kube" |
| 10 | + "github.com/loft-sh/devspace/pkg/util/factory" |
4 | 11 | "github.com/onsi/ginkgo" |
| 12 | + "net/http" |
| 13 | + "os" |
5 | 14 | ) |
6 | 15 |
|
7 | 16 | var _ = DevSpaceDescribe("portforward", func() { |
8 | | - ginkgo.It("should forward ports", func() { |
9 | | - // TODO |
10 | | - }) |
| 17 | + initialDir, err := os.Getwd() |
| 18 | + if err != nil { |
| 19 | + panic(err) |
| 20 | + } |
11 | 21 |
|
12 | | - ginkgo.It("should restart port forwarding", func() { |
13 | | - // TODO |
14 | | - }) |
| 22 | + // create a new factory |
| 23 | + var ( |
| 24 | + f factory.Factory |
| 25 | + kubeClient *kube.KubeHelper |
| 26 | + ) |
| 27 | + |
| 28 | + ginkgo.BeforeEach(func() { |
| 29 | + f = framework.NewDefaultFactory() |
15 | 30 |
|
16 | | - ginkgo.It("should reverse forward ports", func() { |
17 | | - // TODO |
| 31 | + kubeClient, err = kube.NewKubeHelper() |
| 32 | + framework.ExpectNoError(err) |
| 33 | + |
| 34 | + go func() { |
| 35 | + http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { |
| 36 | + _, _ = fmt.Fprintf(w, "Hello World!") |
| 37 | + }) |
| 38 | + fmt.Println("Server started at port 8888") |
| 39 | + err := http.ListenAndServe(":8888", nil) |
| 40 | + framework.ExpectNoError(err) |
| 41 | + }() |
18 | 42 | }) |
19 | 43 |
|
20 | | - ginkgo.It("should restart reverse forward ports", func() { |
21 | | - // TODO |
| 44 | + ginkgo.It("should forward and reverse forward ports", func() { |
| 45 | + tempDir, err := framework.CopyToTempDir("tests/portforward/testdata/portforward-simple") |
| 46 | + framework.ExpectNoError(err) |
| 47 | + defer framework.CleanupTempDir(initialDir, tempDir) |
| 48 | + |
| 49 | + ns, err := kubeClient.CreateNamespace("portforward") |
| 50 | + framework.ExpectNoError(err) |
| 51 | + defer framework.ExpectDeleteNamespace(kubeClient, ns) |
| 52 | + |
| 53 | + // create a new dev command and start it |
| 54 | + done := make(chan error) |
| 55 | + cancelCtx, cancel := context.WithCancel(context.Background()) |
| 56 | + defer cancel() |
| 57 | + |
| 58 | + go func() { |
| 59 | + defer ginkgo.GinkgoRecover() |
| 60 | + devCmd := &cmd.RunPipelineCmd{ |
| 61 | + GlobalFlags: &flags.GlobalFlags{ |
| 62 | + NoWarn: true, |
| 63 | + Namespace: ns, |
| 64 | + ConfigPath: "devspace.yaml", |
| 65 | + }, |
| 66 | + Pipeline: "dev", |
| 67 | + Ctx: cancelCtx, |
| 68 | + } |
| 69 | + done <- devCmd.RunDefault(f) |
| 70 | + }() |
| 71 | + |
| 72 | + nginxResp := "<!DOCTYPE html>\n<html>\n<head>\n<title>Welcome to nginx!</title>\n<style>\nhtml { color-scheme: light dark; }\nbody { width: 35em; margin: 0 auto;\nfont-family: Tahoma, Verdana, Arial, sans-serif; }\n</style>\n</head>\n<body>\n<h1>Welcome to nginx!</h1>\n<p>If you see this page, the nginx web server is successfully installed and\nworking. Further configuration is required.</p>\n\n<p>For online documentation and support please refer to\n<a href=\"http://nginx.org/\">nginx.org</a>.<br/>\nCommercial support is available at\n<a href=\"http://nginx.com/\">nginx.com</a>.</p>\n\n<p><em>Thank you for using nginx.</em></p>\n</body>\n</html>" |
| 73 | + framework.ExpectRemoteCurlContents("nginx", ns, "localhost", nginxResp) |
| 74 | + framework.ExpectLocalCurlContents("http://localhost:3000", nginxResp) |
| 75 | + |
| 76 | + httpServerResp := "Hello World!" |
| 77 | + framework.ExpectLocalCurlContents("http://localhost:8888", httpServerResp) |
| 78 | + framework.ExpectRemoteCurlContents("nginx", ns, "localhost:8888", httpServerResp) |
| 79 | + |
| 80 | + cancel() |
| 81 | + err = <-done |
| 82 | + framework.ExpectNoError(err) |
22 | 83 | }) |
| 84 | + |
23 | 85 | }) |
0 commit comments