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