/
docker_client_stub.go
99 lines (86 loc) · 3.96 KB
/
docker_client_stub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package dockerapi
import (
"context"
"io"
"runtime"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
)
// StubAPIClient implements the client.APIClient interface, but allows
// you to specify the behavior of each of the methods.
type StubAPIClient struct {
client.APIClient
calls map[string]int
ContainerCreateFn func(_ context.Context, config *container.Config, hostConfig *container.HostConfig, networking *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error)
ContainerInspectFn func(_ context.Context, containerID string) (types.ContainerJSON, error)
ContainerKillFn func(_ context.Context, containerID, signal string) error
ContainerRemoveFn func(_ context.Context, containerID string, options types.ContainerRemoveOptions) error
ContainerStartFn func(_ context.Context, containerID string, options types.ContainerStartOptions) error
ContainerStopFn func(_ context.Context, containerID string, timeout *time.Duration) error
ImagePullFn func(_ context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error)
EventsFn func(_ context.Context, options types.EventsOptions) (<-chan events.Message, <-chan error)
}
// NewStubAPIClient returns an initialized StubAPIClient
func NewStubAPIClient() *StubAPIClient {
return &StubAPIClient{
calls: make(map[string]int),
}
}
// If function A calls updateCountsForSelf,
// The callCount[A] value will be incremented
func (sa *StubAPIClient) called() {
pc, _, _, ok := runtime.Caller(1)
if !ok {
panic("failed to update counts")
}
// longName looks like 'github.com/docker/swarmkit/agent/exec.(*StubController).Prepare:1'
longName := runtime.FuncForPC(pc).Name()
parts := strings.Split(longName, ".")
tail := strings.Split(parts[len(parts)-1], ":")
sa.calls[tail[0]]++
}
// ContainerCreate is part of the APIClient interface
func (sa *StubAPIClient) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networking *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) {
sa.called()
return sa.ContainerCreateFn(ctx, config, hostConfig, networking, containerName)
}
// ContainerInspect is part of the APIClient interface
func (sa *StubAPIClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
sa.called()
return sa.ContainerInspectFn(ctx, containerID)
}
// ContainerKill is part of the APIClient interface
func (sa *StubAPIClient) ContainerKill(ctx context.Context, containerID, signal string) error {
sa.called()
return sa.ContainerKillFn(ctx, containerID, signal)
}
// ContainerRemove is part of the APIClient interface
func (sa *StubAPIClient) ContainerRemove(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error {
sa.called()
return sa.ContainerRemoveFn(ctx, containerID, options)
}
// ContainerStart is part of the APIClient interface
func (sa *StubAPIClient) ContainerStart(ctx context.Context, containerID string, options types.ContainerStartOptions) error {
sa.called()
return sa.ContainerStartFn(ctx, containerID, options)
}
// ContainerStop is part of the APIClient interface
func (sa *StubAPIClient) ContainerStop(ctx context.Context, containerID string, timeout *time.Duration) error {
sa.called()
return sa.ContainerStopFn(ctx, containerID, timeout)
}
// ImagePull is part of the APIClient interface
func (sa *StubAPIClient) ImagePull(ctx context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error) {
sa.called()
return sa.ImagePullFn(ctx, refStr, options)
}
// Events is part of the APIClient interface
func (sa *StubAPIClient) Events(ctx context.Context, options types.EventsOptions) (<-chan events.Message, <-chan error) {
sa.called()
return sa.EventsFn(ctx, options)
}