github.com/boson-project/source-to-image@v1.2.0/pkg/docker/test/client.go (about)

     1  package test
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"errors"
     7  	"fmt"
     8  	"io"
     9  	"io/ioutil"
    10  	"net"
    11  	"time"
    12  
    13  	dockertypes "github.com/docker/docker/api/types"
    14  	dockercontainer "github.com/docker/docker/api/types/container"
    15  	dockernetwork "github.com/docker/docker/api/types/network"
    16  	"golang.org/x/net/context"
    17  )
    18  
    19  // FakeConn fakes a net.Conn
    20  type FakeConn struct {
    21  }
    22  
    23  // Read reads bytes
    24  func (c FakeConn) Read(b []byte) (n int, err error) {
    25  	return 0, nil
    26  }
    27  
    28  // Write writes bytes
    29  func (c FakeConn) Write(b []byte) (n int, err error) {
    30  	return 0, nil
    31  }
    32  
    33  // Close closes the connection
    34  func (c FakeConn) Close() error {
    35  	return nil
    36  }
    37  
    38  // LocalAddr returns the local address
    39  func (c FakeConn) LocalAddr() net.Addr {
    40  	ip, _ := net.ResolveIPAddr("ip4", "127.0.0.1")
    41  	return ip
    42  }
    43  
    44  // RemoteAddr returns the remote address
    45  func (c FakeConn) RemoteAddr() net.Addr {
    46  	ip, _ := net.ResolveIPAddr("ip4", "127.0.0.1")
    47  	return ip
    48  }
    49  
    50  // SetDeadline sets the deadline
    51  func (c FakeConn) SetDeadline(t time.Time) error {
    52  	return nil
    53  }
    54  
    55  // SetReadDeadline sets the read deadline
    56  func (c FakeConn) SetReadDeadline(t time.Time) error {
    57  	return nil
    58  }
    59  
    60  // SetWriteDeadline sets the write deadline
    61  func (c FakeConn) SetWriteDeadline(t time.Time) error {
    62  	return nil
    63  }
    64  
    65  // FakeDockerClient provides a Fake client for Docker testing
    66  type FakeDockerClient struct {
    67  	CopyToContainerID      string
    68  	CopyToContainerPath    string
    69  	CopyToContainerContent io.Reader
    70  
    71  	CopyFromContainerID   string
    72  	CopyFromContainerPath string
    73  	CopyFromContainerErr  error
    74  
    75  	WaitContainerID             string
    76  	WaitContainerResult         int
    77  	WaitContainerErr            error
    78  	WaitContainerErrInspectJSON dockertypes.ContainerJSON
    79  
    80  	ContainerCommitID       string
    81  	ContainerCommitOptions  dockertypes.ContainerCommitOptions
    82  	ContainerCommitResponse dockertypes.IDResponse
    83  	ContainerCommitErr      error
    84  
    85  	BuildImageOpts dockertypes.ImageBuildOptions
    86  	BuildImageErr  error
    87  	Images         map[string]dockertypes.ImageInspect
    88  
    89  	Containers map[string]dockercontainer.Config
    90  
    91  	PullFail error
    92  
    93  	Calls []string
    94  }
    95  
    96  // NewFakeDockerClient returns a new FakeDockerClient
    97  func NewFakeDockerClient() *FakeDockerClient {
    98  	return &FakeDockerClient{
    99  		Images:     make(map[string]dockertypes.ImageInspect),
   100  		Containers: make(map[string]dockercontainer.Config),
   101  		Calls:      make([]string, 0),
   102  	}
   103  }
   104  
   105  // ImageInspectWithRaw returns the image information and its raw representation.
   106  func (d *FakeDockerClient) ImageInspectWithRaw(ctx context.Context, imageID string) (dockertypes.ImageInspect, []byte, error) {
   107  	d.Calls = append(d.Calls, "inspect_image")
   108  
   109  	if _, exists := d.Images[imageID]; exists {
   110  		return d.Images[imageID], nil, nil
   111  	}
   112  	return dockertypes.ImageInspect{}, nil, fmt.Errorf("No such image: %q", imageID)
   113  }
   114  
   115  // CopyToContainer copies content into the container filesystem.
   116  func (d *FakeDockerClient) CopyToContainer(ctx context.Context, container, path string, content io.Reader, opts dockertypes.CopyToContainerOptions) error {
   117  	d.CopyToContainerID = container
   118  	d.CopyToContainerPath = path
   119  	d.CopyToContainerContent = content
   120  	return nil
   121  }
   122  
   123  // CopyFromContainer gets the content from the container and returns it as a Reader
   124  // to manipulate it in the host. It's up to the caller to close the reader.
   125  func (d *FakeDockerClient) CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, dockertypes.ContainerPathStat, error) {
   126  	d.CopyFromContainerID = container
   127  	d.CopyFromContainerPath = srcPath
   128  	return ioutil.NopCloser(bytes.NewReader([]byte(""))), dockertypes.ContainerPathStat{}, d.CopyFromContainerErr
   129  }
   130  
   131  // ContainerWait pauses execution until a container exits.
   132  func (d *FakeDockerClient) ContainerWait(ctx context.Context, containerID string, condition dockercontainer.WaitCondition) (<-chan dockercontainer.ContainerWaitOKBody, <-chan error) {
   133  	d.WaitContainerID = containerID
   134  	resultC := make(chan dockercontainer.ContainerWaitOKBody)
   135  	errC := make(chan error, 1)
   136  
   137  	go func() {
   138  		if d.WaitContainerErr != nil {
   139  			errC <- d.WaitContainerErr
   140  			return
   141  		}
   142  
   143  		resultC <- dockercontainer.ContainerWaitOKBody{StatusCode: int64(d.WaitContainerResult)}
   144  	}()
   145  
   146  	return resultC, errC
   147  }
   148  
   149  // ContainerCommit applies changes into a container and creates a new tagged image.
   150  func (d *FakeDockerClient) ContainerCommit(ctx context.Context, container string, options dockertypes.ContainerCommitOptions) (dockertypes.IDResponse, error) {
   151  	d.ContainerCommitID = container
   152  	d.ContainerCommitOptions = options
   153  	return d.ContainerCommitResponse, d.ContainerCommitErr
   154  }
   155  
   156  // ContainerAttach attaches a connection to a container in the server.
   157  func (d *FakeDockerClient) ContainerAttach(ctx context.Context, container string, options dockertypes.ContainerAttachOptions) (dockertypes.HijackedResponse, error) {
   158  	d.Calls = append(d.Calls, "attach")
   159  	return dockertypes.HijackedResponse{Conn: FakeConn{}, Reader: bufio.NewReader(&bytes.Buffer{})}, nil
   160  }
   161  
   162  // ImageBuild sends request to the daemon to build images.
   163  func (d *FakeDockerClient) ImageBuild(ctx context.Context, buildContext io.Reader, options dockertypes.ImageBuildOptions) (dockertypes.ImageBuildResponse, error) {
   164  	d.BuildImageOpts = options
   165  	return dockertypes.ImageBuildResponse{
   166  		Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
   167  	}, d.BuildImageErr
   168  }
   169  
   170  // ContainerCreate creates a new container based in the given configuration.
   171  func (d *FakeDockerClient) ContainerCreate(ctx context.Context, config *dockercontainer.Config, hostConfig *dockercontainer.HostConfig, networkingConfig *dockernetwork.NetworkingConfig, containerName string) (dockercontainer.ContainerCreateCreatedBody, error) {
   172  	d.Calls = append(d.Calls, "create")
   173  
   174  	d.Containers[containerName] = *config
   175  	return dockercontainer.ContainerCreateCreatedBody{}, nil
   176  }
   177  
   178  // ContainerInspect returns the container information.
   179  func (d *FakeDockerClient) ContainerInspect(ctx context.Context, containerID string) (dockertypes.ContainerJSON, error) {
   180  	d.Calls = append(d.Calls, "inspect_container")
   181  	return d.WaitContainerErrInspectJSON, nil
   182  }
   183  
   184  // ContainerRemove kills and removes a container from the docker host.
   185  func (d *FakeDockerClient) ContainerRemove(ctx context.Context, containerID string, options dockertypes.ContainerRemoveOptions) error {
   186  	d.Calls = append(d.Calls, "remove")
   187  
   188  	if _, exists := d.Containers[containerID]; exists {
   189  		delete(d.Containers, containerID)
   190  		return nil
   191  	}
   192  	return errors.New("container does not exist")
   193  }
   194  
   195  // ContainerKill terminates the container process but does not remove the container from the docker host.
   196  func (d *FakeDockerClient) ContainerKill(ctx context.Context, containerID, signal string) error {
   197  	return nil
   198  }
   199  
   200  // ContainerStart sends a request to the docker daemon to start a container.
   201  func (d *FakeDockerClient) ContainerStart(ctx context.Context, containerID string, options dockertypes.ContainerStartOptions) error {
   202  	d.Calls = append(d.Calls, "start")
   203  	return nil
   204  }
   205  
   206  // ImagePull requests the docker host to pull an image from a remote registry.
   207  func (d *FakeDockerClient) ImagePull(ctx context.Context, ref string, options dockertypes.ImagePullOptions) (io.ReadCloser, error) {
   208  	d.Calls = append(d.Calls, "pull")
   209  
   210  	if d.PullFail != nil {
   211  		return nil, d.PullFail
   212  	}
   213  
   214  	return ioutil.NopCloser(bytes.NewReader([]byte{})), nil
   215  }
   216  
   217  // ImageRemove removes an image from the docker host.
   218  func (d *FakeDockerClient) ImageRemove(ctx context.Context, imageID string, options dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDeleteResponseItem, error) {
   219  	d.Calls = append(d.Calls, "remove_image")
   220  
   221  	if _, exists := d.Images[imageID]; exists {
   222  		delete(d.Images, imageID)
   223  		return []dockertypes.ImageDeleteResponseItem{}, nil
   224  	}
   225  	return []dockertypes.ImageDeleteResponseItem{}, errors.New("image does not exist")
   226  }
   227  
   228  // ServerVersion returns information of the docker client and server host.
   229  func (d *FakeDockerClient) ServerVersion(ctx context.Context) (dockertypes.Version, error) {
   230  	return dockertypes.Version{}, nil
   231  }