hook_test.go 3.5 KB

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