From 0e474e38f138c25645fdef497c8c546a3a94239f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 18 Aug 2025 15:21:14 +0200 Subject: [PATCH] [28.x] cli/command/container: TestRunPullTermination: rewrite with streamformatter This makes the test slightly closer to the actual code in the daemon producing the progress response; https://github.com/moby/moby/blob/cd844fd0b2047eff6854600d375545b3ec01de48/daemon/images/image_pull.go#L58-L70 https://github.com/moby/moby/blob/cd844fd0b2047eff6854600d375545b3ec01de48/daemon/internal/distribution/utils/progress.go#L14-L34 This is a modified version of 69854c4e08c502f57a1fed4ec58236fae229db37 with some changes specific to the 28.x branch (the variant on master had some patches for the moby/api and moby/client transition). Signed-off-by: Sebastiaan van Stijn (cherry picked from commit 69854c4e08c502f57a1fed4ec58236fae229db37) Signed-off-by: Sebastiaan van Stijn --- cli/command/container/run_test.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/cli/command/container/run_test.go b/cli/command/container/run_test.go index f60ebde360..17a931c8c0 100644 --- a/cli/command/container/run_test.go +++ b/cli/command/container/run_test.go @@ -2,9 +2,7 @@ package container import ( "context" - "encoding/json" "errors" - "fmt" "io" "net" "syscall" @@ -20,7 +18,8 @@ import ( "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/network" - "github.com/docker/docker/pkg/jsonmessage" + "github.com/docker/docker/pkg/progress" + "github.com/docker/docker/pkg/streamformatter" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/pflag" "gotest.tools/v3/assert" @@ -242,23 +241,19 @@ func TestRunPullTermination(t *testing.T) { _ = server.Close() }) go func() { - enc := json.NewEncoder(server) + id := test.RandomID()[:12] // short-ID + progressOutput := streamformatter.NewJSONProgressOutput(server, true) for i := 0; i < 100; i++ { select { case <-ctx.Done(): assert.NilError(t, server.Close(), "failed to close imageCreateFunc server") return default: - assert.NilError(t, enc.Encode(jsonmessage.JSONMessage{ - Status: "Downloading", - ID: fmt.Sprintf("id-%d", i), - TimeNano: time.Now().UnixNano(), - Time: time.Now().Unix(), - Progress: &jsonmessage.JSONProgress{ - Current: int64(i), - Total: 100, - Start: 0, - }, + assert.NilError(t, progressOutput.WriteProgress(progress.Progress{ + ID: id, + Message: "Downloading", + Current: int64(i), + Total: 100, })) time.Sleep(100 * time.Millisecond) }