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