Skip to content

Commit

Permalink
watch integration test
Browse files Browse the repository at this point in the history
Signed-off-by: Maysun J Faisal <maysun.j.faisal@ibm.com>
  • Loading branch information
maysunfaisal committed May 7, 2020
1 parent c40a8e2 commit da5d1f5
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,12 @@ jobs:

- <<: *base-test
stage: test
name: "docker devfile push and delete command integration tests"
name: "docker devfile push, watch and delete command integration tests"
script:
- make bin
- sudo cp odo /usr/bin
- travis_wait make test-cmd-docker-devfile-push
- travis_wait make test-cmd-docker-devfile-watch
- travis_wait make test-cmd-docker-devfile-catalog
- travis_wait make test-cmd-docker-devfile-delete

Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ test-cmd-devfile-url:
test-cmd-docker-devfile-push:
ginkgo $(GINKGO_FLAGS) -focus="odo docker devfile push command tests" tests/integration/devfile/docker/

# Run odo watch docker devfile command tests
.PHONY: test-cmd-docker-devfile-watch
test-cmd-docker-devfile-watch:
ginkgo $(GINKGO_FLAGS) -focus="odo docker devfile watch command tests" tests/integration/devfile/docker/

# Run odo url docker devfile command tests
.PHONY: test-cmd-docker-devfile-url
test-cmd-docker-devfile-url:
Expand Down
3 changes: 1 addition & 2 deletions pkg/odo/cli/component/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package component
import (
"fmt"
"os"
"path/filepath"

"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/devfile/adapters"
Expand Down Expand Up @@ -82,7 +81,7 @@ func (wo *WatchOptions) Complete(name string, cmd *cobra.Command, args []string)
wo.Context = genericclioptions.NewDevfileContext(cmd)

// Set the source path to either the context or current working directory (if context not set)
wo.sourcePath, err = util.GetAbsPath(filepath.Dir(wo.componentContext))
wo.sourcePath, err = util.GetAbsPath(wo.componentContext)
if err != nil {
return errors.Wrap(err, "unable to get source path")
}
Expand Down
50 changes: 50 additions & 0 deletions tests/integration/devfile/cmd_devfile_watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ import (
. "github.com/onsi/gomega"

"github.com/openshift/odo/tests/helper"
"github.com/openshift/odo/tests/integration/devfile/utils"
)

var _ = Describe("odo devfile watch command tests", func() {
var namespace string
var context string
var cmpName string
var projectDirPath string
var currentWorkingDirectory string

// TODO: all oc commands in all devfile related test should get replaced by kubectl
// TODO: to goal is not to use "oc"
oc := helper.NewOcRunner("oc")

// Setup up state for each test spec
// create new project (not set as active) and new context directory for each test spec
// This is run after every Spec (It)
Expand All @@ -31,6 +38,9 @@ var _ = Describe("odo devfile watch command tests", func() {
namespace = helper.CreateRandProject()
}
currentWorkingDirectory = helper.Getwd()
projectDir := "/projectDir"
projectDirPath = context + projectDir
cmpName = helper.RandString(6)
helper.Chdir(context)
})

Expand Down Expand Up @@ -86,4 +96,44 @@ var _ = Describe("odo devfile watch command tests", func() {
Expect(output).To(ContainSubstring("Error: unknown flag: --devfile"))
})
})

Context("when executing odo watch after odo push", func() {
It("should listen for file changes", func() {
// Devfile push requires experimental mode to be set
helper.CmdShouldPass("odo", "preference", "set", "Experimental", "true")

helper.CmdShouldPass("git", "clone", "https://github.com/che-samples/web-nodejs-sample.git", projectDirPath)
helper.Chdir(projectDirPath)

helper.CmdShouldPass("odo", "create", "nodejs", "--project", namespace, cmpName)

helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
Expect(output).To(ContainSubstring("Changes successfully pushed to component"))

// odo watch and validate
utils.OdoWatch(cmpName, namespace, projectDirPath, []string{"Executing devbuild command", "Executing devrun command"}, oc, "kube")
})
})

Context("when executing odo watch after odo push with custom commands", func() {
It("should listen for file changes", func() {
// Devfile push requires experimental mode to be set
helper.CmdShouldPass("odo", "preference", "set", "Experimental", "true")

helper.CmdShouldPass("git", "clone", "https://github.com/che-samples/web-nodejs-sample.git", projectDirPath)
helper.Chdir(projectDirPath)

helper.CmdShouldPass("odo", "create", "nodejs", "--project", namespace, cmpName)

helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

output := helper.CmdShouldPass("odo", "push", "--build-command", "build", "--run-command", "run", "--devfile", "devfile.yaml", "--project", namespace)
Expect(output).To(ContainSubstring("Changes successfully pushed to component"))

// odo watch and validate
utils.OdoWatch(cmpName, namespace, projectDirPath, []string{"Executing build command", "Executing run command"}, oc, "kube")
})
})
})
82 changes: 82 additions & 0 deletions tests/integration/devfile/docker/cmd_docker_devfile_watch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package docker

