resize_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package container
  2. import (
  3. "context"
  4. "net/http"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/api/types/network"
  10. req "github.com/docker/docker/integration-cli/request"
  11. "github.com/docker/docker/integration/util/request"
  12. "github.com/docker/docker/internal/testutil"
  13. "github.com/gotestyourself/gotestyourself/poll"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. func TestResize(t *testing.T) {
  18. defer setupTest(t)()
  19. client := request.NewAPIClient(t)
  20. ctx := context.Background()
  21. cID := runSimpleContainer(ctx, t, client, "")
  22. poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
  23. err := client.ContainerResize(ctx, cID, types.ResizeOptions{
  24. Height: 40,
  25. Width: 40,
  26. })
  27. require.NoError(t, err)
  28. }
  29. func TestResizeWithInvalidSize(t *testing.T) {
  30. defer setupTest(t)()
  31. client := request.NewAPIClient(t)
  32. ctx := context.Background()
  33. cID := runSimpleContainer(ctx, t, client, "")
  34. poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
  35. endpoint := "/containers/" + cID + "/resize?h=foo&w=bar"
  36. res, _, err := req.Post(endpoint)
  37. require.NoError(t, err)
  38. assert.Equal(t, res.StatusCode, http.StatusBadRequest)
  39. }
  40. func TestResizeWhenContainerNotStarted(t *testing.T) {
  41. defer setupTest(t)()
  42. client := request.NewAPIClient(t)
  43. ctx := context.Background()
  44. cID := runSimpleContainer(ctx, t, client, "", func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig) {
  45. config.Cmd = []string{"echo"}
  46. })
  47. poll.WaitOn(t, containerIsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
  48. err := client.ContainerResize(ctx, cID, types.ResizeOptions{
  49. Height: 40,
  50. Width: 40,
  51. })
  52. testutil.ErrorContains(t, err, "is not running")
  53. }