Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential issues with E2E tests #6543

49 changes: 30 additions & 19 deletions tests/e2escenarios/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ var _ = Describe("E2E Test", func() {
helper.CommonAfterEach(commonVar)
})

waitRemoteApp := func(urlInContainer, assertString string) {
cmp := helper.NewComponent(componentName, "app", "Dev", commonVar.Project, commonVar.CliRunner)
Eventually(func() string {
stdout, _ := cmp.Exec("runtime", nil, "curl", urlInContainer)
return stdout
}, 120*time.Second, 15*time.Second).Should(Equal(assertString))
}
rm3l marked this conversation as resolved.
Show resolved Hide resolved

checkIfDevEnvIsUp := func(url, assertString string) {
Eventually(func() string {
resp, err := http.Get(fmt.Sprintf("http://%s", url))
Expand Down Expand Up @@ -83,18 +91,23 @@ var _ = Describe("E2E Test", func() {
var ports map[string]string

devSession, _, _, ports, err = helper.StartDevMode(helper.DevSessionOpts{})
helper.ReplaceString(filepath.Join(commonVar.Context, "server.js"), "from Node.js", "from updated Node.js")
Expect(err).ToNot(HaveOccurred())
waitRemoteApp("http://127.0.0.1:3000", "Hello from Node.js Starter Application!")
checkIfDevEnvIsUp(ports["3000"], "Hello from Node.js Starter Application!")

helper.ReplaceString(filepath.Join(commonVar.Context, "server.js"), "from Node.js", "from updated Node.js")
_, _, _, err = devSession.WaitSync()
Expect(err).ToNot(HaveOccurred())
// "should update the changes"
waitRemoteApp("http://127.0.0.1:3000", "Hello from updated Node.js Starter Application!")
checkIfDevEnvIsUp(ports["3000"], "Hello from updated Node.js Starter Application!")

// "changes are made to the applications"
helper.ReplaceString(filepath.Join(commonVar.Context, "server.js"), "from updated Node.js", "from Node.js app v2")
_, _, _, err = devSession.WaitSync()
Expect(err).ToNot(HaveOccurred())
// "should deploy new changes"
waitRemoteApp("http://127.0.0.1:3000", "Hello from Node.js app v2 Starter Application!")
checkIfDevEnvIsUp(ports["3000"], "Hello from Node.js app v2 Starter Application!")

// "running odo list"
Expand Down Expand Up @@ -135,6 +148,7 @@ var _ = Describe("E2E Test", func() {
_, _, _, err = devSession.WaitSync()
Expect(err).ToNot(HaveOccurred())
// "should update the changes"
waitRemoteApp("http://127.0.0.1:3000", "Hello from Node.js app v3 Starter Application!")
checkIfDevEnvIsUp(ports["3000"], "Hello from Node.js app v3 Starter Application!")

// should list both dev,deploy
Expand Down Expand Up @@ -201,22 +215,23 @@ var _ = Describe("E2E Test", func() {
var ports map[string]string

devSession, _, _, ports, err = helper.StartDevMode(helper.DevSessionOpts{})
helper.ReplaceString(filepath.Join(commonVar.Context, "server.js"), "from Node.js", "from updated Node.js")
Expect(err).ToNot(HaveOccurred())
waitRemoteApp("http://127.0.0.1:3000", "Hello from Node.js Starter Application!")
checkIfDevEnvIsUp(ports["3000"], "Hello from Node.js Starter Application!")

helper.ReplaceString(filepath.Join(commonVar.Context, "server.js"), "from Node.js", "from updated Node.js")
_, _, _, err = devSession.WaitSync()
Expect(err).ToNot(HaveOccurred())

// "should update the changes"
waitRemoteApp("http://127.0.0.1:3000", "Hello from updated Node.js Starter Application!")
checkIfDevEnvIsUp(ports["3000"], "Hello from updated Node.js Starter Application!")

// "changes are made made to the applications"

// "changes are made to the applications"
helper.ReplaceString(filepath.Join(commonVar.Context, "server.js"), "from updated Node.js", "from Node.js app v2")
_, _, _, err = devSession.WaitSync()
Expect(err).ToNot(HaveOccurred())

// "should deploy new changes"
waitRemoteApp("http://127.0.0.1:3000", "Hello from Node.js app v2 Starter Application!")
checkIfDevEnvIsUp(ports["3000"], "Hello from Node.js app v2 Starter Application!")

// "running odo list"
Expand Down Expand Up @@ -256,6 +271,7 @@ var _ = Describe("E2E Test", func() {
helper.ReplaceString(filepath.Join(commonVar.Context, "server.js"), "from Node.js app v2", "from Node.js app v3")

// "should update the changes"
waitRemoteApp("http://127.0.0.1:3000", "Hello from Node.js app v3 Starter Application!")
checkIfDevEnvIsUp(ports["3000"], "Hello from Node.js app v3 Starter Application!")

// should list both dev,deploy
Expand Down Expand Up @@ -363,13 +379,11 @@ var _ = Describe("E2E Test", func() {
helper.Cmd("odo", "add", "binding", "--name", bindingName, "--service", "cluster-example-initdb", "--bind-as-files=false").ShouldPass()

// Get new random port after restart
Eventually(func() map[string]string {
_, _, ports, err = devSession.GetInfo()
Expect(err).ToNot(HaveOccurred())
return ports
}, 180, 10).ShouldNot(BeEmpty())
_, _, ports, err = devSession.WaitRestartPortforward()
Expect(err).ToNot(HaveOccurred())

// "send data"
waitRemoteApp("http://127.0.0.1:8080/ping", "pong")
data := sendDataEntry(ports["8080"])
Expect(data["message"]).To(Equal("User created successfully"))

Expand All @@ -386,18 +400,15 @@ var _ = Describe("E2E Test", func() {
stdout = helper.Cmd("odo", "list").ShouldPass().Out()
helper.MatchAllInOutput(stdout, []string{componentName, "Go", "Dev", bindingName})

// "exit dev mode"
devSession.Stop()
devSession.WaitEnd()

// remove bindings and check devfile to not contain binding info
// TODO: move `remove binding` inside devsession after https://github.com/redhat-developer/odo/issues/6101 is fixed
helper.Cmd("odo", "remove", "binding", "--name", bindingName).ShouldPass()

devSession, _, _, _, err = helper.StartDevMode(helper.DevSessionOpts{})
_, _, _, err = devSession.WaitSync()
Expect(err).To(BeNil())
stdout = helper.Cmd("odo", "describe", "binding").ShouldPass().Out()
Expect(stdout).To(ContainSubstring("No ServiceBinding used by the current component"))
Eventually(func() string { return helper.Cmd("odo", "describe", "binding").ShouldRun().Out() }).
WithTimeout(120 * time.Second).
WithPolling(5 * time.Second).
Should(ContainSubstring("No ServiceBinding used by the current component"))

devSession.Stop()
devSession.WaitEnd()
Expand Down
7 changes: 7 additions & 0 deletions tests/examples/source/devfiles/go/router/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package router

import (
"net/http"

"go-postgres/middleware"

"github.com/gorilla/mux"
Expand All @@ -11,6 +13,11 @@ func Router() *mux.Router {

router := mux.NewRouter()

router.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("pong"))
})

router.HandleFunc("/api/user/{id}", middleware.GetUser).Methods("GET", "OPTIONS")
router.HandleFunc("/api/user", middleware.GetAllUser).Methods("GET", "OPTIONS")
router.HandleFunc("/api/newuser", middleware.CreateUser).Methods("POST", "OPTIONS")
Expand Down
4 changes: 2 additions & 2 deletions tests/helper/component_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func (o *ClusterComponent) ExpectIsNotDeployed() {
Expect(string(stdout)).To(Not(ContainSubstring(deploymentName)))
}

func (o *ClusterComponent) Exec(container string, args ...string) string {
func (o *ClusterComponent) Exec(container string, success *bool, args ...string) (string, string) {
podName := o.cli.GetRunningPodNameByComponent(o.name, o.namespace)
return o.cli.Exec(podName, o.namespace, append([]string{"-c", container, "--"}, args...)...)
return o.cli.Exec(podName, o.namespace, success, append([]string{"-c", container, "--"}, args...)...)
}

func (o *ClusterComponent) GetEnvVars(string) map[string]string {
Expand Down
7 changes: 5 additions & 2 deletions tests/helper/component_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ type Component interface {
ExpectIsDeployed()
// ExpectIsNotDeployed checks that the component is not deployed
ExpectIsNotDeployed()
// Exec executes the command in specific container of the component
Exec(container string, args ...string) string
// Exec executes the command in specific container of the component.
// If success is true, the command exit code is expected to be 0.
// If success is false, the command exit code is expected to be non-zero.
// If success is nil, the command is just supposed to run, with no assertion on its exit code.
Exec(container string, success *bool, args ...string) (string, string)
rm3l marked this conversation as resolved.
Show resolved Hide resolved
// GetEnvVars returns the environment variables defined for the container
GetEnvVars(container string) map[string]string
// GetLabels returns the labels defined for the component
Expand Down
16 changes: 11 additions & 5 deletions tests/helper/component_podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,32 @@ func (o *PodmanComponent) ExpectIsNotDeployed() {
Expect(string(stdout)).ToNot(ContainSubstring(podName))
}

func (o *PodmanComponent) Exec(container string, args ...string) string {
func (o *PodmanComponent) Exec(container string, success *bool, args ...string) (string, string) {
containerName := fmt.Sprintf("%s-%s-%s", o.componentName, o.app, container)
cmdargs := []string{"exec", "--interactive"}
cmdargs = append(cmdargs, "--tty")
cmdargs = append(cmdargs, containerName)
cmdargs = append(cmdargs, args...)

command := exec.Command("podman", cmdargs...)
out, err := command.Output()
out, err := command.CombinedOutput()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("%s: %s", err, string(exiterr.Stderr))
}
}
Expect(err).ToNot(HaveOccurred())
return string(out)
if success != nil {
if *success {
Expect(err).ToNot(HaveOccurred())
} else {
Expect(err).Should(HaveOccurred())
}
}
return string(out), ""
}

func (o *PodmanComponent) GetEnvVars(container string) map[string]string {
envs := o.Exec(container, "env")
envs, _ := o.Exec(container, ToBoolPtr(true), "env")
return splitLines(envs)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/helper/helper_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "github.com/onsi/gomega/gexec"
type CliRunner interface {
Run(args ...string) *gexec.Session
ExecListDir(podName string, projectName string, dir string) string
Exec(podName string, projectName string, args ...string) string
Exec(podName string, projectName string, success *bool, args ...string) (string, string)
CheckCmdOpInRemoteDevfilePod(podName string, containerName string, prjName string, cmd []string, checkOp func(cmdOp string, err error) bool) bool
GetRunningPodNameByComponent(compName string, namespace string) string
GetVolumeMountNamesandPathsFromContainer(deployName string, containerName, namespace string) string
Expand Down
2 changes: 1 addition & 1 deletion tests/helper/helper_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (o DevSession) WaitSync() ([]byte, []byte, map[string]string, error) {
}

func (o DevSession) WaitRestartPortforward() ([]byte, []byte, map[string]string, error) {
WaitForOutputToContain("Forwarding from", 30, 5, o.session)
WaitForOutputToContain("Forwarding from", 240, 10, o.session)
return o.GetInfo()
}

Expand Down
4 changes: 4 additions & 0 deletions tests/helper/helper_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,7 @@ func SetDefaultDevfileRegistryAsStaging() {
Cmd("odo", "preference", "remove", "registry", registryName, "-f").ShouldPass()
Cmd("odo", "preference", "add", "registry", registryName, addRegistryURL).ShouldPass()
}

func ToBoolPtr(b bool) *bool {
rm3l marked this conversation as resolved.
Show resolved Hide resolved
return &b
}
13 changes: 10 additions & 3 deletions tests/helper/helper_kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,21 @@ func (kubectl KubectlRunner) Run(args ...string) *gexec.Session {
}

// Exec allows generic execution of commands, returning the contents of stdout
func (kubectl KubectlRunner) Exec(podName string, projectName string, args ...string) string {
func (kubectl KubectlRunner) Exec(podName string, projectName string, success *bool, args ...string) (string, string) {

cmd := []string{"exec", podName, "--namespace", projectName}

cmd = append(cmd, args...)

stdOut := Cmd(kubectl.path, cmd...).ShouldPass().Out()
return stdOut
cmdWrapper := Cmd(kubectl.path, cmd...)
if success == nil {
cmdWrapper = cmdWrapper.ShouldRun()
} else if *success {
cmdWrapper = cmdWrapper.ShouldPass()
} else {
cmdWrapper = cmdWrapper.ShouldFail()
}
return cmdWrapper.OutAndErr()
}

// ExecListDir returns dir list in specified location of pod
Expand Down
15 changes: 10 additions & 5 deletions tests/helper/helper_oc.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,19 @@ func (oc OcRunner) ExecListDir(podName string, projectName string, dir string) s
}

// Exec allows generic execution of commands, returning the contents of stdout
func (oc OcRunner) Exec(podName string, projectName string, args ...string) string {

func (oc OcRunner) Exec(podName string, projectName string, success *bool, args ...string) (string, string) {
cmd := []string{"exec", podName, "--namespace", projectName}

cmd = append(cmd, args...)

stdOut := Cmd(oc.path, cmd...).ShouldPass().Out()
return stdOut
cmdWrapper := Cmd(oc.path, cmd...)
if success == nil {
cmdWrapper = cmdWrapper.ShouldRun()
} else if *success {
cmdWrapper = cmdWrapper.ShouldPass()
} else {
cmdWrapper = cmdWrapper.ShouldFail()
}
return cmdWrapper.OutAndErr()
}

// CheckCmdOpInRemoteCmpPod runs the provided command on remote component pod and returns the return value of command output handler function passed to it
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/cmd_dev_debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var _ = Describe("odo dev debug command tests", func() {
// #6056
It("should not add a DEBUG_PORT variable to the container", func() {
cmp := helper.NewComponent(cmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
stdout := cmp.Exec("runtime", "sh", "-c", "echo -n ${DEBUG_PORT}")
stdout, _ := cmp.Exec("runtime", helper.ToBoolPtr(true), "sh", "-c", "echo -n ${DEBUG_PORT}")
Expect(stdout).To(BeEmpty())
})
}))
Expand Down
Loading