Merge pull request #8125 from somaopensource/8102-fix
Check whether net.ParseIP returned nil or not
This commit is contained in:
commit
4778d7d52d
|
@ -401,6 +401,9 @@ func AllocatePort(job *engine.Job) engine.Status {
|
||||||
|
|
||||||
if hostIP != "" {
|
if hostIP != "" {
|
||||||
ip = net.ParseIP(hostIP)
|
ip = net.ParseIP(hostIP)
|
||||||
|
if ip == nil {
|
||||||
|
return job.Errorf("Bad parameter: invalid host ip %s", hostIP)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// host ip, proto, and host port
|
// host ip, proto, and host port
|
||||||
|
|
|
@ -39,6 +39,17 @@ func newPortAllocationJob(eng *engine.Engine, port int) (job *engine.Job) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newPortAllocationJobWithInvalidHostIP(eng *engine.Engine, port int) (job *engine.Job) {
|
||||||
|
strPort := strconv.Itoa(port)
|
||||||
|
|
||||||
|
job = eng.Job("allocate_port", "container_id")
|
||||||
|
job.Setenv("HostIP", "localhost")
|
||||||
|
job.Setenv("HostPort", strPort)
|
||||||
|
job.Setenv("Proto", "tcp")
|
||||||
|
job.Setenv("ContainerPort", strPort)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func TestAllocatePortDetection(t *testing.T) {
|
func TestAllocatePortDetection(t *testing.T) {
|
||||||
eng := engine.New()
|
eng := engine.New()
|
||||||
eng.Logging = false
|
eng.Logging = false
|
||||||
|
@ -66,3 +77,28 @@ func TestAllocatePortDetection(t *testing.T) {
|
||||||
t.Fatal("Duplicate port allocation granted by AllocatePort")
|
t.Fatal("Duplicate port allocation granted by AllocatePort")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHostnameFormatChecking(t *testing.T) {
|
||||||
|
eng := engine.New()
|
||||||
|
eng.Logging = false
|
||||||
|
|
||||||
|
freePort := findFreePort(t)
|
||||||
|
|
||||||
|
// Init driver
|
||||||
|
job := eng.Job("initdriver")
|
||||||
|
if res := InitDriver(job); res != engine.StatusOK {
|
||||||
|
t.Fatal("Failed to initialize network driver")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate interface
|
||||||
|
job = eng.Job("allocate_interface", "container_id")
|
||||||
|
if res := Allocate(job); res != engine.StatusOK {
|
||||||
|
t.Fatal("Failed to allocate network interface")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate port with invalid HostIP, expect failure with Bad Request http status
|
||||||
|
job = newPortAllocationJobWithInvalidHostIP(eng, freePort)
|
||||||
|
if res := AllocatePort(job); res == engine.StatusOK {
|
||||||
|
t.Fatal("Failed to check invalid HostIP")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue