Skip to content

Commit 4a82b01

Browse files
authored
Merge pull request kubernetes#77814 from brandon-mabey/cri-runtime-service-error-injection
Fix error injection surface in FakeRuntimeService
2 parents 2525ab8 + ac2d38f commit 4a82b01

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

staging/src/k8s.io/cri-api/pkg/apis/testing/fake_runtime_service.go

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func (r *FakeRuntimeService) popError(f string) error {
126126
return nil
127127
}
128128
err, errs := errs[0], errs[1:]
129+
r.Errors[f] = errs
129130
return err
130131
}
131132

@@ -144,6 +145,9 @@ func (r *FakeRuntimeService) Version(apiVersion string) (*runtimeapi.VersionResp
144145
defer r.Unlock()
145146

146147
r.Called = append(r.Called, "Version")
148+
if err := r.popError("Version"); err != nil {
149+
return nil, err
150+
}
147151

148152
return &runtimeapi.VersionResponse{
149153
Version: FakeVersion,
@@ -158,6 +162,9 @@ func (r *FakeRuntimeService) Status() (*runtimeapi.RuntimeStatus, error) {
158162
defer r.Unlock()
159163

160164
r.Called = append(r.Called, "Status")
165+
if err := r.popError("Status"); err != nil {
166+
return nil, err
167+
}
161168

162169
return r.FakeStatus, nil
163170
}
@@ -167,6 +174,9 @@ func (r *FakeRuntimeService) RunPodSandbox(config *runtimeapi.PodSandboxConfig,
167174
defer r.Unlock()
168175

169176
r.Called = append(r.Called, "RunPodSandbox")
177+
if err := r.popError("RunPodSandbox"); err != nil {
178+
return "", err
179+
}
170180

171181
// PodSandboxID should be randomized for real container runtime, but here just use
172182
// fixed name from BuildSandboxName() for easily making fake sandboxes.
@@ -196,6 +206,9 @@ func (r *FakeRuntimeService) StopPodSandbox(podSandboxID string) error {
196206
defer r.Unlock()
197207

198208
r.Called = append(r.Called, "StopPodSandbox")
209+
if err := r.popError("StopPodSandbox"); err != nil {
210+
return err
211+
}
199212

200213
if s, ok := r.Sandboxes[podSandboxID]; ok {
201214
s.State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
@@ -211,6 +224,9 @@ func (r *FakeRuntimeService) RemovePodSandbox(podSandboxID string) error {
211224
defer r.Unlock()
212225

213226
r.Called = append(r.Called, "RemovePodSandbox")
227+
if err := r.popError("RemovePodSandbox"); err != nil {
228+
return err
229+
}
214230

215231
// Remove the pod sandbox
216232
delete(r.Sandboxes, podSandboxID)
@@ -223,6 +239,9 @@ func (r *FakeRuntimeService) PodSandboxStatus(podSandboxID string) (*runtimeapi.
223239
defer r.Unlock()
224240

225241
r.Called = append(r.Called, "PodSandboxStatus")
242+
if err := r.popError("PodSandboxStatus"); err != nil {
243+
return nil, err
244+
}
226245

227246
s, ok := r.Sandboxes[podSandboxID]
228247
if !ok {
@@ -238,6 +257,9 @@ func (r *FakeRuntimeService) ListPodSandbox(filter *runtimeapi.PodSandboxFilter)
238257
defer r.Unlock()
239258

240259
r.Called = append(r.Called, "ListPodSandbox")
260+
if err := r.popError("ListPodSandbox"); err != nil {
261+
return nil, err
262+
}
241263

242264
result := make([]*runtimeapi.PodSandbox, 0)
243265
for id, s := range r.Sandboxes {
@@ -272,6 +294,10 @@ func (r *FakeRuntimeService) PortForward(*runtimeapi.PortForwardRequest) (*runti
272294
defer r.Unlock()
273295

274296
r.Called = append(r.Called, "PortForward")
297+
if err := r.popError("PortForward"); err != nil {
298+
return nil, err
299+
}
300+
275301
return &runtimeapi.PortForwardResponse{}, nil
276302
}
277303

@@ -280,6 +306,9 @@ func (r *FakeRuntimeService) CreateContainer(podSandboxID string, config *runtim
280306
defer r.Unlock()
281307

282308
r.Called = append(r.Called, "CreateContainer")
309+
if err := r.popError("CreateContainer"); err != nil {
310+
return "", err
311+
}
283312

284313
// ContainerID should be randomized for real container runtime, but here just use
285314
// fixed BuildContainerName() for easily making fake containers.
@@ -309,6 +338,9 @@ func (r *FakeRuntimeService) StartContainer(containerID string) error {
309338
defer r.Unlock()
310339

311340
r.Called = append(r.Called, "StartContainer")
341+
if err := r.popError("StartContainer"); err != nil {
342+
return err
343+
}
312344

313345
c, ok := r.Containers[containerID]
314346
if !ok {
@@ -327,6 +359,9 @@ func (r *FakeRuntimeService) StopContainer(containerID string, timeout int64) er
327359
defer r.Unlock()
328360

329361
r.Called = append(r.Called, "StopContainer")
362+
if err := r.popError("StopContainer"); err != nil {
363+
return err
364+
}
330365

331366
c, ok := r.Containers[containerID]
332367
if !ok {
@@ -347,6 +382,9 @@ func (r *FakeRuntimeService) RemoveContainer(containerID string) error {
347382
defer r.Unlock()
348383

349384
r.Called = append(r.Called, "RemoveContainer")
385+
if err := r.popError("RemoveContainer"); err != nil {
386+
return err
387+
}
350388

351389
// Remove the container
352390
delete(r.Containers, containerID)
@@ -359,6 +397,9 @@ func (r *FakeRuntimeService) ListContainers(filter *runtimeapi.ContainerFilter)
359397
defer r.Unlock()
360398

361399
r.Called = append(r.Called, "ListContainers")
400+
if err := r.popError("ListContainers"); err != nil {
401+
return nil, err
402+
}
362403

363404
result := make([]*runtimeapi.Container, 0)
364405
for _, s := range r.Containers {
@@ -398,6 +439,9 @@ func (r *FakeRuntimeService) ContainerStatus(containerID string) (*runtimeapi.Co
398439
defer r.Unlock()
399440

400441
r.Called = append(r.Called, "ContainerStatus")
442+
if err := r.popError("ContainerStatus"); err != nil {
443+
return nil, err
444+
}
401445

402446
c, ok := r.Containers[containerID]
403447
if !ok {
@@ -413,22 +457,27 @@ func (r *FakeRuntimeService) UpdateContainerResources(string, *runtimeapi.LinuxC
413457
defer r.Unlock()
414458

415459
r.Called = append(r.Called, "UpdateContainerResources")
416-
return nil
460+
return r.popError("UpdateContainerResources")
417461
}
418462

419463
func (r *FakeRuntimeService) ExecSync(containerID string, cmd []string, timeout time.Duration) (stdout []byte, stderr []byte, err error) {
420464
r.Lock()
421465
defer r.Unlock()
422466

423467
r.Called = append(r.Called, "ExecSync")
424-
return nil, nil, nil
468+
err = r.popError("ExecSync")
469+
return
425470
}
426471

427472
func (r *FakeRuntimeService) Exec(*runtimeapi.ExecRequest) (*runtimeapi.ExecResponse, error) {
428473
r.Lock()
429474
defer r.Unlock()
430475

431476
r.Called = append(r.Called, "Exec")
477+
if err := r.popError("Exec"); err != nil {
478+
return nil, err
479+
}
480+
432481
return &runtimeapi.ExecResponse{}, nil
433482
}
434483

@@ -437,11 +486,19 @@ func (r *FakeRuntimeService) Attach(req *runtimeapi.AttachRequest) (*runtimeapi.
437486
defer r.Unlock()
438487

439488
r.Called = append(r.Called, "Attach")
489+
if err := r.popError("Attach"); err != nil {
490+
return nil, err
491+
}
492+
440493
return &runtimeapi.AttachResponse{}, nil
441494
}
442495

443496
func (r *FakeRuntimeService) UpdateRuntimeConfig(runtimeCOnfig *runtimeapi.RuntimeConfig) error {
444-
return nil
497+
r.Lock()
498+
defer r.Unlock()
499+
500+
r.Called = append(r.Called, "UpdateRuntimeConfig")
501+
return r.popError("UpdateRuntimeConfig")
445502
}
446503

447504
func (r *FakeRuntimeService) SetFakeContainerStats(containerStats []*runtimeapi.ContainerStats) {
@@ -459,6 +516,9 @@ func (r *FakeRuntimeService) ContainerStats(containerID string) (*runtimeapi.Con
459516
defer r.Unlock()
460517

461518
r.Called = append(r.Called, "ContainerStats")
519+
if err := r.popError("ContainerStats"); err != nil {
520+
return nil, err
521+
}
462522

463523
s, found := r.FakeContainerStats[containerID]
464524
if !found {
@@ -472,6 +532,9 @@ func (r *FakeRuntimeService) ListContainerStats(filter *runtimeapi.ContainerStat
472532
defer r.Unlock()
473533

474534
r.Called = append(r.Called, "ListContainerStats")
535+
if err := r.popError("ListContainerStats"); err != nil {
536+
return nil, err
537+
}
475538

476539
var result []*runtimeapi.ContainerStats
477540
for _, c := range r.Containers {

0 commit comments

Comments
 (0)