|
| 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 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "syscall" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/containerd/cgroups/v3" |
| 28 | + "github.com/containerd/containerd/v2/integration/images" |
| 29 | + "github.com/containerd/containerd/v2/integration/remote" |
| 30 | + "github.com/stretchr/testify/assert" |
| 31 | + "github.com/stretchr/testify/require" |
| 32 | + runtime "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 33 | +) |
| 34 | + |
| 35 | +func newContainerdProcess(t *testing.T, cgroupWritable bool) *ctrdProc { |
| 36 | + configDir := t.TempDir() |
| 37 | + configPath := filepath.Join(configDir, "config.toml") |
| 38 | + config := fmt.Sprintf(` |
| 39 | +version = 3 |
| 40 | +
|
| 41 | +[plugins.'io.containerd.cri.v1.runtime'.containerd.runtimes.runc] |
| 42 | + cgroup_writable = %t |
| 43 | +`, |
| 44 | + cgroupWritable) |
| 45 | + |
| 46 | + err := os.WriteFile(configPath, []byte(config), 0600) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + currentProc := newCtrdProc(t, "containerd", configDir, nil) |
| 50 | + require.NoError(t, currentProc.isReady()) |
| 51 | + |
| 52 | + return currentProc |
| 53 | +} |
| 54 | + |
| 55 | +func TestContainerCgroupWritable(t *testing.T) { |
| 56 | + t.Parallel() |
| 57 | + |
| 58 | + if cgroups.Mode() != cgroups.Unified { |
| 59 | + t.Skip("requires cgroup v2") |
| 60 | + } |
| 61 | + |
| 62 | + testCases := []struct { |
| 63 | + name string |
| 64 | + cgroupWritable bool |
| 65 | + }{ |
| 66 | + { |
| 67 | + name: "writable cgroup", |
| 68 | + cgroupWritable: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "readonly cgroup", |
| 72 | + cgroupWritable: false, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, testCase := range testCases { |
| 77 | + t.Run(testCase.name, func(t *testing.T) { |
| 78 | + currentProc := newContainerdProcess(t, testCase.cgroupWritable) |
| 79 | + |
| 80 | + // Get the runtime service |
| 81 | + runtimeService, err := remote.NewRuntimeService(currentProc.grpcAddress(), 1*time.Minute) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + t.Cleanup(func() { |
| 85 | + cleanupPods(t, runtimeService) |
| 86 | + t.Log("Stopping containerd process") |
| 87 | + require.NoError(t, currentProc.kill(syscall.SIGTERM)) |
| 88 | + require.NoError(t, currentProc.wait(5*time.Minute)) |
| 89 | + }) |
| 90 | + |
| 91 | + imageName := images.Get(images.BusyBox) |
| 92 | + pullImagesByCRI(t, currentProc.criImageService(t), imageName) |
| 93 | + |
| 94 | + // Create a test sandbox |
| 95 | + sbConfig := &runtime.PodSandboxConfig{ |
| 96 | + Metadata: &runtime.PodSandboxMetadata{ |
| 97 | + Name: "sandbox", |
| 98 | + Namespace: "cgroup-writable", |
| 99 | + }, |
| 100 | + } |
| 101 | + sb, err := runtimeService.RunPodSandbox(sbConfig, "") |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + containerName := "cgroup-writable-test" |
| 105 | + cnConfig := &runtime.ContainerConfig{ |
| 106 | + Metadata: &runtime.ContainerMetadata{ |
| 107 | + Name: containerName, |
| 108 | + }, |
| 109 | + Image: &runtime.ImageSpec{ |
| 110 | + Image: imageName, |
| 111 | + }, |
| 112 | + Command: []string{"sh", "-c", "sleep 1d"}, |
| 113 | + } |
| 114 | + |
| 115 | + cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig) |
| 116 | + require.NoError(t, err) |
| 117 | + defer func() { |
| 118 | + assert.NoError(t, runtimeService.RemoveContainer(cn)) |
| 119 | + }() |
| 120 | + |
| 121 | + require.NoError(t, runtimeService.StartContainer(cn)) |
| 122 | + defer func() { |
| 123 | + assert.NoError(t, runtimeService.StopContainer(cn, 30)) |
| 124 | + }() |
| 125 | + |
| 126 | + status, err := runtimeService.ContainerStatus(cn) |
| 127 | + require.NoError(t, err) |
| 128 | + assert.Equal(t, status.GetState(), runtime.ContainerState_CONTAINER_RUNNING) |
| 129 | + |
| 130 | + // Execute a command to verify if cgroup is writable |
| 131 | + _, stderr, err := runtimeService.ExecSync(cn, []string{"mkdir", "sys/fs/cgroup/dummy-group"}, 2) |
| 132 | + if testCase.cgroupWritable { |
| 133 | + require.NoError(t, err) |
| 134 | + require.Empty(t, stderr) |
| 135 | + } else { |
| 136 | + require.Error(t, err) |
| 137 | + require.Contains(t, string(stderr), "mkdir: can't create directory 'sys/fs/cgroup/dummy-group': Read-only file system") |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments