watch_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. "context"
  16. "fmt"
  17. "os"
  18. "slices"
  19. "strings"
  20. "testing"
  21. "time"
  22. "github.com/compose-spec/compose-go/v2/types"
  23. "github.com/docker/cli/cli/streams"
  24. "github.com/docker/compose/v2/internal/sync"
  25. "github.com/docker/compose/v2/pkg/api"
  26. "github.com/docker/compose/v2/pkg/mocks"
  27. "github.com/docker/compose/v2/pkg/watch"
  28. moby "github.com/docker/docker/api/types"
  29. "github.com/docker/docker/api/types/filters"
  30. "github.com/docker/docker/api/types/image"
  31. "github.com/jonboulle/clockwork"
  32. "github.com/stretchr/testify/require"
  33. "go.uber.org/mock/gomock"
  34. "gotest.tools/v3/assert"
  35. )
  36. func TestDebounceBatching(t *testing.T) {
  37. ch := make(chan fileEvent)
  38. clock := clockwork.NewFakeClock()
  39. ctx, stop := context.WithCancel(context.Background())
  40. t.Cleanup(stop)
  41. trigger := types.Trigger{
  42. Path: "/",
  43. }
  44. matcher := watch.EmptyMatcher{}
  45. eventBatchCh := batchDebounceEvents(ctx, clock, quietPeriod, ch)
  46. for i := 0; i < 100; i++ {
  47. path := "/a"
  48. if i%2 == 0 {
  49. path = "/b"
  50. }
  51. event := maybeFileEvent(trigger, path, matcher)
  52. require.NotNil(t, event)
  53. ch <- *event
  54. }
  55. // we sent 100 events + the debouncer
  56. clock.BlockUntil(101)
  57. clock.Advance(quietPeriod)
  58. select {
  59. case batch := <-eventBatchCh:
  60. slices.SortFunc(batch, func(a, b fileEvent) int {
  61. return strings.Compare(a.HostPath, b.HostPath)
  62. })
  63. assert.Equal(t, len(batch), 2)
  64. assert.Equal(t, batch[0].HostPath, "/a")
  65. assert.Equal(t, batch[1].HostPath, "/b")
  66. case <-time.After(50 * time.Millisecond):
  67. t.Fatal("timed out waiting for events")
  68. }
  69. clock.BlockUntil(1)
  70. clock.Advance(quietPeriod)
  71. // there should only be a single batch
  72. select {
  73. case batch := <-eventBatchCh:
  74. t.Fatalf("unexpected events: %v", batch)
  75. case <-time.After(50 * time.Millisecond):
  76. // channel is empty
  77. }
  78. }
  79. type testWatcher struct {
  80. events chan watch.FileEvent
  81. errors chan error
  82. }
  83. func (t testWatcher) Start() error {
  84. return nil
  85. }
  86. func (t testWatcher) Close() error {
  87. return nil
  88. }
  89. func (t testWatcher) Events() chan watch.FileEvent {
  90. return t.events
  91. }
  92. func (t testWatcher) Errors() chan error {
  93. return t.errors
  94. }
  95. type stdLogger struct{}
  96. func (s stdLogger) Log(containerName, message string) {
  97. fmt.Printf("%s: %s\n", containerName, message)
  98. }
  99. func (s stdLogger) Err(containerName, message string) {
  100. fmt.Fprintf(os.Stderr, "%s: %s\n", containerName, message)
  101. }
  102. func (s stdLogger) Status(container, msg string) {
  103. fmt.Printf("%s: %s\n", container, msg)
  104. }
  105. func (s stdLogger) Register(container string) {
  106. }
  107. func TestWatch_Sync(t *testing.T) {
  108. mockCtrl := gomock.NewController(t)
  109. cli := mocks.NewMockCli(mockCtrl)
  110. cli.EXPECT().Err().Return(streams.NewOut(os.Stderr)).AnyTimes()
  111. apiClient := mocks.NewMockAPIClient(mockCtrl)
  112. apiClient.EXPECT().ContainerList(gomock.Any(), gomock.Any()).Return([]moby.Container{
  113. testContainer("test", "123", false),
  114. }, nil).AnyTimes()
  115. // we expect the image to be pruned
  116. apiClient.EXPECT().ImageList(gomock.Any(), image.ListOptions{
  117. Filters: filters.NewArgs(
  118. filters.Arg("dangling", "true"),
  119. filters.Arg("label", api.ProjectLabel+"=myProjectName"),
  120. ),
  121. }).Return([]image.Summary{
  122. {ID: "123"},
  123. {ID: "456"},
  124. }, nil).Times(1)
  125. apiClient.EXPECT().ImageRemove(gomock.Any(), "123", image.RemoveOptions{}).Times(1)
  126. apiClient.EXPECT().ImageRemove(gomock.Any(), "456", image.RemoveOptions{}).Times(1)
  127. //
  128. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  129. ctx, cancelFunc := context.WithCancel(context.Background())
  130. t.Cleanup(cancelFunc)
  131. proj := types.Project{
  132. Name: "myProjectName",
  133. Services: types.Services{
  134. "test": {
  135. Name: "test",
  136. },
  137. },
  138. }
  139. watcher := testWatcher{
  140. events: make(chan watch.FileEvent),
  141. errors: make(chan error),
  142. }
  143. syncer := newFakeSyncer()
  144. clock := clockwork.NewFakeClock()
  145. go func() {
  146. service := composeService{
  147. dockerCli: cli,
  148. clock: clock,
  149. }
  150. err := service.watchEvents(ctx, &proj, "test", api.WatchOptions{
  151. Build: &api.BuildOptions{},
  152. LogTo: stdLogger{},
  153. Prune: true,
  154. }, watcher, syncer, []types.Trigger{
  155. {
  156. Path: "/sync",
  157. Action: "sync",
  158. Target: "/work",
  159. Ignore: []string{"ignore"},
  160. },
  161. {
  162. Path: "/rebuild",
  163. Action: "rebuild",
  164. },
  165. })
  166. assert.NilError(t, err)
  167. }()
  168. watcher.Events() <- watch.NewFileEvent("/sync/changed")
  169. watcher.Events() <- watch.NewFileEvent("/sync/changed/sub")
  170. clock.BlockUntil(3)
  171. clock.Advance(quietPeriod)
  172. select {
  173. case actual := <-syncer.synced:
  174. require.ElementsMatch(t, []sync.PathMapping{
  175. {HostPath: "/sync/changed", ContainerPath: "/work/changed"},
  176. {HostPath: "/sync/changed/sub", ContainerPath: "/work/changed/sub"},
  177. }, actual)
  178. case <-time.After(100 * time.Millisecond):
  179. t.Error("timeout")
  180. }
  181. watcher.Events() <- watch.NewFileEvent("/sync/ignore")
  182. watcher.Events() <- watch.NewFileEvent("/sync/ignore/sub")
  183. watcher.Events() <- watch.NewFileEvent("/sync/changed")
  184. clock.BlockUntil(4)
  185. clock.Advance(quietPeriod)
  186. select {
  187. case actual := <-syncer.synced:
  188. require.ElementsMatch(t, []sync.PathMapping{
  189. {HostPath: "/sync/changed", ContainerPath: "/work/changed"},
  190. }, actual)
  191. case <-time.After(100 * time.Millisecond):
  192. t.Error("timed out waiting for events")
  193. }
  194. watcher.Events() <- watch.NewFileEvent("/rebuild")
  195. watcher.Events() <- watch.NewFileEvent("/sync/changed")
  196. clock.BlockUntil(4)
  197. clock.Advance(quietPeriod)
  198. select {
  199. case batch := <-syncer.synced:
  200. t.Fatalf("received unexpected events: %v", batch)
  201. case <-time.After(100 * time.Millisecond):
  202. // expected
  203. }
  204. // TODO: there's not a great way to assert that the rebuild attempt happened
  205. }
  206. type fakeSyncer struct {
  207. synced chan []sync.PathMapping
  208. }
  209. func newFakeSyncer() *fakeSyncer {
  210. return &fakeSyncer{
  211. synced: make(chan []sync.PathMapping),
  212. }
  213. }
  214. func (f *fakeSyncer) Sync(_ context.Context, _ types.ServiceConfig, paths []sync.PathMapping) error {
  215. f.synced <- paths
  216. return nil
  217. }