Skip to content

Commit 268f519

Browse files
committed
update fake
1 parent bb25997 commit 268f519

File tree

4 files changed

+290
-4
lines changed

4 files changed

+290
-4
lines changed

cli/docker_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
dockertypes "github.com/docker/docker/api/types"
1717
"github.com/docker/docker/api/types/container"
18+
"github.com/docker/docker/api/types/image"
1819
"github.com/docker/docker/api/types/network"
1920
v1 "github.com/opencontainers/image-spec/specs-go/v1"
2021
"github.com/spf13/afero"
@@ -486,7 +487,7 @@ func TestDocker(t *testing.T) {
486487
authB64 := base64.URLEncoding.EncodeToString(raw)
487488

488489
client := clitest.DockerClient(t, ctx)
489-
client.ImagePullFn = func(_ context.Context, _ string, options dockertypes.ImagePullOptions) (io.ReadCloser, error) {
490+
client.ImagePullFn = func(_ context.Context, _ string, options image.PullOptions) (io.ReadCloser, error) {
490491
// Assert that we call the image pull function with the credentials.
491492
require.Equal(t, authB64, options.RegistryAuth)
492493
return io.NopCloser(bytes.NewReader(nil)), nil

dockerutil/dockerfake/client.go

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
package dockerfake
2+
3+
import (
4+
"context"
5+
"io"
6+
"strings"
7+
8+
"github.com/docker/docker/api/types"
9+
dockertypes "github.com/docker/docker/api/types"
10+
containertypes "github.com/docker/docker/api/types/container"
11+
"github.com/docker/docker/api/types/events"
12+
"github.com/docker/docker/api/types/filters"
13+
"github.com/docker/docker/api/types/image"
14+
networktypes "github.com/docker/docker/api/types/network"
15+
"github.com/docker/docker/api/types/registry"
16+
"github.com/docker/docker/api/types/system"
17+
specs "github.com/opencontainers/image-spec/specs-go/v1"
18+
19+
"github.com/coder/envbox/dockerutil"
20+
)
21+
22+
var _ dockerutil.DockerClient = MockClient{}
23+
24+
// MockClient provides overrides for functions that are called in envbox.
25+
type MockClient struct {
26+
ImagePullFn func(_ context.Context, ref string, options image.PullOptions) (io.ReadCloser, error)
27+
ContainerCreateFn func(_ context.Context, config *containertypes.Config, hostConfig *containertypes.HostConfig, networkingConfig *networktypes.NetworkingConfig, _ *specs.Platform, containerName string) (containertypes.CreateResponse, error)
28+
ImagePruneFn func(_ context.Context, pruneFilter filters.Args) (dockertypes.ImagesPruneReport, error)
29+
ContainerStartFn func(_ context.Context, container string, options containertypes.StartOptions) error
30+
ContainerExecAttachFn func(_ context.Context, execID string, config dockertypes.ExecStartCheck) (dockertypes.HijackedResponse, error)
31+
ContainerExecCreateFn func(_ context.Context, container string, config dockertypes.ExecConfig) (dockertypes.IDResponse, error)
32+
ContainerExecStartFn func(_ context.Context, execID string, config dockertypes.ExecStartCheck) error
33+
ContainerExecInspectFn func(_ context.Context, execID string) (dockertypes.ContainerExecInspect, error)
34+
ContainerInspectFn func(_ context.Context, container string) (dockertypes.ContainerJSON, error)
35+
ContainerRemoveFn func(_ context.Context, container string, options containertypes.RemoveOptions) error
36+
PingFn func(_ context.Context) (dockertypes.Ping, error)
37+
}
38+
39+
func (MockClient) ImageBuild(_ context.Context, _ io.Reader, _ dockertypes.ImageBuildOptions) (dockertypes.ImageBuildResponse, error) {
40+
panic("not implemented")
41+
}
42+
43+
func (MockClient) BuildCachePrune(_ context.Context, _ dockertypes.BuildCachePruneOptions) (*dockertypes.BuildCachePruneReport, error) {
44+
panic("not implemented")
45+
}
46+
47+
func (MockClient) BuildCancel(_ context.Context, _ string) error {
48+
panic("not implemented")
49+
}
50+
51+
func (MockClient) ImageCreate(_ context.Context, _ string, _ image.CreateOptions) (io.ReadCloser, error) {
52+
panic("not implemented")
53+
}
54+
55+
func (MockClient) ImageHistory(_ context.Context, _ string) ([]image.HistoryResponseItem, error) {
56+
panic("not implemented")
57+
}
58+
59+
func (MockClient) ImageImport(_ context.Context, _ image.ImportSource, _ string, _ image.ImportOptions) (io.ReadCloser, error) {
60+
panic("not implemented")
61+
}
62+
63+
func (MockClient) ImageInspectWithRaw(_ context.Context, _ string) (dockertypes.ImageInspect, []byte, error) {
64+
panic("not implemented")
65+
}
66+
67+
func (MockClient) ImageList(_ context.Context, _ image.ListOptions) ([]image.Summary, error) {
68+
panic("not implemented")
69+
}
70+
71+
func (MockClient) ImageLoad(_ context.Context, _ io.Reader, _ bool) (dockertypes.ImageLoadResponse, error) {
72+
panic("not implemented")
73+
}
74+
75+
func (m MockClient) ImagePull(ctx context.Context, ref string, options image.PullOptions) (io.ReadCloser, error) {
76+
if m.ImagePullFn == nil {
77+
return io.NopCloser(strings.NewReader("")), nil
78+
}
79+
return m.ImagePullFn(ctx, ref, options)
80+
}
81+
82+
func (MockClient) ImagePush(_ context.Context, _ string, _ image.PushOptions) (io.ReadCloser, error) {
83+
panic("not implemented")
84+
}
85+
86+
func (MockClient) ImageRemove(_ context.Context, _ string, _ image.RemoveOptions) ([]image.DeleteResponse, error) {
87+
panic("not implemented")
88+
}
89+
90+
func (MockClient) ImageSearch(_ context.Context, _ string, _ dockertypes.ImageSearchOptions) ([]registry.SearchResult, error) {
91+
panic("not implemented")
92+
}
93+
94+
func (MockClient) ImageSave(_ context.Context, _ []string) (io.ReadCloser, error) {
95+
panic("not implemented")
96+
}
97+
98+
func (MockClient) ImageTag(_ context.Context, _ string, _ string) error {
99+
panic("not implemented")
100+
}
101+
102+
func (m MockClient) ImagesPrune(ctx context.Context, pruneFilter filters.Args) (dockertypes.ImagesPruneReport, error) {
103+
if m.ImagePruneFn == nil {
104+
return dockertypes.ImagesPruneReport{}, nil
105+
}
106+
return m.ImagePruneFn(ctx, pruneFilter)
107+
}
108+
109+
func (MockClient) Events(_ context.Context, _ dockertypes.EventsOptions) (<-chan events.Message, <-chan error) {
110+
panic("not implemented")
111+
}
112+
113+
func (MockClient) Info(_ context.Context) (system.Info, error) {
114+
panic("not implemented")
115+
}
116+
117+
func (MockClient) RegistryLogin(_ context.Context, _ registry.AuthConfig) (registry.AuthenticateOKBody, error) {
118+
panic("not implemented")
119+
}
120+
121+
func (MockClient) DiskUsage(_ context.Context, _ dockertypes.DiskUsageOptions) (dockertypes.DiskUsage, error) {
122+
panic("not implemented")
123+
}
124+
125+
func (m MockClient) Ping(ctx context.Context) (dockertypes.Ping, error) {
126+
if m.PingFn == nil {
127+
return dockertypes.Ping{}, nil
128+
}
129+
return m.PingFn(ctx)
130+
}
131+
132+
func (MockClient) ContainerAttach(_ context.Context, _ string, _ containertypes.AttachOptions) (dockertypes.HijackedResponse, error) {
133+
panic("not implemented")
134+
}
135+
136+
func (MockClient) ContainerCommit(_ context.Context, _ string, _ containertypes.CommitOptions) (dockertypes.IDResponse, error) {
137+
panic("not implemented")
138+
}
139+
140+
func (m MockClient) ContainerCreate(ctx context.Context, config *containertypes.Config, hostConfig *containertypes.HostConfig, networkingConfig *networktypes.NetworkingConfig, pspecs *specs.Platform, containerName string) (containertypes.CreateResponse, error) {
141+
if m.ContainerCreateFn == nil {
142+
return containertypes.CreateResponse{}, nil
143+
}
144+
return m.ContainerCreateFn(ctx, config, hostConfig, networkingConfig, pspecs, containerName)
145+
}
146+
147+
func (MockClient) ContainerDiff(_ context.Context, _ string) ([]containertypes.FilesystemChange, error) {
148+
panic("not implemented")
149+
}
150+
151+
func (m MockClient) ContainerExecAttach(ctx context.Context, execID string, config dockertypes.ExecStartCheck) (dockertypes.HijackedResponse, error) {
152+
if m.ContainerExecAttachFn == nil {
153+
return dockertypes.HijackedResponse{}, nil
154+
}
155+
return m.ContainerExecAttachFn(ctx, execID, config)
156+
}
157+
158+
func (m MockClient) ContainerExecCreate(ctx context.Context, name string, config dockertypes.ExecConfig) (dockertypes.IDResponse, error) {
159+
if m.ContainerExecCreateFn == nil {
160+
return dockertypes.IDResponse{}, nil
161+
}
162+
return m.ContainerExecCreateFn(ctx, name, config)
163+
}
164+
165+
func (m MockClient) ContainerExecInspect(ctx context.Context, id string) (dockertypes.ContainerExecInspect, error) {
166+
if m.ContainerExecInspectFn == nil {
167+
return dockertypes.ContainerExecInspect{}, nil
168+
}
169+
170+
return m.ContainerExecInspectFn(ctx, id)
171+
}
172+
173+
func (MockClient) ContainerExecResize(_ context.Context, _ string, _ containertypes.ResizeOptions) error {
174+
panic("not implemented")
175+
}
176+
177+
func (m MockClient) ContainerExecStart(ctx context.Context, execID string, config dockertypes.ExecStartCheck) error {
178+
if m.ContainerExecStartFn == nil {
179+
return nil
180+
}
181+
return m.ContainerExecStartFn(ctx, execID, config)
182+
}
183+
184+
func (MockClient) ContainerExport(_ context.Context, _ string) (io.ReadCloser, error) {
185+
panic("not implemented")
186+
}
187+
188+
func (m MockClient) ContainerInspect(ctx context.Context, name string) (dockertypes.ContainerJSON, error) {
189+
if m.ContainerInspectFn == nil {
190+
return dockertypes.ContainerJSON{}, nil
191+
}
192+
return m.ContainerInspectFn(ctx, name)
193+
}
194+
195+
func (MockClient) ContainerInspectWithRaw(_ context.Context, _ string, _ bool) (dockertypes.ContainerJSON, []byte, error) {
196+
panic("not implemented")
197+
}
198+
199+
func (MockClient) ContainerKill(_ context.Context, _ string, _ string) error {
200+
panic("not implemented")
201+
}
202+
203+
func (MockClient) ContainerList(_ context.Context, _ containertypes.ListOptions) ([]types.Container, error) {
204+
panic("not implemented")
205+
}
206+
207+
func (MockClient) ContainerLogs(_ context.Context, _ string, _ containertypes.LogsOptions) (io.ReadCloser, error) {
208+
panic("not implemented")
209+
}
210+
211+
func (MockClient) ContainerPause(_ context.Context, _ string) error {
212+
panic("not implemented")
213+
}
214+
215+
func (m MockClient) ContainerRemove(ctx context.Context, name string, options containertypes.RemoveOptions) error {
216+
if m.ContainerRemoveFn == nil {
217+
return nil
218+
}
219+
return m.ContainerRemoveFn(ctx, name, options)
220+
}
221+
222+
func (MockClient) ContainerRename(_ context.Context, _ string, _ string) error {
223+
panic("not implemented")
224+
}
225+
226+
func (MockClient) ContainerResize(_ context.Context, _ string, _ containertypes.ResizeOptions) error {
227+
panic("not implemented")
228+
}
229+
230+
func (MockClient) ContainerRestart(_ context.Context, _ string, _ containertypes.StopOptions) error {
231+
panic("not implemented")
232+
}
233+
234+
func (MockClient) ContainerStatPath(_ context.Context, _ string, _ string) (dockertypes.ContainerPathStat, error) {
235+
panic("not implemented")
236+
}
237+
238+
func (MockClient) ContainerStats(_ context.Context, _ string, _ bool) (dockertypes.ContainerStats, error) {
239+
panic("not implemented")
240+
}
241+
242+
func (m MockClient) ContainerStart(ctx context.Context, name string, options containertypes.StartOptions) error {
243+
if m.ContainerStartFn == nil {
244+
return nil
245+
}
246+
return m.ContainerStartFn(ctx, name, options)
247+
}
248+
249+
func (MockClient) ContainerStop(_ context.Context, _ string, _ containertypes.StopOptions) error {
250+
panic("not implemented")
251+
}
252+
253+
func (MockClient) ContainerTop(_ context.Context, _ string, _ []string) (containertypes.ContainerTopOKBody, error) {
254+
panic("not implemented")
255+
}
256+
257+
func (MockClient) ContainerUnpause(_ context.Context, _ string) error {
258+
panic("not implemented")
259+
}
260+
261+
func (MockClient) ContainerUpdate(_ context.Context, _ string, _ containertypes.UpdateConfig) (containertypes.ContainerUpdateOKBody, error) {
262+
panic("not implemented")
263+
}
264+
265+
func (MockClient) ContainerWait(_ context.Context, _ string, _ containertypes.WaitCondition) (<-chan containertypes.WaitResponse, <-chan error) {
266+
panic("not implemented")
267+
}
268+
269+
func (MockClient) CopyFromContainer(_ context.Context, _ string, _ string) (io.ReadCloser, dockertypes.ContainerPathStat, error) {
270+
panic("not implemented")
271+
}
272+
273+
func (MockClient) CopyToContainer(_ context.Context, _ string, _ string, _ io.Reader, _ dockertypes.CopyToContainerOptions) error {
274+
panic("not implemented")
275+
}
276+
277+
func (MockClient) ContainersPrune(_ context.Context, _ filters.Args) (dockertypes.ContainersPruneReport, error) {
278+
panic("not implemented")
279+
}
280+
281+
func (MockClient) ContainerStatsOneShot(_ context.Context, _ string) (dockertypes.ContainerStats, error) {
282+
panic("not implemented")
283+
}

dockerutil/dockerfake/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Package dockerfake contains logic for mocking out Docker-related
22
// functionality.
3+
//
34
//go:generate mockgen -destination ./mock.go -package dockerfake github.com/coder/envbox/dockerutil/dockerfake Client
45
package dockerfake

dockerutil/image.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
dockertypes "github.com/docker/docker/api/types"
1313
"github.com/docker/docker/api/types/container"
1414
"github.com/docker/docker/api/types/filters"
15+
"github.com/docker/docker/api/types/image"
1516
"golang.org/x/xerrors"
1617

1718
"github.com/coder/envbox/buildlog"
@@ -51,7 +52,7 @@ func PullImage(ctx context.Context, config *PullImageConfig) error {
5152

5253
pullImageFn := func() error {
5354
var rd io.ReadCloser
54-
rd, err = config.Client.ImagePull(ctx, config.Image, dockertypes.ImagePullOptions{
55+
rd, err = config.Client.ImagePull(ctx, config.Image, image.PullOptions{
5556
RegistryAuth: authStr,
5657
})
5758
if err != nil {
@@ -176,12 +177,12 @@ func GetImageMetadata(ctx context.Context, client DockerClient, image, username
176177

177178
defer func() {
178179
// We wanna remove this, but it's not a huge deal if it fails.
179-
_ = client.ContainerRemove(ctx, created.ID, dockertypes.ContainerRemoveOptions{
180+
_ = client.ContainerRemove(ctx, created.ID, container.RemoveOptions{
180181
Force: true,
181182
})
182183
}()
183184

184-
err = client.ContainerStart(ctx, created.ID, dockertypes.ContainerStartOptions{})
185+
err = client.ContainerStart(ctx, created.ID, container.StartOptions{})
185186
if err != nil {
186187
return ImageMetadata{}, xerrors.Errorf("start container: %w", err)
187188
}

0 commit comments

Comments
 (0)