diff --git a/pkg/buildpack/oci_layout_package.go b/pkg/buildpack/oci_layout_package.go index eab5516ce4..7ce5039991 100644 --- a/pkg/buildpack/oci_layout_package.go +++ b/pkg/buildpack/oci_layout_package.go @@ -28,7 +28,7 @@ func IsOCILayoutBlob(blob blob2.Blob) (bool, error) { } defer readCloser.Close() - _, _, err = archive.ReadTarEntry(readCloser, "/oci-layout") + _, _, err = archive.ReadTarEntry(readCloser, v1.ImageLayoutFile) if err != nil { if archive.IsEntryNotExist(err) { return false, nil @@ -77,13 +77,13 @@ type ociLayoutPackage struct { func newOCILayoutPackage(blob Blob, kind string) (*ociLayoutPackage, error) { index := &v1.Index{} - if err := unmarshalJSONFromBlob(blob, "/index.json", index); err != nil { + if err := unmarshalJSONFromBlob(blob, v1.ImageIndexFile, index); err != nil { return nil, err } var manifestDescriptor *v1.Descriptor for _, m := range index.Manifests { - if m.MediaType == "application/vnd.docker.distribution.manifest.v2+json" { + if m.MediaType == "application/vnd.docker.distribution.manifest.v2+json" || m.MediaType == v1.MediaTypeImageManifest { manifestDescriptor = &m // nolint:exportloopref break } @@ -167,7 +167,7 @@ func (o *ociLayoutPackage) GetLayer(diffID string) (io.ReadCloser, error) { if paths.CanonicalTarPath(header.Name) == layerPath { finalReader := blobReader - if strings.HasSuffix(layerDescriptor.MediaType, ".gzip") { + if strings.HasSuffix(layerDescriptor.MediaType, "gzip") { finalReader, err = gzip.NewReader(tr) if err != nil { return nil, err diff --git a/pkg/buildpack/oci_layout_package_test.go b/pkg/buildpack/oci_layout_package_test.go index 540b036404..38b8321acd 100644 --- a/pkg/buildpack/oci_layout_package_test.go +++ b/pkg/buildpack/oci_layout_package_test.go @@ -7,6 +7,7 @@ import ( "github.com/buildpacks/lifecycle/api" "github.com/heroku/color" + v1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/sclevine/spec" "github.com/sclevine/spec/report" @@ -24,37 +25,53 @@ func TestOCILayoutPackage(t *testing.T) { spec.Run(t, "Extract", testOCILayoutPackage, spec.Parallel(), spec.Report(report.Terminal{})) } +type testCase struct { + mediatype string + file string +} + func testOCILayoutPackage(t *testing.T, when spec.G, it spec.S) { when("#BuildpacksFromOCILayoutBlob", func() { - it("extracts buildpacks", func() { - mainBP, depBPs, err := buildpack.BuildpacksFromOCILayoutBlob(blob.NewBlob(filepath.Join("testdata", "hello-universe.cnb"))) - h.AssertNil(t, err) - - h.AssertEq(t, mainBP.Descriptor().Info().ID, "io.buildpacks.samples.hello-universe") - h.AssertEq(t, mainBP.Descriptor().Info().Version, "0.0.1") - h.AssertEq(t, len(depBPs), 2) - }) + for _, test := range []testCase{ + { + mediatype: "application/vnd.docker.distribution.manifest.v2+json", + file: "hello-universe.cnb", + }, + { + mediatype: v1.MediaTypeImageManifest, + file: "hello-universe-oci.cnb", + }, + } { + it(fmt.Sprintf("extracts buildpacks, media type: %s", test.mediatype), func() { + mainBP, depBPs, err := buildpack.BuildpacksFromOCILayoutBlob(blob.NewBlob(filepath.Join("testdata", test.file))) + h.AssertNil(t, err) - it("provides readable blobs", func() { - mainBP, depBPs, err := buildpack.BuildpacksFromOCILayoutBlob(blob.NewBlob(filepath.Join("testdata", "hello-universe.cnb"))) - h.AssertNil(t, err) + h.AssertEq(t, mainBP.Descriptor().Info().ID, "io.buildpacks.samples.hello-universe") + h.AssertEq(t, mainBP.Descriptor().Info().Version, "0.0.1") + h.AssertEq(t, len(depBPs), 2) + }) - for _, bp := range append([]buildpack.BuildModule{mainBP}, depBPs...) { - reader, err := bp.Open() + it(fmt.Sprintf("provides readable blobs, media type: %s", test.mediatype), func() { + mainBP, depBPs, err := buildpack.BuildpacksFromOCILayoutBlob(blob.NewBlob(filepath.Join("testdata", test.file))) h.AssertNil(t, err) - _, contents, err := archive.ReadTarEntry( - reader, - fmt.Sprintf("/cnb/buildpacks/%s/%s/buildpack.toml", - bp.Descriptor().Info().ID, - bp.Descriptor().Info().Version, - ), - ) - h.AssertNil(t, err) - h.AssertContains(t, string(contents), bp.Descriptor().Info().ID) - h.AssertContains(t, string(contents), bp.Descriptor().Info().Version) - } - }) + for _, bp := range append([]buildpack.BuildModule{mainBP}, depBPs...) { + reader, err := bp.Open() + h.AssertNil(t, err) + + _, contents, err := archive.ReadTarEntry( + reader, + fmt.Sprintf("/cnb/buildpacks/%s/%s/buildpack.toml", + bp.Descriptor().Info().ID, + bp.Descriptor().Info().Version, + ), + ) + h.AssertNil(t, err) + h.AssertContains(t, string(contents), bp.Descriptor().Info().ID) + h.AssertContains(t, string(contents), bp.Descriptor().Info().Version) + } + }) + } }) when("#ExtensionsFromOCILayoutBlob", func() { diff --git a/pkg/buildpack/testdata/hello-universe-oci.cnb b/pkg/buildpack/testdata/hello-universe-oci.cnb new file mode 100644 index 0000000000..59b4b657fb Binary files /dev/null and b/pkg/buildpack/testdata/hello-universe-oci.cnb differ