Files
moby/integration/container/diff_test.go
T
Gordon ca9318d223 integration/container: Fix flaky TestDiff: replace IsStopped with IsInState(StateExited)
`TestDiff` uses `container.IsStopped()` to wait for the test container
to stop before calling `ContainerDiff`. However, `IsStopped` checks
`State.Running == false`, which is also true in the `"created"` state —
before the container has ever started. This creates a race: on a fast or
lightly-loaded host the poll can succeed before the shell command
`mkdir /foo; echo xyzzy > /foo/bar` executes, causing `ContainerDiff`
to return nil changes and the assertion to fail.

Replace the poll with `IsInState(ctx, apiClient, cID, containertypes.StateExited)`,
which checks `State.Status == "exited"` — guaranteeing the container has
fully run to completion. This mirrors the pattern already used correctly
in `TestDiffStoppedContainer` in the same file.

Signed-off-by: Gordon <GordonTheTurtle@users.noreply.github.com>
2026-07-27 19:12:26 +00:00

59 lines
2.0 KiB
Go

package container
import (
"testing"
"time"
containertypes "github.com/moby/moby/api/types/container"
"github.com/moby/moby/client"
"github.com/moby/moby/v2/integration/internal/container"
"gotest.tools/v3/assert"
"gotest.tools/v3/poll"
"gotest.tools/v3/skip"
)
func TestDiff(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType == "windows", "cannot diff a running container on Windows")
ctx := setupTest(t)
apiClient := testEnv.APIClient()
cID := container.Run(ctx, t, apiClient, container.WithCmd("sh", "-c", `mkdir /foo; echo xyzzy > /foo/bar`))
expected := []containertypes.FilesystemChange{
{Kind: containertypes.ChangeAdd, Path: "/foo"},
{Kind: containertypes.ChangeAdd, Path: "/foo/bar"},
}
poll.WaitOn(t, container.IsInState(ctx, apiClient, cID, containertypes.StateExited), poll.WithTimeout(60*time.Second))
result, err := apiClient.ContainerDiff(ctx, cID, client.ContainerDiffOptions{})
assert.NilError(t, err)
assert.DeepEqual(t, expected, result.Changes)
}
func TestDiffStoppedContainer(t *testing.T) {
// There's no way in Windows to differentiate between an Add or a Modify,
// and all files are under a "Files/" prefix.
skip.If(t, testEnv.DaemonInfo.OSType == "windows", "FIXME")
ctx := setupTest(t)
apiClient := testEnv.APIClient()
cID := container.Run(ctx, t, apiClient, container.WithCmd("sh", "-c", `mkdir /foo; echo xyzzy > /foo/bar`))
poll.WaitOn(t, container.IsInState(ctx, apiClient, cID, containertypes.StateExited), poll.WithTimeout(60*time.Second))
expected := []containertypes.FilesystemChange{
{Kind: containertypes.ChangeAdd, Path: "/foo"},
{Kind: containertypes.ChangeAdd, Path: "/foo/bar"},
}
if testEnv.DaemonInfo.OSType == "windows" {
expected = []containertypes.FilesystemChange{
{Kind: containertypes.ChangeModify, Path: "Files/foo"},
{Kind: containertypes.ChangeModify, Path: "Files/foo/bar"},
}
}
result, err := apiClient.ContainerDiff(ctx, cID, client.ContainerDiffOptions{})
assert.NilError(t, err)
assert.DeepEqual(t, expected, result.Changes)
}