|
| 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 23 | +) |
| 24 | + |
| 25 | +func TestRunWithSystemdAlways(t *testing.T) { |
| 26 | + testutil.DockerIncompatible(t) |
| 27 | + t.Parallel() |
| 28 | + base := testutil.NewBase(t) |
| 29 | + containerName := testutil.Identifier(t) |
| 30 | + defer base.Cmd("container", "rm", "-f", containerName).AssertOK() |
| 31 | + |
| 32 | + base.Cmd("run", "--name", containerName, "--systemd=always", "--entrypoint=/bin/bash", testutil.UbuntuImage, "-c", "mount | grep cgroup").AssertOutContains("(rw,") |
| 33 | + |
| 34 | + base.Cmd("inspect", "--format", "{{json .Config.Labels}}", containerName).AssertOutContains("SIGRTMIN+3") |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | +func TestRunWithSystemdTrueEnabled(t *testing.T) { |
| 39 | + testutil.DockerIncompatible(t) |
| 40 | + t.Parallel() |
| 41 | + base := testutil.NewBase(t) |
| 42 | + containerName := testutil.Identifier(t) |
| 43 | + defer base.Cmd("container", "rm", "-f", containerName).AssertOK() |
| 44 | + |
| 45 | + base.Cmd("run", "-d", "--name", containerName, "--systemd=true", "--entrypoint=/sbin/init", testutil.SystemdImage).AssertOK() |
| 46 | + |
| 47 | + base.Cmd("inspect", "--format", "{{json .Config.Labels}}", containerName).AssertOutContains("SIGRTMIN+3") |
| 48 | + |
| 49 | + base.Cmd("exec", containerName, "systemctl", "list-jobs").AssertOutContains("jobs listed.") |
| 50 | +} |
| 51 | + |
| 52 | +func TestRunWithSystemdTrueDisabled(t *testing.T) { |
| 53 | + testutil.DockerIncompatible(t) |
| 54 | + t.Parallel() |
| 55 | + base := testutil.NewBase(t) |
| 56 | + containerName := testutil.Identifier(t) |
| 57 | + defer base.Cmd("rm", "-f", containerName).AssertOK() |
| 58 | + |
| 59 | + base.Cmd("run", "--name", containerName, "--systemd=true", "--entrypoint=/bin/bash", testutil.SystemdImage, "-c", "systemctl list-jobs || true").AssertCombinedOutContains("System has not been booted with systemd as init system") |
| 60 | +} |
| 61 | + |
| 62 | +func TestRunWithSystemdFalse(t *testing.T) { |
| 63 | + testutil.DockerIncompatible(t) |
| 64 | + t.Parallel() |
| 65 | + base := testutil.NewBase(t) |
| 66 | + containerName := testutil.Identifier(t) |
| 67 | + defer base.Cmd("rm", "-f", containerName).AssertOK() |
| 68 | + |
| 69 | + base.Cmd("run", "--name", containerName, "--systemd=false", "--entrypoint=/bin/bash", testutil.UbuntuImage, "-c", "mount | grep cgroup").AssertOutContains("(ro,") |
| 70 | + |
| 71 | + base.Cmd("inspect", "--format", "{{json .Config.Labels}}", containerName).AssertOutContains("SIGTERM") |
| 72 | +} |
| 73 | + |
| 74 | +func TestRunWithNoSystemd(t *testing.T) { |
| 75 | + testutil.DockerIncompatible(t) |
| 76 | + t.Parallel() |
| 77 | + base := testutil.NewBase(t) |
| 78 | + containerName := testutil.Identifier(t) |
| 79 | + defer base.Cmd("rm", "-f", containerName).AssertOK() |
| 80 | + |
| 81 | + base.Cmd("run", "--name", containerName, "--entrypoint=/bin/bash", testutil.UbuntuImage, "-c", "mount | grep cgroup").AssertOutContains("(ro,") |
| 82 | + |
| 83 | + base.Cmd("inspect", "--format", "{{json .Config.Labels}}", containerName).AssertOutContains("SIGTERM") |
| 84 | +} |
| 85 | + |
| 86 | +func TestRunWithSystemdPrivilegedError(t *testing.T) { |
| 87 | + testutil.DockerIncompatible(t) |
| 88 | + t.Parallel() |
| 89 | + base := testutil.NewBase(t) |
| 90 | + |
| 91 | + base.Cmd("run", "--privileged", "--rm", "--systemd=always", "--entrypoint=/sbin/init", testutil.SystemdImage).AssertCombinedOutContains("if --privileged is used with systemd `--security-opt privileged-without-host-devices` must also be used") |
| 92 | +} |
| 93 | + |
| 94 | +func TestRunWithSystemdPrivilegedSuccess(t *testing.T) { |
| 95 | + testutil.DockerIncompatible(t) |
| 96 | + t.Parallel() |
| 97 | + base := testutil.NewBase(t) |
| 98 | + containerName := testutil.Identifier(t) |
| 99 | + defer base.Cmd("container", "rm", "-f", containerName).AssertOK() |
| 100 | + |
| 101 | + base.Cmd("run", "-d", "--name", containerName, "--privileged", "--security-opt", "privileged-without-host-devices", "--systemd=true", "--entrypoint=/sbin/init", testutil.SystemdImage).AssertOK() |
| 102 | + |
| 103 | + base.Cmd("inspect", "--format", "{{json .Config.Labels}}", containerName).AssertOutContains("SIGRTMIN+3") |
| 104 | +} |
0 commit comments