Skip to content

Commit e67927c

Browse files
committed
test: various prune prompt terminations
Signed-off-by: Alano Terblanche <[email protected]>
1 parent c532931 commit e67927c

File tree

20 files changed

+363
-41
lines changed

20 files changed

+363
-41
lines changed

cli/command/builder/client_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package builder
2+
3+
import (
4+
"context"
5+
6+
"github.com/docker/docker/api/types"
7+
"github.com/docker/docker/client"
8+
)
9+
10+
type fakeClient struct {
11+
client.Client
12+
builderPruneFunc func(ctx context.Context, opts types.BuildCachePruneOptions) (*types.BuildCachePruneReport, error)
13+
}
14+
15+
func (c *fakeClient) BuildCachePrune(ctx context.Context, opts types.BuildCachePruneOptions) (*types.BuildCachePruneReport, error) {
16+
if c.builderPruneFunc != nil {
17+
return c.builderPruneFunc(ctx, opts)
18+
}
19+
return nil, nil
20+
}

cli/command/builder/prune_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package builder
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
"time"
8+
9+
"github.com/docker/cli/internal/test"
10+
"github.com/docker/docker/api/types"
11+
)
12+
13+
func TestBuilderPromptTermination(t *testing.T) {
14+
cli := test.NewFakeCli(&fakeClient{
15+
builderPruneFunc: func(ctx context.Context, opts types.BuildCachePruneOptions) (*types.BuildCachePruneReport, error) {
16+
return nil, errors.New("fakeClient builderPruneFunc should not be called")
17+
},
18+
})
19+
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
20+
cli.SetIn(test.NewFakeStreamIn(time.Second * 2))
21+
cmd := NewPruneCommand(cli)
22+
test.TerminatePrompt(t, cmd, cli, nil)
23+
}

cli/command/container/client_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/docker/docker/api/types"
88
"github.com/docker/docker/api/types/container"
9+
"github.com/docker/docker/api/types/filters"
910
"github.com/docker/docker/api/types/image"
1011
"github.com/docker/docker/api/types/network"
1112
"github.com/docker/docker/api/types/system"
@@ -35,6 +36,7 @@ type fakeClient struct {
3536
containerExecResizeFunc func(id string, options container.ResizeOptions) error
3637
containerRemoveFunc func(ctx context.Context, containerID string, options container.RemoveOptions) error
3738
containerKillFunc func(ctx context.Context, containerID, signal string) error
39+
containerPruneFunc func(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error)
3840
Version string
3941
}
4042

@@ -164,3 +166,10 @@ func (f *fakeClient) ContainerKill(ctx context.Context, containerID, signal stri
164166
}
165167
return nil
166168
}
169+
170+
func (f *fakeClient) ContainersPrune(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error) {
171+
if f.containerPruneFunc != nil {
172+
return f.containerPruneFunc(ctx, pruneFilters)
173+
}
174+
return types.ContainersPruneReport{}, nil
175+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package container
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/docker/cli/internal/test"
9+
"github.com/docker/docker/api/types"
10+
"github.com/docker/docker/api/types/filters"
11+
"github.com/pkg/errors"
12+
)
13+
14+
func TestContainerPrunePromptTermination(t *testing.T) {
15+
cli := test.NewFakeCli(&fakeClient{
16+
containerPruneFunc: func(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error) {
17+
return types.ContainersPruneReport{}, errors.New("fakeClient containerPruneFunc should not be called")
18+
},
19+
})
20+
21+
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
22+
cli.SetIn(test.NewFakeStreamIn(time.Second * 2))
23+
cmd := NewPruneCommand(cli)
24+
test.TerminatePrompt(t, cmd, cli, nil)
25+
}

cli/command/image/prune_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"testing"
7+
"time"
78

89
"github.com/docker/cli/internal/test"
910
"github.com/docker/docker/api/types"
@@ -101,3 +102,15 @@ func TestNewPruneCommandSuccess(t *testing.T) {
101102
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("prune-command-success.%s.golden", tc.name))
102103
}
103104
}
105+
106+
func TestPrunePromptTermination(t *testing.T) {
107+
cli := test.NewFakeCli(&fakeClient{
108+
imagesPruneFunc: func(pruneFilter filters.Args) (types.ImagesPruneReport, error) {
109+
return types.ImagesPruneReport{}, errors.New("fakeClient imagesPruneFunc should not be called")
110+
},
111+
})
112+
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
113+
cli.SetIn(test.NewFakeStreamIn(time.Second * 2))
114+
cmd := NewPruneCommand(cli)
115+
test.TerminatePrompt(t, cmd, cli, nil)
116+
}

cli/command/network/client_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/docker/docker/api/types"
7+
"github.com/docker/docker/api/types/filters"
78
"github.com/docker/docker/api/types/network"
89
"github.com/docker/docker/client"
910
)
@@ -15,6 +16,8 @@ type fakeClient struct {
1516
networkDisconnectFunc func(ctx context.Context, networkID, container string, force bool) error
1617
networkRemoveFunc func(ctx context.Context, networkID string) error
1718
networkListFunc func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error)
19+
networkPruneFunc func(ctx context.Context, pruneFilters filters.Args) (types.NetworksPruneReport, error)
20+
networkInspectFunc func(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, []byte, error)
1821
}
1922

2023
func (c *fakeClient) NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error) {
@@ -52,6 +55,16 @@ func (c *fakeClient) NetworkRemove(ctx context.Context, networkID string) error
5255
return nil
5356
}
5457

55-
func (c *fakeClient) NetworkInspectWithRaw(context.Context, string, types.NetworkInspectOptions) (types.NetworkResource, []byte, error) {
58+
func (c *fakeClient) NetworkInspectWithRaw(ctx context.Context, network string, opts types.NetworkInspectOptions) (types.NetworkResource, []byte, error) {
59+
if c.networkInspectFunc != nil {
60+
return c.networkInspectFunc(ctx, network, opts)
61+
}
5662
return types.NetworkResource{}, nil, nil
5763
}
64+
65+
func (c *fakeClient) NetworksPrune(ctx context.Context, pruneFilter filters.Args) (types.NetworksPruneReport, error) {
66+
if c.networkPruneFunc != nil {
67+
return c.networkPruneFunc(ctx, pruneFilter)
68+
}
69+
return types.NetworksPruneReport{}, nil
70+
}

cli/command/network/prune_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package network
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/docker/cli/internal/test"
9+
"github.com/docker/docker/api/types"
10+
"github.com/docker/docker/api/types/filters"
11+
"github.com/pkg/errors"
12+
)
13+
14+
func TestNetworkPrunePromptTermination(t *testing.T) {
15+
cli := test.NewFakeCli(&fakeClient{
16+
networkPruneFunc: func(ctx context.Context, pruneFilters filters.Args) (types.NetworksPruneReport, error) {
17+
return types.NetworksPruneReport{}, errors.New("fakeClient networkPruneFunc should not be called")
18+
},
19+
})
20+
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
21+
cli.SetIn(test.NewFakeStreamIn(time.Second * 2))
22+
cmd := NewPruneCommand(cli)
23+
test.TerminatePrompt(t, cmd, cli, nil)
24+
}

cli/command/network/remove_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"io"
66
"testing"
7+
"time"
78

89
"github.com/docker/cli/internal/test"
10+
"github.com/docker/docker/api/types"
911
"github.com/docker/docker/errdefs"
1012
"github.com/pkg/errors"
1113
"gotest.tools/v3/assert"
@@ -94,3 +96,23 @@ func TestNetworkRemoveForce(t *testing.T) {
9496
})
9597
}
9698
}
99+
100+
func TestNetworkRemovePromptTermination(t *testing.T) {
101+
cli := test.NewFakeCli(&fakeClient{
102+
networkRemoveFunc: func(ctx context.Context, networkID string) error {
103+
return errors.New("fakeClient networkRemoveFunc should not be called")
104+
},
105+
networkInspectFunc: func(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, []byte, error) {
106+
return types.NetworkResource{
107+
ID: "existing-network",
108+
Name: "existing-network",
109+
Ingress: true,
110+
}, nil, nil
111+
},
112+
})
113+
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
114+
cli.SetIn(test.NewFakeStreamIn(time.Second * 2))
115+
cmd := newRemoveCommand(cli)
116+
cmd.SetArgs([]string{"existing-network"})
117+
test.TerminatePrompt(t, cmd, cli, nil)
118+
}

cli/command/plugin/client_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type fakeClient struct {
1919
pluginInstallFunc func(name string, options types.PluginInstallOptions) (io.ReadCloser, error)
2020
pluginListFunc func(filter filters.Args) (types.PluginsListResponse, error)
2121
pluginInspectFunc func(name string) (*types.Plugin, []byte, error)
22+
pluginUpgradeFunc func(name string, options types.PluginInstallOptions) (io.ReadCloser, error)
2223
}
2324

2425
func (c *fakeClient) PluginCreate(_ context.Context, createContext io.Reader, createOptions types.PluginCreateOptions) error {
@@ -75,3 +76,10 @@ func (c *fakeClient) PluginInspectWithRaw(_ context.Context, name string) (*type
7576
func (c *fakeClient) Info(context.Context) (system.Info, error) {
7677
return system.Info{}, nil
7778
}
79+
80+
func (c *fakeClient) PluginUpgrade(ctx context.Context, name string, options types.PluginInstallOptions) (io.ReadCloser, error) {
81+
if c.pluginUpgradeFunc != nil {
82+
return c.pluginUpgradeFunc(name, options)
83+
}
84+
return nil, nil
85+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Upgrading plugin foo/bar from localhost:5000/foo/bar:latest to localhost:5000/foo/bar:v1.0.0
2+
Plugin images do not match, are you sure? [y/N]

0 commit comments

Comments
 (0)