-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
221 lines (207 loc) · 8.85 KB
/
client_test.go
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
// Copyright 2024 Harald Albrecht.
//
// 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.
// Use mockgen in source mode because our Moby client interface definition is
// for testing only, and mockgen cannot see it in "reflect mode".
package morbyd
import (
"context"
"io"
"slices"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/network"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/thediveo/morbyd/moby"
"github.com/thediveo/morbyd/session"
mock "go.uber.org/mock/gomock"
)
// WithMockController wraps the Docker client in our mock, using the specified
// controller.
func WithMockController(ctrl *mock.Controller, withoutForwarding ...string) session.Opt {
return func(o *session.Options) error {
o.Wrapper = func(client moby.Client) moby.Client {
return newWrappedClient(ctrl, client, withoutForwarding)
}
return nil
}
}
// Any is an instance of gomock's Any matcher; as it is stateless, we can pass
// it around multiple times.
var Any = mock.Any()
// newWrappedClient returns a new MockClient and configures its flight recorder
// to forward all intercepted method calls to the wrapped real Docker client,
// with the calls any number of times.
func newWrappedClient(ctrl *mock.Controller, wrapped moby.Client, withouts []string) moby.Client {
cl := NewMockClient(ctrl)
rec := cl.EXPECT()
rec.Close().DoAndReturn(func() error { return wrapped.Close() })
if !slices.Contains(withouts, "ContainerAttach") {
rec.ContainerAttach(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, container string, options container.AttachOptions) (types.HijackedResponse, error) {
return wrapped.ContainerAttach(ctx, container, options)
})
}
if !slices.Contains(withouts, "ContainerCreate") {
rec.ContainerCreate(Any, Any, Any, Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error) {
return wrapped.ContainerCreate(ctx, config, hostConfig, networkingConfig, platform, containerName)
})
}
if !slices.Contains(withouts, "ContainerInspect") {
rec.ContainerInspect(Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, containerID string) (types.ContainerJSON, error) {
return wrapped.ContainerInspect(ctx, containerID)
})
}
if !slices.Contains(withouts, "ContainerKill") {
rec.ContainerKill(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, containerID, signal string) error {
return wrapped.ContainerKill(ctx, containerID, signal)
})
}
if !slices.Contains(withouts, "ContainerList") {
rec.ContainerList(Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, options container.ListOptions) ([]types.Container, error) {
return wrapped.ContainerList(ctx, options)
})
}
if !slices.Contains(withouts, "ContainerPause") {
rec.ContainerPause(Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, containerID string) error {
return wrapped.ContainerPause(ctx, containerID)
})
}
if !slices.Contains(withouts, "ContainerRemove") {
rec.ContainerRemove(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, containerID string, options container.RemoveOptions) error {
return wrapped.ContainerRemove(ctx, containerID, options)
})
}
if !slices.Contains(withouts, "ContainerRestart") {
rec.ContainerRestart(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, containerID string, options container.StopOptions) error {
return wrapped.ContainerRestart(ctx, containerID, options)
})
}
if !slices.Contains(withouts, "ContainerStart") {
rec.ContainerStart(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, containerID string, options container.StartOptions) error {
return wrapped.ContainerStart(ctx, containerID, options)
})
}
if !slices.Contains(withouts, "ContainerStop") {
rec.ContainerStop(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, containerID string, options container.StopOptions) error {
return wrapped.ContainerStop(ctx, containerID, options)
})
}
if !slices.Contains(withouts, "ContainerUnpause") {
rec.ContainerUnpause(Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, containerID string) error {
return wrapped.ContainerUnpause(ctx, containerID)
})
}
if !slices.Contains(withouts, "ContainerWait") {
rec.ContainerWait(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.WaitResponse, <-chan error) {
return wrapped.ContainerWait(ctx, containerID, condition)
})
}
if !slices.Contains(withouts, "ContainerExecCreate") {
rec.ContainerExecCreate(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error) {
return wrapped.ContainerExecCreate(ctx, container, config)
})
}
if !slices.Contains(withouts, "ContainerExecStart") {
rec.ContainerExecStart(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, execID string, config types.ExecStartCheck) error {
return wrapped.ContainerExecStart(ctx, execID, config)
})
}
if !slices.Contains(withouts, "ContainerExecAttach") {
rec.ContainerExecAttach(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, execID string, config types.ExecStartCheck) (types.HijackedResponse, error) {
return wrapped.ContainerExecAttach(ctx, execID, config)
})
}
if !slices.Contains(withouts, "ContainerExecInspect") {
rec.ContainerExecInspect(Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
return wrapped.ContainerExecInspect(ctx, execID)
})
}
if !slices.Contains(withouts, "ImageBuild") {
rec.ImageBuild(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
return wrapped.ImageBuild(ctx, buildContext, options)
})
}
if !slices.Contains(withouts, "ImageList") {
rec.ImageList(Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, options image.ListOptions) ([]image.Summary, error) {
return wrapped.ImageList(ctx, options)
})
}
if !slices.Contains(withouts, "ImagePull") {
rec.ImagePull(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, refStr string, options image.PullOptions) (io.ReadCloser, error) {
return wrapped.ImagePull(ctx, refStr, options)
})
}
if !slices.Contains(withouts, "ImageRemove") {
rec.ImageRemove(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, imageID string, options image.RemoveOptions) ([]image.DeleteResponse, error) {
return wrapped.ImageRemove(ctx, imageID, options)
})
}
if !slices.Contains(withouts, "ImageTag") {
rec.ImageTag(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, source, target string) error {
return wrapped.ImageTag(ctx, source, target)
})
}
if !slices.Contains(withouts, "NetworkCreate") {
rec.NetworkCreate(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error) {
return wrapped.NetworkCreate(ctx, name, options)
})
}
if !slices.Contains(withouts, "NetworkInspect") {
rec.NetworkInspect(Any, Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, error) {
return wrapped.NetworkInspect(ctx, networkID, options)
})
}
if !slices.Contains(withouts, "NetworkList") {
rec.NetworkList(Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) {
return wrapped.NetworkList(ctx, options)
})
}
if !slices.Contains(withouts, "NetworkRemove") {
rec.NetworkRemove(Any, Any).AnyTimes().
DoAndReturn(func(ctx context.Context, networkID string) error {
return wrapped.NetworkRemove(ctx, networkID)
})
}
if !slices.Contains(withouts, "ServerVersion") {
rec.ServerVersion(Any).AnyTimes().
DoAndReturn(func(ctx context.Context, networkID string) (types.Version, error) {
return wrapped.ServerVersion(ctx)
})
}
return cl
}