github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/container/kill_test.go (about)

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