github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/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 manifeststore "github.com/docker/cli/cli/manifest/store" 13 registryclient "github.com/docker/cli/cli/registry/client" 14 "github.com/docker/cli/cli/trust" 15 clitypes "github.com/docker/cli/types" 16 "github.com/docker/docker/client" 17 notaryclient "github.com/theupdateframework/notary/client" 18 ) 19 20 // NotaryClientFuncType defines a function that returns a fake notary client 21 type NotaryClientFuncType func(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) 22 type clientInfoFuncType func() command.ClientInfo 23 type containerizedEngineFuncType func(string) (clitypes.ContainerizedClient, error) 24 25 // FakeCli emulates the default DockerCli 26 type FakeCli struct { 27 command.DockerCli 28 client client.APIClient 29 configfile *configfile.ConfigFile 30 out *command.OutStream 31 outBuffer *bytes.Buffer 32 err *bytes.Buffer 33 in *command.InStream 34 server command.ServerInfo 35 clientInfoFunc clientInfoFuncType 36 notaryClientFunc NotaryClientFuncType 37 manifestStore manifeststore.Store 38 registryClient registryclient.RegistryClient 39 contentTrust bool 40 containerizedEngineClientFunc containerizedEngineFuncType 41 } 42 43 // NewFakeCli returns a fake for the command.Cli interface 44 func NewFakeCli(client client.APIClient, opts ...func(*FakeCli)) *FakeCli { 45 outBuffer := new(bytes.Buffer) 46 errBuffer := new(bytes.Buffer) 47 c := &FakeCli{ 48 client: client, 49 out: command.NewOutStream(outBuffer), 50 outBuffer: outBuffer, 51 err: errBuffer, 52 in: command.NewInStream(ioutil.NopCloser(strings.NewReader(""))), 53 // Use an empty string for filename so that tests don't create configfiles 54 // Set cli.ConfigFile().Filename to a tempfile to support Save. 55 configfile: configfile.New(""), 56 } 57 for _, opt := range opts { 58 opt(c) 59 } 60 return c 61 } 62 63 // SetIn sets the input of the cli to the specified ReadCloser 64 func (c *FakeCli) SetIn(in *command.InStream) { 65 c.in = in 66 } 67 68 // SetErr sets the stderr stream for the cli to the specified io.Writer 69 func (c *FakeCli) SetErr(err *bytes.Buffer) { 70 c.err = err 71 } 72 73 // SetConfigFile sets the "fake" config file 74 func (c *FakeCli) SetConfigFile(configfile *configfile.ConfigFile) { 75 c.configfile = configfile 76 } 77 78 // Client returns a docker API client 79 func (c *FakeCli) Client() client.APIClient { 80 return c.client 81 } 82 83 // Out returns the output stream (stdout) the cli should write on 84 func (c *FakeCli) Out() *command.OutStream { 85 return c.out 86 } 87 88 // Err returns the output stream (stderr) the cli should write on 89 func (c *FakeCli) Err() io.Writer { 90 return c.err 91 } 92 93 // In returns the input stream the cli will use 94 func (c *FakeCli) In() *command.InStream { 95 return c.in 96 } 97 98 // ConfigFile returns the cli configfile object (to get client configuration) 99 func (c *FakeCli) ConfigFile() *configfile.ConfigFile { 100 return c.configfile 101 } 102 103 // ServerInfo returns API server information for the server used by this client 104 func (c *FakeCli) ServerInfo() command.ServerInfo { 105 return c.server 106 } 107 108 // ClientInfo returns client information 109 func (c *FakeCli) ClientInfo() command.ClientInfo { 110 if c.clientInfoFunc != nil { 111 return c.clientInfoFunc() 112 } 113 return c.DockerCli.ClientInfo() 114 } 115 116 // SetClientInfo sets the internal getter for retrieving a ClientInfo 117 func (c *FakeCli) SetClientInfo(clientInfoFunc clientInfoFuncType) { 118 c.clientInfoFunc = clientInfoFunc 119 } 120 121 // OutBuffer returns the stdout buffer 122 func (c *FakeCli) OutBuffer() *bytes.Buffer { 123 return c.outBuffer 124 } 125 126 // ErrBuffer Buffer returns the stderr buffer 127 func (c *FakeCli) ErrBuffer() *bytes.Buffer { 128 return c.err 129 } 130 131 // SetNotaryClient sets the internal getter for retrieving a NotaryClient 132 func (c *FakeCli) SetNotaryClient(notaryClientFunc NotaryClientFuncType) { 133 c.notaryClientFunc = notaryClientFunc 134 } 135 136 // NotaryClient returns an err for testing unless defined 137 func (c *FakeCli) NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) { 138 if c.notaryClientFunc != nil { 139 return c.notaryClientFunc(imgRefAndAuth, actions) 140 } 141 return nil, fmt.Errorf("no notary client available unless defined") 142 } 143 144 // ManifestStore returns a fake store used for testing 145 func (c *FakeCli) ManifestStore() manifeststore.Store { 146 return c.manifestStore 147 } 148 149 // RegistryClient returns a fake client for testing 150 func (c *FakeCli) RegistryClient(insecure bool) registryclient.RegistryClient { 151 return c.registryClient 152 } 153 154 // SetManifestStore on the fake cli 155 func (c *FakeCli) SetManifestStore(store manifeststore.Store) { 156 c.manifestStore = store 157 } 158 159 // SetRegistryClient on the fake cli 160 func (c *FakeCli) SetRegistryClient(client registryclient.RegistryClient) { 161 c.registryClient = client 162 } 163 164 // ContentTrustEnabled on the fake cli 165 func (c *FakeCli) ContentTrustEnabled() bool { 166 return c.contentTrust 167 } 168 169 // EnableContentTrust on the fake cli 170 func EnableContentTrust(c *FakeCli) { 171 c.contentTrust = true 172 } 173 174 // NewContainerizedEngineClient returns a containerized engine client 175 func (c *FakeCli) NewContainerizedEngineClient(sockPath string) (clitypes.ContainerizedClient, error) { 176 if c.containerizedEngineClientFunc != nil { 177 return c.containerizedEngineClientFunc(sockPath) 178 } 179 return nil, fmt.Errorf("no containerized engine client available unless defined") 180 } 181 182 // SetContainerizedEngineClient on the fake cli 183 func (c *FakeCli) SetContainerizedEngineClient(containerizedEngineClientFunc containerizedEngineFuncType) { 184 c.containerizedEngineClientFunc = containerizedEngineClientFunc 185 }