Skip to content

Commit

Permalink
unit test added for PR #1336
Browse files Browse the repository at this point in the history
Signed-off-by: fatih <fatiiates@gmail.com>
  • Loading branch information
fatiiates authored and dfreilich committed Jan 24, 2022
1 parent fb90077 commit 6797559
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
14 changes: 4 additions & 10 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (
const (
packName = "Pack CLI"

cnbDir = "/cnb"
cnbDir = "/cnb"
buildpacksDir = "/cnb/buildpacks"

orderPath = "/cnb/order.toml"
stackPath = "/cnb/stack.toml"
Expand Down Expand Up @@ -418,16 +419,14 @@ func addBuildpacks(logger logging.Logger, tmpDir string, image imgutil.Image, ad
logger.Debugf("Buildpack %s already exists on builder with same contents, skipping...", style.Symbol(bpInfo.FullName()))
continue
} else {

bpWhiteoutsTmpDir := filepath.Join(tmpDir, strconv.Itoa(i)+"_whiteouts")
if err := os.MkdirAll(bpWhiteoutsTmpDir, os.ModePerm); err != nil {
return errors.Wrap(err, "creating buildpack whiteouts temp dir")
}

deletedMaps := map[string][]byte{
filepath.Join(
cnbDir,
"buildpacks",
buildpacksDir,
strings.ReplaceAll(bpInfo.ID, "/", "_"),
".wh."+bpInfo.Version,
): {},
Expand All @@ -442,12 +441,8 @@ func addBuildpacks(logger logging.Logger, tmpDir string, image imgutil.Image, ad
}

if err := image.AddLayer(whiteoutsTarFile); err != nil {
return errors.Wrapf(err,
"adding whiteout layer tar for buildpack %s",
style.Symbol(bpInfo.FullName()),
)
return errors.Wrap(err, "adding whiteout layer tar")
}

}

logger.Debugf(BuildpackOnBuilderMessage, style.Symbol(bpInfo.FullName()), style.Symbol(existingBPInfo.LayerDiffID), style.Symbol(diffID.String()))
Expand Down Expand Up @@ -487,7 +482,6 @@ func addBuildpacks(logger logging.Logger, tmpDir string, image imgutil.Image, ad
}

func createTarball(tarPath string, data map[string][]byte) error {

tarFile, err := os.Create(tarPath)
if err != nil {
return err
Expand Down
40 changes: 40 additions & 0 deletions internal/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {

when("duplicated buildpack, has different contents", func() {
var bp1v1Alt buildpack.Buildpack
var bp1v1AltWithNewContent buildpack.Buildpack
it.Before(func() {
var err error
bp1v1Alt, err = ifakes.NewFakeBuildpack(dist.BuildpackDescriptor{
Expand All @@ -671,6 +672,45 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
Mixins: []string{"mixinX", "mixinY"},
}},
}, 0644, ifakes.WithExtraBuildpackContents("coolbeans", "a file cool as beans"))

h.AssertNil(t, err)

bp1v1AltWithNewContent, err = ifakes.NewFakeBuildpack(dist.BuildpackDescriptor{
API: api.MustParse("0.2"),
Info: dist.BuildpackInfo{
ID: "buildpack-1-id",
Version: "buildpack-1-version-1",
},
Stacks: []dist.Stack{{
ID: "some.stack.id",
Mixins: []string{"mixinX", "mixinY"},
}},
}, 0644, ifakes.WithExtraBuildpackContents("coolwatermelon", "a file cool as watermelon"))

h.AssertNil(t, err)
})

it("uses the whiteout layers", func() {
logger := logging.NewLogWithWriters(&outBuf, &outBuf, logging.WithVerbose())

subject.AddBuildpack(bp1v1Alt)
subject.AddBuildpack(bp1v1AltWithNewContent)

err := subject.Save(logger, builder.CreatorMetadata{})
h.AssertNil(t, err)

h.AssertEq(t, baseImage.IsSaved(), true)

oldPath := filepath.Join("/cnb", "buildpacks", "buildpack-1-id", "buildpack-1-version-1", "coolbeans")
layer, err := baseImage.FindLayerWithPath(oldPath)

h.AssertEq(t, layer, "")
h.AssertError(t, err, fmt.Sprintf("could not find '%s' in any layer", oldPath))

newPath := filepath.Join("/cnb", "buildpacks", "buildpack-1-id", "buildpack-1-version-1", "coolwatermelon")
layer, err = baseImage.FindLayerWithPath(newPath)

h.AssertNotEq(t, layer, "")
h.AssertNil(t, err)
})

Expand Down

0 comments on commit 6797559

Please sign in to comment.