1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.22
package containerd
import (
"context"
"fmt"
"path/filepath"
"slices"
"testing"
containerdimages "github.com/containerd/containerd/images"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/platforms"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/internal/testutils/specialimage"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
type pushTestCase struct {
name string
indexPlatforms []ocispec.Platform // all platforms supported by the image
availablePlatforms []ocispec.Platform // platforms available locally
requestPlatform *ocispec.Platform // platform requested by the client (not the platform selected for push!)
check func(t *testing.T, img containerdimages.Image, pushDescriptor ocispec.Descriptor, err error)
daemonPlatform *ocispec.Platform
}
func TestImagePushIndex(t *testing.T) {
ctx := namespaces.WithNamespace(context.TODO(), "testing-"+t.Name())
csDir := t.TempDir()
store := &blobsDirContentStore{blobs: filepath.Join(csDir, "blobs/sha256")}
linuxAmd64 := platforms.MustParse("linux/amd64")
darwinArm64 := platforms.MustParse("darwin/arm64")
windowsAmd64 := platforms.MustParse("windows/amd64")
linuxArm64 := platforms.MustParse("linux/arm64")
linuxArmv5 := platforms.MustParse("linux/arm/v5")
linuxArmv7 := platforms.MustParse("linux/arm/v7")
// Image service will have the daemon host platform mocked to linux/amd64.
// Unless test cases specify a different platform.
defaultDaemonPlatform := linuxAmd64
for _, tc := range []pushTestCase{
// No explicit platform requested
{
name: "none requested, all present",
indexPlatforms: []ocispec.Platform{linuxAmd64, darwinArm64, windowsAmd64},
availablePlatforms: []ocispec.Platform{linuxAmd64, darwinArm64, windowsAmd64},
check: wholeIndexSelected,
},
{
name: "none requested, one present",
indexPlatforms: []ocispec.Platform{linuxAmd64, darwinArm64, windowsAmd64},
availablePlatforms: []ocispec.Platform{linuxAmd64},
check: singleManifestSelected(linuxAmd64),
},
{
name: "none requested, two present, daemon platform available",
indexPlatforms: []ocispec.Platform{linuxAmd64, darwinArm64, windowsAmd64},
availablePlatforms: []ocispec.Platform{linuxAmd64, darwinArm64},
check: singleManifestSelected(linuxAmd64),
},
{
name: "none requested, two present, daemon platform NOT available",
indexPlatforms: []ocispec.Platform{linuxAmd64, darwinArm64, windowsAmd64},
availablePlatforms: []ocispec.Platform{darwinArm64, windowsAmd64},
check: multipleCandidates,
},
// Specific platform requested
{
name: "linux/amd64 requested, all present",
indexPlatforms: []ocispec.Platform{linuxAmd64, darwinArm64, windowsAmd64},
availablePlatforms: []ocispec.Platform{linuxAmd64, darwinArm64, windowsAmd64},
requestPlatform: &linuxAmd64,
check: singleManifestSelected(linuxAmd64),
},
{
name: "linux/amd64 requested, but not present",
indexPlatforms: []ocispec.Platform{linuxAmd64, darwinArm64, windowsAmd64},
availablePlatforms: []ocispec.Platform{darwinArm64, windowsAmd64},
requestPlatform: &linuxAmd64,
check: candidateNotFound,
},
// Variant tests
{
name: "linux/arm/v5 requested, but not in index",
indexPlatforms: []ocispec.Platform{linuxAmd64, linuxArmv7},
availablePlatforms: []ocispec.Platform{linuxAmd64, linuxArmv7},
requestPlatform: &linuxArmv5,
check: candidateNotFound,
},
{
name: "linux/arm/v5 requested, but not available",
indexPlatforms: []ocispec.Platform{linuxArm64, linuxArmv7, linuxArmv5},
availablePlatforms: []ocispec.Platform{linuxArm64, linuxArmv7},
requestPlatform: &linuxArmv5,
check: candidateNotFound,
},
{
name: "linux/arm/v7 requested, but not available",
indexPlatforms: []ocispec.Platform{linuxArm64, linuxArmv7, linuxArmv5},
availablePlatforms: []ocispec.Platform{linuxArm64, linuxArmv5},
requestPlatform: &linuxArmv7,
check: candidateNotFound,
},
{
name: "linux/arm/v7 requested on v7 daemon, but not available",
indexPlatforms: []ocispec.Platform{linuxArm64, linuxArmv7, linuxArmv5},
availablePlatforms: []ocispec.Platform{linuxArm64, linuxArmv5},
daemonPlatform: &linuxArmv7,
requestPlatform: &linuxArmv7,
check: candidateNotFound,
},
{
name: "linux/arm/v7 requested on v5 daemon, all available",
indexPlatforms: []ocispec.Platform{linuxArm64, linuxArmv7, linuxArmv5},
availablePlatforms: []ocispec.Platform{linuxArm64, linuxArmv7, linuxArmv5},
daemonPlatform: &linuxArmv5,
requestPlatform: &linuxArmv7,
check: singleManifestSelected(linuxArmv7),
},
{
name: "linux/arm/v5 requested on v7 daemon, all available",
indexPlatforms: []ocispec.Platform{linuxArm64, linuxArmv7, linuxArmv5},
availablePlatforms: []ocispec.Platform{linuxArm64, linuxArmv7, linuxArmv5},
daemonPlatform: &linuxArmv7,
requestPlatform: &linuxArmv5,
check: singleManifestSelected(linuxArmv5),
},
{
name: "none requested on v5 daemon, arm64 not available",
indexPlatforms: []ocispec.Platform{linuxArm64, linuxArmv7, linuxArmv5},
availablePlatforms: []ocispec.Platform{linuxArmv7, linuxArmv5},
daemonPlatform: &linuxArmv5,
requestPlatform: nil,
check: singleManifestSelected(linuxArmv5),
},
{
name: "none requested on v7 daemon, arm64 not available",
indexPlatforms: []ocispec.Platform{linuxArm64, linuxArmv7, linuxArmv5},
availablePlatforms: []ocispec.Platform{linuxArmv7, linuxArmv5},
daemonPlatform: &linuxArmv7,
requestPlatform: nil,
check: singleManifestSelected(linuxArmv7),
},
{
name: "none requested on v7 daemon, v7 not available",
indexPlatforms: []ocispec.Platform{linuxArm64, linuxArmv7, linuxArmv5},
availablePlatforms: []ocispec.Platform{linuxArm64, linuxArmv5},
daemonPlatform: &linuxArmv7,
requestPlatform: nil,
check: singleManifestSelected(linuxArmv5), // Should it fail, because v5 can't be pushed?
},
{
name: "none requested on v7 daemon, v5 in index but not v7, all present",
indexPlatforms: []ocispec.Platform{linuxArm64, linuxArmv5},
availablePlatforms: []ocispec.Platform{linuxArm64, linuxArmv5},
daemonPlatform: &linuxArmv7,
requestPlatform: nil,
check: wholeIndexSelected,
},
{
name: "none requested on v7 daemon, v5 in index but not v7, v5 present",
indexPlatforms: []ocispec.Platform{linuxArm64, linuxArmv5},
availablePlatforms: []ocispec.Platform{linuxArmv5},
daemonPlatform: &linuxArmv7,
requestPlatform: nil,
check: singleManifestSelected(linuxArmv5),
},
} {
t.Run(tc.name, func(t *testing.T) {
imgSvc := fakeImageService(t, ctx, store)
// Mock the daemon platform.
if tc.daemonPlatform != nil {
imgSvc.defaultPlatformOverride = platforms.Only(*tc.daemonPlatform)
} else {
imgSvc.defaultPlatformOverride = platforms.Only(defaultDaemonPlatform)
}
idx, _, err := specialimage.MultiPlatform(csDir, "multiplatform:latest", tc.indexPlatforms)
assert.NilError(t, err)
imgs := imagesFromIndex(idx)
assert.Assert(t, is.Len(imgs, 1))
img := imgs[0]
_, err = imgSvc.images.Create(ctx, img)
assert.NilError(t, err)
for _, platform := range tc.indexPlatforms {
if slices.ContainsFunc(tc.availablePlatforms, platforms.OnlyStrict(platform).Match) {
continue
}
assert.NilError(t, deletePlatform(ctx, imgSvc, img, platform))
}
desc, err := imgSvc.getPushDescriptor(ctx, img, tc.requestPlatform)
tc.check(t, img, desc, err)
})
}
}
func deletePlatform(ctx context.Context, imgSvc *ImageService, img containerdimages.Image, platform ocispec.Platform) error {
var blobs []ocispec.Descriptor
pm := platforms.OnlyStrict(platform)
err := imgSvc.walkImageManifests(ctx, img, func(im *ImageManifest) error {
imPlatform, err := im.ImagePlatform(ctx)
if err != nil {
return fmt.Errorf("failed to determine platform of image manifest %v: %w", im.Target(), err)
}
if !pm.Match(imPlatform) {
return nil
}
return imgSvc.walkPresentChildren(ctx, im.Target(), func(ctx context.Context, d ocispec.Descriptor) error {
blobs = append(blobs, d)
return nil
})
})
if err != nil {
return fmt.Errorf("failed to walk image manifests: %w", err)
}
for _, d := range blobs {
err := imgSvc.content.Delete(ctx, d.Digest)
if err != nil {
return fmt.Errorf("failed to delete blob %v: %w", d.Digest, err)
}
}
return nil
}
// wholeIndexSelected asserts that the push descriptor candidate is for the whole index.
func wholeIndexSelected(t *testing.T, img containerdimages.Image, pushDescriptor ocispec.Descriptor, err error) {
assert.NilError(t, err)
assert.Check(t, is.Equal(pushDescriptor.Digest, img.Target.Digest))
}
// singleManifestSelected asserts that the push descriptor candidate is for a single platform-specific manifest.
func singleManifestSelected(platform ocispec.Platform) func(t *testing.T, img containerdimages.Image, pushDescriptor ocispec.Descriptor, err error) {
pm := platforms.OnlyStrict(platform)
return func(t *testing.T, img containerdimages.Image, pushDescriptor ocispec.Descriptor, err error) {
assert.NilError(t, err)
assert.Assert(t, is.Equal(pushDescriptor.MediaType, ocispec.MediaTypeImageManifest), "the push descriptor isn't for a manifest")
assert.Assert(t, pushDescriptor.Platform != nil, "the push descriptor doesn't have a platform")
assert.Assert(t, pm.Match(*pushDescriptor.Platform), "the push descriptor isn't for the selected platform")
}
}
// candidateNotFound asserts that the no matching candidate was found.
func candidateNotFound(t *testing.T, _ containerdimages.Image, desc ocispec.Descriptor, err error) {
assert.Check(t, errdefs.IsNotFound(err), "expected NotFound error, got %v, candidate: %v", err, desc.Platform)
}
// multipleCandidates asserts that multiple matching candidates were found and no decision could be made.
func multipleCandidates(t *testing.T, _ containerdimages.Image, desc ocispec.Descriptor, err error) {
assert.Check(t, errdefs.IsConflict(err), "expected Conflict error, got %v, candidate: %v", err, desc.Platform)
}
|