Skip to content

Commit

Permalink
Merge pull request #17 from TrekkieCoder/main
Browse files Browse the repository at this point in the history
PR - IPAM quirks
  • Loading branch information
UltraInstinct14 authored Sep 17, 2023
2 parents ce487be + 08fd9e6 commit 122edd7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ipalloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ func (ipa *IPAllocator) AddIPRange(cluster string, cidr string) error {
start = 0
}

val := Ntohl(IPtonl(ip))
msk := uint32(((1 << (32 - sz)) - 1))
if val&msk != 0 {
start = uint64(val & msk)
ignore = start + 1
}

iprSz = (1 << (32 - sz)) - ignore
} else {
ignore := uint64(0)
Expand Down
48 changes: 48 additions & 0 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,54 @@ func TestIPAlloc(t *testing.T) {
if ip.String() != "71.71.71.0" {
t.Fatalf("Failed IP Alloc for 71.71.71.0/31: %s:%s", ip.String(), "71.71.71.0")
}

ipa.AddIPRange(IPClusterDefault, "192.168.10.252/24")
ip, err = ipa.AllocateNewIP(IPClusterDefault, "192.168.10.252/24", IPAMNoIdent)
if err != nil {
t.Fatal("Failed IP Alloc for 192.168.10.252/24 - Check Alloc Algo")
}

if ip.String() != "192.168.10.252" {
t.Fatalf("Failed IP Alloc for 192.168.10.252/24: %s:%s", ip.String(), "192.168.10.252")
}

ip, err = ipa.AllocateNewIP(IPClusterDefault, "192.168.10.252/24", IPAMNoIdent)
if err != nil {
t.Fatal("Failed IP Alloc for 192.168.10.252/24 - Check Alloc Algo")
}

if ip.String() != "192.168.10.253" {
t.Fatalf("Failed IP Alloc for 192.168.10.252/24: %s:%s", ip.String(), "192.168.10.253")
}

ip, err = ipa.AllocateNewIP(IPClusterDefault, "192.168.10.252/24", IPAMNoIdent)
if err != nil {
t.Fatal("Failed IP Alloc for 192.168.10.252/24 - Check Alloc Algo")
}

if ip.String() != "192.168.10.254" {
t.Fatalf("Failed IP Alloc for 192.168.10.252/24: %s:%s", ip.String(), "192.168.10.254")
}

_, err = ipa.AllocateNewIP(IPClusterDefault, "192.168.10.252/24", IPAMNoIdent)
if err == nil {
t.Fatal("Allocated IP Alloc for 192.168.10.252/24 - Check Alloc Algo")
}

err = ipa.DeAllocateIP(IPClusterDefault, "192.168.10.252/24", IPAMNoIdent, "192.168.10.253")
if err != nil {
t.Fatalf("IP DeAlloc failed for %s:%s", "192.168.10.253", err)
}

ip, err = ipa.AllocateNewIP(IPClusterDefault, "192.168.10.252/24", IPAMNoIdent)
if err != nil {
t.Fatal("Failed IP Alloc for 192.168.10.252/24 - Check Alloc Algo")
}

if ip.String() != "192.168.10.253" {
t.Fatalf("Failed IP Alloc for 192.168.10.252/24: %s:%s", ip.String(), "192.168.10.253")
}

}

func TestProber(t *testing.T) {
Expand Down

0 comments on commit 122edd7

Please sign in to comment.