forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
298 lines (236 loc) · 11.9 KB
/
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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) 2017 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
// Description: A mock implementation of virtcontainers that can be used
// for testing.
//
// This implementation calls the function set in the object that
// corresponds to the name of the method (for example, when CreateSandbox()
// is called, that method will try to call CreateSandboxFunc). If no
// function is defined for the method, it will return an error in a
// well-known format. Callers can detect this scenario by calling
// IsMockError().
package vcmock
import (
"context"
"fmt"
"syscall"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/device/api"
"github.com/kata-containers/runtime/virtcontainers/device/config"
vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types"
"github.com/kata-containers/runtime/virtcontainers/types"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
)
// mockErrorPrefix is a string that all errors returned by the mock
// implementation itself will contain as a prefix.
const mockErrorPrefix = "vcmock forced failure"
// SetLogger implements the VC function of the same name.
func (m *VCMock) SetLogger(ctx context.Context, logger *logrus.Entry) {
if m.SetLoggerFunc != nil {
m.SetLoggerFunc(ctx, logger)
}
}
// SetFactory implements the VC function of the same name.
func (m *VCMock) SetFactory(ctx context.Context, factory vc.Factory) {
if m.SetFactoryFunc != nil {
m.SetFactoryFunc(ctx, factory)
}
}
// CreateSandbox implements the VC function of the same name.
func (m *VCMock) CreateSandbox(ctx context.Context, sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error) {
if m.CreateSandboxFunc != nil {
return m.CreateSandboxFunc(ctx, sandboxConfig)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxConfig: %v", mockErrorPrefix, getSelf(), m, sandboxConfig)
}
// DeleteSandbox implements the VC function of the same name.
func (m *VCMock) DeleteSandbox(ctx context.Context, sandboxID string) (vc.VCSandbox, error) {
if m.DeleteSandboxFunc != nil {
return m.DeleteSandboxFunc(ctx, sandboxID)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
// FetchSandbox implements the VC function of the same name.
func (m *VCMock) FetchSandbox(ctx context.Context, sandboxID string) (vc.VCSandbox, error) {
if m.FetchSandboxFunc != nil {
return m.FetchSandboxFunc(ctx, sandboxID)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
// StartSandbox implements the VC function of the same name.
func (m *VCMock) StartSandbox(ctx context.Context, sandboxID string) (vc.VCSandbox, error) {
if m.StartSandboxFunc != nil {
return m.StartSandboxFunc(ctx, sandboxID)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
// StopSandbox implements the VC function of the same name.
func (m *VCMock) StopSandbox(ctx context.Context, sandboxID string, force bool) (vc.VCSandbox, error) {
if m.StopSandboxFunc != nil {
return m.StopSandboxFunc(ctx, sandboxID, force)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
// RunSandbox implements the VC function of the same name.
func (m *VCMock) RunSandbox(ctx context.Context, sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error) {
if m.RunSandboxFunc != nil {
return m.RunSandboxFunc(ctx, sandboxConfig)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxConfig: %v", mockErrorPrefix, getSelf(), m, sandboxConfig)
}
// ListSandbox implements the VC function of the same name.
func (m *VCMock) ListSandbox(ctx context.Context) ([]vc.SandboxStatus, error) {
if m.ListSandboxFunc != nil {
return m.ListSandboxFunc(ctx)
}
return nil, fmt.Errorf("%s: %s", mockErrorPrefix, getSelf())
}
// StatusSandbox implements the VC function of the same name.
func (m *VCMock) StatusSandbox(ctx context.Context, sandboxID string) (vc.SandboxStatus, error) {
if m.StatusSandboxFunc != nil {
return m.StatusSandboxFunc(ctx, sandboxID)
}
return vc.SandboxStatus{}, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
// CreateContainer implements the VC function of the same name.
func (m *VCMock) CreateContainer(ctx context.Context, sandboxID string, containerConfig vc.ContainerConfig) (vc.VCSandbox, vc.VCContainer, error) {
if m.CreateContainerFunc != nil {
return m.CreateContainerFunc(ctx, sandboxID, containerConfig)
}
return nil, nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerConfig: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerConfig)
}
// DeleteContainer implements the VC function of the same name.
func (m *VCMock) DeleteContainer(ctx context.Context, sandboxID, containerID string) (vc.VCContainer, error) {
if m.DeleteContainerFunc != nil {
return m.DeleteContainerFunc(ctx, sandboxID, containerID)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID)
}
// StartContainer implements the VC function of the same name.
func (m *VCMock) StartContainer(ctx context.Context, sandboxID, containerID string) (vc.VCContainer, error) {
if m.StartContainerFunc != nil {
return m.StartContainerFunc(ctx, sandboxID, containerID)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID)
}
// StopContainer implements the VC function of the same name.
func (m *VCMock) StopContainer(ctx context.Context, sandboxID, containerID string) (vc.VCContainer, error) {
if m.StopContainerFunc != nil {
return m.StopContainerFunc(ctx, sandboxID, containerID)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID)
}
// EnterContainer implements the VC function of the same name.
func (m *VCMock) EnterContainer(ctx context.Context, sandboxID, containerID string, cmd types.Cmd) (vc.VCSandbox, vc.VCContainer, *vc.Process, error) {
if m.EnterContainerFunc != nil {
return m.EnterContainerFunc(ctx, sandboxID, containerID, cmd)
}
return nil, nil, nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v, cmd: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID, cmd)
}
// StatusContainer implements the VC function of the same name.
func (m *VCMock) StatusContainer(ctx context.Context, sandboxID, containerID string) (vc.ContainerStatus, error) {
if m.StatusContainerFunc != nil {
return m.StatusContainerFunc(ctx, sandboxID, containerID)
}
return vc.ContainerStatus{}, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID)
}
// StatsContainer implements the VC function of the same name.
func (m *VCMock) StatsContainer(ctx context.Context, sandboxID, containerID string) (vc.ContainerStats, error) {
if m.StatsContainerFunc != nil {
return m.StatsContainerFunc(ctx, sandboxID, containerID)
}
return vc.ContainerStats{}, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID)
}
// StatsSandbox implements the VC function of the same name.
func (m *VCMock) StatsSandbox(ctx context.Context, sandboxID string) (vc.SandboxStats, []vc.ContainerStats, error) {
if m.StatsContainerFunc != nil {
return m.StatsSandboxFunc(ctx, sandboxID)
}
return vc.SandboxStats{}, []vc.ContainerStats{}, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
// KillContainer implements the VC function of the same name.
func (m *VCMock) KillContainer(ctx context.Context, sandboxID, containerID string, signal syscall.Signal, all bool) error {
if m.KillContainerFunc != nil {
return m.KillContainerFunc(ctx, sandboxID, containerID, signal, all)
}
return fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v, signal: %v, all: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID, signal, all)
}
// ProcessListContainer implements the VC function of the same name.
func (m *VCMock) ProcessListContainer(ctx context.Context, sandboxID, containerID string, options vc.ProcessListOptions) (vc.ProcessList, error) {
if m.ProcessListContainerFunc != nil {
return m.ProcessListContainerFunc(ctx, sandboxID, containerID, options)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID)
}
// UpdateContainer implements the VC function of the same name.
func (m *VCMock) UpdateContainer(ctx context.Context, sandboxID, containerID string, resources specs.LinuxResources) error {
if m.UpdateContainerFunc != nil {
return m.UpdateContainerFunc(ctx, sandboxID, containerID, resources)
}
return fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID)
}
// PauseContainer implements the VC function of the same name.
func (m *VCMock) PauseContainer(ctx context.Context, sandboxID, containerID string) error {
if m.PauseContainerFunc != nil {
return m.PauseContainerFunc(ctx, sandboxID, containerID)
}
return fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID)
}
// ResumeContainer implements the VC function of the same name.
func (m *VCMock) ResumeContainer(ctx context.Context, sandboxID, containerID string) error {
if m.ResumeContainerFunc != nil {
return m.ResumeContainerFunc(ctx, sandboxID, containerID)
}
return fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID)
}
// AddDevice implements the VC function of the same name.
func (m *VCMock) AddDevice(ctx context.Context, sandboxID string, info config.DeviceInfo) (api.Device, error) {
if m.AddDeviceFunc != nil {
return m.AddDeviceFunc(ctx, sandboxID, info)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
// AddInterface implements the VC function of the same name.
func (m *VCMock) AddInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interface) (*vcTypes.Interface, error) {
if m.AddInterfaceFunc != nil {
return m.AddInterfaceFunc(ctx, sandboxID, inf)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
// RemoveInterface implements the VC function of the same name.
func (m *VCMock) RemoveInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interface) (*vcTypes.Interface, error) {
if m.RemoveInterfaceFunc != nil {
return m.RemoveInterfaceFunc(ctx, sandboxID, inf)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
// ListInterfaces implements the VC function of the same name.
func (m *VCMock) ListInterfaces(ctx context.Context, sandboxID string) ([]*vcTypes.Interface, error) {
if m.ListInterfacesFunc != nil {
return m.ListInterfacesFunc(ctx, sandboxID)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
// UpdateRoutes implements the VC function of the same name.
func (m *VCMock) UpdateRoutes(ctx context.Context, sandboxID string, routes []*vcTypes.Route) ([]*vcTypes.Route, error) {
if m.UpdateRoutesFunc != nil {
return m.UpdateRoutesFunc(ctx, sandboxID, routes)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
// ListRoutes implements the VC function of the same name.
func (m *VCMock) ListRoutes(ctx context.Context, sandboxID string) ([]*vcTypes.Route, error) {
if m.ListRoutesFunc != nil {
return m.ListRoutesFunc(ctx, sandboxID)
}
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}
func (m *VCMock) CleanupContainer(ctx context.Context, sandboxID, containerID string, force bool) error {
if m.CleanupContainerFunc != nil {
return m.CleanupContainerFunc(ctx, sandboxID, containerID, true)
}
return fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
}