watch_test.go 5.8 KB

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