1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Fix port mapping unit tests

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-01-30 14:52:59 -08:00
parent 167403988d
commit 1d4de9ce1f
3 changed files with 26 additions and 6 deletions

View file

@ -1188,10 +1188,18 @@ func (container *Container) allocateNetwork() error {
portJob.Setenv("Proto", port.Proto())
portJob.Setenv("ContainerPort", port.Port())
portEnv, err := portJob.Stdout.AddEnv()
if err != nil {
return err
}
if err := portJob.Run(); err != nil {
eng.Job("release_interface", container.ID).Run()
return err
}
b.HostIp = portEnv.Get("HostIP")
b.HostPort = portEnv.Get("HostPort")
binding[i] = b
}
bindings[port] = binding
}

View file

@ -50,10 +50,10 @@ var (
"192.168.44.1/24",
}
bridgeIface string
defaultBindingIP net.IP
bridgeNetwork *net.IPNet
bridgeIface string
bridgeNetwork *net.IPNet
defaultBindingIP = net.ParseIP("0.0.0.0")
currentInterfaces = make(map[string]*networkInterface)
)
@ -72,7 +72,9 @@ func InitDriver(job *engine.Job) engine.Status {
bridgeIP = job.Getenv("BridgeIP")
)
defaultBindingIP = net.ParseIP(job.Getenv("DefaultBindingIP"))
if defaultIP := job.Getenv("DefaultBindingIP"); defaultIP != "" {
defaultBindingIP = net.ParseIP(defaultIP)
}
bridgeIface = job.Getenv("BridgeIface")
if bridgeIface == "" {
@ -382,6 +384,8 @@ func Release(job *engine.Job) engine.Status {
// Allocate an external port and map it to the interface
func AllocatePort(job *engine.Job) engine.Status {
var (
err error
ip = defaultBindingIP
id = job.Args[0]
hostIP = job.Getenv("HostIP")
@ -396,7 +400,7 @@ func AllocatePort(job *engine.Job) engine.Status {
}
// host ip, proto, and host port
hostPort, err := portallocator.RequestPort(ip, proto, hostPort)
hostPort, err = portallocator.RequestPort(ip, proto, hostPort)
if err != nil {
job.Error(err)
return engine.StatusErr
@ -423,6 +427,14 @@ func AllocatePort(job *engine.Job) engine.Status {
}
network.PortMappings = append(network.PortMappings, host)
out := engine.Env{}
out.Set("HostIP", ip.String())
out.SetInt("HostPort", hostPort)
if _, err := out.WriteTo(job.Stdout); err != nil {
job.Error(err)
return engine.StatusErr
}
return engine.StatusOK
}

View file

@ -51,7 +51,7 @@ func RequestPort(ip net.IP, proto string, port int) (int, error) {
}
// If the user requested a specific port to be allocated
if port != 0 {
if port > 0 {
if err := registerSetPort(ip, proto, port); err != nil {
return 0, err
}