|
| 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 integration |
| 18 | + |
| 19 | +import ( |
| 20 | + "bufio" |
| 21 | + "context" |
| 22 | + "io" |
| 23 | + "net" |
| 24 | + "os" |
| 25 | + "strconv" |
| 26 | + "strings" |
| 27 | + "syscall" |
| 28 | + "testing" |
| 29 | + "time" |
| 30 | + |
| 31 | + apitask "github.com/containerd/containerd/api/runtime/task/v2" |
| 32 | + "github.com/containerd/containerd/integration/images" |
| 33 | + "github.com/containerd/containerd/namespaces" |
| 34 | + "github.com/containerd/containerd/runtime/v2/shim" |
| 35 | + "github.com/containerd/ttrpc" |
| 36 | + "github.com/stretchr/testify/assert" |
| 37 | + "github.com/stretchr/testify/require" |
| 38 | + exec "golang.org/x/sys/execabs" |
| 39 | +) |
| 40 | + |
| 41 | +// TestIssue7496 is used to reproduce https://github.com/containerd/containerd/issues/7496 |
| 42 | +// |
| 43 | +// NOTE: https://github.com/containerd/containerd/issues/8931 is the same issue. |
| 44 | +func TestIssue7496(t *testing.T) { |
| 45 | + ctx := namespaces.WithNamespace(context.Background(), "k8s.io") |
| 46 | + |
| 47 | + t.Logf("Create a pod config and run sandbox container") |
| 48 | + sbConfig := PodSandboxConfig("sandbox", "issue7496") |
| 49 | + sbID, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler) |
| 50 | + require.NoError(t, err) |
| 51 | + |
| 52 | + shimCli := connectToShim(ctx, t, sbID) |
| 53 | + |
| 54 | + delayInSec := 12 |
| 55 | + t.Logf("[shim pid: %d]: Injecting %d seconds delay to umount2 syscall", |
| 56 | + shimPid(ctx, t, shimCli), |
| 57 | + delayInSec) |
| 58 | + |
| 59 | + doneCh := injectDelayToUmount2(ctx, t, shimCli, delayInSec /* CRI plugin uses 10 seconds to delete task */) |
| 60 | + |
| 61 | + t.Logf("Create a container config and run container in a pod") |
| 62 | + pauseImage := images.Get(images.Pause) |
| 63 | + EnsureImageExists(t, pauseImage) |
| 64 | + |
| 65 | + containerConfig := ContainerConfig("pausecontainer", pauseImage) |
| 66 | + cnID, err := runtimeService.CreateContainer(sbID, containerConfig, sbConfig) |
| 67 | + require.NoError(t, err) |
| 68 | + require.NoError(t, runtimeService.StartContainer(cnID)) |
| 69 | + |
| 70 | + t.Logf("Start to StopPodSandbox and RemovePodSandbox") |
| 71 | + ctx, cancelFn := context.WithTimeout(ctx, 3*time.Minute) |
| 72 | + defer cancelFn() |
| 73 | + for { |
| 74 | + select { |
| 75 | + case <-ctx.Done(): |
| 76 | + require.NoError(t, ctx.Err(), "The StopPodSandbox should be done in time") |
| 77 | + default: |
| 78 | + } |
| 79 | + |
| 80 | + err := runtimeService.StopPodSandbox(sbID) |
| 81 | + if err != nil { |
| 82 | + t.Logf("Failed to StopPodSandbox: %v", err) |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + err = runtimeService.RemovePodSandbox(sbID) |
| 87 | + if err == nil { |
| 88 | + break |
| 89 | + } |
| 90 | + t.Logf("Failed to RemovePodSandbox: %v", err) |
| 91 | + time.Sleep(1 * time.Second) |
| 92 | + } |
| 93 | + |
| 94 | + t.Logf("PodSandbox %s has been deleted and start to wait for strace exit", sbID) |
| 95 | + select { |
| 96 | + case <-time.After(15 * time.Second): |
| 97 | + resp, err := shimCli.Connect(ctx, &apitask.ConnectRequest{}) |
| 98 | + assert.Error(t, err, "should failed to call shim connect API") |
| 99 | + |
| 100 | + t.Errorf("Strace doesn't exit in time") |
| 101 | + |
| 102 | + t.Logf("Cleanup the shim (pid: %d)", resp.GetShimPid()) |
| 103 | + syscall.Kill(int(resp.GetShimPid()), syscall.SIGKILL) |
| 104 | + <-doneCh |
| 105 | + case <-doneCh: |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// injectDelayToUmount2 uses strace(1) to inject delay on umount2 syscall to |
| 110 | +// simulate IO pressure because umount2 might force kernel to syncfs, for |
| 111 | +// example, umount overlayfs rootfs which doesn't with volatile. |
| 112 | +// |
| 113 | +// REF: https://man7.org/linux/man-pages/man1/strace.1.html |
| 114 | +func injectDelayToUmount2(ctx context.Context, t *testing.T, shimCli apitask.TaskService, delayInSec int) chan struct{} { |
| 115 | + pid := shimPid(ctx, t, shimCli) |
| 116 | + |
| 117 | + doneCh := make(chan struct{}) |
| 118 | + |
| 119 | + cmd := exec.CommandContext(ctx, "strace", |
| 120 | + "-p", strconv.Itoa(int(pid)), "-f", // attach to all the threads |
| 121 | + "--detach-on=execve", // stop to attach runc child-processes |
| 122 | + "--trace=umount2", // only trace umount2 syscall |
| 123 | + "-e", "inject=umount2:delay_enter="+strconv.Itoa(delayInSec)+"s", |
| 124 | + ) |
| 125 | + cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} |
| 126 | + |
| 127 | + pipeR, pipeW := io.Pipe() |
| 128 | + cmd.Stdout = pipeW |
| 129 | + cmd.Stderr = pipeW |
| 130 | + |
| 131 | + require.NoError(t, cmd.Start()) |
| 132 | + |
| 133 | + // ensure that strace has attached to the shim |
| 134 | + readyCh := make(chan struct{}) |
| 135 | + go func() { |
| 136 | + defer close(doneCh) |
| 137 | + |
| 138 | + bufReader := bufio.NewReader(pipeR) |
| 139 | + _, err := bufReader.Peek(1) |
| 140 | + assert.NoError(t, err, "failed to ensure that strace has attached to shim") |
| 141 | + |
| 142 | + close(readyCh) |
| 143 | + io.Copy(os.Stdout, bufReader) |
| 144 | + t.Logf("Strace has exited") |
| 145 | + }() |
| 146 | + |
| 147 | + go func() { |
| 148 | + defer pipeW.Close() |
| 149 | + assert.NoError(t, cmd.Wait(), "strace should exit with zero code") |
| 150 | + }() |
| 151 | + |
| 152 | + <-readyCh |
| 153 | + return doneCh |
| 154 | +} |
| 155 | + |
| 156 | +func connectToShim(ctx context.Context, t *testing.T, id string) apitask.TaskService { |
| 157 | + addr, err := shim.SocketAddress(ctx, containerdEndpoint, id) |
| 158 | + require.NoError(t, err) |
| 159 | + addr = strings.TrimPrefix(addr, "unix://") |
| 160 | + |
| 161 | + conn, err := net.Dial("unix", addr) |
| 162 | + require.NoError(t, err) |
| 163 | + |
| 164 | + client := ttrpc.NewClient(conn) |
| 165 | + return apitask.NewTaskClient(client) |
| 166 | +} |
| 167 | + |
| 168 | +func shimPid(ctx context.Context, t *testing.T, shimCli apitask.TaskService) uint32 { |
| 169 | + resp, err := shimCli.Connect(ctx, &apitask.ConnectRequest{}) |
| 170 | + require.NoError(t, err) |
| 171 | + return resp.GetShimPid() |
| 172 | +} |
0 commit comments