watch_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Copyright 2023 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 e2e
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "sync/atomic"
  20. "testing"
  21. "github.com/distribution/distribution/v3/uuid"
  22. "github.com/stretchr/testify/require"
  23. "gotest.tools/v3/assert"
  24. "gotest.tools/v3/assert/cmp"
  25. "gotest.tools/v3/icmd"
  26. "gotest.tools/v3/poll"
  27. )
  28. func TestWatch(t *testing.T) {
  29. services := []string{"alpine", "busybox", "debian"}
  30. t.Run("docker cp", func(t *testing.T) {
  31. for _, svcName := range services {
  32. t.Run(svcName, func(t *testing.T) {
  33. t.Helper()
  34. doTest(t, svcName, false)
  35. })
  36. }
  37. })
  38. t.Run("tar", func(t *testing.T) {
  39. for _, svcName := range services {
  40. t.Run(svcName, func(t *testing.T) {
  41. t.Helper()
  42. doTest(t, svcName, true)
  43. })
  44. }
  45. })
  46. }
  47. // NOTE: these tests all share a single Compose file but are safe to run concurrently
  48. func doTest(t *testing.T, svcName string, tarSync bool) {
  49. tmpdir := t.TempDir()
  50. dataDir := filepath.Join(tmpdir, "data")
  51. writeDataFile := func(name string, contents string) {
  52. t.Helper()
  53. dest := filepath.Join(dataDir, name)
  54. require.NoError(t, os.MkdirAll(filepath.Dir(dest), 0o700))
  55. t.Logf("writing %q to %q", contents, dest)
  56. require.NoError(t, os.WriteFile(dest, []byte(contents+"\n"), 0o600))
  57. }
  58. composeFilePath := filepath.Join(tmpdir, "compose.yaml")
  59. CopyFile(t, filepath.Join("fixtures", "watch", "compose.yaml"), composeFilePath)
  60. projName := "e2e-watch-" + svcName
  61. env := []string{
  62. "COMPOSE_FILE=" + composeFilePath,
  63. "COMPOSE_PROJECT_NAME=" + projName,
  64. }
  65. if tarSync {
  66. env = append(env, "COMPOSE_EXPERIMENTAL_WATCH_TAR=1")
  67. }
  68. cli := NewCLI(t, WithEnv(env...))
  69. cleanup := func() {
  70. cli.RunDockerComposeCmd(t, "down", svcName, "--timeout=0", "--remove-orphans", "--volumes")
  71. }
  72. cleanup()
  73. t.Cleanup(cleanup)
  74. cli.RunDockerComposeCmd(t, "up", svcName, "--wait", "--build")
  75. cmd := cli.NewDockerComposeCmd(t, "--verbose", "alpha", "watch", svcName)
  76. // stream output since watch runs in the background
  77. cmd.Stdout = os.Stdout
  78. cmd.Stderr = os.Stderr
  79. r := icmd.StartCmd(cmd)
  80. require.NoError(t, r.Error)
  81. t.Cleanup(func() {
  82. // IMPORTANT: watch doesn't exit on its own, don't leak processes!
  83. if r.Cmd.Process != nil {
  84. _ = r.Cmd.Process.Kill()
  85. }
  86. })
  87. var testComplete atomic.Bool
  88. go func() {
  89. // if the process exits abnormally before the test is done, fail the test
  90. if err := r.Cmd.Wait(); err != nil && !t.Failed() && !testComplete.Load() {
  91. assert.Check(t, cmp.Nil(err))
  92. }
  93. }()
  94. require.NoError(t, os.Mkdir(dataDir, 0o700))
  95. checkFileContents := func(path string, contents string) poll.Check {
  96. return func(pollLog poll.LogT) poll.Result {
  97. if r.Cmd.ProcessState != nil {
  98. return poll.Error(fmt.Errorf("watch process exited early: %s", r.Cmd.ProcessState))
  99. }
  100. res := icmd.RunCmd(cli.NewDockerComposeCmd(t, "exec", svcName, "cat", path))
  101. if strings.Contains(res.Stdout(), contents) {
  102. return poll.Success()
  103. }
  104. return poll.Continue(res.Combined())
  105. }
  106. }
  107. waitForFlush := func() {
  108. sentinelVal := uuid.Generate().String()
  109. writeDataFile("wait.txt", sentinelVal)
  110. poll.WaitOn(t, checkFileContents("/app/data/wait.txt", sentinelVal))
  111. }
  112. t.Logf("Writing to a file until Compose watch is up and running")
  113. poll.WaitOn(t, func(t poll.LogT) poll.Result {
  114. writeDataFile("hello.txt", "hello world")
  115. return checkFileContents("/app/data/hello.txt", "hello world")(t)
  116. })
  117. t.Logf("Modifying file contents")
  118. writeDataFile("hello.txt", "hello watch")
  119. poll.WaitOn(t, checkFileContents("/app/data/hello.txt", "hello watch"))
  120. t.Logf("Deleting file")
  121. require.NoError(t, os.Remove(filepath.Join(dataDir, "hello.txt")))
  122. waitForFlush()
  123. cli.RunDockerComposeCmdNoCheck(t, "exec", svcName, "stat", "/app/data/hello.txt").
  124. Assert(t, icmd.Expected{
  125. ExitCode: 1,
  126. Err: "No such file or directory",
  127. })
  128. t.Logf("Writing to ignored paths")
  129. writeDataFile("data.foo", "ignored")
  130. writeDataFile(filepath.Join("ignored", "hello.txt"), "ignored")
  131. waitForFlush()
  132. cli.RunDockerComposeCmdNoCheck(t, "exec", svcName, "stat", "/app/data/data.foo").
  133. Assert(t, icmd.Expected{
  134. ExitCode: 1,
  135. Err: "No such file or directory",
  136. },
  137. )
  138. cli.RunDockerComposeCmdNoCheck(t, "exec", svcName, "stat", "/app/data/ignored").
  139. Assert(t, icmd.Expected{
  140. ExitCode: 1,
  141. Err: "No such file or directory",
  142. },
  143. )
  144. t.Logf("Creating subdirectory")
  145. require.NoError(t, os.Mkdir(filepath.Join(dataDir, "subdir"), 0o700))
  146. waitForFlush()
  147. cli.RunDockerComposeCmd(t, "exec", svcName, "stat", "/app/data/subdir")
  148. t.Logf("Writing to file in subdirectory")
  149. writeDataFile(filepath.Join("subdir", "file.txt"), "a")
  150. poll.WaitOn(t, checkFileContents("/app/data/subdir/file.txt", "a"))
  151. t.Logf("Writing to file multiple times")
  152. writeDataFile(filepath.Join("subdir", "file.txt"), "x")
  153. writeDataFile(filepath.Join("subdir", "file.txt"), "y")
  154. writeDataFile(filepath.Join("subdir", "file.txt"), "z")
  155. poll.WaitOn(t, checkFileContents("/app/data/subdir/file.txt", "z"))
  156. writeDataFile(filepath.Join("subdir", "file.txt"), "z")
  157. writeDataFile(filepath.Join("subdir", "file.txt"), "y")
  158. writeDataFile(filepath.Join("subdir", "file.txt"), "x")
  159. poll.WaitOn(t, checkFileContents("/app/data/subdir/file.txt", "x"))
  160. t.Logf("Deleting directory")
  161. require.NoError(t, os.RemoveAll(filepath.Join(dataDir, "subdir")))
  162. waitForFlush()
  163. cli.RunDockerComposeCmdNoCheck(t, "exec", svcName, "stat", "/app/data/subdir").
  164. Assert(t, icmd.Expected{
  165. ExitCode: 1,
  166. Err: "No such file or directory",
  167. },
  168. )
  169. testComplete.Store(true)
  170. }