Skip to content

Commit 47b974e

Browse files
committed
integration: add case to reproduce #7496
Signed-off-by: Wei Fu <[email protected]>
1 parent 70a2c95 commit 47b974e

1 file changed

Lines changed: 181 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)