MCPcopy
hub / github.com/docker/compose / doTest

Function doTest

pkg/e2e/watch_test.go:135–286  ·  view source on GitHub ↗

NOTE: these tests all share a single Compose file but are safe to run concurrently (though that's not recommended).

(t *testing.T, svcName string)

Source from the content-addressed store, hash-verified

133// NOTE: these tests all share a single Compose file but are safe to run
134// concurrently (though that's not recommended).
135func doTest(t *testing.T, svcName string) {
136 tmpdir := t.TempDir()
137 dataDir := filepath.Join(tmpdir, "data")
138 configDir := filepath.Join(tmpdir, "config")
139
140 writeTestFile := func(name, contents, sourceDir string) {
141 t.Helper()
142 dest := filepath.Join(sourceDir, name)
143 assert.NilError(t, os.MkdirAll(filepath.Dir(dest), 0o700))
144 t.Logf("writing %q to %q", contents, dest)
145 assert.NilError(t, os.WriteFile(dest, []byte(contents+"\n"), 0o600))
146 }
147 writeDataFile := func(name, contents string) {
148 writeTestFile(name, contents, dataDir)
149 }
150
151 composeFilePath := filepath.Join(tmpdir, "compose.yaml")
152 CopyFile(t, filepath.Join("fixtures", "watch", "compose.yaml"), composeFilePath)
153
154 projName := "e2e-watch-" + svcName
155 env := []string{
156 "COMPOSE_FILE=" + composeFilePath,
157 "COMPOSE_PROJECT_NAME=" + projName,
158 }
159
160 cli := NewCLI(t, WithEnv(env...))
161
162 // important that --rmi is used to prune the images and ensure that watch builds on launch
163 defer cli.cleanupWithDown(t, projName, "--rmi=local")
164
165 cmd := cli.NewDockerComposeCmd(t, "--verbose", "watch", svcName)
166 // stream output since watch runs in the background
167 cmd.Stdout = os.Stdout
168 cmd.Stderr = os.Stderr
169 r := icmd.StartCmd(cmd)
170 assert.NilError(t, r.Error)
171 t.Cleanup(func() {
172 // IMPORTANT: watch doesn't exit on its own, don't leak processes!
173 if r.Cmd.Process != nil {
174 t.Logf("Killing watch process: pid[%d]", r.Cmd.Process.Pid)
175 _ = r.Cmd.Process.Kill()
176 }
177 })
178 var testComplete atomic.Bool
179 go func() {
180 // if the process exits abnormally before the test is done, fail the test
181 if err := r.Cmd.Wait(); err != nil && !t.Failed() && !testComplete.Load() {
182 assert.Check(t, cmp.Nil(err))
183 }
184 }()
185
186 assert.NilError(t, os.Mkdir(dataDir, 0o700))
187
188 checkFileContents := func(path string, contents string) poll.Check {
189 return func(pollLog poll.LogT) poll.Result {
190 if r.Cmd.ProcessState != nil {
191 return poll.Error(fmt.Errorf("watch process exited early: %s", r.Cmd.ProcessState))
192 }

Callers 2

TestWatchFunction · 0.85

Calls 15

cleanupWithDownMethod · 0.95
NewDockerComposeCmdMethod · 0.95
RunDockerComposeCmdMethod · 0.95
CopyFileFunction · 0.85
NewCLIFunction · 0.85
WithEnvFunction · 0.85
TempDirMethod · 0.80
MkdirAllMethod · 0.80
WriteFileMethod · 0.80
ErrorMethod · 0.80
RunCmdMethod · 0.80

Tested by

no test coverage detected