mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Update code for new test cases
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
fccca3542b
commit
9d11db0f8c
2 changed files with 17 additions and 24 deletions
|
@ -19,7 +19,7 @@ var (
|
||||||
ErrNetworkAlreadyAllocated = errors.New("requested network overlaps with existing network")
|
ErrNetworkAlreadyAllocated = errors.New("requested network overlaps with existing network")
|
||||||
ErrNetworkAlreadyRegisterd = errors.New("requested network is already registered")
|
ErrNetworkAlreadyRegisterd = errors.New("requested network is already registered")
|
||||||
ErrNetworkOverlapsWithNameservers = errors.New("requested network overlaps with nameserver")
|
ErrNetworkOverlapsWithNameservers = errors.New("requested network overlaps with nameserver")
|
||||||
ErrNoAvailableIps = errors.New("no available ips on network")
|
ErrNoAvailableIPs = errors.New("no available ip addresses on network")
|
||||||
ErrIPAlreadyAllocated = errors.New("ip already allocated")
|
ErrIPAlreadyAllocated = errors.New("ip already allocated")
|
||||||
|
|
||||||
lock = sync.Mutex{}
|
lock = sync.Mutex{}
|
||||||
|
@ -100,11 +100,11 @@ func getNextIp(network *net.IPNet) (*net.IP, error) {
|
||||||
ownIP = ipToInt(&network.IP)
|
ownIP = ipToInt(&network.IP)
|
||||||
available = availableIPS[n]
|
available = availableIPS[n]
|
||||||
allocated = allocatedIPs[n]
|
allocated = allocatedIPs[n]
|
||||||
|
first, _ = networkRange(network)
|
||||||
first, _ = networkRange(network)
|
base = ipToInt(&first)
|
||||||
base = ipToInt(&first)
|
size = int(networkSize(network.Mask))
|
||||||
|
max = int32(size - 2) // size -1 for the broadcast address, -1 for the gateway address
|
||||||
pos = int32(available.Pop())
|
pos = int32(available.Pop())
|
||||||
)
|
)
|
||||||
|
|
||||||
// We pop and push the position not the ip
|
// We pop and push the position not the ip
|
||||||
|
@ -115,29 +115,22 @@ func getNextIp(network *net.IPNet) (*net.IP, error) {
|
||||||
return ip, nil
|
return ip, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
pos = int32(allocated.PullBack())
|
||||||
size = int(networkSize(network.Mask))
|
|
||||||
max = int32(size - 2) // size -1 for the broadcast address, -1 for the gateway address
|
|
||||||
)
|
|
||||||
|
|
||||||
if pos = int32(allocated.PullBack()); pos == 0 {
|
|
||||||
pos = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := int32(0); i < max; i++ {
|
for i := int32(0); i < max; i++ {
|
||||||
next := int32(base + pos)
|
|
||||||
pos = pos%max + 1
|
pos = pos%max + 1
|
||||||
|
next := int32(base + pos)
|
||||||
|
|
||||||
if next == ownIP {
|
if next == ownIP {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ip := intToIP(next)
|
if !allocated.Exists(int(pos)) {
|
||||||
allocated.Push(int(pos))
|
ip := intToIP(next)
|
||||||
|
allocated.Push(int(pos))
|
||||||
return ip, nil
|
return ip, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil, ErrNoAvailableIps
|
return nil, ErrNoAvailableIPs
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerIP(network *net.IPNet, ip *net.IP) error {
|
func registerIP(network *net.IPNet, ip *net.IP) error {
|
||||||
|
|
|
@ -386,9 +386,9 @@ func TestIPAllocator(t *testing.T) {
|
||||||
// 2(u) - 3(u) - 4(u) - 5(u) - 6(u)
|
// 2(u) - 3(u) - 4(u) - 5(u) - 6(u)
|
||||||
// ↑
|
// ↑
|
||||||
|
|
||||||
assertIPEquals(t, &expectedIPs[3], newIPs[0])
|
assertIPEquals(t, &expectedIPs[2], newIPs[0])
|
||||||
assertIPEquals(t, &expectedIPs[4], newIPs[1])
|
assertIPEquals(t, &expectedIPs[3], newIPs[1])
|
||||||
assertIPEquals(t, &expectedIPs[2], newIPs[2])
|
assertIPEquals(t, &expectedIPs[4], newIPs[2])
|
||||||
|
|
||||||
_, err = RequestIP(network, nil)
|
_, err = RequestIP(network, nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
Loading…
Add table
Reference in a new issue