Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 95179f1

Browse files
committed
Compose Cancel test not compile/run on windows (no signals)
Signed-off-by: Guillaume Tardif <[email protected]>
1 parent 4fa4284 commit 95179f1

File tree

2 files changed

+100
-70
lines changed

2 files changed

+100
-70
lines changed

local/e2e/compose/cancel_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

local/e2e/compose/metrics_test.go

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@
1717
package e2e
1818

1919
import (
20-
"bytes"
2120
"fmt"
22-
"os/exec"
2321
"runtime"
24-
"strings"
25-
"syscall"
2622
"testing"
2723
"time"
2824

@@ -93,69 +89,3 @@ func TestComposeMetrics(t *testing.T) {
9389
}, usage)
9490
})
9591
}
96-
97-
func TestComposeCancel(t *testing.T) {
98-
c := NewParallelE2eCLI(t, binDir)
99-
s := NewMetricsServer(c.MetricsSocket())
100-
s.Start()
101-
defer s.Stop()
102-
103-
started := false
104-
105-
for i := 0; i < 30; i++ {
106-
c.RunDockerCmd("help", "ps")
107-
if len(s.GetUsage()) > 0 {
108-
started = true
109-
fmt.Printf(" [%s] Server up in %d ms\n", t.Name(), i*100)
110-
break
111-
}
112-
time.Sleep(100 * time.Millisecond)
113-
}
114-
assert.Assert(t, started, "Metrics mock server not available after 3 secs")
115-
116-
t.Run("metrics on cancel Compose build", func(t *testing.T) {
117-
s.ResetUsage()
118-
119-
c.RunDockerCmd("compose", "ls")
120-
buildProjectPath := "../compose/fixtures/build-infinite/docker-compose.yml"
121-
122-
// require a separate groupID from the process running tests, in order to simulate ctrl+C from a terminal.
123-
// sending kill signal
124-
cmd, stdout, stderr, err := StartWithNewGroupID(c.NewDockerCmd("compose", "-f", buildProjectPath, "build", "--progress", "plain"))
125-
assert.NilError(t, err)
126-
127-
c.WaitForCondition(func() (bool, string) {
128-
out := stdout.String()
129-
errors := stderr.String()
130-
return strings.Contains(out, "RUN sleep infinity"), fmt.Sprintf("'RUN sleep infinity' not found in : \n%s\nStderr: \n%s\n", out, errors)
131-
}, 30*time.Second, 1*time.Second)
132-
133-
err = syscall.Kill(-cmd.Process.Pid, syscall.SIGINT) // simulate Ctrl-C : send signal to processGroup, children will have same groupId by default
134-
135-
assert.NilError(t, err)
136-
c.WaitForCondition(func() (bool, string) {
137-
out := stdout.String()
138-
errors := stderr.String()
139-
return strings.Contains(out, "CANCELED"), fmt.Sprintf("'CANCELED' not found in : \n%s\nStderr: \n%s\n", out, errors)
140-
}, 10*time.Second, 1*time.Second)
141-
142-
usage := s.GetUsage()
143-
assert.DeepEqual(t, []string{
144-
`{"command":"compose ls","context":"moby","source":"cli","status":"success"}`,
145-
`{"command":"compose build","context":"moby","source":"cli","status":"canceled"}`,
146-
}, usage)
147-
})
148-
}
149-
150-
func StartWithNewGroupID(command icmd.Cmd) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer, error) {
151-
cmd := exec.Command(command.Command[0], command.Command[1:]...)
152-
cmd.Env = command.Env
153-
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
154-
155-
var stdout bytes.Buffer
156-
var stderr bytes.Buffer
157-
cmd.Stdout = &stdout
158-
cmd.Stderr = &stderr
159-
err := cmd.Start()
160-
return cmd, &stdout, &stderr, err
161-
}

0 commit comments

Comments
 (0)