THe port tests in integration-cli tests just for the port-mapping as
seen by Docker daemon. But it doesn't perform a more indepth testing by
checking for the exposed port on the host.
This change helps to fill that gap.
Signed-off-by: Madhu Venugopal <madhu@docker.com>
| ... | ... |
@@ -1,6 +1,7 @@ |
| 1 | 1 |
package main |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
+ "net" |
|
| 4 | 5 |
"os/exec" |
| 5 | 6 |
"sort" |
| 6 | 7 |
"strings" |
| ... | ... |
@@ -145,3 +146,85 @@ func assertPortList(t *testing.T, out string, expected []string) bool {
|
| 145 | 145 |
|
| 146 | 146 |
return true |
| 147 | 147 |
} |
| 148 |
+ |
|
| 149 |
+func TestPortHostBinding(t *testing.T) {
|
|
| 150 |
+ defer deleteAllContainers() |
|
| 151 |
+ |
|
| 152 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", |
|
| 153 |
+ "nc", "-l", "-p", "80") |
|
| 154 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 155 |
+ if err != nil {
|
|
| 156 |
+ t.Fatal(out, err) |
|
| 157 |
+ } |
|
| 158 |
+ firstID := strings.TrimSpace(out) |
|
| 159 |
+ |
|
| 160 |
+ runCmd = exec.Command(dockerBinary, "port", firstID, "80") |
|
| 161 |
+ out, _, err = runCommandWithOutput(runCmd) |
|
| 162 |
+ if err != nil {
|
|
| 163 |
+ t.Fatal(out, err) |
|
| 164 |
+ } |
|
| 165 |
+ |
|
| 166 |
+ if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
|
|
| 167 |
+ t.Error("Port list is not correct")
|
|
| 168 |
+ } |
|
| 169 |
+ |
|
| 170 |
+ runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", |
|
| 171 |
+ "nc", "localhost", "9876") |
|
| 172 |
+ if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
|
| 173 |
+ t.Fatal(out, err) |
|
| 174 |
+ } |
|
| 175 |
+ |
|
| 176 |
+ runCmd = exec.Command(dockerBinary, "rm", "-f", firstID) |
|
| 177 |
+ if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
|
| 178 |
+ t.Fatal(out, err) |
|
| 179 |
+ } |
|
| 180 |
+ |
|
| 181 |
+ runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", |
|
| 182 |
+ "nc", "localhost", "9876") |
|
| 183 |
+ if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 184 |
+ t.Error("Port is still bound after the Container is removed")
|
|
| 185 |
+ } |
|
| 186 |
+ logDone("port - test host binding done")
|
|
| 187 |
+} |
|
| 188 |
+ |
|
| 189 |
+func TestPortExposeHostBinding(t *testing.T) {
|
|
| 190 |
+ defer deleteAllContainers() |
|
| 191 |
+ |
|
| 192 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--expose", "80", "busybox", |
|
| 193 |
+ "nc", "-l", "-p", "80") |
|
| 194 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 195 |
+ if err != nil {
|
|
| 196 |
+ t.Fatal(out, err) |
|
| 197 |
+ } |
|
| 198 |
+ firstID := strings.TrimSpace(out) |
|
| 199 |
+ |
|
| 200 |
+ runCmd = exec.Command(dockerBinary, "port", firstID, "80") |
|
| 201 |
+ out, _, err = runCommandWithOutput(runCmd) |
|
| 202 |
+ if err != nil {
|
|
| 203 |
+ t.Fatal(out, err) |
|
| 204 |
+ } |
|
| 205 |
+ |
|
| 206 |
+ _, exposedPort, err := net.SplitHostPort(out) |
|
| 207 |
+ |
|
| 208 |
+ if err != nil {
|
|
| 209 |
+ t.Fatal(out, err) |
|
| 210 |
+ } |
|
| 211 |
+ |
|
| 212 |
+ runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", |
|
| 213 |
+ "nc", "localhost", strings.TrimSpace(exposedPort)) |
|
| 214 |
+ if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
|
| 215 |
+ t.Fatal(out, err) |
|
| 216 |
+ } |
|
| 217 |
+ |
|
| 218 |
+ runCmd = exec.Command(dockerBinary, "rm", "-f", firstID) |
|
| 219 |
+ if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
|
| 220 |
+ t.Fatal(out, err) |
|
| 221 |
+ } |
|
| 222 |
+ |
|
| 223 |
+ runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", |
|
| 224 |
+ "nc", "localhost", strings.TrimSpace(exposedPort)) |
|
| 225 |
+ if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
|
| 226 |
+ t.Error("Port is still bound after the Container is removed")
|
|
| 227 |
+ } |
|
| 228 |
+ logDone("port - test port expose done")
|
|
| 229 |
+} |