hook_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "net"
  16. "os"
  17. "testing"
  18. "github.com/compose-spec/compose-go/v2/types"
  19. "github.com/containerd/console"
  20. "github.com/docker/cli/cli/streams"
  21. "github.com/moby/moby/api/types/container"
  22. "github.com/moby/moby/client"
  23. "go.uber.org/mock/gomock"
  24. "gotest.tools/v3/assert"
  25. "github.com/docker/compose/v5/pkg/api"
  26. "github.com/docker/compose/v5/pkg/mocks"
  27. )
  28. // TestRunHook_ConsoleSize verifies that ConsoleSize is only passed to ExecAttach
  29. // when the service has TTY enabled. When TTY is disabled, passing a non-zero
  30. // ConsoleSize causes the Docker daemon to return "console size is only supported
  31. // when TTY is enabled" (regression introduced in v5.1.0).
  32. func TestRunHook_ConsoleSize(t *testing.T) {
  33. tests := []struct {
  34. name string
  35. tty bool
  36. expectedConsole client.ConsoleSize
  37. }{
  38. {
  39. name: "no tty - ConsoleSize must be zero",
  40. tty: false,
  41. expectedConsole: client.ConsoleSize{},
  42. },
  43. {
  44. name: "with tty - ConsoleSize should reflect terminal dimensions",
  45. tty: true,
  46. expectedConsole: client.ConsoleSize{Width: 80, Height: 24},
  47. },
  48. }
  49. for _, tc := range tests {
  50. t.Run(tc.name, func(t *testing.T) {
  51. mockCtrl := gomock.NewController(t)
  52. defer mockCtrl.Finish()
  53. mockAPI := mocks.NewMockAPIClient(mockCtrl)
  54. mockCli := mocks.NewMockCli(mockCtrl)
  55. mockCli.EXPECT().Client().Return(mockAPI).AnyTimes()
  56. mockCli.EXPECT().Err().Return(streams.NewOut(os.Stderr)).AnyTimes()
  57. // Create a PTY so GetTtySize() returns real non-zero dimensions,
  58. // simulating an interactive terminal session.
  59. pty, slavePath, err := console.NewPty()
  60. assert.NilError(t, err)
  61. defer pty.Close() //nolint:errcheck
  62. assert.NilError(t, pty.Resize(console.WinSize{Height: 24, Width: 80}))
  63. slaveFile, err := os.OpenFile(slavePath, os.O_RDWR, 0)
  64. assert.NilError(t, err)
  65. defer slaveFile.Close() //nolint:errcheck
  66. mockCli.EXPECT().Out().Return(streams.NewOut(slaveFile)).AnyTimes()
  67. service := types.ServiceConfig{
  68. Name: "test",
  69. Tty: tc.tty,
  70. }
  71. hook := types.ServiceHook{Command: []string{"echo", "hello"}}
  72. ctr := container.Summary{ID: "container123"}
  73. mockAPI.EXPECT().
  74. ExecCreate(gomock.Any(), "container123", gomock.Any()).
  75. Return(client.ExecCreateResult{ID: "exec123"}, nil)
  76. // Return a pipe that immediately closes so the reader gets EOF.
  77. serverConn, clientConn := net.Pipe()
  78. serverConn.Close() //nolint:errcheck
  79. mockAPI.EXPECT().
  80. ExecAttach(gomock.Any(), "exec123", client.ExecAttachOptions{
  81. TTY: tc.tty,
  82. ConsoleSize: tc.expectedConsole,
  83. }).
  84. Return(client.ExecAttachResult{
  85. HijackedResponse: client.NewHijackedResponse(clientConn, ""),
  86. }, nil)
  87. mockAPI.EXPECT().
  88. ExecInspect(gomock.Any(), "exec123", gomock.Any()).
  89. Return(client.ExecInspectResult{ExitCode: 0}, nil)
  90. s, err := NewComposeService(mockCli)
  91. assert.NilError(t, err)
  92. noopListener := func(api.ContainerEvent) {}
  93. err = s.(*composeService).runHook(t.Context(), ctr, service, hook, noopListener)
  94. assert.NilError(t, err)
  95. })
  96. }
  97. }