From 9cde7c374b6209efca7df96c102936e437168d07 Mon Sep 17 00:00:00 2001 From: Parthvi Vala Date: Wed, 12 Apr 2023 17:28:00 +0530 Subject: [PATCH] Refactor integration test Signed-off-by: Parthvi Vala Add dev --debug integration test for podman Signed-off-by: Parthvi Vala Add dev --debug integration test for podman Signed-off-by: Parthvi Vala Fix ci failures Signed-off-by: Parthvi Vala --- pkg/odo/cli/dev/dev.go | 13 ++- tests/integration/cmd_dev_debug_test.go | 85 ++++++++++-------- tests/integration/cmd_dev_test.go | 110 +++++++++++------------- 3 files changed, 106 insertions(+), 102 deletions(-) diff --git a/pkg/odo/cli/dev/dev.go b/pkg/odo/cli/dev/dev.go index e6135627552..a9f34f703ca 100644 --- a/pkg/odo/cli/dev/dev.go +++ b/pkg/odo/cli/dev/dev.go @@ -4,16 +4,18 @@ import ( "context" "errors" "fmt" - "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" - "github.com/redhat-developer/odo/pkg/api" "io" - "k8s.io/klog" "path/filepath" "regexp" "sort" "strconv" "strings" + "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" + "k8s.io/klog" + + "github.com/redhat-developer/odo/pkg/api" + "github.com/spf13/cobra" ktemplates "k8s.io/kubectl/pkg/util/templates" @@ -200,11 +202,6 @@ func (o *DevOptions) Run(ctx context.Context) (err error) { genericclioptions.WarnIfDefaultNamespace(odocontext.GetNamespace(ctx), o.clientset.KubernetesClient) } - // TODO: Remove this once --port-forward has been implemented for podman. - if platform == commonflags.PlatformPodman && len(o.portForwardFlag) != 0 { - fmt.Println() - log.Warning("--port-forward flag has not been implemented for podman yet, we will use the normal port forwarding") - } // check for .gitignore file and add odo-file-index.json to .gitignore. // In case the .gitignore was created by odo, it is purposely not reported as candidate for deletion (via a call to files.ReportLocalFileGeneratedByOdo) // because a .gitignore file is more likely to be modified by the user afterward (for another usage). diff --git a/tests/integration/cmd_dev_debug_test.go b/tests/integration/cmd_dev_debug_test.go index ea9da402b7d..419e7fed991 100644 --- a/tests/integration/cmd_dev_debug_test.go +++ b/tests/integration/cmd_dev_debug_test.go @@ -2,10 +2,11 @@ package integration import ( "fmt" - "k8s.io/utils/pointer" "path/filepath" "strings" + "k8s.io/utils/pointer" + "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" "github.com/redhat-developer/odo/pkg/labels" @@ -40,47 +41,59 @@ var _ = Describe("odo dev debug command tests", func() { helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile-with-debugrun.yaml")).ShouldPass() Expect(helper.VerifyFileExists(".odo/env/env.yaml")).To(BeFalse()) }) - if !podman { - // TODO(pvala): Refactor this when custom port-mapping for port forwarding has been implemented for podman - When("running odo dev with debug flag and custom port mapping for port forwarding", func() { - var devSession helper.DevSession - var ports map[string]string - var ( - LocalPort = helper.GetRandomFreePort() - LocalDebugPort = helper.GetRandomFreePort() - ) - const ( - ContainerPort = "3000" - ContainerDebugPort = "5858" - ) - BeforeEach(func() { - opts := []string{"--debug", fmt.Sprintf("--port-forward=%s:%s", LocalPort, ContainerPort), fmt.Sprintf("--port-forward=%s:%s", LocalDebugPort, ContainerDebugPort)} - var err error - devSession, _, _, ports, err = helper.StartDevMode(helper.DevSessionOpts{ - CmdlineArgs: opts, - NoRandomPorts: true, - }) - Expect(err).ToNot(HaveOccurred()) + When("running odo dev with debug flag and custom port mapping for port forwarding", helper.LabelPodmanIf(podman, func() { + var ( + devSession helper.DevSession + ports map[string]string + ) + var ( + LocalPort = helper.GetRandomFreePort() + LocalDebugPort = helper.GetRandomFreePort() + ) + const ( + ContainerPort = "3000" + ContainerDebugPort = "5858" + ) + + BeforeEach(func() { + opts := []string{"--debug", fmt.Sprintf("--port-forward=%s:%s", LocalPort, ContainerPort), fmt.Sprintf("--port-forward=%s:%s", LocalDebugPort, ContainerDebugPort)} + if podman { + // Debugging works with podman if we use --forward-localhost, but that will not use custom port-mapping; + // so we use --ignore-localhost to simply test if custom ports are still taken into account + opts = append(opts, "--ignore-localhost") + } + var err error + devSession, _, _, ports, err = helper.StartDevMode(helper.DevSessionOpts{ + CmdlineArgs: opts, + NoRandomPorts: true, + RunOnPodman: podman, }) + Expect(err).ToNot(HaveOccurred()) + }) - AfterEach(func() { - devSession.Stop() - devSession.WaitEnd() + AfterEach(func() { + devSession.Stop() + devSession.WaitEnd() + }) + + It("should connect to relevant custom ports forwarded", func() { + By("connecting to the application port", func() { + helper.HttpWaitForWithStatus("http://"+ports[ContainerPort], "Hello from Node.js Starter Application!", 12, 5, 200) }) - It("should connect to relevant custom ports forwarded", func() { - By("connecting to the application port", func() { - helper.HttpWaitForWithStatus("http://"+ports[ContainerPort], "Hello from Node.js Starter Application!", 12, 5, 200) - }) - By("expecting a ws connection when tried to connect on default debug port locally", func() { - // 400 response expected because the endpoint expects a websocket request and we are doing a HTTP GET - // We are just using this to validate if nodejs agent is listening on the other side - url := fmt.Sprintf("http://%s", ports[ContainerDebugPort]) - Expect(url).To(ContainSubstring(LocalDebugPort)) + By("expecting a ws connection when tried to connect on default debug port locally", func() { + // 400 response expected because the endpoint expects a websocket request and we are doing a HTTP GET + // We are just using this to validate if nodejs agent is listening on the other side + url := fmt.Sprintf("http://%s", ports[ContainerDebugPort]) + Expect(url).To(ContainSubstring(LocalDebugPort)) + if !podman { + // this check doesn't work for podman unless we use --forward-localhost, + // but using it beats the purpose of testing with custom port mapping, so we skip this check helper.HttpWaitForWithStatus(url, "WebSockets request was expected", 12, 5, 400) - }) + } }) }) - } + })) + When("running odo dev with debug flag", helper.LabelPodmanIf(podman, func() { var devSession helper.DevSession var ports map[string]string diff --git a/tests/integration/cmd_dev_test.go b/tests/integration/cmd_dev_test.go index 06f370691ea..b970d7583cb 100644 --- a/tests/integration/cmd_dev_test.go +++ b/tests/integration/cmd_dev_test.go @@ -131,6 +131,15 @@ var _ = Describe("odo dev command tests", func() { }) Expect(err).ToNot(HaveOccurred()) })) + + It("should fail when using --random-ports and --port-forward together", helper.LabelPodmanIf(podman, func() { + args := []string{"dev", "--random-ports", "--port-forward=8000:3000"} + if podman { + args = append(args, "--platform", "podman") + } + errOut := helper.Cmd("odo", args...).ShouldFail().Err() + Expect(errOut).To(ContainSubstring("--random-ports and --port-forward cannot be used together")) + })) } It("ensure that index information is updated", func() { @@ -712,69 +721,52 @@ ComponentSettings: }) } - for _, manual := range []bool{true, false} { - for _, customPortForwarding := range []bool{true, false} { - for _, podman := range []bool{true, false} { + for _, podman := range []bool{true, false} { + podman := podman + Context("port-forwarding for the component", helper.LabelPodmanIf(podman, func() { + for _, manual := range []bool{true, false} { manual := manual - customPortForwarding := customPortForwarding - podman := podman - Context("port-forwarding for the component", helper.LabelPodmanIf(podman, func() { - var NoRandomPorts bool - if customPortForwarding { - NoRandomPorts = true - } - When("a component is bootstrapped", func() { - BeforeEach(func() { - helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context) - helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile.yaml")).ShouldPass() - }) - if customPortForwarding { - It("should fail when using --random-ports and --port-forward together", func() { - args := []string{"dev", "--random-ports", "--port-forward=8000:3000"} - if podman { - args = append(args, "--platform", "podman") - } - errOut := helper.Cmd("odo", args...).ShouldFail().Err() - Expect(errOut).To(ContainSubstring("--random-ports and --port-forward cannot be used together")) - }) + When("devfile has no endpoint", func() { + BeforeEach(func() { + if !podman { + helper.Cmd("odo", "set", "project", commonVar.Project).ShouldPass() } + helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context) + helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile-no-endpoint.yaml")).ShouldPass() }) - if !customPortForwarding { - When("devfile has no endpoint", func() { - BeforeEach(func() { - if !podman { - helper.Cmd("odo", "set", "project", commonVar.Project).ShouldPass() - } - helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context) - helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile-no-endpoint.yaml")).ShouldPass() - }) - When("running odo dev", func() { - var devSession helper.DevSession - var ports map[string]string - BeforeEach(func() { - var err error - opts := []string{} - if manual { - opts = append(opts, "--no-watch") - } - devSession, _, _, ports, err = helper.StartDevMode(helper.DevSessionOpts{ - CmdlineArgs: opts, - RunOnPodman: podman, - }) - Expect(err).ToNot(HaveOccurred()) - }) + When("running odo dev", func() { + var devSession helper.DevSession + var ports map[string]string + BeforeEach(func() { + var err error + opts := []string{} + if manual { + opts = append(opts, "--no-watch") + } + devSession, _, _, ports, err = helper.StartDevMode(helper.DevSessionOpts{ + CmdlineArgs: opts, + RunOnPodman: podman, + }) + Expect(err).ToNot(HaveOccurred()) + }) - AfterEach(func() { - devSession.Stop() - devSession.WaitEnd() - }) + AfterEach(func() { + devSession.Stop() + devSession.WaitEnd() + }) - It("should have no endpoint forwarded", func() { - Expect(ports).To(BeEmpty()) - }) - }) + It("should have no endpoint forwarded", func() { + Expect(ports).To(BeEmpty()) }) + }) + }) + + for _, customPortForwarding := range []bool{true, false} { + customPortForwarding := customPortForwarding + var NoRandomPorts bool + if customPortForwarding { + NoRandomPorts = true } When("devfile has single endpoint", func() { var ( @@ -1012,10 +1004,12 @@ ComponentSettings: }) }) - })) + + } } - } + })) } + for _, devfileHandlerCtx := range []struct { name string sourceHandler func(path string, originalCmpName string)