2016-01-19 14:26:15 -08:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-08-24 13:18:39 +02:00
|
|
|
"github.com/distribution/distribution/v3"
|
2023-10-24 13:16:58 -04:00
|
|
|
"github.com/distribution/distribution/v3/internal/dcontext"
|
2020-08-24 13:18:39 +02:00
|
|
|
"github.com/distribution/distribution/v3/manifest/manifestlist"
|
|
|
|
"github.com/distribution/distribution/v3/manifest/schema2"
|
2016-12-16 16:28:34 -08:00
|
|
|
"github.com/opencontainers/go-digest"
|
2016-01-19 14:26:15 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// MakeManifestList constructs a manifest list out of a list of manifest digests
|
|
|
|
func MakeManifestList(blobstatter distribution.BlobStatter, manifestDigests []digest.Digest) (*manifestlist.DeserializedManifestList, error) {
|
2023-10-24 13:16:58 -04:00
|
|
|
ctx := dcontext.Background()
|
2016-01-19 14:26:15 -08:00
|
|
|
|
2023-09-03 22:41:51 +01:00
|
|
|
manifestDescriptors := make([]manifestlist.ManifestDescriptor, 0, len(manifestDigests))
|
2016-01-19 14:26:15 -08:00
|
|
|
for _, manifestDigest := range manifestDigests {
|
|
|
|
descriptor, err := blobstatter.Stat(ctx, manifestDigest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
platformSpec := manifestlist.PlatformSpec{
|
|
|
|
Architecture: "atari2600",
|
|
|
|
OS: "CP/M",
|
|
|
|
Variant: "ternary",
|
|
|
|
Features: []string{"VLIW", "superscalaroutoforderdevnull"},
|
|
|
|
}
|
|
|
|
manifestDescriptor := manifestlist.ManifestDescriptor{
|
|
|
|
Descriptor: descriptor,
|
|
|
|
Platform: platformSpec,
|
|
|
|
}
|
|
|
|
manifestDescriptors = append(manifestDescriptors, manifestDescriptor)
|
|
|
|
}
|
|
|
|
|
|
|
|
return manifestlist.FromDescriptors(manifestDescriptors)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeSchema2Manifest constructs a schema 2 manifest from a given list of digests and returns
|
|
|
|
// the digest of the manifest
|
|
|
|
func MakeSchema2Manifest(repository distribution.Repository, digests []digest.Digest) (distribution.Manifest, error) {
|
2023-10-24 13:16:58 -04:00
|
|
|
ctx := dcontext.Background()
|
2016-01-19 14:26:15 -08:00
|
|
|
blobStore := repository.Blobs(ctx)
|
2023-05-03 18:54:34 +02:00
|
|
|
|
|
|
|
var configJSON []byte
|
|
|
|
|
|
|
|
d, err := blobStore.Put(ctx, schema2.MediaTypeImageConfig, configJSON)
|
|
|
|
if err != nil {
|
2023-05-19 15:38:09 +02:00
|
|
|
return nil, fmt.Errorf("unexpected error storing content in blobstore: %v", err)
|
2023-05-03 18:54:34 +02:00
|
|
|
}
|
|
|
|
builder := schema2.NewManifestBuilder(d, configJSON)
|
|
|
|
for _, digest := range digests {
|
2023-11-18 06:50:40 +00:00
|
|
|
if err := builder.AppendReference(distribution.Descriptor{Digest: digest}); err != nil {
|
|
|
|
return nil, fmt.Errorf("unexpected error building manifest: %v", err)
|
|
|
|
}
|
2016-01-19 14:26:15 -08:00
|
|
|
}
|
|
|
|
|
2022-11-26 13:34:11 +01:00
|
|
|
mfst, err := builder.Build(ctx)
|
2016-01-19 14:26:15 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unexpected error generating manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-11-26 13:34:11 +01:00
|
|
|
return mfst, nil
|
2016-01-19 14:26:15 -08:00
|
|
|
}
|