stop_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package container
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/api/types/network"
  11. "github.com/docker/docker/client"
  12. "github.com/docker/docker/integration/util/request"
  13. "github.com/gotestyourself/gotestyourself/icmd"
  14. "github.com/gotestyourself/gotestyourself/poll"
  15. "github.com/gotestyourself/gotestyourself/skip"
  16. "github.com/stretchr/testify/require"
  17. )
  18. func TestDeleteDevicemapper(t *testing.T) {
  19. skip.IfCondition(t, testEnv.DaemonInfo.Driver != "devicemapper")
  20. defer setupTest(t)()
  21. client := request.NewAPIClient(t)
  22. ctx := context.Background()
  23. foo, err := client.ContainerCreate(ctx,
  24. &container.Config{
  25. Cmd: []string{"echo"},
  26. Image: "busybox",
  27. },
  28. &container.HostConfig{},
  29. &network.NetworkingConfig{},
  30. "foo",
  31. )
  32. require.NoError(t, err)
  33. err = client.ContainerStart(ctx, foo.ID, types.ContainerStartOptions{})
  34. require.NoError(t, err)
  35. inspect, err := client.ContainerInspect(ctx, foo.ID)
  36. require.NoError(t, err)
  37. poll.WaitOn(t, containerIsStopped(ctx, client, foo.ID), poll.WithDelay(100*time.Millisecond))
  38. deviceID := inspect.GraphDriver.Data["DeviceId"]
  39. // Find pool name from device name
  40. deviceName := inspect.GraphDriver.Data["DeviceName"]
  41. devicePrefix := deviceName[:strings.LastIndex(deviceName, "-")]
  42. devicePool := fmt.Sprintf("/dev/mapper/%s-pool", devicePrefix)
  43. result := icmd.RunCommand("dmsetup", "message", devicePool, "0", fmt.Sprintf("delete %s", deviceID))
  44. result.Assert(t, icmd.Success)
  45. err = client.ContainerRemove(ctx, foo.ID, types.ContainerRemoveOptions{})
  46. require.NoError(t, err)
  47. }
  48. func containerIsStopped(ctx context.Context, client client.APIClient, containerID string) func(log poll.LogT) poll.Result {
  49. return func(log poll.LogT) poll.Result {
  50. inspect, err := client.ContainerInspect(ctx, containerID)
  51. switch {
  52. case err != nil:
  53. return poll.Error(err)
  54. case !inspect.State.Running:
  55. return poll.Success()
  56. default:
  57. return poll.Continue("waiting for container to be stopped")
  58. }
  59. }
  60. }