From c8d8cbbba946ab184a86f1776793048ff9be8286 Mon Sep 17 00:00:00 2001 From: Ratnadeep Debnath Date: Mon, 5 Dec 2016 23:16:27 +0530 Subject: [PATCH] Added unittests for openshift buildconfig integration. --- pkg/test/git.go | 71 +++++++ pkg/transformer/openshift/openshift_test.go | 193 +++++++++++++++++++- 2 files changed, 260 insertions(+), 4 deletions(-) create mode 100644 pkg/test/git.go diff --git a/pkg/test/git.go b/pkg/test/git.go new file mode 100644 index 00000000..d3888bcf --- /dev/null +++ b/pkg/test/git.go @@ -0,0 +1,71 @@ +package test + +import ( + "fmt" + "io/ioutil" + "os" + "os/exec" + "testing" +) + +func NewCommand(cmd string) *exec.Cmd { + return exec.Command("sh", "-c", cmd) +} + +func CreateLocalDirectory(t *testing.T) string { + dir, err := ioutil.TempDir(os.TempDir(), "kompose-test-") + if err != nil { + t.Fatal(err) + } + return dir +} + +func CreateLocalGitDirectory(t *testing.T) string { + dir := CreateLocalDirectory(t) + cmd := NewCommand( + `git init && touch README && + git add README && + git commit -m 'testcommit'`) + cmd.Dir = dir + _, err := cmd.Output() + if err != nil { + fmt.Println("create local git dir", err) + t.Fatal(err) + } + return dir +} + +func SetGitRemote(t *testing.T, dir string, remote string, remoteUrl string) { + cmd := NewCommand("git remote add newremote https://git.test.com/somerepo") + cmd.Dir = dir + _, err := cmd.Output() + if err != nil { + fmt.Println("set git remote", err) + t.Fatal(err) + } +} + +func CreateGitRemoteBranch(t *testing.T, dir string, branch string, remote string) { + cmd := NewCommand( + fmt.Sprintf(`git checkout -b %s && + git config branch.%s.remote %s && + git config branch.%s.merge refs/heads/%s`, + branch, branch, remote, branch, branch)) + cmd.Dir = dir + + _, err := cmd.Output() + if err != nil { + fmt.Println("create git branch", err) + t.Fatal(err) + } +} + +func CreateSubdir(t *testing.T, dir string, subdir string) { + cmd := NewCommand(fmt.Sprintf("mkdir -p %s", subdir)) + cmd.Dir = dir + + _, err := cmd.Output() + if err != nil { + t.Fatal(err) + } +} diff --git a/pkg/transformer/openshift/openshift_test.go b/pkg/transformer/openshift/openshift_test.go index 1772802b..0fcef199 100644 --- a/pkg/transformer/openshift/openshift_test.go +++ b/pkg/transformer/openshift/openshift_test.go @@ -17,11 +17,17 @@ limitations under the License. package openshift import ( - "github.com/kubernetes-incubator/kompose/pkg/kobject" - deployapi "github.com/openshift/origin/pkg/deploy/api" + "os" + "path/filepath" + "testing" + "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/runtime" - "testing" + + deployapi "github.com/openshift/origin/pkg/deploy/api" + + "github.com/kubernetes-incubator/kompose/pkg/kobject" + "github.com/kubernetes-incubator/kompose/pkg/test" ) func newServiceConfig() kobject.ServiceConfig { @@ -118,5 +124,184 @@ func TestKomposeConvertRoute(t *testing.T) { if route.Spec.Host != sc.ExposeService { t.Errorf("Expected %s for Spec.Host, actual %s", sc.ExposeService, route.Spec.Host) } - +} + +func TestGetGitRemote(t *testing.T) { + var output string + var err error + + gitDir := test.CreateLocalGitDirectory(t) + test.SetGitRemote(t, gitDir, "newremote", "https://git.test.com/somerepo") + test.CreateGitRemoteBranch(t, gitDir, "newbranch", "newremote") + dir := test.CreateLocalDirectory(t) + defer os.RemoveAll(gitDir) + defer os.RemoveAll(dir) + + testCases := map[string]struct { + expectError bool + dir string + branch string + output string + }{ + "Get git remote for branch success": {false, gitDir, "newbranch", "https://git.test.com/somerepo.git"}, + "Get git remote error in non git dir": {true, dir, "", ""}, + } + + for name, test := range testCases { + t.Log("Test case: ", name) + output, err = getGitCurrentRemoteUrl(test.dir) + + if test.expectError { + if err == nil { + t.Errorf("Expected error, got success instead!") + } + } else { + if err != nil { + t.Errorf("Expected success, got error: %v", err) + } + if output != test.output { + t.Errorf("Expected: %#v, got: %#v", test.output, output) + } + } + } +} + +func TestGitGetCurrentBranch(t *testing.T) { + var output string + var err error + + gitDir := test.CreateLocalGitDirectory(t) + test.SetGitRemote(t, gitDir, "newremote", "https://git.test.com/somerepo") + test.CreateGitRemoteBranch(t, gitDir, "newbranch", "newremote") + dir := test.CreateLocalDirectory(t) + defer os.RemoveAll(gitDir) + defer os.RemoveAll(dir) + + testCases := map[string]struct { + expectError bool + dir string + output string + }{ + "Get git current branch success": {false, gitDir, "newbranch"}, + "Get git current branch error": {true, dir, ""}, + } + + for name, test := range testCases { + t.Log("Test case: ", name) + output, err = getGitCurrentBranch(test.dir) + + if test.expectError { + if err == nil { + t.Errorf("Expected error, got success instead!") + } + } else { + if err != nil { + t.Errorf("Expected success, got error: %v", err) + } + if output != test.output { + t.Errorf("Expected: %#v, got: %#v", test.output, output) + } + } + } +} + +func TestGetComposeFileDir(t *testing.T) { + var output string + var err error + wd, _ := os.Getwd() + + testCases := map[string]struct { + inputFile string + output string + }{ + "Get compose file dir for relative input file path": {"foo/bar.yaml", filepath.Join(wd, "foo")}, + "Get compose file dir for abs input file path": {"/abs/path/to/compose.yaml", "/abs/path/to"}, + } + + for name, test := range testCases { + t.Log("Test case: ", name) + + output, err = getComposeFileDir(test.inputFile) + + if err != nil { + t.Errorf("Expected success, got error: %#v", err) + } + + if output != test.output { + t.Errorf("Expected output: %#v, got: %#v", test.output, output) + } + } +} + +func TestGetAbsBuildContext(t *testing.T) { + var output string + var err error + + gitDir := test.CreateLocalGitDirectory(t) + test.SetGitRemote(t, gitDir, "newremote", "https://git.test.com/somerepo") + test.CreateGitRemoteBranch(t, gitDir, "newbranch", "newremote") + test.CreateSubdir(t, gitDir, "a/b") + dir := test.CreateLocalDirectory(t) + defer os.RemoveAll(gitDir) + defer os.RemoveAll(dir) + + testCases := map[string]struct { + expectError bool + context string + composeFileDir string + output string + }{ + "Get abs build context success": {false, "./b/build", filepath.Join(gitDir, "a"), "a/b/build"}, + "Get abs build context error": {true, "", dir, ""}, + } + + for name, test := range testCases { + t.Log("Test case: ", name) + output, err = getAbsBuildContext(test.context, test.composeFileDir) + + if test.expectError { + if err == nil { + t.Errorf("Expected error, got success instead!") + } + } else { + if err != nil { + t.Errorf("Expected success, got error: %v", err) + } + if output != test.output { + t.Errorf("Expected: %#v, got: %#v", test.output, output) + } + } + } +} + +func TestInitBuildConfig(t *testing.T) { + dir := test.CreateLocalGitDirectory(t) + test.CreateSubdir(t, dir, "a/build") + defer os.RemoveAll(dir) + + serviceName := "serviceA" + composeFileDir := filepath.Join(dir, "a") + repo := "https://git.test.com/org/repo" + branch := "somebranch" + sc := kobject.ServiceConfig{ + Build: "./build", + } + bc := initBuildConfig(serviceName, sc, composeFileDir, repo, branch) + + testCases := map[string]struct { + field string + value string + }{ + "Assert buildconfig source git URI": {bc.Spec.CommonSpec.Source.Git.URI, repo}, + "Assert buildconfig source git Ref": {bc.Spec.CommonSpec.Source.Git.Ref, branch}, + "Assert buildconfig source context dir": {bc.Spec.CommonSpec.Source.ContextDir, "a/build"}, + "Assert buildconfig output name": {bc.Spec.CommonSpec.Output.To.Name, serviceName + ":latest"}, + } + + for name, test := range testCases { + t.Log("Test case: ", name) + if test.field != test.value { + t.Errorf("Expected: %#v, got: %#v", test.value, test.field) + } + } }