diff --git a/pkg/transformer/openshift/openshift.go b/pkg/transformer/openshift/openshift.go index 90a590e4..7820d662 100644 --- a/pkg/transformer/openshift/openshift.go +++ b/pkg/transformer/openshift/openshift.go @@ -147,15 +147,16 @@ func getComposeFileDir(inputFiles []string) (string, error) { } // getAbsBuildContext returns build context relative to project root dir -func getAbsBuildContext(context string, composeFileDir string) (string, error) { +func getAbsBuildContext(context string) (string, error) { cmd := exec.Command("git", "rev-parse", "--show-prefix") - cmd.Dir = composeFileDir + cmd.Dir = context out, err := cmd.Output() if err != nil { return "", err } - prefix := strings.Trim(string(out), "\n") - return filepath.Join(prefix, context), nil + //convert output of command to string + contextDir := strings.Trim(string(out), "\n") + return contextDir, nil } // initImageStream initialize ImageStream object @@ -197,8 +198,8 @@ func (o *OpenShift) initImageStream(name string, service kobject.ServiceConfig, } // initBuildConfig initialize Openshifts BuildConfig Object -func initBuildConfig(name string, service kobject.ServiceConfig, composeFileDir string, repo string, branch string) (*buildapi.BuildConfig, error) { - contextDir, err := getAbsBuildContext(service.Build, composeFileDir) +func initBuildConfig(name string, service kobject.ServiceConfig, repo string, branch string) (*buildapi.BuildConfig, error) { + contextDir, err := getAbsBuildContext(service.Build) if err != nil { return nil, errors.Wrap(err, name+"buildconfig cannot be created due to error in creating build context, getAbsBuildContext failed") } @@ -386,7 +387,7 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C } hasBuild = true } - bc, err := initBuildConfig(name, service, composeFileDir, buildRepo, buildBranch) + bc, err := initBuildConfig(name, service, buildRepo, buildBranch) if err != nil { return nil, errors.Wrap(err, "initBuildConfig failed") } diff --git a/pkg/transformer/openshift/openshift_test.go b/pkg/transformer/openshift/openshift_test.go index 9dc0c26e..cd61265b 100644 --- a/pkg/transformer/openshift/openshift_test.go +++ b/pkg/transformer/openshift/openshift_test.go @@ -243,24 +243,25 @@ func TestGetAbsBuildContext(t *testing.T) { gitDir := testutils.CreateLocalGitDirectory(t) testutils.SetGitRemote(t, gitDir, "newremote", "https://git.test.com/somerepo") testutils.CreateGitRemoteBranch(t, gitDir, "newbranch", "newremote") - testutils.CreateSubdir(t, gitDir, "a/b") + testutils.CreateSubdir(t, gitDir, "a/b/build") + testutils.CreateSubdir(t, gitDir, "build") dir := testutils.CreateLocalDirectory(t) defer os.RemoveAll(gitDir) defer os.RemoveAll(dir) testCases := map[string]struct { - expectError bool - context string - composeFileDir string - output string + expectError bool + context 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, ""}, + "Get abs build context success case-1": {false, filepath.Join(gitDir, "a/b/build"), "a/b/build/"}, + "Get abs build context success case-2": {false, filepath.Join(gitDir, "build"), "build/"}, + "Get abs build context error": {true, "example/build", "example/build/"}, } for name, test := range testCases { t.Log("Test case: ", name) - output, err = getAbsBuildContext(test.context, test.composeFileDir) + output, err = getAbsBuildContext(test.context) if test.expectError { if err == nil { @@ -284,14 +285,13 @@ func TestInitBuildConfig(t *testing.T) { 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", + Build: filepath.Join(dir, "a/build"), Dockerfile: "Dockerfile-alternate", } - bc, err := initBuildConfig(serviceName, sc, composeFileDir, repo, branch) + bc, err := initBuildConfig(serviceName, sc, repo, branch) if err != nil { t.Error(errors.Wrap(err, "initBuildConfig failed")) } @@ -302,7 +302,7 @@ func TestInitBuildConfig(t *testing.T) { }{ "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 source context dir": {bc.Spec.CommonSpec.Source.ContextDir, "a/build/"}, "Assert buildconfig output name": {bc.Spec.CommonSpec.Output.To.Name, serviceName + ":latest"}, "Assert buildconfig dockerfilepath": {bc.Spec.CommonSpec.Strategy.DockerStrategy.DockerfilePath, "Dockerfile-alternate"}, } diff --git a/script/test/cmd/tests.sh b/script/test/cmd/tests.sh index 2f21369c..b3f7a7c5 100755 --- a/script/test/cmd/tests.sh +++ b/script/test/cmd/tests.sh @@ -195,6 +195,17 @@ convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixture # Behavior with -o / convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o $TEMP_DIR/output_file -j" "$TEMP_DIR/output_file" +#### +# Test regarding build context (running kompose from various directories) +CURRENT_DIR=$(pwd) +cd "$KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/" +convert::expect_success_and_warning "kompose convert --provider openshift --stdout -j" "$KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/output-os.json" "Buildconfig using https://github.com/kubernetes-incubator/kompose.git::HEAD as source." +cd "$KOMPOSE_ROOT/script/test/fixtures/" +convert::expect_success_and_warning "kompose convert --provider openshift --stdout -j -f nginx-node-redis/docker-compose.yml" "$KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/output-os.json" "Buildconfig using https://github.com/kubernetes-incubator/kompose.git::HEAD as source." +cd "$KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/node" +convert::expect_success_and_warning "kompose convert --provider openshift --stdout -j -f ../docker-compose.yml" "$KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/output-os.json" "Buildconfig using https://github.com/kubernetes-incubator/kompose.git::HEAD as source." +cd $CURRENT_DIR + # Test related to support docker-compose.yaml beside docker-compose.yml # Store the original path CURRENT_DIR=$(pwd)