Browse code

Add integ test for unpublished ports in ps o/p

- This is a test to assert the fix #13734

Signed-off-by: Alessandro Boch <aboch@docker.com>

Alessandro Boch authored on 2015/06/06 06:50:11
Showing 1 changed files
... ...
@@ -1,8 +1,10 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"net"
5 6
 	"os/exec"
7
+	"regexp"
6 8
 	"sort"
7 9
 	"strings"
8 10
 
... ...
@@ -221,3 +223,96 @@ func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
221 221
 		c.Error("Port is still bound after the Container is removed")
222 222
 	}
223 223
 }
224
+
225
+func stopRemoveContainer(id string, c *check.C) {
226
+	runCmd := exec.Command(dockerBinary, "rm", "-f", id)
227
+	_, _, err := runCommandWithOutput(runCmd)
228
+	c.Assert(err, check.IsNil)
229
+}
230
+
231
+func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
232
+	// Run busybox with command line expose (equivalent to EXPOSE in image's Dockerfile) for the following ports
233
+	port1 := 80
234
+	port2 := 443
235
+	expose1 := fmt.Sprintf("--expose=%d", port1)
236
+	expose2 := fmt.Sprintf("--expose=%d", port2)
237
+	runCmd := exec.Command(dockerBinary, "run", "-d", expose1, expose2, "busybox", "sleep", "5")
238
+	out, _, err := runCommandWithOutput(runCmd)
239
+	c.Assert(err, check.IsNil)
240
+
241
+	// Check docker ps o/p for last created container reports the unpublished ports
242
+	unpPort1 := fmt.Sprintf("%d/tcp", port1)
243
+	unpPort2 := fmt.Sprintf("%d/tcp", port2)
244
+	runCmd = exec.Command(dockerBinary, "ps", "-n=1")
245
+	out, _, err = runCommandWithOutput(runCmd)
246
+	c.Assert(err, check.IsNil)
247
+	if !strings.Contains(out, unpPort1) || !strings.Contains(out, unpPort2) {
248
+		c.Errorf("Missing unpublished ports(s) (%s, %s) in docker ps output: %s", unpPort1, unpPort2, out)
249
+	}
250
+
251
+	// Run the container forcing to publish the exposed ports
252
+	runCmd = exec.Command(dockerBinary, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5")
253
+	out, _, err = runCommandWithOutput(runCmd)
254
+	c.Assert(err, check.IsNil)
255
+
256
+	// Check docker ps o/p for last created container reports the exposed ports in the port bindings
257
+	expBndRegx1 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort1)
258
+	expBndRegx2 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort2)
259
+	runCmd = exec.Command(dockerBinary, "ps", "-n=1")
260
+	out, _, err = runCommandWithOutput(runCmd)
261
+	c.Assert(err, check.IsNil)
262
+	if !expBndRegx1.MatchString(out) || !expBndRegx2.MatchString(out) {
263
+		c.Errorf("Cannot find expected port binding ports(s) (0.0.0.0:xxxxx->%s, 0.0.0.0:xxxxx->%s) in docker ps output:\n%s",
264
+			unpPort1, unpPort2, out)
265
+	}
266
+
267
+	// Run the container specifying explicit port bindings for the exposed ports
268
+	offset := 10000
269
+	pFlag1 := fmt.Sprintf("%d:%d", offset+port1, port1)
270
+	pFlag2 := fmt.Sprintf("%d:%d", offset+port2, port2)
271
+	runCmd = exec.Command(dockerBinary, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5")
272
+	out, _, err = runCommandWithOutput(runCmd)
273
+	c.Assert(err, check.IsNil)
274
+	id := strings.TrimSpace(out)
275
+
276
+	// Check docker ps o/p for last created container reports the specified port mappings
277
+	expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1)
278
+	expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2)
279
+	runCmd = exec.Command(dockerBinary, "ps", "-n=1")
280
+	out, _, err = runCommandWithOutput(runCmd)
281
+	c.Assert(err, check.IsNil)
282
+	if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) {
283
+		c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out)
284
+	}
285
+	// Remove container now otherwise it will interfeer with next test
286
+	stopRemoveContainer(id, c)
287
+
288
+	// Run the container with explicit port bindings and no exposed ports
289
+	runCmd = exec.Command(dockerBinary, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5")
290
+	out, _, err = runCommandWithOutput(runCmd)
291
+	c.Assert(err, check.IsNil)
292
+	id = strings.TrimSpace(out)
293
+
294
+	// Check docker ps o/p for last created container reports the specified port mappings
295
+	runCmd = exec.Command(dockerBinary, "ps", "-n=1")
296
+	out, _, err = runCommandWithOutput(runCmd)
297
+	c.Assert(err, check.IsNil)
298
+	if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) {
299
+		c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out)
300
+	}
301
+	// Remove container now otherwise it will interfeer with next test
302
+	stopRemoveContainer(id, c)
303
+
304
+	// Run the container with one unpublished exposed port and one explicit port binding
305
+	runCmd = exec.Command(dockerBinary, "run", "-d", expose1, "-p", pFlag2, "busybox", "sleep", "5")
306
+	out, _, err = runCommandWithOutput(runCmd)
307
+	c.Assert(err, check.IsNil)
308
+
309
+	// Check docker ps o/p for last created container reports the specified unpublished port and port mapping
310
+	runCmd = exec.Command(dockerBinary, "ps", "-n=1")
311
+	out, _, err = runCommandWithOutput(runCmd)
312
+	c.Assert(err, check.IsNil)
313
+	if !strings.Contains(out, unpPort1) || !strings.Contains(out, expBnd2) {
314
+		c.Errorf("Missing unpublished ports or port binding (%s, %s) in docker ps output: %s", unpPort1, expBnd2, out)
315
+	}
316
+}