2016-12-25 16:23:35 -05:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2017-07-05 14:19:52 -04:00
|
|
|
"bytes"
|
2017-09-14 16:16:54 -04:00
|
|
|
"fmt"
|
2016-12-25 16:23:35 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-02-27 12:39:35 -05:00
|
|
|
"strings"
|
2016-12-25 16:23:35 -05:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-09-14 16:16:54 -04:00
|
|
|
"github.com/docker/cli/cli/trust"
|
2017-05-08 12:33:45 -04:00
|
|
|
"github.com/docker/docker/client"
|
2017-10-30 12:21:41 -04:00
|
|
|
notaryclient "github.com/theupdateframework/notary/client"
|
2016-12-25 16:23:35 -05:00
|
|
|
)
|
|
|
|
|
2017-09-14 16:16:54 -04:00
|
|
|
type notaryClientFuncType func(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error)
|
2017-12-26 09:40:17 -05:00
|
|
|
type clientInfoFuncType func() command.ClientInfo
|
2017-09-14 16:16:54 -04:00
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
// FakeCli emulates the default DockerCli
|
|
|
|
type FakeCli struct {
|
|
|
|
command.DockerCli
|
2017-09-14 16:16:54 -04:00
|
|
|
client client.APIClient
|
|
|
|
configfile *configfile.ConfigFile
|
|
|
|
out *command.OutStream
|
|
|
|
outBuffer *bytes.Buffer
|
|
|
|
err *bytes.Buffer
|
|
|
|
in *command.InStream
|
|
|
|
server command.ServerInfo
|
2017-12-26 09:40:17 -05:00
|
|
|
clientInfoFunc clientInfoFuncType
|
2017-09-14 16:16:54 -04:00
|
|
|
notaryClientFunc notaryClientFuncType
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 14:19:52 -04:00
|
|
|
// NewFakeCli returns a fake for the command.Cli interface
|
|
|
|
func NewFakeCli(client client.APIClient) *FakeCli {
|
|
|
|
outBuffer := new(bytes.Buffer)
|
|
|
|
errBuffer := new(bytes.Buffer)
|
2016-12-25 16:23:35 -05:00
|
|
|
return &FakeCli{
|
2017-06-21 15:13:44 -04:00
|
|
|
client: client,
|
2017-07-05 14:19:52 -04:00
|
|
|
out: command.NewOutStream(outBuffer),
|
|
|
|
outBuffer: outBuffer,
|
|
|
|
err: errBuffer,
|
2017-06-21 15:13:44 -04:00
|
|
|
in: command.NewInStream(ioutil.NopCloser(strings.NewReader(""))),
|
|
|
|
configfile: configfile.New("configfile"),
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIn sets the input of the cli to the specified ReadCloser
|
2017-03-30 20:21:14 -04:00
|
|
|
func (c *FakeCli) SetIn(in *command.InStream) {
|
2016-12-25 16:23:35 -05:00
|
|
|
c.in = in
|
|
|
|
}
|
|
|
|
|
2017-02-22 15:43:13 -05:00
|
|
|
// SetErr sets the stderr stream for the cli to the specified io.Writer
|
2017-07-05 14:19:52 -04:00
|
|
|
func (c *FakeCli) SetErr(err *bytes.Buffer) {
|
2017-02-27 12:39:35 -05:00
|
|
|
c.err = err
|
|
|
|
}
|
|
|
|
|
2017-07-05 14:34:16 -04:00
|
|
|
// SetConfigFile sets the "fake" config file
|
|
|
|
func (c *FakeCli) SetConfigFile(configfile *configfile.ConfigFile) {
|
2017-02-27 12:39:35 -05:00
|
|
|
c.configfile = configfile
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
// Client returns a docker API client
|
|
|
|
func (c *FakeCli) Client() client.APIClient {
|
|
|
|
return c.client
|
|
|
|
}
|
|
|
|
|
2017-02-27 12:39:35 -05:00
|
|
|
// Out returns the output stream (stdout) the cli should write on
|
2016-12-25 16:23:35 -05:00
|
|
|
func (c *FakeCli) Out() *command.OutStream {
|
2017-03-30 20:21:14 -04:00
|
|
|
return c.out
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
|
2017-02-27 12:39:35 -05:00
|
|
|
// Err returns the output stream (stderr) the cli should write on
|
|
|
|
func (c *FakeCli) Err() io.Writer {
|
|
|
|
return c.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// In returns the input stream the cli will use
|
2016-12-25 16:23:35 -05:00
|
|
|
func (c *FakeCli) In() *command.InStream {
|
2017-03-30 20:21:14 -04:00
|
|
|
return c.in
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
2017-02-27 12:39:35 -05:00
|
|
|
|
|
|
|
// ConfigFile returns the cli configfile object (to get client configuration)
|
|
|
|
func (c *FakeCli) ConfigFile() *configfile.ConfigFile {
|
|
|
|
return c.configfile
|
|
|
|
}
|
2017-06-21 15:13:44 -04:00
|
|
|
|
|
|
|
// ServerInfo returns API server information for the server used by this client
|
|
|
|
func (c *FakeCli) ServerInfo() command.ServerInfo {
|
|
|
|
return c.server
|
|
|
|
}
|
2017-07-05 14:19:52 -04:00
|
|
|
|
2017-12-26 09:40:17 -05:00
|
|
|
// ClientInfo returns client information
|
|
|
|
func (c *FakeCli) ClientInfo() command.ClientInfo {
|
|
|
|
if c.clientInfoFunc != nil {
|
|
|
|
return c.clientInfoFunc()
|
|
|
|
}
|
|
|
|
return c.DockerCli.ClientInfo()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetClientInfo sets the internal getter for retrieving a ClientInfo
|
|
|
|
func (c *FakeCli) SetClientInfo(clientInfoFunc clientInfoFuncType) {
|
|
|
|
c.clientInfoFunc = clientInfoFunc
|
|
|
|
}
|
|
|
|
|
2017-07-05 14:19:52 -04:00
|
|
|
// OutBuffer returns the stdout buffer
|
|
|
|
func (c *FakeCli) OutBuffer() *bytes.Buffer {
|
|
|
|
return c.outBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrBuffer Buffer returns the stderr buffer
|
|
|
|
func (c *FakeCli) ErrBuffer() *bytes.Buffer {
|
|
|
|
return c.err
|
|
|
|
}
|
2017-09-14 16:16:54 -04:00
|
|
|
|
|
|
|
// SetNotaryClient sets the internal getter for retrieving a NotaryClient
|
|
|
|
func (c *FakeCli) SetNotaryClient(notaryClientFunc notaryClientFuncType) {
|
|
|
|
c.notaryClientFunc = notaryClientFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotaryClient returns an err for testing unless defined
|
|
|
|
func (c *FakeCli) NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) {
|
|
|
|
if c.notaryClientFunc != nil {
|
|
|
|
return c.notaryClientFunc(imgRefAndAuth, actions)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no notary client available unless defined")
|
|
|
|
}
|