Fix delete containers and make sure it prints errors correctly.
Rename Result.Fails to Result.Assert()
Create a constant for the default expected.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
... | ... |
@@ -90,11 +90,11 @@ func (s *DockerSuite) TestApiDockerApiVersion(c *check.C) { |
90 | 90 |
|
91 | 91 |
// Test using the env var first |
92 | 92 |
result := icmd.RunCmd(icmd.Cmd{ |
93 |
- Command: binaryWithArgs([]string{"-H", server.URL[7:], "version"}), |
|
94 |
- Env: []string{"DOCKER_API_VERSION=xxx"}, |
|
93 |
+ Command: binaryWithArgs("-H="+server.URL[7:], "version"), |
|
94 |
+ Env: appendBaseEnv(false, "DOCKER_API_VERSION=xxx"), |
|
95 | 95 |
}) |
96 |
- result.Assert(c, icmd.Expected{Out: "API version: xxx", ExitCode: 1}) |
|
97 |
- c.Assert(svrVersion, check.Equals, "/vxxx/version") |
|
96 |
+ c.Assert(result, icmd.Matches, icmd.Expected{Out: "API version: xxx", ExitCode: 1}) |
|
97 |
+ c.Assert(svrVersion, check.Equals, "/vxxx/version", check.Commentf("%s", result.Compare(icmd.Success))) |
|
98 | 98 |
} |
99 | 99 |
|
100 | 100 |
func (s *DockerSuite) TestApiErrorJSON(c *check.C) { |
... | ... |
@@ -163,7 +163,7 @@ func (s *DockerSuite) TestAttachPausedContainer(c *check.C) { |
163 | 163 |
dockerCmd(c, "pause", "test") |
164 | 164 |
|
165 | 165 |
result := dockerCmdWithResult("attach", "test") |
166 |
- result.Assert(c, icmd.Expected{ |
|
166 |
+ c.Assert(result, icmd.Matches, icmd.Expected{ |
|
167 | 167 |
Error: "exit status 1", |
168 | 168 |
ExitCode: 1, |
169 | 169 |
Err: "You cannot attach to a paused container, unpause it first", |
... | ... |
@@ -5065,7 +5065,7 @@ func (s *DockerSuite) TestBuildDockerfileOutsideContext(c *check.C) { |
5065 | 5065 |
filepath.Join(ctx, "dockerfile2"), |
5066 | 5066 |
} { |
5067 | 5067 |
result := dockerCmdWithResult("build", "-t", name, "--no-cache", "-f", dockerfilePath, ".") |
5068 |
- result.Assert(c, icmd.Expected{ |
|
5068 |
+ c.Assert(result, icmd.Matches, icmd.Expected{ |
|
5069 | 5069 |
Err: "must be within the build context", |
5070 | 5070 |
ExitCode: 1, |
5071 | 5071 |
}) |
... | ... |
@@ -910,7 +910,7 @@ func (s *DockerDaemonSuite) TestDaemonDefaultNetworkInvalidClusterConfig(c *chec |
910 | 910 |
|
911 | 911 |
// Start daemon with docker0 bridge |
912 | 912 |
result := icmd.RunCommand("ifconfig", defaultNetworkBridge) |
913 |
- result.Assert(c, icmd.Expected{}) |
|
913 |
+ c.Assert(result, icmd.Matches, icmd.Success) |
|
914 | 914 |
|
915 | 915 |
err = d.Restart(fmt.Sprintf("--cluster-store=%s", discoveryBackend)) |
916 | 916 |
c.Assert(err, checker.IsNil) |
... | ... |
@@ -2251,7 +2251,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartWithUnpausedRunningContainer(t *che |
2251 | 2251 |
ctrBinary, |
2252 | 2252 |
"--address", "unix:///var/run/docker/libcontainerd/docker-containerd.sock", |
2253 | 2253 |
"containers", "resume", cid) |
2254 |
- result.Assert(t, icmd.Expected{}) |
|
2254 |
+ t.Assert(result, icmd.Matches, icmd.Success) |
|
2255 | 2255 |
|
2256 | 2256 |
// Give time to containerd to process the command if we don't |
2257 | 2257 |
// the resume event might be received after we do the inspect |
... | ... |
@@ -63,7 +63,7 @@ func (s *DockerSuite) TestEventsUntag(c *check.C) { |
63 | 63 |
Command: []string{dockerBinary, "events", "--since=1"}, |
64 | 64 |
Timeout: time.Millisecond * 2500, |
65 | 65 |
}) |
66 |
- result.Assert(c, icmd.Expected{Timeout: true}) |
|
66 |
+ c.Assert(result, icmd.Matches, icmd.Expected{Timeout: true}) |
|
67 | 67 |
|
68 | 68 |
events := strings.Split(result.Stdout(), "\n") |
69 | 69 |
nEvents := len(events) |
... | ... |
@@ -280,7 +280,7 @@ func (s *DockerSuite) TestEventsImageLoad(c *check.C) { |
280 | 280 |
dockerCmd(c, "load", "-i", "saveimg.tar") |
281 | 281 |
|
282 | 282 |
result := icmd.RunCommand("rm", "-rf", "saveimg.tar") |
283 |
- result.Assert(c, icmd.Expected{}) |
|
283 |
+ c.Assert(result, icmd.Matches, icmd.Success) |
|
284 | 284 |
|
285 | 285 |
out, _ = dockerCmd(c, "images", "-q", "--no-trunc", myImageName) |
286 | 286 |
imageID := strings.TrimSpace(out) |
... | ... |
@@ -124,7 +124,7 @@ func (s *DockerSuite) TestExecExitStatus(c *check.C) { |
124 | 124 |
runSleepingContainer(c, "-d", "--name", "top") |
125 | 125 |
|
126 | 126 |
result := icmd.RunCommand(dockerBinary, "exec", "top", "sh", "-c", "exit 23") |
127 |
- result.Assert(c, icmd.Expected{ExitCode: 23, Error: "exit status 23"}) |
|
127 |
+ c.Assert(result, icmd.Matches, icmd.Expected{ExitCode: 23, Error: "exit status 23"}) |
|
128 | 128 |
} |
129 | 129 |
|
130 | 130 |
func (s *DockerSuite) TestExecPausedContainer(c *check.C) { |
... | ... |
@@ -479,7 +479,7 @@ func (s *DockerSuite) TestDockerNetworkInspectWithID(c *check.C) { |
479 | 479 |
|
480 | 480 |
func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) { |
481 | 481 |
result := dockerCmdWithResult("network", "inspect", "host", "none") |
482 |
- result.Assert(c, icmd.Expected{}) |
|
482 |
+ c.Assert(result, icmd.Matches, icmd.Success) |
|
483 | 483 |
|
484 | 484 |
networkResources := []types.NetworkResource{} |
485 | 485 |
err := json.Unmarshal([]byte(result.Stdout()), &networkResources) |
... | ... |
@@ -488,7 +488,7 @@ func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) { |
488 | 488 |
|
489 | 489 |
// Should print an error, return an exitCode 1 *but* should print the host network |
490 | 490 |
result = dockerCmdWithResult("network", "inspect", "host", "nonexistent") |
491 |
- result.Assert(c, icmd.Expected{ |
|
491 |
+ c.Assert(result, icmd.Matches, icmd.Expected{ |
|
492 | 492 |
ExitCode: 1, |
493 | 493 |
Err: "Error: No such network: nonexistent", |
494 | 494 |
Out: "host", |
... | ... |
@@ -500,7 +500,7 @@ func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) { |
500 | 500 |
|
501 | 501 |
// Should print an error and return an exitCode, nothing else |
502 | 502 |
result = dockerCmdWithResult("network", "inspect", "nonexistent") |
503 |
- result.Assert(c, icmd.Expected{ |
|
503 |
+ c.Assert(result, icmd.Matches, icmd.Expected{ |
|
504 | 504 |
ExitCode: 1, |
505 | 505 |
Err: "Error: No such network: nonexistent", |
506 | 506 |
Out: "[]", |
... | ... |
@@ -206,7 +206,7 @@ func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) { |
206 | 206 |
c.Assert(containerOut, checker.Equals, secondID) |
207 | 207 |
|
208 | 208 |
result := dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish") |
209 |
- result.Assert(c, icmd.Expected{ |
|
209 |
+ c.Assert(result, icmd.Matches, icmd.Expected{ |
|
210 | 210 |
ExitCode: 1, |
211 | 211 |
Err: "Unrecognised filter value for status", |
212 | 212 |
}) |
... | ... |
@@ -63,7 +63,7 @@ func (s *DockerSuite) TestRenameCheckNames(c *check.C) { |
63 | 63 |
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name)) |
64 | 64 |
|
65 | 65 |
result := dockerCmdWithResult("inspect", "-f={{.Name}}", "first_name") |
66 |
- result.Assert(c, icmd.Expected{ |
|
66 |
+ c.Assert(result, icmd.Matches, icmd.Expected{ |
|
67 | 67 |
ExitCode: 1, |
68 | 68 |
Err: "No such container, image or task: first_name", |
69 | 69 |
}) |
... | ... |
@@ -1864,11 +1864,11 @@ func (s *DockerSuite) TestRunInteractiveWithRestartPolicy(c *check.C) { |
1864 | 1864 |
}) |
1865 | 1865 |
c.Assert(result.Error, checker.IsNil) |
1866 | 1866 |
defer func() { |
1867 |
- dockerCmdWithResult("stop", name).Assert(c, icmd.Expected{}) |
|
1867 |
+ dockerCmdWithResult("stop", name).Assert(c, icmd.Success) |
|
1868 | 1868 |
}() |
1869 | 1869 |
|
1870 | 1870 |
result = icmd.WaitOnCmd(10*time.Second, result) |
1871 |
- result.Assert(c, icmd.Expected{ExitCode: 11}) |
|
1871 |
+ c.Assert(result, icmd.Matches, icmd.Expected{ExitCode: 11}) |
|
1872 | 1872 |
} |
1873 | 1873 |
|
1874 | 1874 |
// Test for #2267 |
... | ... |
@@ -57,7 +57,7 @@ func (s *DockerSuite) TestVolumeCliInspectMulti(c *check.C) { |
57 | 57 |
dockerCmd(c, "volume", "create", "--name", "not-shown") |
58 | 58 |
|
59 | 59 |
result := dockerCmdWithResult("volume", "inspect", "--format={{ .Name }}", "test1", "test2", "doesntexist", "not-shown") |
60 |
- result.Assert(c, icmd.Expected{ |
|
60 |
+ c.Assert(result, icmd.Matches, icmd.Expected{ |
|
61 | 61 |
ExitCode: 1, |
62 | 62 |
Err: "No such volume: doesntexist", |
63 | 63 |
}) |
... | ... |
@@ -403,7 +403,7 @@ func (s *DockerSuite) TestDockerNetworkMacVlanBridgeInternalMode(c *check.C) { |
403 | 403 |
|
404 | 404 |
// access outside of the network should fail |
405 | 405 |
result := dockerCmdWithTimeout(time.Second, "exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8") |
406 |
- result.Assert(c, icmd.Expected{Timeout: true}) |
|
406 |
+ c.Assert(result, icmd.Matches, icmd.Expected{Timeout: true}) |
|
407 | 407 |
|
408 | 408 |
// intra-network communications should succeed |
409 | 409 |
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") |
... | ... |
@@ -443,7 +443,7 @@ func (s *DockerSuite) TestDockerNetworkIpvlanL2InternalMode(c *check.C) { |
443 | 443 |
|
444 | 444 |
// access outside of the network should fail |
445 | 445 |
result := dockerCmdWithTimeout(time.Second, "exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8") |
446 |
- c.Assert(result.Error, check.NotNil) |
|
446 |
+ c.Assert(result, icmd.Matches, icmd.Expected{Timeout: true}) |
|
447 | 447 |
// intra-network communications should succeed |
448 | 448 |
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") |
449 | 449 |
c.Assert(err, check.IsNil) |
... | ... |
@@ -484,7 +484,7 @@ func (s *DockerSuite) TestDockerNetworkIpvlanL3InternalMode(c *check.C) { |
484 | 484 |
|
485 | 485 |
// access outside of the network should fail |
486 | 486 |
result := dockerCmdWithTimeout(time.Second, "exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8") |
487 |
- c.Assert(result.Error, check.NotNil) |
|
487 |
+ c.Assert(result, icmd.Matches, icmd.Expected{Timeout: true}) |
|
488 | 488 |
// intra-network communications should succeed |
489 | 489 |
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") |
490 | 490 |
c.Assert(err, check.IsNil) |
... | ... |
@@ -228,8 +228,9 @@ func readBody(b io.ReadCloser) ([]byte, error) { |
228 | 228 |
return ioutil.ReadAll(b) |
229 | 229 |
} |
230 | 230 |
|
231 |
-func deleteContainer(container string) error { |
|
232 |
- return icmd.RunCommand(dockerBinary, "rm", "-fv", container).Error |
|
231 |
+func deleteContainer(container ...string) error { |
|
232 |
+ result := icmd.RunCommand(dockerBinary, append([]string{"rm", "-fv"}, container...)...) |
|
233 |
+ return result.Compare(icmd.Success) |
|
233 | 234 |
} |
234 | 235 |
|
235 | 236 |
func getAllContainers() (string, error) { |
... | ... |
@@ -248,13 +249,15 @@ func deleteAllContainers() error { |
248 | 248 |
fmt.Println(containers) |
249 | 249 |
return err |
250 | 250 |
} |
251 |
+ if containers == "" { |
|
252 |
+ return nil |
|
253 |
+ } |
|
251 | 254 |
|
252 |
- if containers != "" { |
|
253 |
- if err = deleteContainer(containers); err != nil { |
|
254 |
- return err |
|
255 |
- } |
|
255 |
+ err = deleteContainer(strings.Split(strings.TrimSpace(containers), "\n")...) |
|
256 |
+ if err != nil { |
|
257 |
+ fmt.Println(err.Error()) |
|
256 | 258 |
} |
257 |
- return nil |
|
259 |
+ return err |
|
258 | 260 |
} |
259 | 261 |
|
260 | 262 |
func deleteAllNetworks() error { |
... | ... |
@@ -440,7 +443,7 @@ func dockerCmdWithError(args ...string) (string, int, error) { |
440 | 440 |
} |
441 | 441 |
result := icmd.RunCommand(dockerBinary, args...) |
442 | 442 |
if result.Error != nil { |
443 |
- return result.Combined(), result.ExitCode, fmt.Errorf(result.Fails(icmd.Expected{})) |
|
443 |
+ return result.Combined(), result.ExitCode, result.Compare(icmd.Success) |
|
444 | 444 |
} |
445 | 445 |
return result.Combined(), result.ExitCode, result.Error |
446 | 446 |
} |
... | ... |
@@ -453,7 +456,7 @@ func dockerCmdWithStdoutStderr(c *check.C, args ...string) (string, string, int) |
453 | 453 |
result := icmd.RunCommand(dockerBinary, args...) |
454 | 454 |
// TODO: why is c ever nil? |
455 | 455 |
if c != nil { |
456 |
- result.Assert(c, icmd.Expected{}) |
|
456 |
+ c.Assert(result, icmd.Matches, icmd.Success) |
|
457 | 457 |
} |
458 | 458 |
return result.Stdout(), result.Stderr(), result.ExitCode |
459 | 459 |
} |
... | ... |
@@ -463,7 +466,7 @@ func dockerCmd(c *check.C, args ...string) (string, int) { |
463 | 463 |
c.Fatalf(err.Error()) |
464 | 464 |
} |
465 | 465 |
result := icmd.RunCommand(dockerBinary, args...) |
466 |
- result.Assert(c, icmd.Expected{}) |
|
466 |
+ c.Assert(result, icmd.Matches, icmd.Success) |
|
467 | 467 |
return result.Combined(), result.ExitCode |
468 | 468 |
} |
469 | 469 |
|
... | ... |
@@ -471,7 +474,7 @@ func dockerCmdWithResult(args ...string) *icmd.Result { |
471 | 471 |
return icmd.RunCommand(dockerBinary, args...) |
472 | 472 |
} |
473 | 473 |
|
474 |
-func binaryWithArgs(args []string) []string { |
|
474 |
+func binaryWithArgs(args ...string) []string { |
|
475 | 475 |
return append([]string{dockerBinary}, args...) |
476 | 476 |
} |
477 | 477 |
|
... | ... |
@@ -480,7 +483,7 @@ func dockerCmdWithTimeout(timeout time.Duration, args ...string) *icmd.Result { |
480 | 480 |
if err := validateArgs(args...); err != nil { |
481 | 481 |
return &icmd.Result{Error: err} |
482 | 482 |
} |
483 |
- return icmd.RunCmd(icmd.Cmd{Command: binaryWithArgs(args), Timeout: timeout}) |
|
483 |
+ return icmd.RunCmd(icmd.Cmd{Command: binaryWithArgs(args...), Timeout: timeout}) |
|
484 | 484 |
} |
485 | 485 |
|
486 | 486 |
// execute a docker command in a directory |
... | ... |
@@ -488,7 +491,7 @@ func dockerCmdInDir(c *check.C, path string, args ...string) (string, int, error |
488 | 488 |
if err := validateArgs(args...); err != nil { |
489 | 489 |
c.Fatalf(err.Error()) |
490 | 490 |
} |
491 |
- result := icmd.RunCmd(icmd.Cmd{Command: binaryWithArgs(args), Dir: path}) |
|
491 |
+ result := icmd.RunCmd(icmd.Cmd{Command: binaryWithArgs(args...), Dir: path}) |
|
492 | 492 |
return result.Combined(), result.ExitCode, result.Error |
493 | 493 |
} |
494 | 494 |
|
... | ... |
@@ -498,7 +501,7 @@ func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...strin |
498 | 498 |
return &icmd.Result{Error: err} |
499 | 499 |
} |
500 | 500 |
return icmd.RunCmd(icmd.Cmd{ |
501 |
- Command: binaryWithArgs(args), |
|
501 |
+ Command: binaryWithArgs(args...), |
|
502 | 502 |
Timeout: timeout, |
503 | 503 |
Dir: path, |
504 | 504 |
}) |
... | ... |
@@ -10,6 +10,8 @@ import ( |
10 | 10 |
"strings" |
11 | 11 |
"syscall" |
12 | 12 |
"time" |
13 |
+ |
|
14 |
+ "github.com/go-check/check" |
|
13 | 15 |
) |
14 | 16 |
|
15 | 17 |
type testingT interface { |
... | ... |
@@ -61,17 +63,18 @@ type Result struct { |
61 | 61 |
// Assert compares the Result against the Expected struct, and fails the test if |
62 | 62 |
// any of the expcetations are not met. |
63 | 63 |
func (r *Result) Assert(t testingT, exp Expected) { |
64 |
- fails := r.Fails(exp) |
|
65 |
- if fails == "" { |
|
64 |
+ err := r.Compare(exp) |
|
65 |
+ if err == nil { |
|
66 | 66 |
return |
67 | 67 |
} |
68 | 68 |
|
69 | 69 |
_, file, line, _ := runtime.Caller(1) |
70 |
- t.Fatalf("at %s:%d\n%s", filepath.Base(file), line, fails) |
|
70 |
+ t.Fatalf("at %s:%d\n%s", filepath.Base(file), line, err.Error()) |
|
71 | 71 |
} |
72 | 72 |
|
73 |
-// Fails returns a formatted string which reports on any failed expectations |
|
74 |
-func (r *Result) Fails(exp Expected) string { |
|
73 |
+// Compare returns an formatted error with the command, stdout, stderr, exit |
|
74 |
+// code, and any failed expectations |
|
75 |
+func (r *Result) Compare(exp Expected) error { |
|
75 | 76 |
errors := []string{} |
76 | 77 |
add := func(format string, args ...interface{}) { |
77 | 78 |
errors = append(errors, fmt.Sprintf(format, args...)) |
... | ... |
@@ -107,9 +110,9 @@ func (r *Result) Fails(exp Expected) string { |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
if len(errors) == 0 { |
110 |
- return "" |
|
110 |
+ return nil |
|
111 | 111 |
} |
112 |
- return fmt.Sprintf("%s\nFailures:\n%s\n", r, strings.Join(errors, "\n")) |
|
112 |
+ return fmt.Errorf("%s\nFailures:\n%s\n", r, strings.Join(errors, "\n")) |
|
113 | 113 |
} |
114 | 114 |
|
115 | 115 |
func matchOutput(expected string, actual string) bool { |
... | ... |
@@ -151,6 +154,9 @@ type Expected struct { |
151 | 151 |
Err string |
152 | 152 |
} |
153 | 153 |
|
154 |
+// Success is the default expected result |
|
155 |
+var Success = Expected{} |
|
156 |
+ |
|
154 | 157 |
// Stdout returns the stdout of the process as a string |
155 | 158 |
func (r *Result) Stdout() string { |
156 | 159 |
return r.outBuffer.String() |
... | ... |
@@ -175,9 +181,39 @@ func (r *Result) SetExitError(err error) { |
175 | 175 |
r.ExitCode = ProcessExitCode(err) |
176 | 176 |
} |
177 | 177 |
|
178 |
-// Cmd is a command to run. One of Command or CommandArgs can be used to set the |
|
179 |
-// comand line. Command will be paased to shlex and split into a string slice. |
|
180 |
-// CommandArgs is an already split command line. |
|
178 |
+type matches struct{} |
|
179 |
+ |
|
180 |
+// Info returns the CheckerInfo |
|
181 |
+func (m *matches) Info() *check.CheckerInfo { |
|
182 |
+ return &check.CheckerInfo{ |
|
183 |
+ Name: "CommandMatches", |
|
184 |
+ Params: []string{"result", "expected"}, |
|
185 |
+ } |
|
186 |
+} |
|
187 |
+ |
|
188 |
+// Check compares a result against the expected |
|
189 |
+func (m *matches) Check(params []interface{}, names []string) (bool, string) { |
|
190 |
+ result, ok := params[0].(*Result) |
|
191 |
+ if !ok { |
|
192 |
+ return false, fmt.Sprintf("result must be a *Result, not %T", params[0]) |
|
193 |
+ } |
|
194 |
+ expected, ok := params[1].(Expected) |
|
195 |
+ if !ok { |
|
196 |
+ return false, fmt.Sprintf("expected must be an Expected, not %T", params[1]) |
|
197 |
+ } |
|
198 |
+ |
|
199 |
+ err := result.Compare(expected) |
|
200 |
+ if err == nil { |
|
201 |
+ return true, "" |
|
202 |
+ } |
|
203 |
+ return false, err.Error() |
|
204 |
+} |
|
205 |
+ |
|
206 |
+// Matches is a gocheck.Checker for comparing a Result against an Expected |
|
207 |
+var Matches = &matches{} |
|
208 |
+ |
|
209 |
+// Cmd contains the arguments and options for a process to run as part of a test |
|
210 |
+// suite. |
|
181 | 211 |
type Cmd struct { |
182 | 212 |
Command []string |
183 | 213 |
Timeout time.Duration |