resize_test.go 2.6 KB

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