Skip to content

Commit

Permalink
Add integ test to check tty leak
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Wang <[email protected]>
  • Loading branch information
henry118 committed Dec 17, 2024
1 parent 74744d5 commit dfc8bd6
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions integration/container_tty_leak_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package integration

import (
"bytes"
"context"
"fmt"
"net/url"
"os/exec"
"strings"
"testing"

"github.com/containerd/containerd/v2/integration/images"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/client-go/rest"
remoteclient "k8s.io/client-go/tools/remotecommand"
criruntime "k8s.io/cri-api/pkg/apis/runtime/v1"
)

func TestContainerTTYLeakAfterExit(t *testing.T) {
t.Log("Create a sandbox")
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "container-tty-leak-after-exit")

var (
testImage = images.Get(images.BusyBox)
containerName = "test-container-tty-leak"
)

EnsureImageExists(t, testImage)

t.Log("Create a container")
cnConfig := ContainerConfig(
containerName,
testImage,
WithCommand("sh", "-c", "sleep 365d"),
)

cnConfig.Stdin = true
cnConfig.Tty = true

t.Log("Create the container")
cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig)
require.NoError(t, err)
defer func() {
assert.NoError(t, runtimeService.RemoveContainer(cn))
}()

t.Log("Start the container")
require.NoError(t, runtimeService.StartContainer(cn))

pid := getShimPid(t, sb)
assert.Equal(t, 1, numTTY(pid), "start container should increase tty count by 1")

t.Log("Exec in container")
rsp, err := runtimeService.Exec(&criruntime.ExecRequest{
ContainerId: cn,
Cmd: []string{"sh", "-c", "echo tty"},
Stderr: false,
Stdout: true,
Stdin: true,
Tty: true,
})
require.NoError(t, err)

execURL := rsp.Url
URL, err := url.Parse(execURL)
require.NoError(t, err)

outBuf := bytes.NewBuffer(make([]byte, 64))
inbuf := bytes.NewBuffer(nil)

executor, err := remoteclient.NewSPDYExecutor(&rest.Config{}, "POST", URL)
require.NoError(t, err)
streamOptions := remoteclient.StreamOptions{
Stdout: outBuf,
Stderr: outBuf,
Stdin: inbuf,
Tty: true,
}

require.NoError(t, executor.StreamWithContext(context.Background(), streamOptions))
assert.Equal(t, 1, numTTY(pid), "tty count should not increase after exec")

t.Log("Stop the container")
require.NoError(t, runtimeService.StopContainer(cn, 10))

assert.Equal(t, 0, numTTY(pid), "tty count should decrease of container stops")
}

func getShimPid(t *testing.T, sb string) int {
ctx := namespaces.WithNamespace(context.Background(), "k8s.io")
shimCli := connectToShim(ctx, t, containerdEndpoint, 3, sb)
return int(shimPid(ctx, t, shimCli))
}

func numTTY(shimPid int) int {
cmd := exec.Command("sh", "-c", fmt.Sprintf("lsof -p %d | grep ptmx", shimPid))
var stdout bytes.Buffer
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return 0
}
return strings.Count(stdout.String(), "\n")
}

0 comments on commit dfc8bd6

Please sign in to comment.