resize_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //go:build linux
  2. package daemon
  3. import (
  4. "context"
  5. "testing"
  6. "github.com/docker/docker/container"
  7. "github.com/docker/docker/libcontainerd/types"
  8. "gotest.tools/v3/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: container.NewExecStore(),
  15. }
  16. c := &container.Container{
  17. ExecCommands: container.NewExecStore(),
  18. }
  19. ec := &container.ExecConfig{
  20. ID: n,
  21. Container: c,
  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 execResizeMockProcess struct {
  28. types.Process
  29. Width, Height int
  30. }
  31. func (p *execResizeMockProcess) Resize(ctx context.Context, width, height uint32) error {
  32. p.Width = int(width)
  33. p.Height = int(height)
  34. return nil
  35. }
  36. // This test is to make sure that when exec context is ready, resize should call ResizeTerminal via containerd client.
  37. func TestExecResize(t *testing.T) {
  38. n := "TestExecResize"
  39. width := 24
  40. height := 8
  41. mp := &execResizeMockProcess{}
  42. d := &Daemon{
  43. execCommands: container.NewExecStore(),
  44. containers: container.NewMemoryStore(),
  45. }
  46. c := &container.Container{
  47. ID: n,
  48. ExecCommands: container.NewExecStore(),
  49. State: &container.State{Running: true},
  50. }
  51. ec := &container.ExecConfig{
  52. ID: n,
  53. Container: c,
  54. Process: mp,
  55. Started: make(chan struct{}),
  56. }
  57. close(ec.Started)
  58. d.containers.Add(n, c)
  59. d.registerExecCommand(c, ec)
  60. err := d.ContainerExecResize(n, height, width)
  61. assert.NilError(t, err)
  62. assert.Equal(t, mp.Width, width)
  63. assert.Equal(t, mp.Height, height)
  64. }
  65. // This test is to make sure that when exec context is not ready, a timeout error should happen.
  66. // TODO: the expect running time for this test is 10s, which would be too long for unit test.
  67. func TestExecResizeTimeout(t *testing.T) {
  68. n := "TestExecResize"
  69. width := 24
  70. height := 8
  71. mp := &execResizeMockProcess{}
  72. d := &Daemon{
  73. execCommands: container.NewExecStore(),
  74. containers: container.NewMemoryStore(),
  75. }
  76. c := &container.Container{
  77. ID: n,
  78. ExecCommands: container.NewExecStore(),
  79. State: &container.State{Running: true},
  80. }
  81. ec := &container.ExecConfig{
  82. ID: n,
  83. Container: c,
  84. Process: mp,
  85. Started: make(chan struct{}),
  86. }
  87. d.containers.Add(n, c)
  88. d.registerExecCommand(c, ec)
  89. err := d.ContainerExecResize(n, height, width)
  90. assert.ErrorContains(t, err, "timeout waiting for exec session ready")
  91. }