/
docker_mock.go
178 lines (156 loc) · 4.42 KB
/
docker_mock.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
package cloud
import (
"bytes"
"context"
"fmt"
"io"
"math/rand"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/pkg/errors"
)
type dockerClientMock struct {
// API call options
failInit bool
failDownload bool
failBuild bool
failCreate bool
failGet bool
failList bool
failRemove bool
failStart bool
failAttach bool
// Other options
hasOpenPorts bool
baseImage string
containerAttachment *types.HijackedResponse
}
func GetMockClient() *dockerClientMock {
return &dockerClientMock{}
}
func (c *dockerClientMock) generateContainerID() string {
return fmt.Sprintf("container-%d", rand.New(rand.NewSource(time.Now().UnixNano())).Int())
}
func (c *dockerClientMock) Init(string) error {
if c.failInit {
return errors.New("failed to initialize client")
}
return nil
}
func (c *dockerClientMock) EnsureImageDownloaded(context.Context, *host.Host, host.DockerOptions) (string, error) {
if c.failDownload {
return "", errors.New("failed to download image")
}
return c.baseImage, nil
}
func (c *dockerClientMock) BuildImageWithAgent(context.Context, *host.Host, string) (string, error) {
if c.failBuild {
return "", errors.New("failed to build image with agent")
}
return fmt.Sprintf(provisionedImageTag, c.baseImage), nil
}
func (c *dockerClientMock) CreateContainer(context.Context, *host.Host, *host.Host) error {
if c.failCreate {
return errors.New("failed to create container")
}
return nil
}
func (c *dockerClientMock) GetContainer(context.Context, *host.Host, string) (*types.ContainerJSON, error) {
if c.failGet {
return nil, errors.New("failed to inspect container")
}
container := &types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
ID: c.generateContainerID(),
State: &types.ContainerState{Running: true},
},
Config: &container.Config{
ExposedPorts: nat.PortSet{"22/tcp": {}},
},
NetworkSettings: &types.NetworkSettings{
NetworkSettingsBase: types.NetworkSettingsBase{
Ports: nat.PortMap{
"22/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: "5000",
},
},
},
},
},
}
if !c.hasOpenPorts {
container.NetworkSettings = &types.NetworkSettings{}
}
return container, nil
}
func (c *dockerClientMock) GetDockerLogs(_ context.Context, containerID string, _ *host.Host, _ types.ContainerLogsOptions) (io.Reader, error) {
return bytes.NewBufferString("this is a log message"), nil
}
func (c *dockerClientMock) GetDockerStatus(_ context.Context, containerID string, _ *host.Host) (*ContainerStatus, error) {
if containerID == "" {
return &ContainerStatus{HasStarted: false}, nil
}
return &ContainerStatus{HasStarted: true}, nil
}
func (c *dockerClientMock) ListContainers(context.Context, *host.Host) ([]types.Container, error) {
if c.failList {
return nil, errors.New("failed to list containers")
}
container := types.Container{
ID: "container-1",
Ports: []types.Port{
{PublicPort: 5000},
{PublicPort: 5001},
},
Names: []string{
"/container-1",
},
}
return []types.Container{container}, nil
}
func (c *dockerClientMock) RemoveContainer(context.Context, *host.Host, string) error {
if c.failRemove {
return errors.New("failed to remove container")
}
return nil
}
func (c *dockerClientMock) ListImages(context.Context, *host.Host) ([]types.ImageSummary, error) {
if c.failList {
return nil, errors.New("failed to list images")
}
now := time.Now()
image1 := types.ImageSummary{
ID: "image-1",
Containers: 2,
Created: now.Unix(),
}
image2 := types.ImageSummary{
ID: "image-2",
Containers: 2,
Created: now.Add(-10 * time.Minute).Unix(),
}
return []types.ImageSummary{image1, image2}, nil
}
func (c *dockerClientMock) RemoveImage(context.Context, *host.Host, string) error {
if c.failRemove {
return errors.New("failed to remove image")
}
return nil
}
func (c *dockerClientMock) StartContainer(context.Context, *host.Host, string) error {
if c.failStart {
return errors.New("failed to start container")
}
return nil
}
func (c *dockerClientMock) AttachToContainer(context.Context, *host.Host, string, host.DockerOptions) (*types.HijackedResponse, error) {
if c.failAttach {
return c.containerAttachment, errors.New("failed to attach to container")
}
return c.containerAttachment, nil
}