/
client.go
138 lines (121 loc) · 4.02 KB
/
client.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
package test
import (
"sync"
"time"
"github.com/fsouza/go-dockerclient"
)
// FakeDockerClient provides a Fake client for Docker testing
type FakeDockerClient struct {
Image *docker.Image
InspectImageResult []*docker.Image
Container *docker.Container
RemoveImageErr error
InspectImageErr []error
PullImageErr error
CreateContainerErr error
AttachToContainerErr error
StartContainerErr error
WaitContainerResult int
WaitContainerErr error
RemoveContainerErr error
CommitContainerErr error
CopyFromContainerErr error
BuildImageErr error
RemoveImageName string
InspectImageName []string
PullImageOpts docker.PullImageOptions
PullImageAuth docker.AuthConfiguration
CreateContainerOpts docker.CreateContainerOptions
AttachToContainerOpts []docker.AttachToContainerOptions
AttachToContainerSleep time.Duration
StartContainerID string
StartContainerHostConfig *docker.HostConfig
WaitContainerID string
RemoveContainerOpts docker.RemoveContainerOptions
CommitContainerOpts docker.CommitContainerOptions
CopyFromContainerOpts docker.CopyFromContainerOptions
BuildImageOpts docker.BuildImageOptions
mutex sync.Mutex
}
// RemoveImage removes an image from the fake client
func (d *FakeDockerClient) RemoveImage(name string) error {
d.RemoveImageName = name
return d.RemoveImageErr
}
// InspectImage inspects the fake image
func (d *FakeDockerClient) InspectImage(name string) (*docker.Image, error) {
d.InspectImageName = append(d.InspectImageName, name)
i := len(d.InspectImageName) - 1
var img *docker.Image
if i >= len(d.InspectImageResult) {
img = d.Image
} else {
img = d.InspectImageResult[i]
}
var err error
if i >= len(d.InspectImageErr) {
err = nil
} else {
err = d.InspectImageErr[i]
}
return img, err
}
// PullImage pulls the fake image
func (d *FakeDockerClient) PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error {
d.PullImageOpts = opts
d.PullImageAuth = auth
return d.PullImageErr
}
// CreateContainer creates a fake container
func (d *FakeDockerClient) CreateContainer(opts docker.CreateContainerOptions) (*docker.Container, error) {
d.CreateContainerOpts = opts
return d.Container, d.CreateContainerErr
}
// AttachToContainer attaches to a fake container
func (d *FakeDockerClient) AttachToContainer(opts docker.AttachToContainerOptions) error {
d.mutex.Lock()
d.AttachToContainerOpts = append(d.AttachToContainerOpts, opts)
if opts.Success != nil {
opts.Success <- struct{}{}
<-opts.Success
}
d.mutex.Unlock()
if d.AttachToContainerSleep != 0 {
time.Sleep(d.AttachToContainerSleep)
}
return d.AttachToContainerErr
}
// StartContainer starts the fake container
func (d *FakeDockerClient) StartContainer(id string, hostConfig *docker.HostConfig) error {
d.StartContainerID = id
d.StartContainerHostConfig = hostConfig
return d.StartContainerErr
}
// WaitContainer waits for a fake container to finish
func (d *FakeDockerClient) WaitContainer(id string) (int, error) {
d.WaitContainerID = id
return d.WaitContainerResult, d.WaitContainerErr
}
// RemoveContainer removes the fake container
func (d *FakeDockerClient) RemoveContainer(opts docker.RemoveContainerOptions) error {
d.RemoveContainerOpts = opts
return d.RemoveContainerErr
}
// CommitContainer commits the fake container
func (d *FakeDockerClient) CommitContainer(opts docker.CommitContainerOptions) (*docker.Image, error) {
d.CommitContainerOpts = opts
return d.Image, d.CommitContainerErr
}
// CopyFromContainer copies from the fake container
func (d *FakeDockerClient) CopyFromContainer(opts docker.CopyFromContainerOptions) error {
d.CopyFromContainerOpts = opts
return d.CopyFromContainerErr
}
// BuildImage builds image
func (d *FakeDockerClient) BuildImage(opts docker.BuildImageOptions) error {
d.BuildImageOpts = opts
return d.BuildImageErr
}
func (d *FakeDockerClient) InspectContainer(id string) (*docker.Container, error) {
return nil, d.BuildImageErr
}