Signed-off-by: Daniel Nephin <dnephin@docker.com>
... | ... |
@@ -1526,7 +1526,7 @@ func (s *DockerSuite) TestBuildContextCleanup(c *check.C) { |
1526 | 1526 |
if err != nil { |
1527 | 1527 |
c.Fatalf("failed to list contents of tmp dir: %s", err) |
1528 | 1528 |
} |
1529 |
- if err = testutil.CompareDirectoryEntries(entries, entriesFinal); err != nil { |
|
1529 |
+ if err = compareDirectoryEntries(entries, entriesFinal); err != nil { |
|
1530 | 1530 |
c.Fatalf("context should have been deleted, but wasn't") |
1531 | 1531 |
} |
1532 | 1532 |
|
... | ... |
@@ -1550,12 +1550,31 @@ func (s *DockerSuite) TestBuildContextCleanupFailedBuild(c *check.C) { |
1550 | 1550 |
if err != nil { |
1551 | 1551 |
c.Fatalf("failed to list contents of tmp dir: %s", err) |
1552 | 1552 |
} |
1553 |
- if err = testutil.CompareDirectoryEntries(entries, entriesFinal); err != nil { |
|
1553 |
+ if err = compareDirectoryEntries(entries, entriesFinal); err != nil { |
|
1554 | 1554 |
c.Fatalf("context should have been deleted, but wasn't") |
1555 | 1555 |
} |
1556 | 1556 |
|
1557 | 1557 |
} |
1558 | 1558 |
|
1559 |
+// compareDirectoryEntries compares two sets of FileInfo (usually taken from a directory) |
|
1560 |
+// and returns an error if different. |
|
1561 |
+func compareDirectoryEntries(e1 []os.FileInfo, e2 []os.FileInfo) error { |
|
1562 |
+ var ( |
|
1563 |
+ e1Entries = make(map[string]struct{}) |
|
1564 |
+ e2Entries = make(map[string]struct{}) |
|
1565 |
+ ) |
|
1566 |
+ for _, e := range e1 { |
|
1567 |
+ e1Entries[e.Name()] = struct{}{} |
|
1568 |
+ } |
|
1569 |
+ for _, e := range e2 { |
|
1570 |
+ e2Entries[e.Name()] = struct{}{} |
|
1571 |
+ } |
|
1572 |
+ if !reflect.DeepEqual(e1Entries, e2Entries) { |
|
1573 |
+ return fmt.Errorf("entries differ") |
|
1574 |
+ } |
|
1575 |
+ return nil |
|
1576 |
+} |
|
1577 |
+ |
|
1559 | 1578 |
func (s *DockerSuite) TestBuildCmd(c *check.C) { |
1560 | 1579 |
name := "testbuildcmd" |
1561 | 1580 |
expected := "[/bin/echo Hello World]" |
... | ... |
@@ -14,13 +14,13 @@ import ( |
14 | 14 |
"strings" |
15 | 15 |
"time" |
16 | 16 |
|
17 |
+ "syscall" |
|
18 |
+ |
|
17 | 19 |
"github.com/docker/docker/integration-cli/checker" |
18 | 20 |
"github.com/docker/docker/integration-cli/cli" |
19 | 21 |
"github.com/docker/docker/integration-cli/cli/build" |
20 | 22 |
"github.com/docker/docker/integration-cli/cli/build/fakecontext" |
21 |
- "github.com/docker/docker/pkg/testutil" |
|
22 | 23 |
icmd "github.com/docker/docker/pkg/testutil/cmd" |
23 |
- "github.com/docker/go-units" |
|
24 | 24 |
"github.com/go-check/check" |
25 | 25 |
) |
26 | 26 |
|
... | ... |
@@ -191,7 +191,7 @@ func (s *DockerSuite) TestBuildCancellationKillsSleep(c *check.C) { |
191 | 191 |
} |
192 | 192 |
|
193 | 193 |
// Get the exit status of `docker build`, check it exited because killed. |
194 |
- if err := buildCmd.Wait(); err != nil && !testutil.IsKilled(err) { |
|
194 |
+ if err := buildCmd.Wait(); err != nil && !isKilled(err) { |
|
195 | 195 |
c.Fatalf("wait failed during build run: %T %s", err, err) |
196 | 196 |
} |
197 | 197 |
|
... | ... |
@@ -202,3 +202,17 @@ func (s *DockerSuite) TestBuildCancellationKillsSleep(c *check.C) { |
202 | 202 |
// ignore, done |
203 | 203 |
} |
204 | 204 |
} |
205 |
+ |
|
206 |
+func isKilled(err error) bool { |
|
207 |
+ if exitErr, ok := err.(*exec.ExitError); ok { |
|
208 |
+ status, ok := exitErr.Sys().(syscall.WaitStatus) |
|
209 |
+ if !ok { |
|
210 |
+ return false |
|
211 |
+ } |
|
212 |
+ // status.ExitStatus() is required on Windows because it does not |
|
213 |
+ // implement Signal() nor Signaled(). Just check it had a bad exit |
|
214 |
+ // status could mean it was killed (and in tests we do kill) |
|
215 |
+ return (status.Signaled() && status.Signal() == os.Kill) || status.ExitStatus() != 0 |
|
216 |
+ } |
|
217 |
+ return false |
|
218 |
+} |
... | ... |
@@ -9,30 +9,13 @@ import ( |
9 | 9 |
"os" |
10 | 10 |
"os/exec" |
11 | 11 |
"path/filepath" |
12 |
- "reflect" |
|
13 | 12 |
"strings" |
14 |
- "syscall" |
|
15 | 13 |
"time" |
16 | 14 |
|
17 | 15 |
"github.com/docker/docker/pkg/stringutils" |
18 | 16 |
"github.com/docker/docker/pkg/system" |
19 | 17 |
) |
20 | 18 |
|
21 |
-// IsKilled process the specified error and returns whether the process was killed or not. |
|
22 |
-func IsKilled(err error) bool { |
|
23 |
- if exitErr, ok := err.(*exec.ExitError); ok { |
|
24 |
- status, ok := exitErr.Sys().(syscall.WaitStatus) |
|
25 |
- if !ok { |
|
26 |
- return false |
|
27 |
- } |
|
28 |
- // status.ExitStatus() is required on Windows because it does not |
|
29 |
- // implement Signal() nor Signaled(). Just check it had a bad exit |
|
30 |
- // status could mean it was killed (and in tests we do kill) |
|
31 |
- return (status.Signaled() && status.Signal() == os.Kill) || status.ExitStatus() != 0 |
|
32 |
- } |
|
33 |
- return false |
|
34 |
-} |
|
35 |
- |
|
36 | 19 |
func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) { |
37 | 20 |
out, err := cmd.CombinedOutput() |
38 | 21 |
exitCode = system.ProcessExitCode(err) |
... | ... |
@@ -85,25 +68,6 @@ func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, exitCode in |
85 | 85 |
return runCommandWithOutput(cmds[len(cmds)-1]) |
86 | 86 |
} |
87 | 87 |
|
88 |
-// CompareDirectoryEntries compares two sets of FileInfo (usually taken from a directory) |
|
89 |
-// and returns an error if different. |
|
90 |
-func CompareDirectoryEntries(e1 []os.FileInfo, e2 []os.FileInfo) error { |
|
91 |
- var ( |
|
92 |
- e1Entries = make(map[string]struct{}) |
|
93 |
- e2Entries = make(map[string]struct{}) |
|
94 |
- ) |
|
95 |
- for _, e := range e1 { |
|
96 |
- e1Entries[e.Name()] = struct{}{} |
|
97 |
- } |
|
98 |
- for _, e := range e2 { |
|
99 |
- e2Entries[e.Name()] = struct{}{} |
|
100 |
- } |
|
101 |
- if !reflect.DeepEqual(e1Entries, e2Entries) { |
|
102 |
- return fmt.Errorf("entries differ") |
|
103 |
- } |
|
104 |
- return nil |
|
105 |
-} |
|
106 |
- |
|
107 | 88 |
// ListTar lists the entries of a tar. |
108 | 89 |
func ListTar(f io.Reader) ([]string, error) { |
109 | 90 |
tr := tar.NewReader(f) |
... | ... |
@@ -12,47 +12,6 @@ import ( |
12 | 12 |
"time" |
13 | 13 |
) |
14 | 14 |
|
15 |
-func TestIsKilledFalseWithNonKilledProcess(t *testing.T) { |
|
16 |
- var lsCmd *exec.Cmd |
|
17 |
- if runtime.GOOS != "windows" { |
|
18 |
- lsCmd = exec.Command("ls") |
|
19 |
- } else { |
|
20 |
- lsCmd = exec.Command("cmd", "/c", "dir") |
|
21 |
- } |
|
22 |
- |
|
23 |
- err := lsCmd.Run() |
|
24 |
- if IsKilled(err) { |
|
25 |
- t.Fatalf("Expected the ls command to not be killed, was.") |
|
26 |
- } |
|
27 |
-} |
|
28 |
- |
|
29 |
-func TestIsKilledTrueWithKilledProcess(t *testing.T) { |
|
30 |
- var longCmd *exec.Cmd |
|
31 |
- if runtime.GOOS != "windows" { |
|
32 |
- longCmd = exec.Command("top") |
|
33 |
- } else { |
|
34 |
- longCmd = exec.Command("powershell", "while ($true) { sleep 1 }") |
|
35 |
- } |
|
36 |
- |
|
37 |
- // Start a command |
|
38 |
- err := longCmd.Start() |
|
39 |
- if err != nil { |
|
40 |
- t.Fatal(err) |
|
41 |
- } |
|
42 |
- // Capture the error when *dying* |
|
43 |
- done := make(chan error, 1) |
|
44 |
- go func() { |
|
45 |
- done <- longCmd.Wait() |
|
46 |
- }() |
|
47 |
- // Then kill it |
|
48 |
- longCmd.Process.Kill() |
|
49 |
- // Get the error |
|
50 |
- err = <-done |
|
51 |
- if !IsKilled(err) { |
|
52 |
- t.Fatalf("Expected the command to be killed, was not.") |
|
53 |
- } |
|
54 |
-} |
|
55 |
- |
|
56 | 15 |
func TestRunCommandPipelineWithOutputWithNotEnoughCmds(t *testing.T) { |
57 | 16 |
_, _, err := RunCommandPipelineWithOutput(exec.Command("ls")) |
58 | 17 |
expectedError := "pipeline does not have multiple cmds" |
... | ... |
@@ -100,72 +59,6 @@ func TestRunCommandPipelineWithOutput(t *testing.T) { |
100 | 100 |
} |
101 | 101 |
} |
102 | 102 |
|
103 |
-func TestCompareDirectoryEntries(t *testing.T) { |
|
104 |
- tmpFolder, err := ioutil.TempDir("", "integration-cli-utils-compare-directories") |
|
105 |
- if err != nil { |
|
106 |
- t.Fatal(err) |
|
107 |
- } |
|
108 |
- defer os.RemoveAll(tmpFolder) |
|
109 |
- |
|
110 |
- file1 := filepath.Join(tmpFolder, "file1") |
|
111 |
- file2 := filepath.Join(tmpFolder, "file2") |
|
112 |
- os.Create(file1) |
|
113 |
- os.Create(file2) |
|
114 |
- |
|
115 |
- fi1, err := os.Stat(file1) |
|
116 |
- if err != nil { |
|
117 |
- t.Fatal(err) |
|
118 |
- } |
|
119 |
- fi1bis, err := os.Stat(file1) |
|
120 |
- if err != nil { |
|
121 |
- t.Fatal(err) |
|
122 |
- } |
|
123 |
- fi2, err := os.Stat(file2) |
|
124 |
- if err != nil { |
|
125 |
- t.Fatal(err) |
|
126 |
- } |
|
127 |
- |
|
128 |
- cases := []struct { |
|
129 |
- e1 []os.FileInfo |
|
130 |
- e2 []os.FileInfo |
|
131 |
- shouldError bool |
|
132 |
- }{ |
|
133 |
- // Empty directories |
|
134 |
- { |
|
135 |
- []os.FileInfo{}, |
|
136 |
- []os.FileInfo{}, |
|
137 |
- false, |
|
138 |
- }, |
|
139 |
- // Same FileInfos |
|
140 |
- { |
|
141 |
- []os.FileInfo{fi1}, |
|
142 |
- []os.FileInfo{fi1}, |
|
143 |
- false, |
|
144 |
- }, |
|
145 |
- // Different FileInfos but same names |
|
146 |
- { |
|
147 |
- []os.FileInfo{fi1}, |
|
148 |
- []os.FileInfo{fi1bis}, |
|
149 |
- false, |
|
150 |
- }, |
|
151 |
- // Different FileInfos, different names |
|
152 |
- { |
|
153 |
- []os.FileInfo{fi1}, |
|
154 |
- []os.FileInfo{fi2}, |
|
155 |
- true, |
|
156 |
- }, |
|
157 |
- } |
|
158 |
- for _, elt := range cases { |
|
159 |
- err := CompareDirectoryEntries(elt.e1, elt.e2) |
|
160 |
- if elt.shouldError && err == nil { |
|
161 |
- t.Fatalf("Should have return an error, did not with %v and %v", elt.e1, elt.e2) |
|
162 |
- } |
|
163 |
- if !elt.shouldError && err != nil { |
|
164 |
- t.Fatalf("Should have not returned an error, but did : %v with %v and %v", err, elt.e1, elt.e2) |
|
165 |
- } |
|
166 |
- } |
|
167 |
-} |
|
168 |
- |
|
169 | 103 |
// FIXME make an "unhappy path" test for ListTar without "panicking" :-) |
170 | 104 |
func TestListTar(t *testing.T) { |
171 | 105 |
// TODO Windows: Figure out why this fails. Should be portable. |