diff --git a/cli-plugins/plugin/plugin.go b/cli-plugins/plugin/plugin.go index 40a40ba42870..cc9a4378ff1a 100644 --- a/cli-plugins/plugin/plugin.go +++ b/cli-plugins/plugin/plugin.go @@ -61,10 +61,10 @@ func RunPlugin(ctx context.Context, dockerCli *command.DockerCli, plugin *cobra. } // Run is the top-level entry point to the CLI plugin framework. It should be called from your plugin's `main()` function. -func Run(makeCmd func(command.Cli) *cobra.Command, meta manager.Metadata) { +func Run(ctx context.Context, makeCmd func(command.Cli) *cobra.Command, meta manager.Metadata) { otel.SetErrorHandler(debug.OTELErrorHandler) - dockerCli, err := command.NewDockerCli() + dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx)) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) diff --git a/cmd/docker/builder_test.go b/cmd/docker/builder_test.go index 63c25abce64a..505bcd610e0f 100644 --- a/cmd/docker/builder_test.go +++ b/cmd/docker/builder_test.go @@ -23,6 +23,9 @@ import ( var pluginFilename = "docker-buildx" func TestBuildWithBuilder(t *testing.T) { + ctx, cancel := context.WithCancel(context.TODO()) + defer cancel() + testcases := []struct { name string context string @@ -64,12 +67,16 @@ echo '{"SchemaVersion":"0.1.0","Vendor":"Docker Inc.","Version":"v0.6.3","ShortD for _, tt := range testcases { tt := tt t.Run(tt.name, func(t *testing.T) { + ctx2, cancel2 := context.WithCancel(ctx) + defer cancel2() + if tt.builder != "" { t.Setenv("BUILDX_BUILDER", tt.builder) } var b bytes.Buffer dockerCli, err := command.NewDockerCli( + command.WithBaseContext(ctx2), command.WithAPIClient(&fakeClient{}), command.WithInputStream(discard), command.WithCombinedStreams(&b), @@ -98,8 +105,7 @@ echo '{"SchemaVersion":"0.1.0","Vendor":"Docker Inc.","Version":"v0.6.3","ShortD dockerCli.ConfigFile().Aliases = map[string]string{"builder": "buildx"} } - ctx := context.TODO() - tcmd := newDockerCommand(ctx, dockerCli) + tcmd := newDockerCommand(ctx2, dockerCli) tcmd.SetArgs([]string{"build", "."}) cmd, args, err := tcmd.HandleGlobalFlags() @@ -127,6 +133,9 @@ func (c *fakeClient) Ping(_ context.Context) (types.Ping, error) { } func TestBuildkitDisabled(t *testing.T) { + ctx, cancel := context.WithCancel(context.TODO()) + defer cancel() + t.Setenv("DOCKER_BUILDKIT", "0") dir := fs.NewDir(t, t.Name(), @@ -137,6 +146,7 @@ func TestBuildkitDisabled(t *testing.T) { b := bytes.NewBuffer(nil) dockerCli, err := command.NewDockerCli( + command.WithBaseContext(ctx), command.WithAPIClient(&fakeClient{}), command.WithInputStream(discard), command.WithCombinedStreams(b), @@ -145,7 +155,6 @@ func TestBuildkitDisabled(t *testing.T) { assert.NilError(t, dockerCli.Initialize(flags.NewClientOptions())) dockerCli.ConfigFile().CLIPluginsExtraDirs = []string{dir.Path()} - ctx := context.TODO() tcmd := newDockerCommand(ctx, dockerCli) tcmd.SetArgs([]string{"build", "."}) @@ -165,6 +174,9 @@ func TestBuildkitDisabled(t *testing.T) { } func TestBuilderBroken(t *testing.T) { + ctx, cancel := context.WithCancel(context.TODO()) + defer cancel() + dir := fs.NewDir(t, t.Name(), fs.WithFile(pluginFilename, `#!/bin/sh exit 1`, fs.WithMode(0o777)), ) @@ -173,6 +185,7 @@ func TestBuilderBroken(t *testing.T) { b := bytes.NewBuffer(nil) dockerCli, err := command.NewDockerCli( + command.WithBaseContext(ctx), command.WithAPIClient(&fakeClient{}), command.WithInputStream(discard), command.WithCombinedStreams(b), @@ -181,7 +194,6 @@ func TestBuilderBroken(t *testing.T) { assert.NilError(t, dockerCli.Initialize(flags.NewClientOptions())) dockerCli.ConfigFile().CLIPluginsExtraDirs = []string{dir.Path()} - ctx := context.TODO() tcmd := newDockerCommand(ctx, dockerCli) tcmd.SetArgs([]string{"build", "."}) @@ -202,6 +214,8 @@ func TestBuilderBroken(t *testing.T) { func TestBuilderBrokenEnforced(t *testing.T) { t.Setenv("DOCKER_BUILDKIT", "1") + ctx, cancel := context.WithCancel(context.TODO()) + defer cancel() dir := fs.NewDir(t, t.Name(), fs.WithFile(pluginFilename, `#!/bin/sh exit 1`, fs.WithMode(0o777)), @@ -211,6 +225,7 @@ func TestBuilderBrokenEnforced(t *testing.T) { b := bytes.NewBuffer(nil) dockerCli, err := command.NewDockerCli( + command.WithBaseContext(ctx), command.WithAPIClient(&fakeClient{}), command.WithInputStream(discard), command.WithCombinedStreams(b), @@ -219,7 +234,6 @@ func TestBuilderBrokenEnforced(t *testing.T) { assert.NilError(t, dockerCli.Initialize(flags.NewClientOptions())) dockerCli.ConfigFile().CLIPluginsExtraDirs = []string{dir.Path()} - ctx := context.TODO() tcmd := newDockerCommand(ctx, dockerCli) tcmd.SetArgs([]string{"build", "."}) diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index 896b77dbff74..58827427bf96 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -297,7 +297,7 @@ func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string, //nolint:gocyclo func runDocker(ctx context.Context, dockerCli *command.DockerCli) error { - tcmd := newDockerCommand(dockerCli) + tcmd := newDockerCommand(ctx, dockerCli) cmd, args, err := tcmd.HandleGlobalFlags() if err != nil { diff --git a/cmd/docker/docker_test.go b/cmd/docker/docker_test.go index 27fc91add392..5b331a2423b4 100644 --- a/cmd/docker/docker_test.go +++ b/cmd/docker/docker_test.go @@ -16,10 +16,11 @@ import ( func TestClientDebugEnabled(t *testing.T) { defer debug.Disable() + ctx, cancel := context.WithCancel(context.TODO()) + defer cancel() - cli, err := command.NewDockerCli() + cli, err := command.NewDockerCli(command.WithBaseContext(ctx)) assert.NilError(t, err) - ctx := context.TODO() tcmd := newDockerCommand(ctx, cli) tcmd.SetFlag("debug", "true") cmd, _, err := tcmd.HandleGlobalFlags() @@ -41,9 +42,14 @@ func runCliCommand(t *testing.T, r io.ReadCloser, w io.Writer, args ...string) e if w == nil { w = io.Discard } - cli, err := command.NewDockerCli(command.WithInputStream(r), command.WithCombinedStreams(w)) + ctx, cancel := context.WithCancel(context.TODO()) + defer cancel() + + cli, err := command.NewDockerCli( + command.WithBaseContext(ctx), + command.WithInputStream(r), + command.WithCombinedStreams(w)) assert.NilError(t, err) - ctx := context.TODO() tcmd := newDockerCommand(ctx, cli) tcmd.SetArgs(args)