github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/container/client_test.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/api/types/container"
     9  	"github.com/docker/docker/api/types/network"
    10  	"github.com/docker/docker/client"
    11  	specs "github.com/opencontainers/image-spec/specs-go/v1"
    12  )
    13  
    14  type fakeClient struct {
    15  	client.Client
    16  	inspectFunc         func(string) (types.ContainerJSON, error)
    17  	execInspectFunc     func(execID string) (types.ContainerExecInspect, error)
    18  	execCreateFunc      func(container string, config types.ExecConfig) (types.IDResponse, error)
    19  	createContainerFunc func(config *container.Config,
    20  		hostConfig *container.HostConfig,
    21  		networkingConfig *network.NetworkingConfig,
    22  		platform *specs.Platform,
    23  		containerName string) (container.ContainerCreateCreatedBody, error)
    24  	containerStartFunc      func(container string, options types.ContainerStartOptions) error
    25  	imageCreateFunc         func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error)
    26  	infoFunc                func() (types.Info, error)
    27  	containerStatPathFunc   func(container, path string) (types.ContainerPathStat, error)
    28  	containerCopyFromFunc   func(container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error)
    29  	logFunc                 func(string, types.ContainerLogsOptions) (io.ReadCloser, error)
    30  	waitFunc                func(string) (<-chan container.ContainerWaitOKBody, <-chan error)
    31  	containerListFunc       func(types.ContainerListOptions) ([]types.Container, error)
    32  	containerExportFunc     func(string) (io.ReadCloser, error)
    33  	containerExecResizeFunc func(id string, options types.ResizeOptions) error
    34  	containerRemoveFunc     func(ctx context.Context, container string, options types.ContainerRemoveOptions) error
    35  	Version                 string
    36  }
    37  
    38  func (f *fakeClient) ContainerList(_ context.Context, options types.ContainerListOptions) ([]types.Container, error) {
    39  	if f.containerListFunc != nil {
    40  		return f.containerListFunc(options)
    41  	}
    42  	return []types.Container{}, nil
    43  }
    44  
    45  func (f *fakeClient) ContainerInspect(_ context.Context, containerID string) (types.ContainerJSON, error) {
    46  	if f.inspectFunc != nil {
    47  		return f.inspectFunc(containerID)
    48  	}
    49  	return types.ContainerJSON{}, nil
    50  }
    51  
    52  func (f *fakeClient) ContainerExecCreate(_ context.Context, container string, config types.ExecConfig) (types.IDResponse, error) {
    53  	if f.execCreateFunc != nil {
    54  		return f.execCreateFunc(container, config)
    55  	}
    56  	return types.IDResponse{}, nil
    57  }
    58  
    59  func (f *fakeClient) ContainerExecInspect(_ context.Context, execID string) (types.ContainerExecInspect, error) {
    60  	if f.execInspectFunc != nil {
    61  		return f.execInspectFunc(execID)
    62  	}
    63  	return types.ContainerExecInspect{}, nil
    64  }
    65  
    66  func (f *fakeClient) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
    67  	return nil
    68  }
    69  
    70  func (f *fakeClient) ContainerCreate(
    71  	_ context.Context,
    72  	config *container.Config,
    73  	hostConfig *container.HostConfig,
    74  	networkingConfig *network.NetworkingConfig,
    75  	platform *specs.Platform,
    76  	containerName string,
    77  ) (container.ContainerCreateCreatedBody, error) {
    78  	if f.createContainerFunc != nil {
    79  		return f.createContainerFunc(config, hostConfig, networkingConfig, platform, containerName)
    80  	}
    81  	return container.ContainerCreateCreatedBody{}, nil
    82  }
    83  
    84  func (f *fakeClient) ContainerRemove(ctx context.Context, container string, options types.ContainerRemoveOptions) error {
    85  	if f.containerRemoveFunc != nil {
    86  		return f.containerRemoveFunc(ctx, container, options)
    87  	}
    88  	return nil
    89  }
    90  
    91  func (f *fakeClient) ImageCreate(ctx context.Context, parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
    92  	if f.imageCreateFunc != nil {
    93  		return f.imageCreateFunc(parentReference, options)
    94  	}
    95  	return nil, nil
    96  }
    97  
    98  func (f *fakeClient) Info(_ context.Context) (types.Info, error) {
    99  	if f.infoFunc != nil {
   100  		return f.infoFunc()
   101  	}
   102  	return types.Info{}, nil
   103  }
   104  
   105  func (f *fakeClient) ContainerStatPath(_ context.Context, container, path string) (types.ContainerPathStat, error) {
   106  	if f.containerStatPathFunc != nil {
   107  		return f.containerStatPathFunc(container, path)
   108  	}
   109  	return types.ContainerPathStat{}, nil
   110  }
   111  
   112  func (f *fakeClient) CopyFromContainer(_ context.Context, container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
   113  	if f.containerCopyFromFunc != nil {
   114  		return f.containerCopyFromFunc(container, srcPath)
   115  	}
   116  	return nil, types.ContainerPathStat{}, nil
   117  }
   118  
   119  func (f *fakeClient) ContainerLogs(_ context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
   120  	if f.logFunc != nil {
   121  		return f.logFunc(container, options)
   122  	}
   123  	return nil, nil
   124  }
   125  
   126  func (f *fakeClient) ClientVersion() string {
   127  	return f.Version
   128  }
   129  
   130  func (f *fakeClient) ContainerWait(_ context.Context, container string, _ container.WaitCondition) (<-chan container.ContainerWaitOKBody, <-chan error) {
   131  	if f.waitFunc != nil {
   132  		return f.waitFunc(container)
   133  	}
   134  	return nil, nil
   135  }
   136  
   137  func (f *fakeClient) ContainerStart(_ context.Context, container string, options types.ContainerStartOptions) error {
   138  	if f.containerStartFunc != nil {
   139  		return f.containerStartFunc(container, options)
   140  	}
   141  	return nil
   142  }
   143  
   144  func (f *fakeClient) ContainerExport(_ context.Context, container string) (io.ReadCloser, error) {
   145  	if f.containerExportFunc != nil {
   146  		return f.containerExportFunc(container)
   147  	}
   148  	return nil, nil
   149  }
   150  
   151  func (f *fakeClient) ContainerExecResize(_ context.Context, id string, options types.ResizeOptions) error {
   152  	if f.containerExecResizeFunc != nil {
   153  		return f.containerExecResizeFunc(id, options)
   154  	}
   155  	return nil
   156  }