github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/container/stop_linux_test.go (about) 1 package container // import "github.com/demonoid81/moby/integration/container" 2 3 import ( 4 "context" 5 "fmt" 6 "strconv" 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/demonoid81/moby/api/types" 12 "github.com/demonoid81/moby/integration/internal/container" 13 "gotest.tools/v3/assert" 14 "gotest.tools/v3/icmd" 15 "gotest.tools/v3/poll" 16 "gotest.tools/v3/skip" 17 ) 18 19 // TestStopContainerWithTimeout checks that ContainerStop with 20 // a timeout works as documented, i.e. in case of negative timeout 21 // waiting is not limited (issue #35311). 22 func TestStopContainerWithTimeout(t *testing.T) { 23 defer setupTest(t)() 24 client := testEnv.APIClient() 25 ctx := context.Background() 26 27 testCmd := container.WithCmd("sh", "-c", "sleep 2 && exit 42") 28 testData := []struct { 29 doc string 30 timeout int 31 expectedExitCode int 32 }{ 33 // In case container is forcefully killed, 137 is returned, 34 // otherwise the exit code from the above script 35 { 36 "zero timeout: expect forceful container kill", 37 0, 137, 38 }, 39 { 40 "too small timeout: expect forceful container kill", 41 1, 137, 42 }, 43 { 44 "big enough timeout: expect graceful container stop", 45 3, 42, 46 }, 47 { 48 "unlimited timeout: expect graceful container stop", 49 -1, 42, 50 }, 51 } 52 53 for _, d := range testData { 54 d := d 55 t.Run(strconv.Itoa(d.timeout), func(t *testing.T) { 56 t.Parallel() 57 id := container.Run(ctx, t, client, testCmd) 58 59 timeout := time.Duration(d.timeout) * time.Second 60 err := client.ContainerStop(ctx, id, &timeout) 61 assert.NilError(t, err) 62 63 poll.WaitOn(t, container.IsStopped(ctx, client, id), 64 poll.WithDelay(100*time.Millisecond)) 65 66 inspect, err := client.ContainerInspect(ctx, id) 67 assert.NilError(t, err) 68 assert.Equal(t, inspect.State.ExitCode, d.expectedExitCode) 69 }) 70 } 71 } 72 73 func TestDeleteDevicemapper(t *testing.T) { 74 skip.If(t, testEnv.DaemonInfo.Driver != "devicemapper") 75 skip.If(t, testEnv.IsRemoteDaemon) 76 77 defer setupTest(t)() 78 client := testEnv.APIClient() 79 ctx := context.Background() 80 81 id := container.Run(ctx, t, client, container.WithName("foo-"+t.Name()), container.WithCmd("echo")) 82 83 poll.WaitOn(t, container.IsStopped(ctx, client, id), poll.WithDelay(100*time.Millisecond)) 84 85 inspect, err := client.ContainerInspect(ctx, id) 86 assert.NilError(t, err) 87 88 deviceID := inspect.GraphDriver.Data["DeviceId"] 89 90 // Find pool name from device name 91 deviceName := inspect.GraphDriver.Data["DeviceName"] 92 devicePrefix := deviceName[:strings.LastIndex(deviceName, "-")] 93 devicePool := fmt.Sprintf("/dev/mapper/%s-pool", devicePrefix) 94 95 result := icmd.RunCommand("dmsetup", "message", devicePool, "0", fmt.Sprintf("delete %s", deviceID)) 96 result.Assert(t, icmd.Success) 97 98 err = client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{}) 99 assert.NilError(t, err) 100 }