Browse code

Fix port mapping unit tests

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/01/31 07:52:59
Showing 3 changed files
... ...
@@ -1188,10 +1188,18 @@ func (container *Container) allocateNetwork() error {
1188 1188
 			portJob.Setenv("Proto", port.Proto())
1189 1189
 			portJob.Setenv("ContainerPort", port.Port())
1190 1190
 
1191
+			portEnv, err := portJob.Stdout.AddEnv()
1192
+			if err != nil {
1193
+				return err
1194
+			}
1191 1195
 			if err := portJob.Run(); err != nil {
1192 1196
 				eng.Job("release_interface", container.ID).Run()
1193 1197
 				return err
1194 1198
 			}
1199
+			b.HostIp = portEnv.Get("HostIP")
1200
+			b.HostPort = portEnv.Get("HostPort")
1201
+
1202
+			binding[i] = b
1195 1203
 		}
1196 1204
 		bindings[port] = binding
1197 1205
 	}
... ...
@@ -50,10 +50,10 @@ var (
50 50
 		"192.168.44.1/24",
51 51
 	}
52 52
 
53
-	bridgeIface      string
54
-	defaultBindingIP net.IP
55
-	bridgeNetwork    *net.IPNet
53
+	bridgeIface   string
54
+	bridgeNetwork *net.IPNet
56 55
 
56
+	defaultBindingIP  = net.ParseIP("0.0.0.0")
57 57
 	currentInterfaces = make(map[string]*networkInterface)
58 58
 )
59 59
 
... ...
@@ -72,7 +72,9 @@ func InitDriver(job *engine.Job) engine.Status {
72 72
 		bridgeIP       = job.Getenv("BridgeIP")
73 73
 	)
74 74
 
75
-	defaultBindingIP = net.ParseIP(job.Getenv("DefaultBindingIP"))
75
+	if defaultIP := job.Getenv("DefaultBindingIP"); defaultIP != "" {
76
+		defaultBindingIP = net.ParseIP(defaultIP)
77
+	}
76 78
 
77 79
 	bridgeIface = job.Getenv("BridgeIface")
78 80
 	if bridgeIface == "" {
... ...
@@ -382,6 +384,8 @@ func Release(job *engine.Job) engine.Status {
382 382
 // Allocate an external port and map it to the interface
383 383
 func AllocatePort(job *engine.Job) engine.Status {
384 384
 	var (
385
+		err error
386
+
385 387
 		ip            = defaultBindingIP
386 388
 		id            = job.Args[0]
387 389
 		hostIP        = job.Getenv("HostIP")
... ...
@@ -396,7 +400,7 @@ func AllocatePort(job *engine.Job) engine.Status {
396 396
 	}
397 397
 
398 398
 	// host ip, proto, and host port
399
-	hostPort, err := portallocator.RequestPort(ip, proto, hostPort)
399
+	hostPort, err = portallocator.RequestPort(ip, proto, hostPort)
400 400
 	if err != nil {
401 401
 		job.Error(err)
402 402
 		return engine.StatusErr
... ...
@@ -423,6 +427,14 @@ func AllocatePort(job *engine.Job) engine.Status {
423 423
 	}
424 424
 	network.PortMappings = append(network.PortMappings, host)
425 425
 
426
+	out := engine.Env{}
427
+	out.Set("HostIP", ip.String())
428
+	out.SetInt("HostPort", hostPort)
429
+
430
+	if _, err := out.WriteTo(job.Stdout); err != nil {
431
+		job.Error(err)
432
+		return engine.StatusErr
433
+	}
426 434
 	return engine.StatusOK
427 435
 }
428 436
 
... ...
@@ -51,7 +51,7 @@ func RequestPort(ip net.IP, proto string, port int) (int, error) {
51 51
 	}
52 52
 
53 53
 	// If the user requested a specific port to be allocated
54
-	if port != 0 {
54
+	if port > 0 {
55 55
 		if err := registerSetPort(ip, proto, port); err != nil {
56 56
 			return 0, err
57 57
 		}