forked from LaconicNetwork/kompose
Adding code for consistent build context
fixes #445 This PR was partially fixed by docker/libcompose#450, which now gives consistent build context, also it modifies the function getAbsBuildContext to create accurate build context, Unit test are being modified according to new structure.
This commit is contained in:
parent
760d47b022
commit
605d643a84
@ -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")
|
||||
}
|
||||
|
||||
@ -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"},
|
||||
}
|
||||
|
||||
@ -195,6 +195,17 @@ convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixture
|
||||
# Behavior with -o <dirname>/<filename>
|
||||
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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user