import (
"os"
"path/filepath"
"time"

"github.com/openshift/odo/tests/helper"
"github.com/openshift/odo/tests/integration/devfile/utils"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("odo docker devfile watch command tests", func() {
var context, currentWorkingDirectory, cmpName, projectDirPath string
var projectDir = "/projectDir"

dockerClient := helper.NewDockerRunner("docker")

// This is run after every Spec (It)
var _ = BeforeEach(func() {
SetDefaultEventuallyTimeout(10 * time.Minute)
context = helper.CreateNewContext()
currentWorkingDirectory = helper.Getwd()
projectDirPath = context + projectDir
cmpName = helper.RandString(6)
helper.Chdir(context)
os.Setenv("GLOBALODOCONFIG", filepath.Join(context, "config.yaml"))

// Local devfile push requires experimental mode to be set and the pushtarget set to docker
helper.CmdShouldPass("odo", "preference", "set", "Experimental", "true")
helper.CmdShouldPass("odo", "preference", "set", "pushtarget", "docker")
})

// Clean up after the test
// This is run after every Spec (It)
var _ = AfterEach(func() {
// Stop all containers labeled with the component name
label := "component=" + cmpName
dockerClient.StopContainers(label)

helper.Chdir(currentWorkingDirectory)
helper.DeleteDir(context)
os.Unsetenv("GLOBALODOCONFIG")
})

Context("when executing odo watch after odo push", func() {
It("should listen for file changes", func() {
helper.CmdShouldPass("git", "clone", "https://github.com/che-samples/web-nodejs-sample.git", projectDirPath)
helper.Chdir(projectDirPath)

helper.CmdShouldPass("odo", "create", "nodejs", cmpName)

helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml")
Expect(output).To(ContainSubstring("Changes successfully pushed to component"))

// odo watch and validate
utils.OdoWatch(cmpName, "", projectDirPath, []string{"Executing devbuild command", "Executing devrun command"}, dockerClient, "docker")
})
})

Context("when executing odo watch after odo push with custom commands", func() {
It("should listen for file changes", func() {
helper.CmdShouldPass("git", "clone", "https://github.com/che-samples/web-nodejs-sample.git", projectDirPath)
helper.Chdir(projectDirPath)

helper.CmdShouldPass("odo", "create", "nodejs", cmpName)

helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

output := helper.CmdShouldPass("odo", "push", "--build-command", "build", "--run-command", "run", "--devfile", "devfile.yaml")
Expect(output).To(ContainSubstring("Changes successfully pushed to component"))

// odo watch and validate
utils.OdoWatch(cmpName, "", projectDirPath, []string{"Executing build command", "Executing run command"}, dockerClient, "docker")
})
})

})
86 changes: 86 additions & 0 deletions tests/integration/devfile/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package utils

import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/openshift/odo/tests/helper"

Expand Down Expand Up @@ -200,3 +203,86 @@ func ExecWithRestartAttribute(projectDirPath, cmpName, namespace string) {
Expect(output).To(ContainSubstring("if not running"))

}

// OdoWatch creates files, dir in the context and watches for the changes to be pushed
// this was taken from e2e_images_test.go and modified for devfile
func OdoWatch(cmpName, project, context string, stringsToBeMatched []string, runner interface{}, platform string) {

// TODO: all oc commands in all devfile related test should get replaced by kubectl
// TODO: to goal is not to use "oc"
// oc := helper.NewOcRunner("oc")

startSimulationCh := make(chan bool)
go func() {
startMsg := <-startSimulationCh
if startMsg {
err := os.MkdirAll(filepath.Join(context, ".abc"), 0755)
if err != nil {
panic(err)
}
err = os.MkdirAll(filepath.Join(context, "abcd"), 0755)
if err != nil {
panic(err)
}
_, err = os.Create(filepath.Join(context, "a.txt"))
if err != nil {
panic(err)
}

helper.DeleteDir(filepath.Join(context, "abcd"))

// helper.ReplaceString(filepath.Join(context, "server.js"), "Hello", "Hello odo")
helper.ReplaceString(filepath.Join(context, "app", "app.js"), "Hello", "Hello odo")
}
}()

success, err := helper.WatchNonRetCmdStdOut(
("odo watch --context " + context),
time.Duration(10)*time.Minute,
func(output string) bool {
sourcePath := "/projects/nodejs-web-app"

stringsMatched := true

for _, stringToBeMatched := range stringsToBeMatched {
if !strings.Contains(output, stringToBeMatched) {
stringsMatched = false
}
}

if stringsMatched {
// Verify delete from component pod
stdOut := getContainerExecListdir(runner, platform, cmpName, project, sourcePath)
Expect(stdOut).To(ContainSubstring(("a.txt")))
Expect(stdOut).To(ContainSubstring((".abc")))
Expect(stdOut).To(Not(ContainSubstring(("abcd"))))
return true
}
return false
},
startSimulationCh,
func(output string) bool {
return strings.Contains(output, "Waiting for something to change")
})

Expect(success).To(Equal(true))
Expect(err).To(BeNil())
}

func getContainerExecListdir(runner interface{}, platform, cmpName, project, sourcePath string) string {
var stdOut string
// if !reflect.DeepEqual(runner.(helper.OcRunner), helper.OcRunner{}) {

if platform == "kube" {
ocRunner := runner.(helper.OcRunner)
podName := ocRunner.GetRunningPodNameByComponent(cmpName, project)
stdOut = ocRunner.ExecListDir(podName, project, sourcePath)
} else if platform == "docker" {
dockerRunner := runner.(helper.DockerRunner)
containers := dockerRunner.GetRunningContainersByCompAlias(cmpName, "runtime")
Expect(len(containers)).To(Equal(1))
stdOut = dockerRunner.ExecContainer(containers[0], "ls -la "+sourcePath)
}

return stdOut
}

0 comments on commit da5d1f5

Please sign in to comment.