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

Clean e2e test example #1535

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,3 @@ tags
# VSCode specific
.vscode

# local .odo config directory
.odo
*/**/.odo
44 changes: 28 additions & 16 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import:
repo: https://github.com/openshift/kubernetes-api.git
version: origin-3.11-kubernetes-1.11.1
- package: github.com/onsi/gomega
version: v1.3.0
version: v1.5.0
- package: github.com/onsi/ginkgo
version: v1.4.0
version: v1.8.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

- package: github.com/kubernetes-incubator/service-catalog
version: v0.1.31
- package: github.com/satori/go.uuid
Expand All @@ -44,7 +44,7 @@ import:
- package: github.com/Netflix/go-expect
version: master
- package: github.com/stretchr/testify
version: v1.2.2
version: v1.3.0
- package: github.com/posener/complete
version: v1.1.2
- package: github.com/gobwas/glob
Expand Down
10 changes: 5 additions & 5 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func TestOdo(t *testing.T) {
RunSpecs(t, "odo test suite")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the suite function in a separate file to isolate it form the actual test

For example:

Create a file odo_suite_test.go

package e2e

import (
	"testing"

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

func TestOdo(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "odo test suite")
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is intended to provide just a clean direction and template how we can structure our tests.
This should be part of a cleanup effort in a separate PR.

}

var _ = BeforeSuite(func() {
// Save the current project
// commenting this out to resolve e2e tests failures on OC 4
// curProj = runCmdShouldPass("oc project -q")
})
//var _ = BeforeSuite(func() {
// Save the current project
// commenting this out to resolve e2e tests failures on OC 4
// curProj = runCmdShouldPass("oc project -q")
//})

func VerifyAppNameOfComponent(cmpName string, appName string) {
session := runCmdShouldPass(fmt.Sprintf("oc get dc %s-%s --template={{.metadata.labels.'app'}}", cmpName, appName))
Expand Down
103 changes: 103 additions & 0 deletions tests/e2e/helper/helper_filesystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package helper

import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"

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

// CreateNewContext create new empty temporary directory
func CreateNewContext() string {
directory, err := ioutil.TempDir("", "")
Expect(err).NotTo(HaveOccurred())
fmt.Fprintf(GinkgoWriter, "Created dir: %s\n", directory)
return directory
}

// DeleteDir delete directory
func DeleteDir(dir string) {
fmt.Fprintf(GinkgoWriter, "Deleating dir: %s\n", dir)
err := os.RemoveAll(dir)
Expect(err).NotTo(HaveOccurred())

}

// Chdir change current working dir
func Chdir(dir string) {
fmt.Fprintf(GinkgoWriter, "Setting current dir to: %s\n", dir)
err := os.Chdir(dir)
Expect(err).ShouldNot(HaveOccurred())
}

// Getwd retruns current working dir
func Getwd() string {
dir, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
fmt.Fprintf(GinkgoWriter, "Current working dir: %s\n", dir)
return dir
}

// CopyExample copies an example from tests/e2e/examples/<exampleName> into targetDir
func CopyExample(exampleName string, targetDir string) {
// filename of this file
_, filename, _, _ := runtime.Caller(0)
// path to the examples directory
examplesDir := filepath.Join(filepath.Dir(filename), "..", "examples")

src := filepath.Join(examplesDir, exampleName)
info, err := os.Stat(src)
Expect(err).NotTo(HaveOccurred())

err = copyDir(src, targetDir, info)
Expect(err).NotTo(HaveOccurred())
}

// copyDir copy one directory to the other
// this function is called recursively info should start as os.Stat(src)
func copyDir(src string, dst string, info os.FileInfo) error {

if info.IsDir() {
files, err := ioutil.ReadDir(src)
if err != nil {
return err
}

for _, file := range files {
dsrt := filepath.Join(src, file.Name())
ddst := filepath.Join(dst, file.Name())
if err := copyDir(dsrt, ddst, file); err != nil {
return err
}
}
return nil
}

if err := os.MkdirAll(filepath.Dir(dst), os.ModePerm); err != nil {
return err
}

dFile, err := os.Create(dst)
if err != nil {
return err
}
defer dFile.Close()

sFile, err := os.Open(src)
if err != nil {
return err
}
defer sFile.Close()

if err = os.Chmod(dFile.Name(), info.Mode()); err != nil {
return err
}

_, err = io.Copy(dFile, sFile)
return err
}
19 changes: 19 additions & 0 deletions tests/e2e/helper/helper_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package helper

import (
"math/rand"
"time"
)

func init() {
rand.Seed(time.Now().UTC().UnixNano())
}

func randString(n int) string {
const letterBytes = "abcdefghijklmnopqrstuvwxyz"
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
38 changes: 38 additions & 0 deletions tests/e2e/helper/helper_oc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package helper

import (
"fmt"

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

// CreateRandProject create new project with random name (10 letters)
// without writing to the config file (without switching project)
func OcCreateRandProject() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

projectName := randString(10)
fmt.Fprintf(GinkgoWriter, "Creating a new project: %s\n", projectName)
CmdShouldPass(fmt.Sprintf("oc new-project %s --skip-config-write", projectName))
return projectName
}

// OcSwitchProject switch to the project
func OcSwitchProject(project string) {
CmdShouldPass(fmt.Sprintf("oc project %s ", project))
}

// DeleteProject deletes a specified project
func OcDeleteProject(project string) {
fmt.Fprintf(GinkgoWriter, "Deleting project: %s\n", project)
CmdShouldPass(fmt.Sprintf("oc delete project %s --now", project))
}

// OcCurrentProject get currently active project in oc
// returns empty string if there no active project, or no access to the project
func OcGetCurrentProject() string {
stdout, _, exitCode := cmdRunner("oc project -q")
if exitCode == 0 {
return stdout
}
return ""
}
Loading