@@ -10,6 +10,7 @@ import (
10
10
"sync"
11
11
"testing"
12
12
13
+ "github.com/stretchr/testify/assert"
13
14
"github.com/stretchr/testify/require"
14
15
"golang.org/x/sync/errgroup"
15
16
)
@@ -1644,3 +1645,83 @@ func TestNamespaceFromContext(t *testing.T) {
1644
1645
})
1645
1646
}
1646
1647
}
1648
+ func TestAvailablePrefixes (t * testing.T ) {
1649
+ testCases := []struct {
1650
+ name string
1651
+ cidr string
1652
+ expectedTotal uint64
1653
+ expectedAvailablePfx []string
1654
+ }{
1655
+ {
1656
+ name : "192.168.0.0/32" ,
1657
+ cidr : "192.168.0.0/32" ,
1658
+ expectedTotal : 0 ,
1659
+ expectedAvailablePfx : []string {},
1660
+ },
1661
+ {
1662
+ name : "192.168.0.0/31" ,
1663
+ cidr : "192.168.0.0/31" ,
1664
+ expectedTotal : 0 ,
1665
+ expectedAvailablePfx : []string {},
1666
+ },
1667
+ {
1668
+ name : "192.168.0.0/30" ,
1669
+ cidr : "192.168.0.0/30" ,
1670
+ expectedTotal : 1 ,
1671
+ expectedAvailablePfx : []string {"192.168.0.0/30" },
1672
+ },
1673
+ {
1674
+ name : "192.168.0.0/24" ,
1675
+ cidr : "192.168.0.0/24" ,
1676
+ expectedTotal : 64 ,
1677
+ expectedAvailablePfx : []string {"192.168.0.0/24" },
1678
+ },
1679
+ {
1680
+ name : "2001:0db8:85a3::/128" ,
1681
+ cidr : "2001:0db8:85a3::/128" ,
1682
+ expectedTotal : 0 ,
1683
+ expectedAvailablePfx : []string {},
1684
+ },
1685
+ {
1686
+ name : "2001:0db8:85a3::/127" ,
1687
+ cidr : "2001:0db8:85a3::/127" ,
1688
+ expectedTotal : 0 ,
1689
+ expectedAvailablePfx : []string {},
1690
+ },
1691
+ {
1692
+ name : "2001:0db8:85a3::/126" ,
1693
+ cidr : "2001:0db8:85a3::/126" ,
1694
+ expectedTotal : 1 ,
1695
+ expectedAvailablePfx : []string {"2001:db8:85a3::/126" },
1696
+ },
1697
+ {
1698
+ name : "Invalid CIDR" ,
1699
+ cidr : "Invalid CIDR" ,
1700
+ expectedTotal : 0 ,
1701
+ expectedAvailablePfx : []string {},
1702
+ },
1703
+ }
1704
+
1705
+ for _ , tc := range testCases {
1706
+ t .Run (tc .name , func (t * testing.T ) {
1707
+ prefix := & Prefix {
1708
+ Cidr : tc .cidr ,
1709
+ isParent : false ,
1710
+ availableChildPrefixes : make (map [string ]bool ),
1711
+ }
1712
+
1713
+ totalAvailable , availablePrefixes := prefix .availablePrefixes ()
1714
+
1715
+ assert .Equal (
1716
+ t , tc .expectedTotal , totalAvailable ,
1717
+ "Expected totalAvailable: %d, got: %d" ,
1718
+ tc .expectedTotal , totalAvailable ,
1719
+ )
1720
+ assert .ElementsMatchf (
1721
+ t , availablePrefixes , tc .expectedAvailablePfx ,
1722
+ "Expected availablePrefixes: %v, got: %v" ,
1723
+ tc .expectedAvailablePfx , availablePrefixes ,
1724
+ )
1725
+ })
1726
+ }
1727
+ }
0 commit comments