Skip to content

Commit

Permalink
Implemented descriptors and created a test for the open function
Browse files Browse the repository at this point in the history
Signed-off-by: Domenico Luciani <[email protected]>
  • Loading branch information
Domenico Luciani committed Sep 29, 2023
1 parent 4591bc8 commit 847948d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 26 deletions.
28 changes: 21 additions & 7 deletions pkg/buildpack/flattener.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package buildpack

type Flattener interface {
FlatBuildpacks()
import "io"

type BuildFlattenModule interface {
Open() (io.ReadCloser, error)
Descriptors() []Descriptor
}

type buildpacksFlattenerModule struct {
descriptors []Descriptor
}

type BuildpacksFlattener struct {
func NewBuildpacksFlattenerModule(buildmodules []BuildModule) BuildFlattenModule {
var bpFlattenerModule buildpacksFlattenerModule

for _, module := range buildmodules {
bpFlattenerModule.descriptors = append(bpFlattenerModule.descriptors, module.Descriptor())
}

return bpFlattenerModule
}

func NewBuildpacksFlattener() BuildpacksFlattener {
return BuildpacksFlattener{}
func (bfm buildpacksFlattenerModule) Descriptors() []Descriptor {
return bfm.descriptors
}

func (bp BuildpacksFlattener) FlatBuildpacks(bps []BuildModule) []BuildModule {
return bps //TODO: Implementation
func (bfm buildpacksFlattenerModule) Open() (io.ReadCloser, error) {
return nil, nil
}
79 changes: 60 additions & 19 deletions pkg/buildpack/flattener_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package buildpack_test

import (
"github.com/buildpacks/pack/pkg/archive"
"io"
"os"
"path/filepath"
"testing"

"github.com/buildpacks/lifecycle/api"
Expand All @@ -21,7 +25,6 @@ func testFlattener(t *testing.T, when spec.G, it spec.S) {
var (
bp1 buildpack.BuildModule
bp2 buildpack.BuildModule
bp3 buildpack.BuildModule
err error
)

Expand All @@ -43,28 +46,66 @@ func testFlattener(t *testing.T, when spec.G, it spec.S) {
},
}, 0644)
h.AssertNil(t, err)

bp3, err = ifakes.NewFakeBuildpack(dist.BuildpackDescriptor{
WithAPI: api.MustParse("0.2"),
WithInfo: dist.ModuleInfo{
ID: "buildpack-3-id",
Version: "buildpack-3-version",
},
}, 0644)
h.AssertNil(t, err)
})

when("Flattener has been called", func() {
var (
flattener buildpack.BuildpacksFlattener
)
it.Before(func() {
flattener = buildpack.NewBuildpacksFlattener()
when("Create a NewBuildpackFlattenModule", func() {
it("creates a buildpackFlattenModule with the correct information", func() {
buildpackFlattenerModule := buildpack.NewBuildpacksFlattenerModule([]buildpack.BuildModule{bp1, bp2})

h.AssertEq(t, len(buildpackFlattenerModule.Descriptors()), 2)
})
})

when("Open is called", func() {
var writeBlobToFile = func(bp buildpack.BuildFlattenModule) string {
t.Helper()

bpReader, err := bp.Open()
h.AssertNil(t, err)

tmpDir, err := os.MkdirTemp("", "")
h.AssertNil(t, err)

it("flats the buildpacks that has been passed", func() {
bps := flattener.FlatBuildpacks([]buildpack.BuildModule{bp1, bp2, bp3})
h.AssertEq(t, len(bps), 1)
p := filepath.Join(tmpDir, "bp.tar")
bpWriter, err := os.Create(p)
h.AssertNil(t, err)

_, err = io.Copy(bpWriter, bpReader)
h.AssertNil(t, err)

err = bpReader.Close()
h.AssertNil(t, err)

return p
}

it("Returns the reader about the content of any flatten buildpack", func() {
buildpackFlattenerModule := buildpack.NewBuildpacksFlattenerModule([]buildpack.BuildModule{bp1, bp2})

tarPath := writeBlobToFile(buildpackFlattenerModule)

assertDirExists(t, tarPath, "/cnb/buildpacks/buildpack-1-id")
assertDirExists(t, tarPath, "/cnb/buildpacks/buildpack-1-id/buildpack-1-version")
assertDirExists(t, tarPath, "/cnb/buildpacks/buildpack-1-id/buildpack-1-version/bin")
assertExecutableExists(t, tarPath, "/cnb/buildpacks/buildpack-1-id/buildpack-1-version/bin/build", "build-contents")
assertExecutableExists(t, tarPath, "/cnb/buildpacks/buildpack-1-id/buildpack-1-version/bin/detect", "detect-contents")
assertExecutableExists(t, tarPath, "/cnb/buildpacks/buildpack-2-id/buildpack-2-version/bin/build", "build-contents")
assertExecutableExists(t, tarPath, "/cnb/buildpacks/buildpack-2-id/buildpack-2-version/bin/detect", "detect-contents")
})
})
}

func assertExecutableExists(t *testing.T, tarPath, entryPath, content string) {
h.AssertOnTarEntry(t, tarPath, entryPath,
h.HasFileMode(0755),
h.HasModTime(archive.NormalizedDateTime),
h.ContentEquals(content))
}

func assertDirExists(t *testing.T, tarPath string, entryPath string) {
h.AssertOnTarEntry(t, tarPath, entryPath,
h.IsDirectory(),
h.HasFileMode(0755),
h.HasModTime(archive.NormalizedDateTime),
)
}

0 comments on commit 847948d

Please sign in to comment.