|
| 1 | +//go:build linux |
| 2 | +// +build linux |
| 3 | + |
| 4 | +/* |
| 5 | + Copyright The containerd Authors. |
| 6 | +
|
| 7 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + you may not use this file except in compliance with the License. |
| 9 | + You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | + Unless required by applicable law or agreed to in writing, software |
| 14 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + See the License for the specific language governing permissions and |
| 17 | + limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package integration |
| 21 | + |
| 22 | +import ( |
| 23 | + "encoding/json" |
| 24 | + "fmt" |
| 25 | + "os" |
| 26 | + "path/filepath" |
| 27 | + "runtime" |
| 28 | + "strings" |
| 29 | + "testing" |
| 30 | + |
| 31 | + criapiv1 "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 32 | + |
| 33 | + "github.com/containerd/containerd/pkg/failpoint" |
| 34 | + "github.com/stretchr/testify/require" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + failpointRuntimeHandler = "runc-fp" |
| 39 | + |
| 40 | + failpointShimPrefixKey = "io.containerd.runtime.v2.shim.failpoint." |
| 41 | + |
| 42 | + failpointCNIStateDirKey = "cniFailpointControlStateDir" |
| 43 | +) |
| 44 | + |
| 45 | +func TestRunPodSandboxWithSetupCNIFailure(t *testing.T) { |
| 46 | + if runtime.GOOS != "linux" { |
| 47 | + t.Skip() |
| 48 | + } |
| 49 | + |
| 50 | + t.Logf("Inject CNI failpoint") |
| 51 | + conf := &failpointConf{ |
| 52 | + Add: "1*error(you-shall-not-pass!)", |
| 53 | + } |
| 54 | + |
| 55 | + sbConfig := PodSandboxConfig(t.Name(), "failpoint") |
| 56 | + injectCNIFailpoint(t, sbConfig, conf) |
| 57 | + |
| 58 | + t.Logf("Create a sandbox") |
| 59 | + _, err := runtimeService.RunPodSandbox(sbConfig, failpointRuntimeHandler) |
| 60 | + require.Error(t, err) |
| 61 | + require.Equal(t, true, strings.Contains(err.Error(), "you-shall-not-pass!")) |
| 62 | + |
| 63 | + t.Logf("Retry to create sandbox with same config") |
| 64 | + sb, err := runtimeService.RunPodSandbox(sbConfig, failpointRuntimeHandler) |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + err = runtimeService.StopPodSandbox(sb) |
| 68 | + require.NoError(t, err) |
| 69 | + |
| 70 | + err = runtimeService.RemovePodSandbox(sb) |
| 71 | + require.NoError(t, err) |
| 72 | +} |
| 73 | + |
| 74 | +func TestRunPodSandboxWithShimStartFailure(t *testing.T) { |
| 75 | + if runtime.GOOS != "linux" { |
| 76 | + t.Skip() |
| 77 | + } |
| 78 | + |
| 79 | + t.Logf("Inject Shim failpoint") |
| 80 | + |
| 81 | + sbConfig := PodSandboxConfig(t.Name(), "failpoint") |
| 82 | + injectShimFailpoint(t, sbConfig, map[string]string{ |
| 83 | + "Start": "1*error(no hard feelings)", |
| 84 | + }) |
| 85 | + |
| 86 | + t.Logf("Create a sandbox") |
| 87 | + _, err := runtimeService.RunPodSandbox(sbConfig, failpointRuntimeHandler) |
| 88 | + require.Error(t, err) |
| 89 | + require.Equal(t, true, strings.Contains(err.Error(), "no hard feelings")) |
| 90 | +} |
| 91 | + |
| 92 | +// failpointConf is used to describe cmdAdd/cmdDel/cmdCheck command's failpoint. |
| 93 | +type failpointConf struct { |
| 94 | + Add string `json:"cmdAdd"` |
| 95 | + Del string `json:"cmdDel"` |
| 96 | + Check string `json:"cmdCheck"` |
| 97 | +} |
| 98 | + |
| 99 | +func injectCNIFailpoint(t *testing.T, sbConfig *criapiv1.PodSandboxConfig, conf *failpointConf) { |
| 100 | + stateDir := t.TempDir() |
| 101 | + |
| 102 | + metadata := sbConfig.Metadata |
| 103 | + fpFilename := filepath.Join(stateDir, |
| 104 | + fmt.Sprintf("%s-%s.json", metadata.Namespace, metadata.Name)) |
| 105 | + |
| 106 | + data, err := json.Marshal(conf) |
| 107 | + require.NoError(t, err) |
| 108 | + |
| 109 | + err = os.WriteFile(fpFilename, data, 0666) |
| 110 | + require.NoError(t, err) |
| 111 | + |
| 112 | + sbConfig.Annotations[failpointCNIStateDirKey] = stateDir |
| 113 | +} |
| 114 | + |
| 115 | +func injectShimFailpoint(t *testing.T, sbConfig *criapiv1.PodSandboxConfig, methodFps map[string]string) { |
| 116 | + for method, fp := range methodFps { |
| 117 | + _, err := failpoint.NewFailpoint(method, fp) |
| 118 | + require.NoError(t, err, "check failpoint %s for shim method %s", fp, method) |
| 119 | + |
| 120 | + sbConfig.Annotations[failpointShimPrefixKey+method] = fp |
| 121 | + } |
| 122 | +} |
0 commit comments