|
| 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 sbserver |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + criconfig "github.com/containerd/containerd/pkg/cri/config" |
| 24 | + "github.com/containerd/containerd/plugin" |
| 25 | + "github.com/opencontainers/runc/libcontainer/cgroups/systemd" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + runtime "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 28 | +) |
| 29 | + |
| 30 | +func newFakeRuntimeConfig(runcV2, systemdCgroup bool) criconfig.Runtime { |
| 31 | + r := criconfig.Runtime{Type: "default", Options: map[string]interface{}{}} |
| 32 | + if runcV2 { |
| 33 | + r.Type = plugin.RuntimeRuncV2 |
| 34 | + if systemdCgroup { |
| 35 | + r.Options["SystemdCgroup"] = true |
| 36 | + } |
| 37 | + } |
| 38 | + return r |
| 39 | +} |
| 40 | + |
| 41 | +func TestRuntimeConfig(t *testing.T) { |
| 42 | + autoDetected := runtime.CgroupDriver_CGROUPFS |
| 43 | + if systemd.IsRunningSystemd() { |
| 44 | + autoDetected = runtime.CgroupDriver_SYSTEMD |
| 45 | + } |
| 46 | + |
| 47 | + for _, test := range []struct { |
| 48 | + desc string |
| 49 | + defaultRuntime string |
| 50 | + runtimes map[string]criconfig.Runtime |
| 51 | + expectedCgroupDriver runtime.CgroupDriver |
| 52 | + }{ |
| 53 | + { |
| 54 | + desc: "no runtimes", |
| 55 | + expectedCgroupDriver: autoDetected, |
| 56 | + }, |
| 57 | + { |
| 58 | + desc: "non-runc runtime", |
| 59 | + defaultRuntime: "non-runc", |
| 60 | + runtimes: map[string]criconfig.Runtime{"non-runc": newFakeRuntimeConfig(false, false)}, |
| 61 | + expectedCgroupDriver: autoDetected, |
| 62 | + }, |
| 63 | + { |
| 64 | + desc: "no default, pick first in alphabetical order", |
| 65 | + runtimes: map[string]criconfig.Runtime{ |
| 66 | + "non-runc": newFakeRuntimeConfig(false, false), |
| 67 | + "runc-2": newFakeRuntimeConfig(true, true), |
| 68 | + "runc": newFakeRuntimeConfig(true, false), |
| 69 | + "non-runc-2": newFakeRuntimeConfig(false, false), |
| 70 | + }, |
| 71 | + expectedCgroupDriver: runtime.CgroupDriver_CGROUPFS, |
| 72 | + }, |
| 73 | + { |
| 74 | + desc: "pick default, cgroupfs", |
| 75 | + defaultRuntime: "runc-2", |
| 76 | + runtimes: map[string]criconfig.Runtime{ |
| 77 | + "non-runc": newFakeRuntimeConfig(false, false), |
| 78 | + "runc": newFakeRuntimeConfig(true, true), |
| 79 | + "runc-2": newFakeRuntimeConfig(true, false), |
| 80 | + }, |
| 81 | + expectedCgroupDriver: runtime.CgroupDriver_CGROUPFS, |
| 82 | + }, |
| 83 | + { |
| 84 | + desc: "pick default, systemd", |
| 85 | + defaultRuntime: "runc-2", |
| 86 | + runtimes: map[string]criconfig.Runtime{ |
| 87 | + "non-runc": newFakeRuntimeConfig(false, false), |
| 88 | + "runc": newFakeRuntimeConfig(true, false), |
| 89 | + "runc-2": newFakeRuntimeConfig(true, true), |
| 90 | + }, |
| 91 | + expectedCgroupDriver: runtime.CgroupDriver_SYSTEMD, |
| 92 | + }, |
| 93 | + } { |
| 94 | + test := test |
| 95 | + t.Run(test.desc, func(t *testing.T) { |
| 96 | + c := newTestCRIService() |
| 97 | + c.config.PluginConfig.ContainerdConfig.DefaultRuntimeName = test.defaultRuntime |
| 98 | + c.config.PluginConfig.ContainerdConfig.Runtimes = test.runtimes |
| 99 | + |
| 100 | + resp, err := c.RuntimeConfig(context.TODO(), &runtime.RuntimeConfigRequest{}) |
| 101 | + assert.NoError(t, err) |
| 102 | + assert.Equal(t, test.expectedCgroupDriver, resp.Linux.CgroupDriver, "got unexpected cgroup driver") |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments