|
| 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 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/containerd/containerd/v2/integration/images" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +func TestContainerIOLeakAfterStartFailed(t *testing.T) { |
| 33 | + if f := os.Getenv("RUNC_FLAVOR"); f != "" && f != "runc" { |
| 34 | + t.Skip("test requires runc") |
| 35 | + } |
| 36 | + t.Log("Create a sandbox") |
| 37 | + sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "container-io-leak-after-start-failed") |
| 38 | + testImage := images.Get(images.BusyBox) |
| 39 | + EnsureImageExists(t, testImage) |
| 40 | + |
| 41 | + t.Log("Create a container") |
| 42 | + cnConfig := ContainerConfig( |
| 43 | + "containerIOLeakTest", |
| 44 | + testImage, |
| 45 | + WithCommand("something-that-doesnt-exist"), |
| 46 | + ) |
| 47 | + t.Log("Create the container") |
| 48 | + cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig) |
| 49 | + require.NoError(t, err) |
| 50 | + pid := getShimPid(t, sb) |
| 51 | + require.Error(t, runtimeService.StartContainer(cn)) |
| 52 | + assert.Equal(t, 0, numPipe(pid)) |
| 53 | +} |
| 54 | + |
| 55 | +func numPipe(shimPid int) int { |
| 56 | + cmd := exec.Command("sh", "-c", fmt.Sprintf("lsof -p %d | grep pipe", shimPid)) |
| 57 | + var stdout bytes.Buffer |
| 58 | + cmd.Stdout = &stdout |
| 59 | + if err := cmd.Run(); err != nil { |
| 60 | + return 0 |
| 61 | + } |
| 62 | + return strings.Count(stdout.String(), "\n") |
| 63 | +} |
0 commit comments