Skip to content

Commit

Permalink
Refactor integration test
Browse files Browse the repository at this point in the history
Signed-off-by: Parthvi Vala <[email protected]>

Add dev --debug integration test for podman

Signed-off-by: Parthvi Vala <[email protected]>

Add dev --debug integration test for podman

Signed-off-by: Parthvi Vala <[email protected]>

Fix ci failures

Signed-off-by: Parthvi Vala <[email protected]>
  • Loading branch information
valaparthvi committed Apr 13, 2023
1 parent b44b274 commit 30a685e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 102 deletions.
13 changes: 5 additions & 8 deletions pkg/odo/cli/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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).
Expand Down
85 changes: 49 additions & 36 deletions tests/integration/cmd_dev_debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
110 changes: 52 additions & 58 deletions tests/integration/cmd_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -1012,10 +1004,12 @@ ComponentSettings:
})

})
}))

}
}
}
}))
}

for _, devfileHandlerCtx := range []struct {
name string
sourceHandler func(path string, originalCmpName string)
Expand Down

0 comments on commit 30a685e

Please sign in to comment.