github.com/kubesphere/s2irun@v3.2.1+incompatible/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  	PushFail error
    93  
    94  	Calls []string
    95  }
    96  
    97  // NewFakeDockerClient returns a new FakeDockerClient
    98  func NewFakeDockerClient() *FakeDockerClient {
    99  	return &FakeDockerClient{
   100  		Images:     make(map[string]dockertypes.ImageInspect),
   101  		Containers: make(map[string]dockercontainer.Config),
   102  		Calls:      make([]string, 0),
   103  	}
   104  }
   105  
   106  // ImageInspectWithRaw returns the image information and its raw representation.
   107  func (d *FakeDockerClient) ImageInspectWithRaw(ctx context.Context, imageID string) (dockertypes.ImageInspect, []byte, error) {
   108  	d.Calls = append(d.Calls, "inspect_image")
   109  
   110  	if _, exists := d.Images[imageID]; exists {
   111  		return d.Images[imageID], nil, nil
   112  	}
   113  	return dockertypes.ImageInspect{}, nil, fmt.Errorf("No such image: %q", imageID)
   114  }
   115  
   116  // CopyToContainer copies content into the container filesystem.
   117  func (d *FakeDockerClient) CopyToContainer(ctx context.Context, container, path string, content io.Reader, opts dockertypes.CopyToContainerOptions) error {
   118  	d.CopyToContainerID = container
   119  	d.CopyToContainerPath = path
   120  	d.CopyToContainerContent = content
   121  	return nil
   122  }
   123  
   124  // CopyFromContainer gets the content from the container and returns it as a Reader
   125  // to manipulate it in the host. It's up to the caller to close the reader.
   126  func (d *FakeDockerClient) CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, dockertypes.ContainerPathStat, error) {
   127  	d.CopyFromContainerID = container
   128  	d.CopyFromContainerPath = srcPath
   129  	return ioutil.NopCloser(bytes.NewReader([]byte(""))), dockertypes.ContainerPathStat{}, d.CopyFromContainerErr
   130  }
   131  
   132  // ContainerWait pauses execution until a container exits.
   133  func (d *FakeDockerClient) ContainerWait(ctx context.Context, containerID string, condition dockercontainer.WaitCondition) (<-chan dockercontainer.ContainerWaitOKBody, <-chan error) {
   134  	d.WaitContainerID = containerID
   135  	resultC := make(chan dockercontainer.ContainerWaitOKBody)
   136  	errC := make(chan error, 1)
   137  
   138  	go func() {
   139  		if d.WaitContainerErr != nil {
   140  			errC <- d.WaitContainerErr
   141  			return
   142  		}
   143  
   144  		resultC <- dockercontainer.ContainerWaitOKBody{StatusCode: int64(d.WaitContainerResult)}
   145  	}()
   146  
   147  	return resultC, errC
   148  }
   149  
   150  // ContainerCommit applies changes into a container and creates a new tagged image.
   151  func (d *FakeDockerClient) ContainerCommit(ctx context.Context, container string, options dockertypes.ContainerCommitOptions) (dockertypes.IDResponse, error) {
   152  	d.ContainerCommitID = container
   153  	d.ContainerCommitOptions = options
   154  	return d.ContainerCommitResponse, d.ContainerCommitErr
   155  }
   156  
   157  // ContainerAttach attaches a connection to a container in the server.
   158  func (d *FakeDockerClient) ContainerAttach(ctx context.Context, container string, options dockertypes.ContainerAttachOptions) (dockertypes.HijackedResponse, error) {
   159  	d.Calls = append(d.Calls, "attach")
   160  	return dockertypes.HijackedResponse{Conn: FakeConn{}, Reader: bufio.NewReader(&bytes.Buffer{})}, nil
   161  }
   162  
   163  // ImageBuild sends request to the daemon to build images.
   164  func (d *FakeDockerClient) ImageBuild(ctx context.Context, buildContext io.Reader, options dockertypes.ImageBuildOptions) (dockertypes.ImageBuildResponse, error) {
   165  	d.BuildImageOpts = options
   166  	return dockertypes.ImageBuildResponse{
   167  		Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
   168  	}, d.BuildImageErr
   169  }
   170  
   171  // ContainerCreate creates a new container based in the given configuration.
   172  func (d *FakeDockerClient) ContainerCreate(ctx context.Context, config *dockercontainer.Config, hostConfig *dockercontainer.HostConfig, networkingConfig *dockernetwork.NetworkingConfig, containerName string) (dockercontainer.ContainerCreateCreatedBody, error) {
   173  	d.Calls = append(d.Calls, "create")
   174  
   175  	d.Containers[containerName] = *config
   176  	return dockercontainer.ContainerCreateCreatedBody{}, nil
   177  }
   178  
   179  // ContainerInspect returns the container information.
   180  func (d *FakeDockerClient) ContainerInspect(ctx context.Context, containerID string) (dockertypes.ContainerJSON, error) {
   181  	d.Calls = append(d.Calls, "inspect_container")
   182  	return d.WaitContainerErrInspectJSON, nil
   183  }
   184  
   185  // ContainerRemove kills and removes a container from the docker host.
   186  func (d *FakeDockerClient) ContainerRemove(ctx context.Context, containerID string, options dockertypes.ContainerRemoveOptions) error {
   187  	d.Calls = append(d.Calls, "remove")
   188  
   189  	if _, exists := d.Containers[containerID]; exists {
   190  		delete(d.Containers, containerID)
   191  		return nil
   192  	}
   193  	return errors.New("container does not exist")
   194  }
   195  
   196  // ContainerKill terminates the container process but does not remove the container from the docker host.
   197  func (d *FakeDockerClient) ContainerKill(ctx context.Context, containerID, signal string) error {
   198  	return nil
   199  }
   200  
   201  // ContainerStart sends a request to the docker daemon to start a container.
   202  func (d *FakeDockerClient) ContainerStart(ctx context.Context, containerID string, options dockertypes.ContainerStartOptions) error {
   203  	d.Calls = append(d.Calls, "start")
   204  	return nil
   205  }
   206  
   207  // ImagePull requests the docker host to pull an image from a remote registry.
   208  func (d *FakeDockerClient) ImagePull(ctx context.Context, ref string, options dockertypes.ImagePullOptions) (io.ReadCloser, error) {
   209  	d.Calls = append(d.Calls, "pull")
   210  
   211  	if d.PullFail != nil {
   212  		return nil, d.PullFail
   213  	}
   214  
   215  	return ioutil.NopCloser(bytes.NewReader([]byte{})), nil
   216  }
   217  
   218  func (d *FakeDockerClient) ImagePush(ctx context.Context, ref string, options dockertypes.ImagePushOptions) (io.ReadCloser, error) {
   219  	d.Calls = append(d.Calls, "push")
   220  	if d.PullFail != nil {
   221  		return nil, d.PullFail
   222  	}
   223  	return ioutil.NopCloser(bytes.NewReader([]byte{})), nil
   224  }
   225  
   226  // ImageRemove removes an image from the docker host.
   227  func (d *FakeDockerClient) ImageRemove(ctx context.Context, imageID string, options dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDeleteResponseItem, error) {
   228  	d.Calls = append(d.Calls, "remove_image")
   229  
   230  	if _, exists := d.Images[imageID]; exists {
   231  		delete(d.Images, imageID)
   232  		return []dockertypes.ImageDeleteResponseItem{}, nil
   233  	}
   234  	return []dockertypes.ImageDeleteResponseItem{}, errors.New("image does not exist")
   235  }
   236  
   237  // ServerVersion returns information of the docker client and server host.
   238  func (d *FakeDockerClient) ServerVersion(ctx context.Context) (dockertypes.Version, error) {
   239  	return dockertypes.Version{}, nil
   240  }