resize_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // +build linux
  2. package daemon
  3. import (
  4. "context"
  5. "testing"
  6. "github.com/docker/docker/container"
  7. "github.com/docker/docker/daemon/exec"
  8. "github.com/gotestyourself/gotestyourself/assert"
  9. )
  10. // This test simply verify that when a wrong ID used, a specific error should be returned for exec resize.
  11. func TestExecResizeNoSuchExec(t *testing.T) {
  12. n := "TestExecResize"
  13. d := &Daemon{
  14. execCommands: exec.NewStore(),
  15. }
  16. c := &container.Container{
  17. ExecCommands: exec.NewStore(),
  18. }
  19. ec := &exec.Config{
  20. ID: n,
  21. }
  22. d.registerExecCommand(c, ec)
  23. err := d.ContainerExecResize("nil", 24, 8)
  24. assert.ErrorContains(t, err, "No such exec instance")
  25. }
  26. type execResizeMockContainerdClient struct {
  27. MockContainerdClient
  28. ProcessID string
  29. ContainerID string
  30. Width int
  31. Height int
  32. }
  33. func (c *execResizeMockContainerdClient) ResizeTerminal(ctx context.Context, containerID, processID string, width, height int) error {
  34. c.ProcessID = processID
  35. c.ContainerID = containerID
  36. c.Width = width
  37. c.Height = height
  38. return nil
  39. }
  40. // This test is to make sure that when exec context is ready, resize should call ResizeTerminal via containerd client.
  41. func TestExecResize(t *testing.T) {
  42. n := "TestExecResize"
  43. width := 24
  44. height := 8
  45. ec := &exec.Config{
  46. ID: n,
  47. ContainerID: n,
  48. Started: make(chan struct{}),
  49. }
  50. close(ec.Started)
  51. mc := &execResizeMockContainerdClient{}
  52. d := &Daemon{
  53. execCommands: exec.NewStore(),
  54. containerd: mc,
  55. containers: container.NewMemoryStore(),
  56. }
  57. c := &container.Container{
  58. ExecCommands: exec.NewStore(),
  59. State: &container.State{Running: true},
  60. }
  61. d.containers.Add(n, c)
  62. d.registerExecCommand(c, ec)
  63. err := d.ContainerExecResize(n, height, width)
  64. assert.NilError(t, err)
  65. assert.Equal(t, mc.Width, width)
  66. assert.Equal(t, mc.Height, height)
  67. assert.Equal(t, mc.ProcessID, n)
  68. assert.Equal(t, mc.ContainerID, n)
  69. }
  70. // This test is to make sure that when exec context is not ready, a timeout error should happen.
  71. // TODO: the expect running time for this test is 10s, which would be too long for unit test.
  72. func TestExecResizeTimeout(t *testing.T) {
  73. n := "TestExecResize"
  74. width := 24
  75. height := 8
  76. ec := &exec.Config{
  77. ID: n,
  78. ContainerID: n,
  79. Started: make(chan struct{}),
  80. }
  81. mc := &execResizeMockContainerdClient{}
  82. d := &Daemon{
  83. execCommands: exec.NewStore(),
  84. containerd: mc,
  85. containers: container.NewMemoryStore(),
  86. }
  87. c := &container.Container{
  88. ExecCommands: exec.NewStore(),
  89. State: &container.State{Running: true},
  90. }
  91. d.containers.Add(n, c)
  92. d.registerExecCommand(c, ec)
  93. err := d.ContainerExecResize(n, height, width)
  94. assert.ErrorContains(t, err, "timeout waiting for exec session ready")
  95. }