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

Function TestComposeCancel

pkg/e2e/cancel_test.go:36–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

34)
35
36func TestComposeCancel(t *testing.T) {
37 c := NewParallelCLI(t)
38
39 t.Run("metrics on cancel Compose build", func(t *testing.T) {
40 const buildProjectPath = "fixtures/build-infinite/compose.yaml"
41
42 ctx, cancel := context.WithCancel(t.Context())
43 defer cancel()
44
45 // require a separate groupID from the process running tests, in order to simulate ctrl+C from a terminal.
46 // sending kill signal
47 var stdout, stderr utils.SafeBuffer
48 cmd, err := StartWithNewGroupID(
49 ctx,
50 c.NewDockerComposeCmd(t, "-f", buildProjectPath, "build", "--progress", "plain"),
51 &stdout,
52 &stderr,
53 )
54 assert.NilError(t, err)
55 processDone := make(chan error, 1)
56 go func() {
57 defer close(processDone)
58 processDone <- cmd.Wait()
59 }()
60
61 c.WaitForCondition(t, func() (bool, string) {
62 out := stdout.String()
63 errors := stderr.String()
64 return strings.Contains(out,
65 "RUN sleep infinity"), fmt.Sprintf("'RUN sleep infinity' not found in : \n%s\nStderr: \n%s\n", out,
66 errors)
67 }, 30*time.Second, 1*time.Second)
68
69 // simulate Ctrl-C : send signal to processGroup, children will have same groupId by default
70 err = syscall.Kill(-cmd.Process.Pid, syscall.SIGINT)
71 assert.NilError(t, err)
72
73 select {
74 case <-ctx.Done():
75 t.Fatal("test context canceled")
76 case err := <-processDone:
77 // TODO(milas): Compose should really not return exit code 130 here,
78 // this is an old hack for the compose-cli wrapper
79 assert.Error(t, err, "exit status 130",
80 "STDOUT:\n%s\nSTDERR:\n%s\n", stdout.String(), stderr.String())
81 case <-time.After(10 * time.Second):
82 t.Fatal("timeout waiting for Compose exit")
83 }
84 })
85}
86
87func StartWithNewGroupID(ctx context.Context, command icmd.Cmd, stdout *utils.SafeBuffer, stderr *utils.SafeBuffer) (*exec.Cmd, error) {
88 cmd := exec.CommandContext(ctx, command.Command[0], command.Command[1:]...)

Callers

nothing calls this directly

Calls 9

StringMethod · 0.95
NewParallelCLIFunction · 0.85
StartWithNewGroupIDFunction · 0.85
NewDockerComposeCmdMethod · 0.80
WaitForConditionMethod · 0.80
ErrorMethod · 0.80
WaitMethod · 0.65
KillMethod · 0.65
DoneMethod · 0.65

Tested by

no test coverage detected