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