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

Commit d99a510

Browse files
authored
Merge pull request #1586 from gtardif/fix_win_test_paths
2 parents 1607370 + df7f57e commit d99a510

File tree

3 files changed

+126
-80
lines changed

3 files changed

+126
-80
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/compose_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ package e2e
1818

1919
import (
2020
"fmt"
21+
"io/ioutil"
2122
"net/http"
2223
"os"
2324
"path/filepath"
2425
"regexp"
26+
"runtime"
2527
"strings"
2628
"testing"
2729
"time"
@@ -76,13 +78,12 @@ func TestLocalComposeUp(t *testing.T) {
7678
})
7779

7880
t.Run("check compose labels", func(t *testing.T) {
79-
wd, _ := os.Getwd()
8081
res := c.RunDockerCmd("inspect", projectName+"_web_1")
8182
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
8283
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "compose-e2e-demo"`})
8384
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "False",`})
8485
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.config-hash":`})
85-
res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`"com.docker.compose.project.config_files": "%s/fixtures/sentences/compose.yaml"`, wd)})
86+
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.config_files":`})
8687
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.working_dir":`})
8788
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.service": "web"`})
8889
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version":`})
@@ -132,21 +133,31 @@ func TestLocalComposeUp(t *testing.T) {
132133
})
133134
}
134135

136+
func binExt() string {
137+
binaryExt := ""
138+
if runtime.GOOS == "windows" {
139+
binaryExt = ".exe"
140+
}
141+
return binaryExt
142+
}
135143
func TestComposeUsingCliPlugin(t *testing.T) {
136144
c := NewParallelE2eCLI(t, binDir)
137145

138-
err := os.Remove(filepath.Join(c.ConfigDir, "cli-plugins", "docker-compose"))
146+
err := os.Remove(filepath.Join(c.ConfigDir, "cli-plugins", "docker-compose"+binExt()))
139147
assert.NilError(t, err)
140148
res := c.RunDockerOrExitError("compose", "ls")
141149
res.Assert(t, icmd.Expected{Err: "'compose' is not a docker command", ExitCode: 1})
142150
}
143151

144152
func TestComposeCliPluginWithoutCloudIntegration(t *testing.T) {
145-
c := NewParallelE2eCLI(t, binDir)
153+
newBinFolder, cleanup, err := SetupExistingCLI() // do not share bin folder with other tests
154+
assert.NilError(t, err)
155+
defer cleanup()
156+
c := NewParallelE2eCLI(t, newBinFolder)
146157

147-
err := os.Remove(filepath.Join(binDir, "docker"))
158+
err = os.Remove(filepath.Join(newBinFolder, "docker"+binExt()))
148159
assert.NilError(t, err)
149-
err = os.Rename(filepath.Join(binDir, "com.docker.cli"), filepath.Join(binDir, "docker"))
160+
err = os.Rename(filepath.Join(newBinFolder, "com.docker.cli"+binExt()), filepath.Join(newBinFolder, "docker"+binExt()))
150161
assert.NilError(t, err)
151162
res := c.RunDockerOrExitError("compose", "ls")
152163
res.Assert(t, icmd.Expected{Out: "NAME STATUS", ExitCode: 0})
@@ -166,10 +177,10 @@ func TestDownComposefileInParentFolder(t *testing.T) {
166177

167178
c := NewParallelE2eCLI(t, binDir)
168179

169-
tmpFolder, err := os.MkdirTemp("fixtures/simple-composefile", "test-tmp")
170-
projectName := strings.TrimPrefix(tmpFolder, "fixtures/simple-composefile/")
171-
defer os.Remove(tmpFolder) //nolint: errcheck
180+
tmpFolder, err := ioutil.TempDir("fixtures/simple-composefile", "test-tmp")
172181
assert.NilError(t, err)
182+
defer os.Remove(tmpFolder) //nolint: errcheck
183+
projectName := filepath.Base(tmpFolder)
173184

174185
res := c.RunDockerCmd("compose", "--project-directory", tmpFolder, "up", "-d")
175186
res.Assert(t, icmd.Expected{Err: "Started", ExitCode: 0})

local/e2e/compose/metrics_test.go

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
package e2e
1818

1919
import (
20-
"bytes"
2120
"fmt"
22-
"os/exec"
23-
"strings"
24-
"syscall"
21+
"runtime"
2522
"testing"
2623
"time"
2724

@@ -53,7 +50,11 @@ func TestComposeMetrics(t *testing.T) {
5350
s.ResetUsage()
5451

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

0 commit comments

Comments
 (0)