package incus import ( "net/netip" "testing" ) func TestPickSubnetAvoidsTaken(t *testing.T) { taken := []netip.Prefix{ netip.MustParsePrefix("10.0.0.0/8"), // the LAN, a /8 netip.MustParsePrefix("172.16.100.0/24"), // first choice gone netip.MustParsePrefix("172.16.101.0/24"), // and the second netip.MustParsePrefix("172.28.0.0/14"), // Docker's configured pool netip.MustParsePrefix("192.168.80.0/20"), // a Docker network } got := PickSubnet(taken) if got.String() != "172.16.102.0/24" { t.Fatalf("got %s", got) } if Gateway(got) != "172.16.102.1/24" { t.Fatalf("gateway %s", Gateway(got)) } } func TestPickSubnetWholeBlockTaken(t *testing.T) { // 172.16.0.0/12 covers every 172.16.x candidate; the picker must move to 192.168 got := PickSubnet([]netip.Prefix{netip.MustParsePrefix("172.16.0.0/12")}) if got.String() != "192.168.254.0/24" { t.Fatalf("got %s", got) } } func TestOverlapsEitherWay(t *testing.T) { a := netip.MustParsePrefix("172.16.5.0/24") if !overlaps(a, netip.MustParsePrefix("172.16.0.0/12")) || !overlaps(netip.MustParsePrefix("172.16.0.0/12"), a) { t.Fatal("a /24 inside a /12 overlaps both ways") } if overlaps(a, netip.MustParsePrefix("172.16.6.0/24")) { t.Fatal("neighbours do not overlap") } }