Skip to content

Commit d8f8242

Browse files
committed
integration: add case to reproduce #7496
Update: change imports and use pb's field instead of GetXXX. Signed-off-by: Wei Fu <[email protected]> (cherry picked from commit 5bdd9ca) Signed-off-by: Wei Fu <[email protected]>
1 parent c523c1d commit d8f8242

1 file changed

Lines changed: 171 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)