Signed-off-by: Xiaoxu Chen <chenxiaoxu14@otcaix.iscas.ac.cn>
refactor integration-cli/docker_cli_netmode_test.go use Assert statement
part of #16756
Signed-off-by: Xiaoxu Chen <chenxiaoxu14@otcaix.iscas.ac.cn>
update integration-cli/docker_cli_netmode_test.go
part of #16756
Signed-off-by: Xiaoxu Chen <chenxiaoxu14@otcaix.iscas.ac.cn>
update docker_cli_netmode_test.go
part of #16756
Signed-off-by: Xiaoxu Chen <chenxiaoxu14@otcaix.iscas.ac.cn>
update integration-cli/docker_cli_netmode_test.go
part of #16756
Signed-off-by: Xiaoxu Chen <chenxiaoxu14@otcaix.iscas.ac.cn>
update integration-cli/docker_cli_netmode_test.go
part of #16756
Signed-off-by: Xiaoxu Chen <chenxiaoxu14@otcaix.iscas.ac.cn>
update integration-cli/docker_cli_netmode_test.go
part of #16756
Signed-off-by: Xiaoxu Chen <chenxiaoxu14@otcaix.iscas.ac.cn>
update integration-cli/docker_cli_netmode_test.go
part of #16756
Signed-off-by: Xiaoxu Chen <chenxiaoxu14@otcaix.iscas.ac.cn>
| ... | ... |
@@ -1,9 +1,7 @@ |
| 1 | 1 |
package main |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
- "os/exec" |
|
| 5 |
- "strings" |
|
| 6 |
- |
|
| 4 |
+ "github.com/docker/docker/pkg/integration/checker" |
|
| 7 | 5 |
"github.com/docker/docker/runconfig" |
| 8 | 6 |
"github.com/go-check/check" |
| 9 | 7 |
) |
| ... | ... |
@@ -14,152 +12,80 @@ import ( |
| 14 | 14 |
// the command executed in a container did really run PS correctly. |
| 15 | 15 |
const stringCheckPS = "PID USER" |
| 16 | 16 |
|
| 17 |
-// checkContains is a helper function that validates a command output did |
|
| 18 |
-// contain what was expected. |
|
| 19 |
-func checkContains(expected string, out string, c *check.C) {
|
|
| 20 |
- if !strings.Contains(out, expected) {
|
|
| 21 |
- c.Fatalf("Expected '%s', got '%s'", expected, out)
|
|
| 22 |
- } |
|
| 17 |
+// DockerCmdWithFail executes a docker command that is supposed to fail and returns |
|
| 18 |
+// the output, the exit code. If the command returns an Nil error, it will fail and |
|
| 19 |
+// stop the tests. |
|
| 20 |
+func dockerCmdWithFail(c *check.C, args ...string) (string, int) {
|
|
| 21 |
+ out, status, err := dockerCmdWithError(args...) |
|
| 22 |
+ c.Assert(err, check.NotNil, check.Commentf("%v", out))
|
|
| 23 |
+ return out, status |
|
| 23 | 24 |
} |
| 24 | 25 |
|
| 25 | 26 |
func (s *DockerSuite) TestNetHostname(c *check.C) {
|
| 26 | 27 |
testRequires(c, DaemonIsLinux, NotUserNamespace) |
| 27 | 28 |
|
| 28 |
- var ( |
|
| 29 |
- out string |
|
| 30 |
- err error |
|
| 31 |
- runCmd *exec.Cmd |
|
| 32 |
- ) |
|
| 33 |
- |
|
| 34 |
- runCmd = exec.Command(dockerBinary, "run", "-h=name", "busybox", "ps") |
|
| 35 |
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
|
| 36 |
- c.Fatalf(out, err) |
|
| 37 |
- } |
|
| 38 |
- checkContains(stringCheckPS, out, c) |
|
| 39 |
- |
|
| 40 |
- runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "ps") |
|
| 41 |
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
|
| 42 |
- c.Fatalf(out, err) |
|
| 43 |
- } |
|
| 44 |
- checkContains(stringCheckPS, out, c) |
|
| 45 |
- |
|
| 46 |
- runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=bridge", "busybox", "ps") |
|
| 47 |
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
|
| 48 |
- c.Fatalf(out, err) |
|
| 49 |
- } |
|
| 50 |
- checkContains(stringCheckPS, out, c) |
|
| 51 |
- |
|
| 52 |
- runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=none", "busybox", "ps") |
|
| 53 |
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
|
| 54 |
- c.Fatalf(out, err) |
|
| 55 |
- } |
|
| 56 |
- checkContains(stringCheckPS, out, c) |
|
| 57 |
- |
|
| 58 |
- runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=host", "busybox", "ps") |
|
| 59 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 60 |
- c.Fatalf(out, err) |
|
| 61 |
- } |
|
| 62 |
- checkContains(runconfig.ErrConflictNetworkHostname.Error(), out, c) |
|
| 63 |
- |
|
| 64 |
- runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=container:other", "busybox", "ps") |
|
| 65 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 66 |
- c.Fatalf(out, err, c) |
|
| 67 |
- } |
|
| 68 |
- checkContains(runconfig.ErrConflictNetworkHostname.Error(), out, c) |
|
| 69 |
- |
|
| 70 |
- runCmd = exec.Command(dockerBinary, "run", "--net=container", "busybox", "ps") |
|
| 71 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 72 |
- c.Fatalf(out, err, c) |
|
| 73 |
- } |
|
| 74 |
- checkContains("--net: invalid net mode: invalid container format container:<name|id>", out, c)
|
|
| 75 |
- |
|
| 76 |
- runCmd = exec.Command(dockerBinary, "run", "--net=weird", "busybox", "ps") |
|
| 77 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 78 |
- c.Fatalf(out, err) |
|
| 79 |
- } |
|
| 80 |
- checkContains("network weird not found", out, c)
|
|
| 29 |
+ out, _ := dockerCmd(c, "run", "-h=name", "busybox", "ps") |
|
| 30 |
+ c.Assert(out, checker.Contains, stringCheckPS) |
|
| 31 |
+ |
|
| 32 |
+ out, _ = dockerCmd(c, "run", "--net=host", "busybox", "ps") |
|
| 33 |
+ c.Assert(out, checker.Contains, stringCheckPS) |
|
| 34 |
+ |
|
| 35 |
+ out, _ = dockerCmd(c, "run", "-h=name", "--net=bridge", "busybox", "ps") |
|
| 36 |
+ c.Assert(out, checker.Contains, stringCheckPS) |
|
| 37 |
+ |
|
| 38 |
+ out, _ = dockerCmd(c, "run", "-h=name", "--net=none", "busybox", "ps") |
|
| 39 |
+ c.Assert(out, checker.Contains, stringCheckPS) |
|
| 40 |
+ |
|
| 41 |
+ out, _ = dockerCmdWithFail(c, "run", "-h=name", "--net=host", "busybox", "ps") |
|
| 42 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkHostname.Error()) |
|
| 43 |
+ |
|
| 44 |
+ out, _ = dockerCmdWithFail(c, "run", "-h=name", "--net=container:other", "busybox", "ps") |
|
| 45 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkHostname.Error()) |
|
| 46 |
+ |
|
| 47 |
+ out, _ = dockerCmdWithFail(c, "run", "--net=container", "busybox", "ps") |
|
| 48 |
+ c.Assert(out, checker.Contains, "--net: invalid net mode: invalid container format container:<name|id>") |
|
| 49 |
+ |
|
| 50 |
+ out, _ = dockerCmdWithFail(c, "run", "--net=weird", "busybox", "ps") |
|
| 51 |
+ c.Assert(out, checker.Contains, "network weird not found") |
|
| 81 | 52 |
} |
| 82 | 53 |
|
| 83 | 54 |
func (s *DockerSuite) TestConflictContainerNetworkAndLinks(c *check.C) {
|
| 84 | 55 |
testRequires(c, DaemonIsLinux, NotUserNamespace) |
| 85 |
- var ( |
|
| 86 |
- out string |
|
| 87 |
- err error |
|
| 88 |
- runCmd *exec.Cmd |
|
| 89 |
- ) |
|
| 90 |
- |
|
| 91 |
- runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps") |
|
| 92 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 93 |
- c.Fatalf(out, err) |
|
| 94 |
- } |
|
| 95 |
- checkContains(runconfig.ErrConflictContainerNetworkAndLinks.Error(), out, c) |
|
| 96 |
- |
|
| 97 |
- runCmd = exec.Command(dockerBinary, "run", "--net=host", "--link=zip:zap", "busybox", "ps") |
|
| 98 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 99 |
- c.Fatalf(out, err) |
|
| 100 |
- } |
|
| 101 |
- checkContains(runconfig.ErrConflictHostNetworkAndLinks.Error(), out, c) |
|
| 56 |
+ |
|
| 57 |
+ out, _ := dockerCmdWithFail(c, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps") |
|
| 58 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictContainerNetworkAndLinks.Error()) |
|
| 59 |
+ |
|
| 60 |
+ out, _ = dockerCmdWithFail(c, "run", "--net=host", "--link=zip:zap", "busybox", "ps") |
|
| 61 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error()) |
|
| 102 | 62 |
} |
| 103 | 63 |
|
| 104 | 64 |
func (s *DockerSuite) TestConflictNetworkModeAndOptions(c *check.C) {
|
| 105 | 65 |
testRequires(c, DaemonIsLinux, NotUserNamespace) |
| 106 |
- var ( |
|
| 107 |
- out string |
|
| 108 |
- err error |
|
| 109 |
- runCmd *exec.Cmd |
|
| 110 |
- ) |
|
| 111 |
- |
|
| 112 |
- runCmd = exec.Command(dockerBinary, "run", "--net=host", "--dns=8.8.8.8", "busybox", "ps") |
|
| 113 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 114 |
- c.Fatalf(out, err) |
|
| 115 |
- } |
|
| 116 |
- checkContains(runconfig.ErrConflictNetworkAndDNS.Error(), out, c) |
|
| 117 |
- |
|
| 118 |
- runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps") |
|
| 119 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 120 |
- c.Fatalf(out, err) |
|
| 121 |
- } |
|
| 122 |
- checkContains(runconfig.ErrConflictNetworkAndDNS.Error(), out, c) |
|
| 123 |
- |
|
| 124 |
- runCmd = exec.Command(dockerBinary, "run", "--net=host", "--add-host=name:8.8.8.8", "busybox", "ps") |
|
| 125 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 126 |
- c.Fatalf(out, err) |
|
| 127 |
- } |
|
| 128 |
- checkContains(runconfig.ErrConflictNetworkHosts.Error(), out, c) |
|
| 129 |
- |
|
| 130 |
- runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps") |
|
| 131 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 132 |
- c.Fatalf(out, err) |
|
| 133 |
- } |
|
| 134 |
- checkContains(runconfig.ErrConflictNetworkHosts.Error(), out, c) |
|
| 135 |
- |
|
| 136 |
- runCmd = exec.Command(dockerBinary, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps") |
|
| 137 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 138 |
- c.Fatalf(out, err) |
|
| 139 |
- } |
|
| 140 |
- checkContains(runconfig.ErrConflictContainerNetworkAndMac.Error(), out, c) |
|
| 141 |
- |
|
| 142 |
- runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps") |
|
| 143 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 144 |
- c.Fatalf(out, err) |
|
| 145 |
- } |
|
| 146 |
- checkContains(runconfig.ErrConflictContainerNetworkAndMac.Error(), out, c) |
|
| 147 |
- |
|
| 148 |
- runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "-P", "busybox", "ps") |
|
| 149 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 150 |
- c.Fatalf(out, err) |
|
| 151 |
- } |
|
| 152 |
- checkContains(runconfig.ErrConflictNetworkPublishPorts.Error(), out, c) |
|
| 153 |
- |
|
| 154 |
- runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "-p", "8080", "busybox", "ps") |
|
| 155 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 156 |
- c.Fatalf(out, err) |
|
| 157 |
- } |
|
| 158 |
- checkContains(runconfig.ErrConflictNetworkPublishPorts.Error(), out, c) |
|
| 159 |
- |
|
| 160 |
- runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps") |
|
| 161 |
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 162 |
- c.Fatalf(out, err) |
|
| 163 |
- } |
|
| 164 |
- checkContains(runconfig.ErrConflictNetworkExposePorts.Error(), out, c) |
|
| 66 |
+ |
|
| 67 |
+ out, _ := dockerCmdWithFail(c, "run", "--net=host", "--dns=8.8.8.8", "busybox", "ps") |
|
| 68 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkAndDNS.Error()) |
|
| 69 |
+ |
|
| 70 |
+ out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps") |
|
| 71 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkAndDNS.Error()) |
|
| 72 |
+ |
|
| 73 |
+ out, _ = dockerCmdWithFail(c, "run", "--net=host", "--add-host=name:8.8.8.8", "busybox", "ps") |
|
| 74 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkHosts.Error()) |
|
| 75 |
+ |
|
| 76 |
+ out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps") |
|
| 77 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkHosts.Error()) |
|
| 78 |
+ |
|
| 79 |
+ out, _ = dockerCmdWithFail(c, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps") |
|
| 80 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictContainerNetworkAndMac.Error()) |
|
| 81 |
+ |
|
| 82 |
+ out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps") |
|
| 83 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictContainerNetworkAndMac.Error()) |
|
| 84 |
+ |
|
| 85 |
+ out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "-P", "busybox", "ps") |
|
| 86 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkPublishPorts.Error()) |
|
| 87 |
+ |
|
| 88 |
+ out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "-p", "8080", "busybox", "ps") |
|
| 89 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkPublishPorts.Error()) |
|
| 90 |
+ |
|
| 91 |
+ out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps") |
|
| 92 |
+ c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkExposePorts.Error()) |
|
| 165 | 93 |
} |