...
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
22 type NotaryClientFuncType func(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error)
23
24
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
44 func NewFakeCli(apiClient client.APIClient, opts ...func(*FakeCli)) *FakeCli {
45 outBuffer := new(bytes.Buffer)
46 errBuffer := new(bytes.Buffer)
47 c := &FakeCli{
48 client: apiClient,
49 out: streams.NewOut(outBuffer),
50 outBuffer: outBuffer,
51 err: errBuffer,
52 in: streams.NewIn(io.NopCloser(strings.NewReader(""))),
53
54
55 configfile: configfile.New(""),
56 currentContext: command.DefaultContextName,
57 }
58 for _, opt := range opts {
59 opt(c)
60 }
61 return c
62 }
63
64
65 func (c *FakeCli) SetIn(in *streams.In) {
66 c.in = in
67 }
68
69
70 func (c *FakeCli) SetErr(err *bytes.Buffer) {
71 c.err = err
72 }
73
74
75 func (c *FakeCli) SetOut(out *streams.Out) {
76 c.out = out
77 }
78
79
80 func (c *FakeCli) SetConfigFile(configFile *configfile.ConfigFile) {
81 c.configfile = configFile
82 }
83
84
85 func (c *FakeCli) SetContextStore(contextStore store.Store) {
86 c.contextStore = contextStore
87 }
88
89
90 func (c *FakeCli) SetCurrentContext(name string) {
91 c.currentContext = name
92 }
93
94
95 func (c *FakeCli) SetDockerEndpoint(ep docker.Endpoint) {
96 c.dockerEndpoint = ep
97 }
98
99
100 func (c *FakeCli) Client() client.APIClient {
101 return c.client
102 }
103
104
105 func (c *FakeCli) CurrentVersion() string {
106 return c.DefaultVersion()
107 }
108
109
110 func (c *FakeCli) Out() *streams.Out {
111 return c.out
112 }
113
114
115 func (c *FakeCli) Err() io.Writer {
116 return c.err
117 }
118
119
120 func (c *FakeCli) In() *streams.In {
121 return c.in
122 }
123
124
125 func (c *FakeCli) ConfigFile() *configfile.ConfigFile {
126 return c.configfile
127 }
128
129
130 func (c *FakeCli) ContextStore() store.Store {
131 return c.contextStore
132 }
133
134
135 func (c *FakeCli) CurrentContext() string {
136 return c.currentContext
137 }
138
139
140 func (c *FakeCli) DockerEndpoint() docker.Endpoint {
141 return c.dockerEndpoint
142 }
143
144
145 func (c *FakeCli) ServerInfo() command.ServerInfo {
146 return c.server
147 }
148
149
150 func (c *FakeCli) OutBuffer() *bytes.Buffer {
151 return c.outBuffer
152 }
153
154
155 func (c *FakeCli) ErrBuffer() *bytes.Buffer {
156 return c.err
157 }
158
159
160 func (c *FakeCli) ResetOutputBuffers() {
161 c.outBuffer.Reset()
162 c.err.Reset()
163 }
164
165
166 func (c *FakeCli) SetNotaryClient(notaryClientFunc NotaryClientFuncType) {
167 c.notaryClientFunc = notaryClientFunc
168 }
169
170
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
179 func (c *FakeCli) ManifestStore() manifeststore.Store {
180 return c.manifestStore
181 }
182
183
184 func (c *FakeCli) RegistryClient(bool) registryclient.RegistryClient {
185 return c.registryClient
186 }
187
188
189 func (c *FakeCli) SetManifestStore(manifestStore manifeststore.Store) {
190 c.manifestStore = manifestStore
191 }
192
193
194 func (c *FakeCli) SetRegistryClient(registryClient registryclient.RegistryClient) {
195 c.registryClient = registryClient
196 }
197
198
199 func (c *FakeCli) ContentTrustEnabled() bool {
200 return c.contentTrust
201 }
202
203
204 func EnableContentTrust(c *FakeCli) {
205 c.contentTrust = true
206 }
207
208
209 func (c *FakeCli) BuildKitEnabled() (bool, error) {
210 return true, nil
211 }
212
View as plain text