This repository was archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmock_test.go
More file actions
285 lines (258 loc) · 8.14 KB
/
mock_test.go
File metadata and controls
285 lines (258 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) 2018 SUSE LLC. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
)
type mockClient struct {
createFail bool
createWarnings bool
removeFail bool
startFail bool
waitSleep time.Duration
waitFail bool
commandFail bool
commandExit int
listFail bool
listEmpty bool
listReturnOneImage bool
logFail bool
lastCmd []string
killFail bool
commitFail bool
inspectFail bool
zypperBadVersion bool
zypperGoodVersion bool
suppressLog bool
}
func (mc *mockClient) ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error) {
if mc.listFail {
return nil, errors.New("List Failed")
}
if mc.listEmpty {
return nil, nil
}
// Let's return some more or less realistic images.
if mc.listReturnOneImage {
return []types.ImageSummary{
types.ImageSummary{
ID: "2",
ParentID: "0", // Not used
Size: 254515796, // 254.5 MB
VirtualSize: 254515796,
RepoTags: []string{"opensuse:13.2"},
Created: time.Now().UnixNano(),
},
}, nil
}
return []types.ImageSummary{
types.ImageSummary{
ID: "1",
ParentID: "0", // Not used
Size: 254515796, // 254.5 MB
VirtualSize: 254515796,
RepoTags: []string{"opensuse:latest", "opensuse:tag"},
Created: time.Now().UnixNano(),
},
types.ImageSummary{
ID: "2",
ParentID: "0", // Not used
Size: 254515796, // 254.5 MB
VirtualSize: 254515796,
RepoTags: []string{"opensuse:13.2"},
Created: time.Now().UnixNano(),
},
types.ImageSummary{
ID: "3",
ParentID: "0", // Not used
Size: 254515796, // 254.5 MB
VirtualSize: 254515796,
RepoTags: []string{"ubuntu:latest"},
Created: time.Now().UnixNano(),
},
types.ImageSummary{
ID: "4",
ParentID: "0", // Not used
Size: 254515796, // 254.5 MB
VirtualSize: 254515796,
RepoTags: []string{}, // Invalid image
Created: time.Now().UnixNano(),
},
types.ImageSummary{
ID: "5",
ParentID: "0", // Not used
Size: 254515796, // 254.5 MB
VirtualSize: 254515796,
RepoTags: []string{"busybox:latest"}, // Invalid image
Created: time.Now().UnixNano(),
},
}, nil
}
func (mc *mockClient) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) {
var (
warnings []string
name string
)
if mc.createFail {
return container.ContainerCreateCreatedBody{}, errors.New("Create failed")
}
if mc.createWarnings {
warnings = []string{"warning"}
}
// workaround: TestHasZypperMinVersion{Fail,Success} breaks TestSetupLoggerHome
// due to this string being printed to the log file.
if !mc.suppressLog {
name = fmt.Sprintf("zypper-docker-private-%s", config.Image)
}
mc.lastCmd = config.Cmd
return container.ContainerCreateCreatedBody{ID: name, Warnings: warnings}, nil
}
func (mc *mockClient) ContainerStart(ctx context.Context, containerID string, options types.ContainerStartOptions) error {
if mc.startFail {
return errors.New("Start failed")
}
if containerID == "zypper-docker-private-3" {
// Ubuntu doesn't have zypper: fail.
return errors.New("Start failed")
}
return nil
}
func (mc *mockClient) ContainerRemove(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error {
if mc.removeFail {
return errors.New("Remove failed")
}
if !mc.suppressLog {
log.Printf("Removed container %v", containerID)
}
return nil
}
func (mc *mockClient) ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.ContainerWaitOKBody, <-chan error) {
resultC := make(chan container.ContainerWaitOKBody)
errC := make(chan error)
go func() {
time.Sleep(mc.waitSleep)
if mc.waitFail {
errC <- errors.New("Wait failed")
resultC <- container.ContainerWaitOKBody{StatusCode: -1}
return
}
if mc.commandFail {
// If commandExit was not specified, just exit with 1.
if mc.commandExit == 0 {
resultC <- container.ContainerWaitOKBody{StatusCode: 1}
errC <- nil
return
}
resultC <- container.ContainerWaitOKBody{StatusCode: (int64)(mc.commandExit)}
errC <- nil
return
}
resultC <- container.ContainerWaitOKBody{StatusCode: 0}
errC <- nil
}()
return resultC, errC
}
func (mc *mockClient) ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
var err error
if mc.logFail {
return nil, fmt.Errorf("Fake log failure")
}
cb := &closingBuffer{bytes.NewBuffer([]byte{})}
if mc.zypperBadVersion {
_, err = cb.WriteString("Unknown option '--severity'\n")
} else if mc.zypperGoodVersion {
_, err = cb.WriteString("Missing argument for --severity\n")
} else {
_, err = cb.WriteString("streaming buffer initialized\n")
}
return cb, err
}
func (mc *mockClient) ContainerKill(ctx context.Context, containerID, signal string) error {
if mc.killFail {
return fmt.Errorf("Fake failure while killing container")
}
return nil
}
func (mc *mockClient) ContainerCommit(ctx context.Context, container string, options types.ContainerCommitOptions) (types.IDResponse, error) {
if mc.commitFail {
return types.IDResponse{ID: ""}, fmt.Errorf("Fake failure while committing container")
}
return types.IDResponse{ID: "fake image ID"}, nil
}
func (mc *mockClient) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
if mc.listFail {
return []types.Container{},
fmt.Errorf("Fake failure while listing containers")
}
if mc.listEmpty {
return []types.Container{}, nil
}
return []types.Container{
types.Container{
ID: "35ae93c88cf8ab18da63bb2ad2dfd2399d745f292a344625fbb65892b7c25a01",
Names: []string{"/suse"},
Image: "opensuse:13.2",
ImageID: "sha256:7f31a825a11ec6557fbddd5fea8b823a4709ee552233352e435b4840e14388bd",
},
types.Container{
ID: "2",
Names: []string{"/not_suse"},
Image: "busybox:latest",
ImageID: "2",
},
types.Container{
ID: "3",
Names: []string{"/ubuntu"},
Image: "ubuntu:latest",
ImageID: "3",
},
types.Container{
ID: "4",
Names: []string{"/unknown_image"},
Image: "foo",
ImageID: "4",
},
}, nil
}
func (mc *mockClient) ContainerResize(ctx context.Context, containerID string, options types.ResizeOptions) error {
// Do nothing
return nil
}
func (mc *mockClient) ImageInspectWithRaw(ctx context.Context, imageID string) (types.ImageInspect, []byte, error) {
if mc.inspectFail {
return types.ImageInspect{}, []byte{}, errors.New("inspect fail")
}
return types.ImageInspect{ID: "1", Config: &container.Config{Image: "1"}}, []byte{}, nil
}
func (mc *mockClient) ImageRemove(ctx context.Context, image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) {
if mc.removeFail {
return []types.ImageDeleteResponseItem{}, errors.New("remove fail")
}
return nil, nil
}
func (mc *mockClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
if mc.inspectFail {
return types.ContainerJSON{}, errors.New("inspect fail")
}
return types.ContainerJSON{Config: &container.Config{Image: "1"}}, nil
}