resize_test.go 2.5 KB

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