Skip to content

Commit 709df33

Browse files
thaJeztahBenehiko
authored andcommitted
feat: use main func ctx for cobra and use ctx in tests
Explicitly create the context and set it on the CLI, instead of depending on NewDockerCli() to instance a default context. Co-authored-by: Sebastiaan van Stijn <github@gone.nl> Co-authored-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
1 parent 8651906 commit 709df33

6 files changed

Lines changed: 34 additions & 12 deletions

File tree

cli-plugins/plugin/plugin.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ func RunPlugin(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager
3636
PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
3737
var err error
3838
persistentPreRunOnce.Do(func() {
39-
cmdContext := cmd.Context()
40-
// TODO: revisit and make sure this check makes sense
41-
// see: https://github.com/docker/cli/pull/4599#discussion_r1422487271
42-
if cmdContext == nil {
43-
cmdContext = context.TODO()
44-
}
45-
ctx, cancel := context.WithCancel(cmdContext)
39+
ctx, cancel := context.WithCancel(cmd.Context())
4640
cmd.SetContext(ctx)
4741
// Set up the context to cancel based on signalling via CLI socket.
4842
socket.ConnectAndWait(cancel)

cmd/docker/builder_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import (
2323
var pluginFilename = "docker-buildx"
2424

2525
func TestBuildWithBuilder(t *testing.T) {
26+
ctx, cancel := context.WithCancel(context.TODO())
27+
defer cancel()
28+
2629
testcases := []struct {
2730
name string
2831
context string
@@ -64,12 +67,16 @@ echo '{"SchemaVersion":"0.1.0","Vendor":"Docker Inc.","Version":"v0.6.3","ShortD
6467
for _, tt := range testcases {
6568
tt := tt
6669
t.Run(tt.name, func(t *testing.T) {
70+
ctx2, cancel2 := context.WithCancel(ctx)
71+
defer cancel2()
72+
6773
if tt.builder != "" {
6874
t.Setenv("BUILDX_BUILDER", tt.builder)
6975
}
7076

7177
var b bytes.Buffer
7278
dockerCli, err := command.NewDockerCli(
79+
command.WithBaseContext(ctx2),
7380
command.WithAPIClient(&fakeClient{}),
7481
command.WithInputStream(discard),
7582
command.WithCombinedStreams(&b),
@@ -126,6 +133,9 @@ func (c *fakeClient) Ping(_ context.Context) (types.Ping, error) {
126133
}
127134

128135
func TestBuildkitDisabled(t *testing.T) {
136+
ctx, cancel := context.WithCancel(context.TODO())
137+
defer cancel()
138+
129139
t.Setenv("DOCKER_BUILDKIT", "0")
130140

131141
dir := fs.NewDir(t, t.Name(),
@@ -136,6 +146,7 @@ func TestBuildkitDisabled(t *testing.T) {
136146
b := bytes.NewBuffer(nil)
137147

138148
dockerCli, err := command.NewDockerCli(
149+
command.WithBaseContext(ctx),
139150
command.WithAPIClient(&fakeClient{}),
140151
command.WithInputStream(discard),
141152
command.WithCombinedStreams(b),
@@ -163,6 +174,9 @@ func TestBuildkitDisabled(t *testing.T) {
163174
}
164175

165176
func TestBuilderBroken(t *testing.T) {
177+
ctx, cancel := context.WithCancel(context.TODO())
178+
defer cancel()
179+
166180
dir := fs.NewDir(t, t.Name(),
167181
fs.WithFile(pluginFilename, `#!/bin/sh exit 1`, fs.WithMode(0o777)),
168182
)
@@ -171,6 +185,7 @@ func TestBuilderBroken(t *testing.T) {
171185
b := bytes.NewBuffer(nil)
172186

173187
dockerCli, err := command.NewDockerCli(
188+
command.WithBaseContext(ctx),
174189
command.WithAPIClient(&fakeClient{}),
175190
command.WithInputStream(discard),
176191
command.WithCombinedStreams(b),
@@ -199,6 +214,8 @@ func TestBuilderBroken(t *testing.T) {
199214

200215
func TestBuilderBrokenEnforced(t *testing.T) {
201216
t.Setenv("DOCKER_BUILDKIT", "1")
217+
ctx, cancel := context.WithCancel(context.TODO())
218+
defer cancel()
202219

203220
dir := fs.NewDir(t, t.Name(),
204221
fs.WithFile(pluginFilename, `#!/bin/sh exit 1`, fs.WithMode(0o777)),
@@ -208,6 +225,7 @@ func TestBuilderBrokenEnforced(t *testing.T) {
208225
b := bytes.NewBuffer(nil)
209226

210227
dockerCli, err := command.NewDockerCli(
228+
command.WithBaseContext(ctx),
211229
command.WithAPIClient(&fakeClient{}),
212230
command.WithInputStream(discard),
213231
command.WithCombinedStreams(b),

cmd/docker/docker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
func main() {
3131
ctx := context.Background()
32+
3233
dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx))
3334
if err != nil {
3435
fmt.Fprintln(os.Stderr, err)
@@ -352,7 +353,7 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
352353
// We've parsed global args already, so reset args to those
353354
// which remain.
354355
cmd.SetArgs(args)
355-
err = cmd.Execute()
356+
err = cmd.ExecuteContext(ctx)
356357

357358
// If the command is being executed in an interactive terminal
358359
// and hook are enabled, run the plugin hooks.

cmd/docker/docker_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"context"
56
"io"
67
"os"
78
"testing"
@@ -15,8 +16,10 @@ import (
1516

1617
func TestClientDebugEnabled(t *testing.T) {
1718
defer debug.Disable()
19+
ctx, cancel := context.WithCancel(context.TODO())
20+
defer cancel()
1821

19-
cli, err := command.NewDockerCli()
22+
cli, err := command.NewDockerCli(command.WithBaseContext(ctx))
2023
assert.NilError(t, err)
2124
tcmd := newDockerCommand(cli)
2225
tcmd.SetFlag("debug", "true")
@@ -39,7 +42,13 @@ func runCliCommand(t *testing.T, r io.ReadCloser, w io.Writer, args ...string) e
3942
if w == nil {
4043
w = io.Discard
4144
}
42-
cli, err := command.NewDockerCli(command.WithInputStream(r), command.WithCombinedStreams(w))
45+
ctx, cancel := context.WithCancel(context.TODO())
46+
defer cancel()
47+
48+
cli, err := command.NewDockerCli(
49+
command.WithBaseContext(ctx),
50+
command.WithInputStream(r),
51+
command.WithCombinedStreams(w))
4352
assert.NilError(t, err)
4453
tcmd := newDockerCommand(cli)
4554

docs/generate/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func gen(opts *options) error {
3737
Use: "docker [OPTIONS] COMMAND [ARG...]",
3838
Short: "The base command for the Docker CLI.",
3939
}
40+
4041
clientOpts, _ := cli.SetupRootCommand(cmd)
4142
if err := dockerCLI.Initialize(clientOpts); err != nil {
4243
return err

e2e/cli-plugins/plugins/nopersistentprerun/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func main() {
1313
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
14-
cmd := &cobra.Command{
14+
return &cobra.Command{
1515
Use: "nopersistentprerun",
1616
Short: "Testing without PersistentPreRun hooks",
1717
// PersistentPreRunE: Not specified, we need to test that it works in the absence of an explicit call
@@ -25,7 +25,6 @@ func main() {
2525
return nil
2626
},
2727
}
28-
return cmd
2928
},
3029
manager.Metadata{
3130
SchemaVersion: "0.1.0",

0 commit comments

Comments
 (0)