c8d/host_platform: Override spec not matcher

Tests should replace the detected host platform, not the matcher built
from it.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2026-06-08 18:52:25 +02:00
parent 7cd7ecb87e
commit ce89caeaa7
6 changed files with 14 additions and 15 deletions

View File

@@ -37,7 +37,7 @@ func TestImageLoad(t *testing.T) {
imgSvc := fakeImageService(t, ctx, store)
// Mock the daemon platform.
imgSvc.defaultPlatformOverride = platforms.Only(linuxAmd64)
imgSvc.defaultPlatformOverride = &linuxAmd64
tryLoad := func(ctx context.Context, t *testing.T, dir string, platformList []ocispec.Platform) error {
tarRc, err := archive.Tar(dir, compression.None)

View File

@@ -195,11 +195,11 @@ func TestImagePushIndex(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
imgSvc := fakeImageService(t, ctx, store)
// Mock the daemon platform.
daemonPlatform := defaultDaemonPlatform
if tc.daemonPlatform != nil {
imgSvc.defaultPlatformOverride = platforms.Only(*tc.daemonPlatform)
} else {
imgSvc.defaultPlatformOverride = platforms.Only(defaultDaemonPlatform)
daemonPlatform = *tc.daemonPlatform
}
imgSvc.defaultPlatformOverride = &daemonPlatform
idx, _, err := specialimage.MultiPlatform(csDir, "multiplatform:latest", tc.indexPlatforms)
assert.NilError(t, err)

View File

@@ -37,7 +37,7 @@ func TestImageMultiplatformSaveShallowWithNative(t *testing.T) {
imgSvc := fakeImageService(t, ctx, store)
// Mock the native platform.
imgSvc.defaultPlatformOverride = platforms.Only(native)
imgSvc.defaultPlatformOverride = &native
idx, _, err := specialimage.PartialMultiPlatform(contentDir, "partial-with-native:latest", specialimage.PartialOpts{
Stored: []ocispec.Platform{native, riscv64},
@@ -98,7 +98,7 @@ func TestImageMultiplatformSaveShallowWithoutNative(t *testing.T) {
imgSvc := fakeImageService(t, ctx, store)
// Mock the native platform.
imgSvc.defaultPlatformOverride = platforms.Only(native)
imgSvc.defaultPlatformOverride = &native
idx, _, err := specialimage.PartialMultiPlatform(contentDir, "partial-without-native:latest", specialimage.PartialOpts{
Stored: []ocispec.Platform{arm64, riscv64},

View File

@@ -67,7 +67,7 @@ func (i *ImageService) matchRequestedOrDefault(
func (i *ImageService) hostPlatformMatcher() platforms.MatchComparer {
// Allow to override the host platform for testing purposes.
if i.defaultPlatformOverride != nil {
return i.defaultPlatformOverride
return platforms.Only(*i.defaultPlatformOverride)
}
return platforms.Default()
}

View File

@@ -58,11 +58,11 @@ type indexTestCase struct {
}
func TestMatcherOnLinuxArm64v8(t *testing.T) {
daemonPlatform := platforms.Only(ocispec.Platform{
daemonPlatform := ocispec.Platform{
OS: "linux",
Architecture: "arm64",
Variant: "v8",
})
}
yes := true
no := false
@@ -96,11 +96,11 @@ func TestMatcherOnLinuxArm64v8(t *testing.T) {
func TestMatcherOnWindowsAmd64(t *testing.T) {
skip.If(t, runtime.GOOS != "windows", "TODO: containerd matcher only matches OSVersion when on Windows")
daemonPlatform := platforms.Only(ocispec.Platform{
daemonPlatform := ocispec.Platform{
OS: "windows",
Architecture: "amd64",
OSVersion: "10.0.18362",
})
}
for _, indexTc := range []indexTestCase{
{
@@ -121,9 +121,9 @@ func TestMatcherOnWindowsAmd64(t *testing.T) {
}
}
func testOnlyAndOnlyStrict(t *testing.T, daemonPlatform platforms.MatchComparer, indexTc indexTestCase) {
func testOnlyAndOnlyStrict(t *testing.T, daemonPlatform ocispec.Platform, indexTc indexTestCase) {
imgSvc := ImageService{}
imgSvc.defaultPlatformOverride = daemonPlatform
imgSvc.defaultPlatformOverride = &daemonPlatform
t.Run(indexTc.name, func(t *testing.T) {
indexTc := indexTc

View File

@@ -13,7 +13,6 @@ import (
"github.com/containerd/containerd/v2/plugins"
cerrdefs "github.com/containerd/errdefs"
"github.com/containerd/log"
"github.com/containerd/platforms"
"github.com/moby/moby/v2/daemon/container"
"github.com/moby/moby/v2/daemon/containerd/identitycache"
daemonevents "github.com/moby/moby/v2/daemon/events"
@@ -46,7 +45,7 @@ type ImageService struct {
identity imageIdentityState
// defaultPlatformOverride is used in tests to override the host platform.
defaultPlatformOverride platforms.MatchComparer
defaultPlatformOverride *ocispec.Platform
}
type ImageServiceConfig struct {