github.com/rumpl/bof@v23.0.0-rc.2+incompatible/integration/container/kill_test.go (about) 1 package container // import "github.com/docker/docker/integration/container" 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/docker/docker/client" 9 "github.com/docker/docker/integration/internal/container" 10 "github.com/docker/docker/testutil/request" 11 "gotest.tools/v3/assert" 12 is "gotest.tools/v3/assert/cmp" 13 "gotest.tools/v3/poll" 14 "gotest.tools/v3/skip" 15 ) 16 17 func TestKillContainerInvalidSignal(t *testing.T) { 18 defer setupTest(t)() 19 client := testEnv.APIClient() 20 ctx := context.Background() 21 id := container.Run(ctx, t, client) 22 23 err := client.ContainerKill(ctx, id, "0") 24 assert.ErrorContains(t, err, "Error response from daemon:") 25 assert.ErrorContains(t, err, "nvalid signal: 0") // match "(I|i)nvalid" case-insensitive to allow testing against older daemons. 26 poll.WaitOn(t, container.IsInState(ctx, client, id, "running"), poll.WithDelay(100*time.Millisecond)) 27 28 err = client.ContainerKill(ctx, id, "SIG42") 29 assert.ErrorContains(t, err, "Error response from daemon:") 30 assert.ErrorContains(t, err, "nvalid signal: SIG42") // match "(I|i)nvalid" case-insensitive to allow testing against older daemons. 31 poll.WaitOn(t, container.IsInState(ctx, client, id, "running"), poll.WithDelay(100*time.Millisecond)) 32 } 33 34 func TestKillContainer(t *testing.T) { 35 defer setupTest(t)() 36 client := testEnv.APIClient() 37 38 testCases := []struct { 39 doc string 40 signal string 41 status string 42 skipOs string 43 }{ 44 { 45 doc: "no signal", 46 signal: "", 47 status: "exited", 48 skipOs: "", 49 }, 50 { 51 doc: "non killing signal", 52 signal: "SIGWINCH", 53 status: "running", 54 skipOs: "windows", 55 }, 56 { 57 doc: "killing signal", 58 signal: "SIGTERM", 59 status: "exited", 60 skipOs: "", 61 }, 62 } 63 64 for _, tc := range testCases { 65 tc := tc 66 t.Run(tc.doc, func(t *testing.T) { 67 skip.If(t, testEnv.OSType == tc.skipOs, "Windows does not support SIGWINCH") 68 ctx := context.Background() 69 id := container.Run(ctx, t, client) 70 err := client.ContainerKill(ctx, id, tc.signal) 71 assert.NilError(t, err) 72 73 poll.WaitOn(t, container.IsInState(ctx, client, id, tc.status), poll.WithDelay(100*time.Millisecond)) 74 }) 75 } 76 } 77 78 func TestKillWithStopSignalAndRestartPolicies(t *testing.T) { 79 skip.If(t, testEnv.OSType == "windows", "Windows only supports 1.25 or later") 80 defer setupTest(t)() 81 client := testEnv.APIClient() 82 83 testCases := []struct { 84 doc string 85 stopsignal string 86 status string 87 }{ 88 { 89 doc: "same-signal-disables-restart-policy", 90 stopsignal: "TERM", 91 status: "exited", 92 }, 93 { 94 doc: "different-signal-keep-restart-policy", 95 stopsignal: "CONT", 96 status: "running", 97 }, 98 } 99 100 for _, tc := range testCases { 101 tc := tc 102 t.Run(tc.doc, func(t *testing.T) { 103 ctx := context.Background() 104 id := container.Run(ctx, t, client, 105 container.WithRestartPolicy("always"), 106 func(c *container.TestContainerConfig) { 107 c.Config.StopSignal = tc.stopsignal 108 }) 109 err := client.ContainerKill(ctx, id, "TERM") 110 assert.NilError(t, err) 111 112 poll.WaitOn(t, container.IsInState(ctx, client, id, tc.status), poll.WithDelay(100*time.Millisecond)) 113 }) 114 } 115 } 116 117 func TestKillStoppedContainer(t *testing.T) { 118 skip.If(t, testEnv.OSType == "windows", "Windows only supports 1.25 or later") 119 defer setupTest(t)() 120 ctx := context.Background() 121 client := testEnv.APIClient() 122 id := container.Create(ctx, t, client) 123 err := client.ContainerKill(ctx, id, "SIGKILL") 124 assert.Assert(t, is.ErrorContains(err, "")) 125 assert.Assert(t, is.Contains(err.Error(), "is not running")) 126 } 127 128 func TestKillStoppedContainerAPIPre120(t *testing.T) { 129 skip.If(t, testEnv.OSType == "windows", "Windows only supports 1.25 or later") 130 defer setupTest(t)() 131 ctx := context.Background() 132 client := request.NewAPIClient(t, client.WithVersion("1.19")) 133 id := container.Create(ctx, t, client) 134 err := client.ContainerKill(ctx, id, "SIGKILL") 135 assert.NilError(t, err) 136 } 137 138 func TestKillDifferentUserContainer(t *testing.T) { 139 // TODO Windows: Windows does not yet support -u (Feb 2016). 140 skip.If(t, testEnv.OSType == "windows", "User containers (container.Config.User) are not yet supported on %q platform", testEnv.OSType) 141 142 defer setupTest(t)() 143 ctx := context.Background() 144 client := request.NewAPIClient(t, client.WithVersion("1.19")) 145 146 id := container.Run(ctx, t, client, func(c *container.TestContainerConfig) { 147 c.Config.User = "daemon" 148 }) 149 poll.WaitOn(t, container.IsInState(ctx, client, id, "running"), poll.WithDelay(100*time.Millisecond)) 150 151 err := client.ContainerKill(ctx, id, "SIGKILL") 152 assert.NilError(t, err) 153 poll.WaitOn(t, container.IsInState(ctx, client, id, "exited"), poll.WithDelay(100*time.Millisecond)) 154 } 155 156 func TestInspectOomKilledTrue(t *testing.T) { 157 skip.If(t, testEnv.DaemonInfo.OSType == "windows") 158 skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none") 159 skip.If(t, !testEnv.DaemonInfo.MemoryLimit || !testEnv.DaemonInfo.SwapLimit) 160 skip.If(t, testEnv.DaemonInfo.CgroupVersion == "2", "FIXME: flaky on cgroup v2 (https://github.com/moby/moby/issues/41929)") 161 162 defer setupTest(t)() 163 ctx := context.Background() 164 client := testEnv.APIClient() 165 166 cID := container.Run(ctx, t, client, container.WithCmd("sh", "-c", "x=a; while true; do x=$x$x$x$x; done"), func(c *container.TestContainerConfig) { 167 c.HostConfig.Resources.Memory = 32 * 1024 * 1024 168 }) 169 170 poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond)) 171 172 inspect, err := client.ContainerInspect(ctx, cID) 173 assert.NilError(t, err) 174 assert.Check(t, is.Equal(true, inspect.State.OOMKilled)) 175 } 176 177 func TestInspectOomKilledFalse(t *testing.T) { 178 skip.If(t, testEnv.DaemonInfo.OSType == "windows" || !testEnv.DaemonInfo.MemoryLimit || !testEnv.DaemonInfo.SwapLimit) 179 180 defer setupTest(t)() 181 ctx := context.Background() 182 client := testEnv.APIClient() 183 184 cID := container.Run(ctx, t, client, container.WithCmd("sh", "-c", "echo hello world")) 185 186 poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond)) 187 188 inspect, err := client.ContainerInspect(ctx, cID) 189 assert.NilError(t, err) 190 assert.Check(t, is.Equal(false, inspect.State.OOMKilled)) 191 }