compose/pkg/e2e/image_identity_test.go
Guillaume Lours a9d054fb17 fix(build): canonical content-digest producer, single image-label writer
Image identities recorded for staleness detection were produced by
several independent paths yielding different digest kinds for the same
image: the platform check compared flat inspect fields while the digest
picked a manifest with the host matcher (never the service's pinned
platform), a wrong-platform summary just discarded still leaked its
digest into the label, bake substituted digests host-side in batch, and
the classic builder recorded the raw build-stream ID as-is. Any of those
mismatches makes the next up see a phantom image change and recreate
containers.

Converge every producer on one selection (matchLocalManifest /
localContentDigest): the shared parallel inspect feeds both the digest
and the platform check, platform-pinned services resolve THEIR platform's
manifest in-process (no extra API call), and both builders route through
canonicalBuiltDigest. Registry-only builds (push-only, multi-platform
without load) keep the builder-reported digest — volatile but honest, an
actual rebuild is still detected, where a stable placeholder would hide
real image changes. ensureImagesExists' final loop becomes the label's
single writer so the pinned resolution can't be overwritten, superseded
only by pull/build results already platform-resolved by their producers —
and when a pull or build refreshed the shared entry mid-run (a digest
resolved for whichever service triggered it), a service pinned on another
platform re-resolves its own with one extra inspect, in that case only.

With every producer converged, TestUpIdempotentContainerdStore is
un-skipped here.

Signed-off-by: Guillaume Lours <glours@users.noreply.github.com>
2026-08-13 12:03:29 +02:00

76 lines
3.1 KiB
Go

/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"fmt"
"strings"
"testing"
"gotest.tools/v3/assert"
)
// TestUpIdempotentContainerdStore reproduces the scenario described in
// https://github.com/docker/compose/pull/13998 : under the containerd image
// store, a multi-platform image pulled by the first `up` gets its
// `com.docker.compose.image` label set from the raw digest returned by
// `image inspect` (the manifest-list/index digest). On the next `up`, the
// image is now local, so compose recomputes the label from the per-platform
// manifest digest instead. Those two digests differ for the very same image,
// so compose believes the image changed and recreates the container even
// though nothing did.
//
// `up` MUST be idempotent: running it twice in a row without any change
// must not recreate any container.
func TestUpIdempotentContainerdStore(t *testing.T) {
c := NewCLI(t)
requireContainerdStore(t, c)
const projectName = "compose-e2e-image-identity-idempotent"
const image = "alpine:3.20"
const composeFile = "./fixtures/image-identity/compose.yaml"
t.Cleanup(func() {
c.cleanupWithDown(t, projectName)
c.RunDockerOrExitError(t, "rmi", "-f", image)
})
// Start from an image that was never pulled locally: the first `up`
// below will pull it, exercising the multi-platform pull path.
c.RunDockerOrExitError(t, "rmi", "-f", image)
c.RunDockerComposeCmd(t, "-f", composeFile, "--project-name", projectName, "up", "-d")
containerID := c.RunDockerCmd(t, "inspect", fmt.Sprintf("%s-app-1", projectName), "-f", "{{.Id}}").Stdout()
res := c.RunDockerComposeCmd(t, "-f", composeFile, "--project-name", projectName, "up", "-d")
assert.Check(t, !strings.Contains(res.Combined(), "Recreate"), "second `up` should not recreate anything, got: %s", res.Combined())
newContainerID := c.RunDockerCmd(t, "inspect", fmt.Sprintf("%s-app-1", projectName), "-f", "{{.Id}}").Stdout()
assert.Equal(t, containerID, newContainerID, "container should not have been recreated by an idempotent `up`")
}
// requireContainerdStore skips the test unless the docker daemon backing the
// CLI instance uses the containerd image store (`io.containerd.snapshotter.v1`
// driver), which is a prerequisite for the image identity bug this test
// covers.
func requireContainerdStore(t *testing.T, c *CLI) {
t.Helper()
res := c.RunDockerCmd(t, "info", "-f", "{{json .DriverStatus}}")
if !strings.Contains(res.Stdout(), "io.containerd.snapshotter.v1") {
t.Skip("Skipping test: daemon is not using the containerd image store")
}
}