Skip to content

Commit

Permalink
chore: context usage in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alano Terblanche <[email protected]>
  • Loading branch information
Benehiko committed Apr 10, 2024
1 parent 45408b7 commit 0c8869f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cli-plugins/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 19 additions & 5 deletions cmd/docker/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(),
Expand All @@ -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),
Expand All @@ -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", "."})

Expand All @@ -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)),
)
Expand All @@ -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),
Expand All @@ -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", "."})

Expand All @@ -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)),
Expand All @@ -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),
Expand All @@ -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", "."})

Expand Down
2 changes: 1 addition & 1 deletion cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions cmd/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 0c8869f

Please sign in to comment.