diff --git a/tests/integration/cmd_dev_test.go b/tests/integration/cmd_dev_test.go index aa0edf27401..017b999a893 100644 --- a/tests/integration/cmd_dev_test.go +++ b/tests/integration/cmd_dev_test.go @@ -712,216 +712,230 @@ ComponentSettings: }) } - // TODO(pvala): Merge this into the test below once custom port mapping for port-forwarding has been implemented for podman. - Context("port-forwarding for the component with custom port mapping", func() { - When("a component is bootstrapped", func() { - BeforeEach(func() { - 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.yaml")).ShouldPass() - }) - - It("should fail when using --random-ports and --port-forward together", func() { - errOut := helper.Cmd("odo", "dev", "--random-ports", "--port-forward=8000:3000").ShouldFail().Err() - Expect(errOut).To(ContainSubstring("--random-ports and --port-forward cannot be used together")) - }) - - }) - When("devfile has single endpoint", func() { - var ( - LocalPort = helper.GetRandomFreePort() - ) - const ( - ContainerPort = "3000" - ) - BeforeEach(func() { - 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.yaml")).ShouldPass() - }) - - When("running odo dev", func() { - var devSession helper.DevSession - var ports map[string]string + for _, podman := range []bool{true} { + // TODO(pvala): Merge this into the test below once custom port mapping for port-forwarding has been implemented for podman. + Context("port-forwarding for the component with custom port mapping", helper.LabelPodmanIf(podman, func() { + When("a component is bootstrapped", func() { BeforeEach(func() { - var err error - opts := []string{fmt.Sprintf("--port-forward=%s:%s", LocalPort, ContainerPort)} - devSession, _, _, ports, err = helper.StartDevMode(helper.DevSessionOpts{ - CmdlineArgs: opts, - NoRandomPorts: true, - }) - Expect(err).ToNot(HaveOccurred()) + // 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.yaml")).ShouldPass() }) - AfterEach(func() { - devSession.Stop() - devSession.WaitEnd() + 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")) }) - It("should expose the endpoint on localhost", func() { - url := fmt.Sprintf("http://%s", ports[ContainerPort]) - Expect(url).To(ContainSubstring(LocalPort)) - resp, err := http.Get(url) - Expect(err).ToNot(HaveOccurred()) - defer resp.Body.Close() - - body, _ := io.ReadAll(resp.Body) - helper.MatchAllInOutput(string(body), []string{"Hello from Node.js Starter Application!"}) - Expect(err).ToNot(HaveOccurred()) + }) + When("devfile has single endpoint", func() { + var ( + LocalPort = helper.GetRandomFreePort() + ) + const ( + ContainerPort = "3000" + ) + BeforeEach(func() { + // 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.yaml")).ShouldPass() }) - When("modifying memoryLimit for container in Devfile", func() { - var stdout string - var stderr string + When("running odo dev", func() { + var devSession helper.DevSession + var ports map[string]string BeforeEach(func() { - src := "memoryLimit: 1024Mi" - dst := "memoryLimit: 1023Mi" - helper.ReplaceString("devfile.yaml", src, dst) var err error - var stdoutBytes []byte - var stderrBytes []byte - stdoutBytes, stderrBytes, ports, err = devSession.WaitSync() - Expect(err).Should(Succeed()) - stdout = string(stdoutBytes) - stderr = string(stderrBytes) + opts := []string{fmt.Sprintf("--port-forward=%s:%s", LocalPort, ContainerPort)} + devSession, _, _, ports, err = helper.StartDevMode(helper.DevSessionOpts{ + CmdlineArgs: opts, + NoRandomPorts: true, + RunOnPodman: podman, + }) + Expect(err).ToNot(HaveOccurred()) }) - It("should react on the Devfile modification", func() { - By("not warning users that odo dev needs to be restarted", func() { - warning := "Please restart 'odo dev'" - Expect(stdout).ShouldNot(ContainSubstring(warning)) - Expect(stderr).ShouldNot(ContainSubstring(warning)) - }) - By("updating the pod", func() { - podName := commonVar.CliRunner.GetRunningPodNameByComponent(cmpName, commonVar.Project) - bufferOutput := commonVar.CliRunner.Run("get", "pods", podName, "-o", "jsonpath='{.spec.containers[0].resources.requests.memory}'").Out.Contents() - output := string(bufferOutput) - Expect(output).To(ContainSubstring("1023Mi")) + AfterEach(func() { + devSession.Stop() + devSession.WaitEnd() + }) + + It("should expose the endpoint on localhost", func() { + url := fmt.Sprintf("http://%s", ports[ContainerPort]) + Expect(url).To(ContainSubstring(LocalPort)) + resp, err := http.Get(url) + Expect(err).ToNot(HaveOccurred()) + defer resp.Body.Close() + + body, _ := io.ReadAll(resp.Body) + helper.MatchAllInOutput(string(body), []string{"Hello from Node.js Starter Application!"}) + Expect(err).ToNot(HaveOccurred()) + }) + + When("modifying memoryLimit for container in Devfile", func() { + var stdout string + var stderr string + BeforeEach(func() { + src := "memoryLimit: 1024Mi" + dst := "memoryLimit: 1023Mi" + helper.ReplaceString("devfile.yaml", src, dst) + var err error + var stdoutBytes []byte + var stderrBytes []byte + stdoutBytes, stderrBytes, ports, err = devSession.WaitSync() + Expect(err).Should(Succeed()) + stdout = string(stdoutBytes) + stderr = string(stderrBytes) }) - By("exposing the endpoint", func() { - Eventually(func(g Gomega) { - url := fmt.Sprintf("http://%s", ports[ContainerPort]) - Expect(url).To(ContainSubstring(LocalPort)) - resp, err := http.Get(url) - g.Expect(err).ToNot(HaveOccurred()) - defer resp.Body.Close() + It("should react on the Devfile modification", func() { + if podman { + By("warning users that odo dev needs to be restarted", func() { + Expect(stdout).To(ContainSubstring( + "Detected changes in the Devfile, but this is not supported yet on Podman. Please restart 'odo dev' for such changes to be applied.")) + }) + } else { + By("not warning users that odo dev needs to be restarted", func() { + warning := "Please restart 'odo dev'" + Expect(stdout).ShouldNot(ContainSubstring(warning)) + Expect(stderr).ShouldNot(ContainSubstring(warning)) + }) + By("updating the pod", func() { + podName := commonVar.CliRunner.GetRunningPodNameByComponent(cmpName, commonVar.Project) + bufferOutput := commonVar.CliRunner.Run("get", "pods", podName, "-o", "jsonpath='{.spec.containers[0].resources.requests.memory}'").Out.Contents() + output := string(bufferOutput) + Expect(output).To(ContainSubstring("1023Mi")) + }) - body, _ := io.ReadAll(resp.Body) - for _, i := range []string{"Hello from Node.js Starter Application!"} { - g.Expect(string(body)).To(ContainSubstring(i)) - } - g.Expect(err).ToNot(HaveOccurred()) - }).WithPolling(1 * time.Second).WithTimeout(20 * time.Second).Should(Succeed()) + By("exposing the endpoint", func() { + Eventually(func(g Gomega) { + url := fmt.Sprintf("http://%s", ports[ContainerPort]) + Expect(url).To(ContainSubstring(LocalPort)) + resp, err := http.Get(url) + g.Expect(err).ToNot(HaveOccurred()) + defer resp.Body.Close() + + body, _ := io.ReadAll(resp.Body) + for _, i := range []string{"Hello from Node.js Starter Application!"} { + g.Expect(string(body)).To(ContainSubstring(i)) + } + g.Expect(err).ToNot(HaveOccurred()) + }).WithPolling(1 * time.Second).WithTimeout(20 * time.Second).Should(Succeed()) + }) + } }) }) }) }) - }) - When("devfile has multiple endpoints", func() { - var ( - LocalPort1 = helper.GetRandomFreePort() - LocalPort2 = helper.GetRandomFreePort() - LocalPort3 = helper.GetRandomFreePort() - ) - const ( - // ContainerPort are hard-coded from devfile-with-multiple-endpoints.yaml - // Note 1: Debug endpoints will not be exposed for this instance, so we do not add custom mapping for them. - // Note 2: We add custom mapping for all the endpoints so that none of them are assigned random ports from the 20001-30001 range; - // Note 2(contd.): this is to avoid a race condition where a test running in parallel is also assigned similar ranged port the one here, and we fail to access either of them. - ContainerPort1 = "3000" - ContainerPort2 = "4567" - ContainerPort3 = "7890" - ) - BeforeEach(func() { - helper.Cmd("odo", "set", "project", commonVar.Project).ShouldPass() - helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project-with-multiple-endpoints"), commonVar.Context) - helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile-with-multiple-endpoints.yaml")).ShouldPass() - }) - - When("running odo dev", func() { - var devSession helper.DevSession - var ports map[string]string + When("devfile has multiple endpoints", func() { + var ( + LocalPort1 = helper.GetRandomFreePort() + LocalPort2 = helper.GetRandomFreePort() + LocalPort3 = helper.GetRandomFreePort() + ) + const ( + // ContainerPort are hard-coded from devfile-with-multiple-endpoints.yaml + // Note 1: Debug endpoints will not be exposed for this instance, so we do not add custom mapping for them. + // Note 2: We add custom mapping for all the endpoints so that none of them are assigned random ports from the 20001-30001 range; + // Note 2(contd.): this is to avoid a race condition where a test running in parallel is also assigned similar ranged port the one here, and we fail to access either of them. + ContainerPort1 = "3000" + ContainerPort2 = "4567" + ContainerPort3 = "7890" + ) BeforeEach(func() { - opts := []string{fmt.Sprintf("--port-forward=%s:%s", LocalPort1, ContainerPort1), fmt.Sprintf("--port-forward=%s:%s", LocalPort2, ContainerPort2), fmt.Sprintf("--port-forward=%s:%s", LocalPort3, ContainerPort3)} - var err error - devSession, _, _, ports, err = helper.StartDevMode(helper.DevSessionOpts{ - CmdlineArgs: opts, - NoRandomPorts: true, - }) - Expect(err).ToNot(HaveOccurred()) - }) - - AfterEach(func() { - devSession.Stop() - devSession.WaitEnd() + // helper.Cmd("odo", "set", "project", commonVar.Project).ShouldPass() + helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project-with-multiple-endpoints"), commonVar.Context) + helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile-with-multiple-endpoints.yaml")).ShouldPass() }) - It("should expose all endpoints on localhost regardless of exposure", func() { - By("not exposing debug endpoints", func() { - for _, p := range []int{5005, 5006} { - _, found := ports[strconv.Itoa(p)] - Expect(found).To(BeFalse(), fmt.Sprintf("debug port %d should not be forwarded", p)) - } + When("running odo dev", func() { + var devSession helper.DevSession + var ports map[string]string + BeforeEach(func() { + opts := []string{fmt.Sprintf("--port-forward=%s:%s", LocalPort1, ContainerPort1), fmt.Sprintf("--port-forward=%s:%s", LocalPort2, ContainerPort2), fmt.Sprintf("--port-forward=%s:%s", LocalPort3, ContainerPort3)} + var err error + devSession, _, _, ports, err = helper.StartDevMode(helper.DevSessionOpts{ + CmdlineArgs: opts, + NoRandomPorts: true, + RunOnPodman: podman, + }) + Expect(err).ToNot(HaveOccurred()) }) - getServerResponse := func(containerPort, localPort string) (string, error) { - url := fmt.Sprintf("http://%s", ports[containerPort]) - Expect(url).To(ContainSubstring(localPort)) - resp, err := http.Get(url) - if err != nil { - return "", err - } - defer resp.Body.Close() + AfterEach(func() { + devSession.Stop() + devSession.WaitEnd() + }) - body, _ := io.ReadAll(resp.Body) - return string(body), nil - } - containerPorts := []string{ContainerPort1, ContainerPort2, ContainerPort3} - localPorts := []string{LocalPort1, LocalPort2, LocalPort3} - - for i := range containerPorts { - containerPort := containerPorts[i] - localPort := localPorts[i] - By(fmt.Sprintf("exposing a port targeting container port %s", containerPort), func() { - r, err := getServerResponse(containerPort, localPort) - Expect(err).ShouldNot(HaveOccurred()) - helper.MatchAllInOutput(r, []string{"Hello from Node.js Starter Application!"}) + It("should expose all endpoints on localhost regardless of exposure", func() { + By("not exposing debug endpoints", func() { + for _, p := range []int{5005, 5006} { + _, found := ports[strconv.Itoa(p)] + Expect(found).To(BeFalse(), fmt.Sprintf("debug port %d should not be forwarded", p)) + } }) - } - helper.ReplaceString("server.js", "Hello from Node.js", "H3110 from Node.js") + getServerResponse := func(containerPort, localPort string) (string, error) { + url := fmt.Sprintf("http://%s", ports[containerPort]) + Expect(url).To(ContainSubstring(localPort)) + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() - var stdout, stderr []byte - var err error - stdout, stderr, _, err = devSession.WaitSync() - Expect(err).Should(Succeed()) - - By("not warning users that odo dev needs to be restarted because the Devfile has not changed", func() { - warning := "Please restart 'odo dev'" - Expect(stdout).ShouldNot(ContainSubstring(warning)) - Expect(stderr).ShouldNot(ContainSubstring(warning)) - }) - - for i := range containerPorts { - containerPort := containerPorts[i] - localPort := localPorts[i] - By(fmt.Sprintf("returning the right response when querying port forwarded for container port %s", containerPort), - func() { - Eventually(func(g Gomega) string { - r, err := getServerResponse(containerPort, localPort) - g.Expect(err).ShouldNot(HaveOccurred()) - return r - }, 180, 10).Should(Equal("H3110 from Node.js Starter Application!")) + body, _ := io.ReadAll(resp.Body) + return string(body), nil + } + containerPorts := []string{ContainerPort1, ContainerPort2, ContainerPort3} + localPorts := []string{LocalPort1, LocalPort2, LocalPort3} + + for i := range containerPorts { + containerPort := containerPorts[i] + localPort := localPorts[i] + By(fmt.Sprintf("exposing a port targeting container port %s", containerPort), func() { + r, err := getServerResponse(containerPort, localPort) + Expect(err).ShouldNot(HaveOccurred()) + helper.MatchAllInOutput(r, []string{"Hello from Node.js Starter Application!"}) }) - } - }) - }) + } - }) - }) + helper.ReplaceString("server.js", "Hello from Node.js", "H3110 from Node.js") + + var stdout, stderr []byte + var err error + stdout, stderr, _, err = devSession.WaitSync() + Expect(err).Should(Succeed()) + By("not warning users that odo dev needs to be restarted because the Devfile has not changed", func() { + warning := "Please restart 'odo dev'" + Expect(stdout).ShouldNot(ContainSubstring(warning)) + Expect(stderr).ShouldNot(ContainSubstring(warning)) + }) + + for i := range containerPorts { + containerPort := containerPorts[i] + localPort := localPorts[i] + By(fmt.Sprintf("returning the right response when querying port forwarded for container port %s", containerPort), + func() { + Eventually(func(g Gomega) string { + r, err := getServerResponse(containerPort, localPort) + g.Expect(err).ShouldNot(HaveOccurred()) + return r + }, 180, 10).Should(Equal("H3110 from Node.js Starter Application!")) + }) + } + }) + }) + + }) + })) + } for _, manual := range []bool{false, true} { for _, podman := range []bool{false, true} { manual := manual