|
| 1 | +package proxycommands |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/loft-sh/devspace/cmd" |
| 12 | + "github.com/loft-sh/devspace/cmd/flags" |
| 13 | + "github.com/loft-sh/devspace/e2e/framework" |
| 14 | + "github.com/loft-sh/devspace/e2e/kube" |
| 15 | + "github.com/loft-sh/devspace/pkg/util/factory" |
| 16 | + "github.com/onsi/ginkgo" |
| 17 | + corev1 "k8s.io/api/core/v1" |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | + "k8s.io/apimachinery/pkg/util/wait" |
| 20 | +) |
| 21 | + |
| 22 | +var _ = DevSpaceDescribe("proxyCommands", func() { |
| 23 | + initialDir, err := os.Getwd() |
| 24 | + if err != nil { |
| 25 | + panic(err) |
| 26 | + } |
| 27 | + |
| 28 | + // create a new factory |
| 29 | + var ( |
| 30 | + f factory.Factory |
| 31 | + kubeClient *kube.KubeHelper |
| 32 | + ) |
| 33 | + |
| 34 | + ginkgo.BeforeEach(func() { |
| 35 | + f = framework.NewDefaultFactory() |
| 36 | + |
| 37 | + kubeClient, err = kube.NewKubeHelper() |
| 38 | + framework.ExpectNoError(err) |
| 39 | + }) |
| 40 | + |
| 41 | + ginkgo.It("devspace dev should proxy commands to host machine", func() { |
| 42 | + tempDir, err := framework.CopyToTempDir("tests/proxycommands/testdata/proxycommands-simple") |
| 43 | + framework.ExpectNoError(err) |
| 44 | + defer framework.CleanupTempDir(initialDir, tempDir) |
| 45 | + |
| 46 | + ns, err := kubeClient.CreateNamespace("proxycommands") |
| 47 | + framework.ExpectNoError(err) |
| 48 | + defer framework.ExpectDeleteNamespace(kubeClient, ns) |
| 49 | + |
| 50 | + // create a new dev command and start it |
| 51 | + done := make(chan error) |
| 52 | + cancelCtx, cancel := context.WithCancel(context.Background()) |
| 53 | + defer cancel() |
| 54 | + |
| 55 | + go func() { |
| 56 | + defer ginkgo.GinkgoRecover() |
| 57 | + |
| 58 | + devCmd := &cmd.DevCmd{ |
| 59 | + GlobalFlags: &flags.GlobalFlags{ |
| 60 | + NoWarn: true, |
| 61 | + Namespace: ns, |
| 62 | + }, |
| 63 | + Ctx: cancelCtx, |
| 64 | + } |
| 65 | + |
| 66 | + done <- devCmd.Run(f) |
| 67 | + }() |
| 68 | + |
| 69 | + // Check that the command is proxied to the host. |
| 70 | + var stdout, stderr bytes.Buffer |
| 71 | + cmd := exec.Command("uname", "-n") |
| 72 | + cmd.Stdout = &stdout |
| 73 | + cmd.Stderr = &stderr |
| 74 | + err = cmd.Run() |
| 75 | + framework.ExpectNoError(err) |
| 76 | + |
| 77 | + // Get the expected Pod hostname |
| 78 | + var pods *corev1.PodList |
| 79 | + err = wait.Poll(time.Second, time.Minute, func() (done bool, err error) { |
| 80 | + pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: "app.kubernetes.io/component=test"}) |
| 81 | + if err != nil { |
| 82 | + return false, err |
| 83 | + } |
| 84 | + fmt.Printf("%+v\n", pods) |
| 85 | + return len(pods.Items) > 0, nil |
| 86 | + }) |
| 87 | + framework.ExpectNoError(err) |
| 88 | + podName := pods.Items[0].Name |
| 89 | + |
| 90 | + framework.ExpectLocalFileContents("host.out", stdout.String()) |
| 91 | + framework.ExpectRemoteFileContents("alpine", ns, "container.out", fmt.Sprintf("%s\n", podName)) |
| 92 | + |
| 93 | + cancel() |
| 94 | + |
| 95 | + err = <-done |
| 96 | + framework.ExpectNoError(err) |
| 97 | + }) |
| 98 | +}) |
0 commit comments