github.com/docker/docker-ce@v17.12.1-ce-rc2+incompatible/components/cli/internal/test/cli.go (about)

     1  package test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"strings"
     9  
    10  	"github.com/docker/cli/cli/command"
    11  	"github.com/docker/cli/cli/config/configfile"
    12  	"github.com/docker/cli/cli/trust"
    13  	"github.com/docker/docker/client"
    14  	notaryclient "github.com/theupdateframework/notary/client"
    15  )
    16  
    17  type notaryClientFuncType func(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error)
    18  
    19  // FakeCli emulates the default DockerCli
    20  type FakeCli struct {
    21  	command.DockerCli
    22  	client           client.APIClient
    23  	configfile       *configfile.ConfigFile
    24  	out              *command.OutStream
    25  	outBuffer        *bytes.Buffer
    26  	err              *bytes.Buffer
    27  	in               *command.InStream
    28  	server           command.ServerInfo
    29  	notaryClientFunc notaryClientFuncType
    30  }
    31  
    32  // NewFakeCli returns a fake for the command.Cli interface
    33  func NewFakeCli(client client.APIClient) *FakeCli {
    34  	outBuffer := new(bytes.Buffer)
    35  	errBuffer := new(bytes.Buffer)
    36  	return &FakeCli{
    37  		client:     client,
    38  		out:        command.NewOutStream(outBuffer),
    39  		outBuffer:  outBuffer,
    40  		err:        errBuffer,
    41  		in:         command.NewInStream(ioutil.NopCloser(strings.NewReader(""))),
    42  		configfile: configfile.New("configfile"),
    43  	}
    44  }
    45  
    46  // SetIn sets the input of the cli to the specified ReadCloser
    47  func (c *FakeCli) SetIn(in *command.InStream) {
    48  	c.in = in
    49  }
    50  
    51  // SetErr sets the stderr stream for the cli to the specified io.Writer
    52  func (c *FakeCli) SetErr(err *bytes.Buffer) {
    53  	c.err = err
    54  }
    55  
    56  // SetConfigFile sets the "fake" config file
    57  func (c *FakeCli) SetConfigFile(configfile *configfile.ConfigFile) {
    58  	c.configfile = configfile
    59  }
    60  
    61  // Client returns a docker API client
    62  func (c *FakeCli) Client() client.APIClient {
    63  	return c.client
    64  }
    65  
    66  // Out returns the output stream (stdout) the cli should write on
    67  func (c *FakeCli) Out() *command.OutStream {
    68  	return c.out
    69  }
    70  
    71  // Err returns the output stream (stderr) the cli should write on
    72  func (c *FakeCli) Err() io.Writer {
    73  	return c.err
    74  }
    75  
    76  // In returns the input stream the cli will use
    77  func (c *FakeCli) In() *command.InStream {
    78  	return c.in
    79  }
    80  
    81  // ConfigFile returns the cli configfile object (to get client configuration)
    82  func (c *FakeCli) ConfigFile() *configfile.ConfigFile {
    83  	return c.configfile
    84  }
    85  
    86  // ServerInfo returns API server information for the server used by this client
    87  func (c *FakeCli) ServerInfo() command.ServerInfo {
    88  	return c.server
    89  }
    90  
    91  // OutBuffer returns the stdout buffer
    92  func (c *FakeCli) OutBuffer() *bytes.Buffer {
    93  	return c.outBuffer
    94  }
    95  
    96  // ErrBuffer Buffer returns the stderr buffer
    97  func (c *FakeCli) ErrBuffer() *bytes.Buffer {
    98  	return c.err
    99  }
   100  
   101  // SetNotaryClient sets the internal getter for retrieving a NotaryClient
   102  func (c *FakeCli) SetNotaryClient(notaryClientFunc notaryClientFuncType) {
   103  	c.notaryClientFunc = notaryClientFunc
   104  }
   105  
   106  // NotaryClient returns an err for testing unless defined
   107  func (c *FakeCli) NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) {
   108  	if c.notaryClientFunc != nil {
   109  		return c.notaryClientFunc(imgRefAndAuth, actions)
   110  	}
   111  	return nil, fmt.Errorf("no notary client available unless defined")
   112  }