|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package container |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "gotest.tools/v3/assert" |
| 25 | + |
| 26 | + "github.com/containerd/errdefs" |
| 27 | +) |
| 28 | + |
| 29 | +func TestWalkAllForce(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + |
| 32 | + walkResults := map[string]struct { |
| 33 | + count int |
| 34 | + err error |
| 35 | + }{ |
| 36 | + "found": {count: 1}, |
| 37 | + "missing": {count: 0}, |
| 38 | + "broken": {count: -1, err: errors.New("failed to delete task: no such directory")}, |
| 39 | + "other": {count: -1, err: errors.New("cannot kill")}, |
| 40 | + "gone": {count: -1, err: errdefs.ErrNotFound}, |
| 41 | + } |
| 42 | + var walked []string |
| 43 | + walk := func(_ context.Context, req string) (int, error) { |
| 44 | + walked = append(walked, req) |
| 45 | + res := walkResults[req] |
| 46 | + return res.count, res.err |
| 47 | + } |
| 48 | + |
| 49 | + tests := []struct { |
| 50 | + name string |
| 51 | + reqs []string |
| 52 | + expectedErr string |
| 53 | + expectedRun []string |
| 54 | + }{ |
| 55 | + { |
| 56 | + name: "removed containers succeed", |
| 57 | + reqs: []string{"found"}, |
| 58 | + expectedRun: []string{"found"}, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "a container that does not exist is not an error", |
| 62 | + reqs: []string{"missing"}, |
| 63 | + expectedRun: []string{"missing"}, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "a container that vanishes during removal is not an error", |
| 67 | + reqs: []string{"gone"}, |
| 68 | + expectedRun: []string{"gone"}, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "a container that cannot be removed fails", |
| 72 | + reqs: []string{"broken"}, |
| 73 | + expectedErr: "1 errors:\nfailed to delete task: no such directory", |
| 74 | + expectedRun: []string{"broken"}, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "it keeps going after a failure and reports every one", |
| 78 | + reqs: []string{"broken", "found", "missing", "other"}, |
| 79 | + expectedErr: "2 errors:\nfailed to delete task: no such directory\ncannot kill", |
| 80 | + expectedRun: []string{"broken", "found", "missing", "other"}, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tc := range tests { |
| 85 | + t.Run(tc.name, func(t *testing.T) { |
| 86 | + walked = nil |
| 87 | + err := walkAllForce(context.Background(), tc.reqs, walk) |
| 88 | + if tc.expectedErr == "" { |
| 89 | + assert.NilError(t, err) |
| 90 | + } else { |
| 91 | + assert.Error(t, err, tc.expectedErr) |
| 92 | + } |
| 93 | + assert.DeepEqual(t, walked, tc.expectedRun) |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments