Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Priya Wadhwa committed Jul 19, 2018
1 parent 98ee3d1 commit 82c7012
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pkg/dockerfile/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func Dependencies(index int, stages []instructions.Stage, buildArgs *BuildArgs)
if stageIndex <= index {
continue
}
sourceImage, err := util.RetrieveSourceImage(stageIndex, stages)
sourceImage, err := util.RetrieveSourceImage(stageIndex, buildArgs.ReplacementEnvs(nil), stages)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func Test_Dependencies(t *testing.T) {
helloPath,
testDir,
},
nil,
{},
}

for index := range stages {
Expand Down Expand Up @@ -125,7 +125,7 @@ func Test_DependenciesWithArg(t *testing.T) {
helloPath,
testDir,
},
nil,
{},
}
buildArgs := NewBuildArgs([]string{fmt.Sprintf("hienv=%s", helloPath)})

Expand Down
11 changes: 3 additions & 8 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (
"strconv"

"github.com/GoogleContainerTools/kaniko/pkg/snapshot"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball"
Expand Down Expand Up @@ -72,14 +72,9 @@ func DoBuild(k KanikoBuildArgs) (v1.Image, error) {
return nil, err
}
for index, stage := range stages {
baseImage, err := util.ResolveEnvironmentReplacement(stage.BaseName, k.Args, false)
if err != nil {
return nil, err
}
finalStage := index == len(stages)-1
// Unpack file system to root
logrus.Infof("Unpacking filesystem of %s...", baseImage)
sourceImage, err := util.RetrieveSourceImage(index, stages)
sourceImage, err := util.RetrieveSourceImage(index, k.Args, stages)
if err != nil {
return nil, err
}
Expand All @@ -93,7 +88,7 @@ func DoBuild(k KanikoBuildArgs) (v1.Image, error) {
return nil, err
}
imageConfig, err := sourceImage.ConfigFile()
if baseImage == constants.NoBaseImage {
if sourceImage == empty.Image {
imageConfig.Config.Env = constants.ScratchEnvVars
}
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions pkg/util/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ var (
)

// RetrieveSourceImage returns the base image of the stage at index
func RetrieveSourceImage(index int, stages []instructions.Stage) (v1.Image, error) {
func RetrieveSourceImage(index int, buildArgs []string, stages []instructions.Stage) (v1.Image, error) {
currentStage := stages[index]
currentBaseName, err := ResolveEnvironmentReplacement(currentStage.BaseName, buildArgs, false)
if err != nil {
return nil, err
}
// First, check if the base image is a scratch image
if currentStage.BaseName == constants.NoBaseImage {
if currentBaseName == constants.NoBaseImage {
logrus.Info("No base image, nothing to extract")
return empty.Image, nil
}
Expand All @@ -51,12 +55,12 @@ func RetrieveSourceImage(index int, stages []instructions.Stage) (v1.Image, erro
if i > index {
continue
}
if stage.Name == currentStage.BaseName {
if stage.Name == currentBaseName {
return retrieveTarImage(i)
}
}
// Otherwise, initialize image as usual
return retrieveRemoteImage(currentStage.BaseName)
return retrieveRemoteImage(currentBaseName)
}

func tarballImage(index int) (v1.Image, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/image_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ func Test_StandardImage(t *testing.T) {
return nil, nil
}
retrieveRemoteImage = mock
actual, err := RetrieveSourceImage(0, stages)
actual, err := RetrieveSourceImage(0, nil, stages)
testutil.CheckErrorAndDeepEqual(t, false, err, nil, actual)
}
func Test_ScratchImage(t *testing.T) {
stages, err := parse(dockerfile)
if err != nil {
t.Error(err)
}
actual, err := RetrieveSourceImage(1, stages)
actual, err := RetrieveSourceImage(1, nil, stages)
expected := empty.Image
testutil.CheckErrorAndDeepEqual(t, false, err, expected, actual)
}
Expand All @@ -79,7 +79,7 @@ func Test_TarImage(t *testing.T) {
return nil, nil
}
retrieveTarImage = mock
actual, err := RetrieveSourceImage(2, stages)
actual, err := RetrieveSourceImage(2, nil, stages)
testutil.CheckErrorAndDeepEqual(t, false, err, nil, actual)
}

Expand Down

0 comments on commit 82c7012

Please sign in to comment.