|
| 1 | +// +build !windows |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright 2020 Docker Compose CLI authors |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package e2e |
| 20 | + |
| 21 | +import ( |
| 22 | + "bytes" |
| 23 | + "fmt" |
| 24 | + "os/exec" |
| 25 | + "strings" |
| 26 | + "syscall" |
| 27 | + "testing" |
| 28 | + "time" |
| 29 | + |
| 30 | + "gotest.tools/v3/assert" |
| 31 | + "gotest.tools/v3/icmd" |
| 32 | + |
| 33 | + . "github.com/docker/compose-cli/utils/e2e" |
| 34 | +) |
| 35 | + |
| 36 | +func TestComposeCancel(t *testing.T) { |
| 37 | + c := NewParallelE2eCLI(t, binDir) |
| 38 | + s := NewMetricsServer(c.MetricsSocket()) |
| 39 | + s.Start() |
| 40 | + defer s.Stop() |
| 41 | + |
| 42 | + started := false |
| 43 | + |
| 44 | + for i := 0; i < 30; i++ { |
| 45 | + c.RunDockerCmd("help", "ps") |
| 46 | + if len(s.GetUsage()) > 0 { |
| 47 | + started = true |
| 48 | + fmt.Printf(" [%s] Server up in %d ms\n", t.Name(), i*100) |
| 49 | + break |
| 50 | + } |
| 51 | + time.Sleep(100 * time.Millisecond) |
| 52 | + } |
| 53 | + assert.Assert(t, started, "Metrics mock server not available after 3 secs") |
| 54 | + |
| 55 | + t.Run("metrics on cancel Compose build", func(t *testing.T) { |
| 56 | + s.ResetUsage() |
| 57 | + |
| 58 | + c.RunDockerCmd("compose", "ls") |
| 59 | + buildProjectPath := "../compose/fixtures/build-infinite/docker-compose.yml" |
| 60 | + |
| 61 | + // require a separate groupID from the process running tests, in order to simulate ctrl+C from a terminal. |
| 62 | + // sending kill signal |
| 63 | + cmd, stdout, stderr, err := StartWithNewGroupID(c.NewDockerCmd("compose", "-f", buildProjectPath, "build", "--progress", "plain")) |
| 64 | + assert.NilError(t, err) |
| 65 | + |
| 66 | + c.WaitForCondition(func() (bool, string) { |
| 67 | + out := stdout.String() |
| 68 | + errors := stderr.String() |
| 69 | + return strings.Contains(out, "RUN sleep infinity"), fmt.Sprintf("'RUN sleep infinity' not found in : \n%s\nStderr: \n%s\n", out, errors) |
| 70 | + }, 30*time.Second, 1*time.Second) |
| 71 | + |
| 72 | + err = syscall.Kill(-cmd.Process.Pid, syscall.SIGINT) // simulate Ctrl-C : send signal to processGroup, children will have same groupId by default |
| 73 | + |
| 74 | + assert.NilError(t, err) |
| 75 | + c.WaitForCondition(func() (bool, string) { |
| 76 | + out := stdout.String() |
| 77 | + errors := stderr.String() |
| 78 | + return strings.Contains(out, "CANCELED"), fmt.Sprintf("'CANCELED' not found in : \n%s\nStderr: \n%s\n", out, errors) |
| 79 | + }, 10*time.Second, 1*time.Second) |
| 80 | + |
| 81 | + usage := s.GetUsage() |
| 82 | + assert.DeepEqual(t, []string{ |
| 83 | + `{"command":"compose ls","context":"moby","source":"cli","status":"success"}`, |
| 84 | + `{"command":"compose build","context":"moby","source":"cli","status":"canceled"}`, |
| 85 | + }, usage) |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +func StartWithNewGroupID(command icmd.Cmd) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer, error) { |
| 90 | + cmd := exec.Command(command.Command[0], command.Command[1:]...) |
| 91 | + cmd.Env = command.Env |
| 92 | + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| 93 | + |
| 94 | + var stdout bytes.Buffer |
| 95 | + var stderr bytes.Buffer |
| 96 | + cmd.Stdout = &stdout |
| 97 | + cmd.Stderr = &stderr |
| 98 | + err := cmd.Start() |
| 99 | + return cmd, &stdout, &stderr, err |
| 100 | +} |
0 commit comments