Skip to content

Commit

Permalink
fixing an issue when a docker media-type is updated and the value is …
Browse files Browse the repository at this point in the history
…override with OCI media-type

Signed-off-by: Juan Bustamante <[email protected]>
  • Loading branch information
jjbustamante committed Apr 30, 2024
1 parent d4a3c74 commit 2e42a19
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 35 deletions.
10 changes: 10 additions & 0 deletions cnb_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (h *CNBIndex) replaceDescriptor(digest name.Digest, withFun func(descriptor
if err != nil {
return err

Check warning on line 165 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L162-L165

Added lines #L162 - L165 were not covered by tests
}
mediaType := desc.MediaType
if desc.Platform == nil {
desc.Platform = &v1.Platform{}

Check warning on line 169 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L167-L169

Added lines #L167 - L169 were not covered by tests
}
Expand All @@ -176,6 +177,15 @@ func (h *CNBIndex) replaceDescriptor(digest name.Digest, withFun func(descriptor
Descriptor: desc,

Check warning on line 177 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L175-L177

Added lines #L175 - L177 were not covered by tests
}
h.ImageIndex = mutate.AppendManifests(mutate.RemoveManifests(h.ImageIndex, match.Digests(desc.Digest)), add)

Check warning on line 179 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L179

Added line #L179 was not covered by tests

// Avoid overriding the original media-type
mediaTypeAfter, err := h.ImageIndex.MediaType()
if err != nil {
return err

Check warning on line 184 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L182-L184

Added lines #L182 - L184 were not covered by tests
}
if mediaTypeAfter != mediaType {
h.ImageIndex = mutate.IndexMediaType(h.ImageIndex, mediaType)

Check warning on line 187 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L186-L187

Added lines #L186 - L187 were not covered by tests
}
return nil

Check warning on line 189 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L189

Added line #L189 was not covered by tests
}

Expand Down
101 changes: 66 additions & 35 deletions layout/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,45 +326,76 @@ func testIndex(t *testing.T, when spec.G, it spec.S) {

when("index exists on disk", func() {
when("#FromBaseIndex", func() {
it.Before(func() {
idx = setupIndex(t, "busybox-multi-platform", imgutil.WithXDGRuntimePath(tmpDir), imgutil.FromBaseIndex(baseIndexPath))
localPath = filepath.Join(tmpDir, "busybox-multi-platform")
digest1, err = name.NewDigest("busybox@sha256:e18f2c12bb4ea582045415243370a3d9cf3874265aa2867f21a35e630ebe45a7")
h.AssertNil(t, err)
})

when("digest is provided", func() {
when("attributes already exists", func() {
it("platform attributes are updated on disk", func() {
h.AssertNil(t, idx.SetOS(digest1, "linux-2"))
h.AssertNil(t, idx.SetArchitecture(digest1, "arm-2"))
h.AssertNil(t, idx.SetVariant(digest1, "v6-2"))
h.AssertNil(t, idx.SaveDir())

index := h.ReadIndexManifest(t, localPath)
h.AssertEq(t, len(index.Manifests), 2)
h.AssertEq(t, index.Manifests[1].Digest.String(), "sha256:e18f2c12bb4ea582045415243370a3d9cf3874265aa2867f21a35e630ebe45a7")
h.AssertEq(t, index.Manifests[1].Platform.OS, "linux-2")
h.AssertEq(t, index.Manifests[1].Platform.Architecture, "arm-2")
h.AssertEq(t, index.Manifests[1].Platform.Variant, "v6-2")
when("oci media-type is used", func() {
it.Before(func() {
idx = setupIndex(t, "busybox-multi-platform", imgutil.WithXDGRuntimePath(tmpDir), imgutil.FromBaseIndex(baseIndexPath))
localPath = filepath.Join(tmpDir, "busybox-multi-platform")
digest1, err = name.NewDigest("busybox@sha256:e18f2c12bb4ea582045415243370a3d9cf3874265aa2867f21a35e630ebe45a7")
h.AssertNil(t, err)
})

it("platform attributes are updated on disk", func() {
h.AssertNil(t, idx.SetOS(digest1, "linux-2"))
h.AssertNil(t, idx.SetArchitecture(digest1, "arm-2"))
h.AssertNil(t, idx.SetVariant(digest1, "v6-2"))
h.AssertNil(t, idx.SaveDir())

index := h.ReadIndexManifest(t, localPath)
h.AssertEq(t, len(index.Manifests), 2)
h.AssertEq(t, index.Manifests[1].Digest.String(), "sha256:e18f2c12bb4ea582045415243370a3d9cf3874265aa2867f21a35e630ebe45a7")
h.AssertEq(t, index.Manifests[1].Platform.OS, "linux-2")
h.AssertEq(t, index.Manifests[1].Platform.Architecture, "arm-2")
h.AssertEq(t, index.Manifests[1].Platform.Variant, "v6-2")
})

it("new annotation are appended on disk", func() {
annotations := map[string]string{
"some-key": "some-value",
}
h.AssertNil(t, idx.SetAnnotations(digest1, annotations))
h.AssertNil(t, idx.SaveDir())

index := h.ReadIndexManifest(t, localPath)
h.AssertEq(t, len(index.Manifests), 2)

// When updating a digest, it will be appended at the end
h.AssertEq(t, index.Manifests[1].Digest.String(), "sha256:e18f2c12bb4ea582045415243370a3d9cf3874265aa2867f21a35e630ebe45a7")

// in testdata we have 7 annotations + 1 new
h.AssertEq(t, len(index.Manifests[1].Annotations), 8)
h.AssertEq(t, index.Manifests[1].Annotations["some-key"], "some-value")
})
})

it("new annotation are appended on disk", func() {
annotations := map[string]string{
"some-key": "some-value",
}
h.AssertNil(t, idx.SetAnnotations(digest1, annotations))
h.AssertNil(t, idx.SaveDir())

index := h.ReadIndexManifest(t, localPath)
h.AssertEq(t, len(index.Manifests), 2)

// When updating a digest, it will be appended at the end
h.AssertEq(t, index.Manifests[1].Digest.String(), "sha256:e18f2c12bb4ea582045415243370a3d9cf3874265aa2867f21a35e630ebe45a7")

// in testdata we have 7 annotations + 1 new
h.AssertEq(t, len(index.Manifests[1].Annotations), 8)
h.AssertEq(t, index.Manifests[1].Annotations["some-key"], "some-value")
when("docker media-type is used", func() {
it.Before(func() {
baseIndexPath = filepath.Join(testDataDir, "index-with-docker-media-type")
idx = setupIndex(t, "some-docker-index", imgutil.WithXDGRuntimePath(tmpDir), imgutil.FromBaseIndex(baseIndexPath))
localPath = filepath.Join(tmpDir, imgutil.MakeFileSafeName("some-docker-index"))
digest1, err = name.NewDigest("some-docker-manifest@sha256:a564fd8f0684d2e119b73db7fb89280a665ebb18e8c30f26d163b4c0da8a8090")
h.AssertNil(t, err)
})

it("new annotation are appended on disk and media-type is not override", func() {
annotations := map[string]string{
"some-key": "some-value",
}
h.AssertNil(t, idx.SetAnnotations(digest1, annotations))
h.AssertNil(t, idx.SaveDir())

index := h.ReadIndexManifest(t, localPath)
h.AssertEq(t, len(index.Manifests), 1)
h.AssertEq(t, index.MediaType, types.DockerManifestList)

// When updating a digest, it will be appended at the end
h.AssertEq(t, index.Manifests[0].Digest.String(), "sha256:a564fd8f0684d2e119b73db7fb89280a665ebb18e8c30f26d163b4c0da8a8090")

// in testdata we have 7 annotations + 1 new
h.AssertEq(t, len(index.Manifests[0].Annotations), 1)
h.AssertEq(t, index.Manifests[0].Annotations["some-key"], "some-value")
})
})
})
})
Expand Down
15 changes: 15 additions & 0 deletions layout/testdata/layout/index-with-docker-media-type/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 940,
"digest": "sha256:a564fd8f0684d2e119b73db7fb89280a665ebb18e8c30f26d163b4c0da8a8090",
"platform": {
"architecture": "",
"os": "linux"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"imageLayoutVersion": "1.0.0"
}
5 changes: 5 additions & 0 deletions testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,8 @@ func AssertRemoteImageIndex(t *testing.T, repoName string, mediaType types.Media
}

func ReadIndexManifest(t *testing.T, path string) *v1.IndexManifest {
t.Helper()

indexPath := filepath.Join(path, "index.json")
AssertPathExists(t, filepath.Join(path, "oci-layout"))
AssertPathExists(t, indexPath)
Expand All @@ -640,6 +642,8 @@ func ReadIndexManifest(t *testing.T, path string) *v1.IndexManifest {
}

func ReadManifest(t *testing.T, digest v1.Hash, path string) *v1.Manifest {
t.Helper()

manifestPath := filepath.Join(path, "blobs", digest.Algorithm, digest.Hex)
AssertPathExists(t, manifestPath)

Expand All @@ -653,6 +657,7 @@ func ReadManifest(t *testing.T, digest v1.Hash, path string) *v1.Manifest {
}

func ReadConfigFile(t *testing.T, manifest *v1.Manifest, path string) *v1.ConfigFile {
t.Helper()
digest := manifest.Config.Digest
configPath := filepath.Join(path, "blobs", digest.Algorithm, digest.Hex)
AssertPathExists(t, configPath)
Expand Down

0 comments on commit 2e42a19

Please sign in to comment.