@@ -23,6 +23,7 @@ import (
2323 v1 "github.com/google/go-containerregistry/pkg/v1"
2424 "github.com/google/go-containerregistry/pkg/v1/empty"
2525 "github.com/google/go-containerregistry/pkg/v1/mutate"
26+ "github.com/google/go-containerregistry/pkg/v1/partial"
2627 "github.com/google/go-containerregistry/pkg/v1/random"
2728 "github.com/google/go-containerregistry/pkg/v1/types"
2829 "github.com/google/go-containerregistry/pkg/v1/validate"
@@ -180,3 +181,55 @@ func TestIndexImmutability(t *testing.T) {
180181 }
181182 })
182183}
184+
185+ // TestAppend_ArtifactType tests that appending an image manifest that has a
186+ // non-standard config.mediaType to an index, results in the image's
187+ // config.mediaType being hoisted into the descriptor inside the index,
188+ // as artifactType.
189+ func TestAppend_ArtifactType (t * testing.T ) {
190+ for _ , c := range []struct {
191+ desc , configMediaType , wantArtifactType string
192+ }{{
193+ desc : "standard config.mediaType, no artifactType" ,
194+ configMediaType : string (types .DockerConfigJSON ),
195+ wantArtifactType : "" ,
196+ }, {
197+ desc : "non-standard config.mediaType, want artifactType" ,
198+ configMediaType : "application/vnd.custom.something" ,
199+ wantArtifactType : "application/vnd.custom.something" ,
200+ }} {
201+ t .Run (c .desc , func (t * testing.T ) {
202+ img , err := random .Image (1 , 1 )
203+ if err != nil {
204+ t .Fatalf ("random.Image: %v" , err )
205+ }
206+ img = mutate .ConfigMediaType (img , types .MediaType (c .configMediaType ))
207+ idx := mutate .AppendManifests (empty .Index , mutate.IndexAddendum {
208+ Add : img ,
209+ })
210+ mf , err := idx .IndexManifest ()
211+ if err != nil {
212+ t .Fatalf ("IndexManifest: %v" , err )
213+ }
214+ if got := mf .Manifests [0 ].ArtifactType ; got != c .wantArtifactType {
215+ t .Errorf ("manifest artifactType: got %q, want %q" , got , c .wantArtifactType )
216+ }
217+
218+ desc , err := partial .Descriptor (img )
219+ if err != nil {
220+ t .Fatalf ("partial.Descriptor: %v" , err )
221+ }
222+ if got := desc .ArtifactType ; got != c .wantArtifactType {
223+ t .Errorf ("descriptor artifactType: got %q, want %q" , got , c .wantArtifactType )
224+ }
225+
226+ gotAT , err := partial .ArtifactType (img )
227+ if err != nil {
228+ t .Fatalf ("partial.ArtifactType: %v" , err )
229+ }
230+ if gotAT != c .wantArtifactType {
231+ t .Errorf ("partial.ArtifactType: got %q, want %q" , gotAT , c .wantArtifactType )
232+ }
233+ })
234+ }
235+ }
0 commit comments