github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/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 24 // FakeCli emulates the default DockerCli 25 type FakeCli struct { 26 command.DockerCli 27 client client.APIClient 28 configfile *configfile.ConfigFile 29 out *streams.Out 30 outBuffer *bytes.Buffer 31 err *bytes.Buffer 32 in *streams.In 33 server command.ServerInfo 34 notaryClientFunc NotaryClientFuncType 35 manifestStore manifeststore.Store 36 registryClient registryclient.RegistryClient 37 contentTrust bool 38 contextStore store.Store 39 currentContext string 40 dockerEndpoint docker.Endpoint 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: streams.NewOut(outBuffer), 50 outBuffer: outBuffer, 51 err: errBuffer, 52 in: streams.NewIn(io.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 currentContext: command.DefaultContextName, 57 } 58 for _, opt := range opts { 59 opt(c) 60 } 61 return c 62 } 63 64 // SetIn sets the input of the cli to the specified ReadCloser 65 func (c *FakeCli) SetIn(in *streams.In) { 66 c.in = in 67 } 68 69 // SetErr sets the stderr stream for the cli to the specified io.Writer 70 func (c *FakeCli) SetErr(err *bytes.Buffer) { 71 c.err = err 72 } 73 74 // SetOut sets the stdout stream for the cli to the specified io.Writer 75 func (c *FakeCli) SetOut(out *streams.Out) { 76 c.out = out 77 } 78 79 // SetConfigFile sets the "fake" config file 80 func (c *FakeCli) SetConfigFile(configfile *configfile.ConfigFile) { 81 c.configfile = configfile 82 } 83 84 // SetContextStore sets the "fake" context store 85 func (c *FakeCli) SetContextStore(store store.Store) { 86 c.contextStore = store 87 } 88 89 // SetCurrentContext sets the "fake" current context 90 func (c *FakeCli) SetCurrentContext(name string) { 91 c.currentContext = name 92 } 93 94 // SetDockerEndpoint sets the "fake" docker endpoint 95 func (c *FakeCli) SetDockerEndpoint(ep docker.Endpoint) { 96 c.dockerEndpoint = ep 97 } 98 99 // Client returns a docker API client 100 func (c *FakeCli) Client() client.APIClient { 101 return c.client 102 } 103 104 // CurrentVersion returns the API version used by FakeCli. 105 func (c *FakeCli) CurrentVersion() string { 106 return c.DefaultVersion() 107 } 108 109 // Out returns the output stream (stdout) the cli should write on 110 func (c *FakeCli) Out() *streams.Out { 111 return c.out 112 } 113 114 // Err returns the output stream (stderr) the cli should write on 115 func (c *FakeCli) Err() io.Writer { 116 return c.err 117 } 118 119 // In returns the input stream the cli will use 120 func (c *FakeCli) In() *streams.In { 121 return c.in 122 } 123 124 // ConfigFile returns the cli configfile object (to get client configuration) 125 func (c *FakeCli) ConfigFile() *configfile.ConfigFile { 126 return c.configfile 127 } 128 129 // ContextStore returns the cli context store 130 func (c *FakeCli) ContextStore() store.Store { 131 return c.contextStore 132 } 133 134 // CurrentContext returns the cli context 135 func (c *FakeCli) CurrentContext() string { 136 return c.currentContext 137 } 138 139 // DockerEndpoint returns the current DockerEndpoint 140 func (c *FakeCli) DockerEndpoint() docker.Endpoint { 141 return c.dockerEndpoint 142 } 143 144 // ServerInfo returns API server information for the server used by this client 145 func (c *FakeCli) ServerInfo() command.ServerInfo { 146 return c.server 147 } 148 149 // OutBuffer returns the stdout buffer 150 func (c *FakeCli) OutBuffer() *bytes.Buffer { 151 return c.outBuffer 152 } 153 154 // ErrBuffer Buffer returns the stderr buffer 155 func (c *FakeCli) ErrBuffer() *bytes.Buffer { 156 return c.err 157 } 158 159 // ResetOutputBuffers resets the .OutBuffer() and.ErrBuffer() back to empty 160 func (c *FakeCli) ResetOutputBuffers() { 161 c.outBuffer.Reset() 162 c.err.Reset() 163 } 164 165 // SetNotaryClient sets the internal getter for retrieving a NotaryClient 166 func (c *FakeCli) SetNotaryClient(notaryClientFunc NotaryClientFuncType) { 167 c.notaryClientFunc = notaryClientFunc 168 } 169 170 // NotaryClient returns an err for testing unless defined 171 func (c *FakeCli) NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) { 172 if c.notaryClientFunc != nil { 173 return c.notaryClientFunc(imgRefAndAuth, actions) 174 } 175 return nil, fmt.Errorf("no notary client available unless defined") 176 } 177 178 // ManifestStore returns a fake store used for testing 179 func (c *FakeCli) ManifestStore() manifeststore.Store { 180 return c.manifestStore 181 } 182 183 // RegistryClient returns a fake client for testing 184 func (c *FakeCli) RegistryClient(insecure bool) registryclient.RegistryClient { 185 return c.registryClient 186 } 187 188 // SetManifestStore on the fake cli 189 func (c *FakeCli) SetManifestStore(store manifeststore.Store) { 190 c.manifestStore = store 191 } 192 193 // SetRegistryClient on the fake cli 194 func (c *FakeCli) SetRegistryClient(client registryclient.RegistryClient) { 195 c.registryClient = client 196 } 197 198 // ContentTrustEnabled on the fake cli 199 func (c *FakeCli) ContentTrustEnabled() bool { 200 return c.contentTrust 201 } 202 203 // EnableContentTrust on the fake cli 204 func EnableContentTrust(c *FakeCli) { 205 c.contentTrust = true 206 } 207 208 // BuildKitEnabled on the fake cli 209 func (c *FakeCli) BuildKitEnabled() (bool, error) { 210 return true, nil 211 }