github.com/ali-iotechsys/cli@v20.10.0+incompatible/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/context/docker" 13 "github.com/docker/cli/cli/context/store" 14 manifeststore "github.com/docker/cli/cli/manifest/store" 15 registryclient "github.com/docker/cli/cli/registry/client" 16 "github.com/docker/cli/cli/streams" 17 "github.com/docker/cli/cli/trust" 18 "github.com/docker/docker/client" 19 notaryclient "github.com/theupdateframework/notary/client" 20 ) 21 22 // NotaryClientFuncType defines a function that returns a fake notary client 23 type NotaryClientFuncType func(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) 24 type clientInfoFuncType func() command.ClientInfo 25 26 // FakeCli emulates the default DockerCli 27 type FakeCli struct { 28 command.DockerCli 29 client client.APIClient 30 configfile *configfile.ConfigFile 31 out *streams.Out 32 outBuffer *bytes.Buffer 33 err *bytes.Buffer 34 in *streams.In 35 server command.ServerInfo 36 clientInfoFunc clientInfoFuncType 37 notaryClientFunc NotaryClientFuncType 38 manifestStore manifeststore.Store 39 registryClient registryclient.RegistryClient 40 contentTrust bool 41 contextStore store.Store 42 currentContext string 43 dockerEndpoint docker.Endpoint 44 } 45 46 // NewFakeCli returns a fake for the command.Cli interface 47 func NewFakeCli(client client.APIClient, opts ...func(*FakeCli)) *FakeCli { 48 outBuffer := new(bytes.Buffer) 49 errBuffer := new(bytes.Buffer) 50 c := &FakeCli{ 51 client: client, 52 out: streams.NewOut(outBuffer), 53 outBuffer: outBuffer, 54 err: errBuffer, 55 in: streams.NewIn(ioutil.NopCloser(strings.NewReader(""))), 56 // Use an empty string for filename so that tests don't create configfiles 57 // Set cli.ConfigFile().Filename to a tempfile to support Save. 58 configfile: configfile.New(""), 59 } 60 for _, opt := range opts { 61 opt(c) 62 } 63 return c 64 } 65 66 // SetIn sets the input of the cli to the specified ReadCloser 67 func (c *FakeCli) SetIn(in *streams.In) { 68 c.in = in 69 } 70 71 // SetErr sets the stderr stream for the cli to the specified io.Writer 72 func (c *FakeCli) SetErr(err *bytes.Buffer) { 73 c.err = err 74 } 75 76 // SetOut sets the stdout stream for the cli to the specified io.Writer 77 func (c *FakeCli) SetOut(out *streams.Out) { 78 c.out = out 79 } 80 81 // SetConfigFile sets the "fake" config file 82 func (c *FakeCli) SetConfigFile(configfile *configfile.ConfigFile) { 83 c.configfile = configfile 84 } 85 86 // SetContextStore sets the "fake" context store 87 func (c *FakeCli) SetContextStore(store store.Store) { 88 c.contextStore = store 89 } 90 91 // SetCurrentContext sets the "fake" current context 92 func (c *FakeCli) SetCurrentContext(name string) { 93 c.currentContext = name 94 } 95 96 // SetDockerEndpoint sets the "fake" docker endpoint 97 func (c *FakeCli) SetDockerEndpoint(ep docker.Endpoint) { 98 c.dockerEndpoint = ep 99 } 100 101 // Client returns a docker API client 102 func (c *FakeCli) Client() client.APIClient { 103 return c.client 104 } 105 106 // Out returns the output stream (stdout) the cli should write on 107 func (c *FakeCli) Out() *streams.Out { 108 return c.out 109 } 110 111 // Err returns the output stream (stderr) the cli should write on 112 func (c *FakeCli) Err() io.Writer { 113 return c.err 114 } 115 116 // In returns the input stream the cli will use 117 func (c *FakeCli) In() *streams.In { 118 return c.in 119 } 120 121 // ConfigFile returns the cli configfile object (to get client configuration) 122 func (c *FakeCli) ConfigFile() *configfile.ConfigFile { 123 return c.configfile 124 } 125 126 // ContextStore returns the cli context store 127 func (c *FakeCli) ContextStore() store.Store { 128 return c.contextStore 129 } 130 131 // CurrentContext returns the cli context 132 func (c *FakeCli) CurrentContext() string { 133 return c.currentContext 134 } 135 136 // DockerEndpoint returns the current DockerEndpoint 137 func (c *FakeCli) DockerEndpoint() docker.Endpoint { 138 return c.dockerEndpoint 139 } 140 141 // ServerInfo returns API server information for the server used by this client 142 func (c *FakeCli) ServerInfo() command.ServerInfo { 143 return c.server 144 } 145 146 // ClientInfo returns client information 147 func (c *FakeCli) ClientInfo() command.ClientInfo { 148 if c.clientInfoFunc != nil { 149 return c.clientInfoFunc() 150 } 151 return c.DockerCli.ClientInfo() 152 } 153 154 // SetClientInfo sets the internal getter for retrieving a ClientInfo 155 func (c *FakeCli) SetClientInfo(clientInfoFunc clientInfoFuncType) { 156 c.clientInfoFunc = clientInfoFunc 157 } 158 159 // OutBuffer returns the stdout buffer 160 func (c *FakeCli) OutBuffer() *bytes.Buffer { 161 return c.outBuffer 162 } 163 164 // ErrBuffer Buffer returns the stderr buffer 165 func (c *FakeCli) ErrBuffer() *bytes.Buffer { 166 return c.err 167 } 168 169 // ResetOutputBuffers resets the .OutBuffer() and.ErrBuffer() back to empty 170 func (c *FakeCli) ResetOutputBuffers() { 171 c.outBuffer.Reset() 172 c.err.Reset() 173 } 174 175 // SetNotaryClient sets the internal getter for retrieving a NotaryClient 176 func (c *FakeCli) SetNotaryClient(notaryClientFunc NotaryClientFuncType) { 177 c.notaryClientFunc = notaryClientFunc 178 } 179 180 // NotaryClient returns an err for testing unless defined 181 func (c *FakeCli) NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) { 182 if c.notaryClientFunc != nil { 183 return c.notaryClientFunc(imgRefAndAuth, actions) 184 } 185 return nil, fmt.Errorf("no notary client available unless defined") 186 } 187 188 // ManifestStore returns a fake store used for testing 189 func (c *FakeCli) ManifestStore() manifeststore.Store { 190 return c.manifestStore 191 } 192 193 // RegistryClient returns a fake client for testing 194 func (c *FakeCli) RegistryClient(insecure bool) registryclient.RegistryClient { 195 return c.registryClient 196 } 197 198 // SetManifestStore on the fake cli 199 func (c *FakeCli) SetManifestStore(store manifeststore.Store) { 200 c.manifestStore = store 201 } 202 203 // SetRegistryClient on the fake cli 204 func (c *FakeCli) SetRegistryClient(client registryclient.RegistryClient) { 205 c.registryClient = client 206 } 207 208 // ContentTrustEnabled on the fake cli 209 func (c *FakeCli) ContentTrustEnabled() bool { 210 return c.contentTrust 211 } 212 213 // EnableContentTrust on the fake cli 214 func EnableContentTrust(c *FakeCli) { 215 c.contentTrust = true 216 } 217 218 // StackOrchestrator return the selected stack orchestrator 219 func (c *FakeCli) StackOrchestrator(flagValue string) (command.Orchestrator, error) { 220 configOrchestrator := "" 221 if c.configfile != nil { 222 configOrchestrator = c.configfile.StackOrchestrator 223 } 224 ctxOrchestrator := "" 225 if c.currentContext != "" && c.contextStore != nil { 226 meta, err := c.contextStore.GetMetadata(c.currentContext) 227 if err != nil { 228 return "", err 229 } 230 context, err := command.GetDockerContext(meta) 231 if err != nil { 232 return "", err 233 } 234 ctxOrchestrator = string(context.StackOrchestrator) 235 } 236 return command.GetStackOrchestrator(flagValue, ctxOrchestrator, configOrchestrator, c.err) 237 }