Skip to content

Commit

Permalink
add subnet ut
Browse files Browse the repository at this point in the history
Signed-off-by: bobz965 <[email protected]>
  • Loading branch information
bobz965 committed Aug 29, 2024
1 parent 1343d0d commit 6c90013
Showing 1 changed file with 157 additions and 0 deletions.
157 changes: 157 additions & 0 deletions pkg/ipam/subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,3 +806,160 @@ func TestAddOrUpdateIPPool(t *testing.T) {
require.Equal(t, defaultPool.V4Using.String(), "")
require.Equal(t, defaultPool.V6Using.String(), "")
}

func TestRemoveIPPool(t *testing.T) {
excludeIps := []string{
"10.0.0.2", "10.0.0.4", "10.0.0.100",
"10.0.0.252", "10.0.0.253", "10.0.0.254",
"2001:db8::2", "2001:db8::4", "2001:db8::100",
"2001:db8::252", "2001:db8::253", "2001:db8::254",
}
subnetName := "dualSubnet"
subnet, err := NewSubnet(subnetName, "10.0.0.0/16,2001:db8::/64", excludeIps)
require.NoError(t, err)
// check default pool
defaultPool := subnet.IPPools[""]
require.NotNil(t, defaultPool)
require.NotNil(t, defaultPool.V4IPs)
require.NotNil(t, defaultPool.V6IPs)
require.NotNil(t, defaultPool.V4Free)
require.NotNil(t, defaultPool.V6Free)
require.NotNil(t, defaultPool.V4Available)
require.NotNil(t, defaultPool.V6Available)
require.NotNil(t, defaultPool.V4Reserved)
require.NotNil(t, defaultPool.V6Reserved)
require.NotNil(t, defaultPool.V4IPs)
require.NotNil(t, defaultPool.V4IPs)
require.NotNil(t, defaultPool.V4Released)
require.NotNil(t, defaultPool.V6Released)
require.NotNil(t, defaultPool.V4Using)
require.NotNil(t, defaultPool.V6Using)
require.Equal(t, defaultPool.V4IPs.String(), "10.0.0.1-10.0.255.254")
require.Equal(t, defaultPool.V6IPs.String(), "2001:db8::1-2001:db8::ffff:ffff:ffff:fffe")
require.Equal(t, defaultPool.V4Free.String(), "10.0.0.1,10.0.0.3,10.0.0.5-10.0.0.99,10.0.0.101-10.0.0.251,10.0.0.255-10.0.255.254")
require.Equal(t, defaultPool.V6Free.String(), "2001:db8::1,2001:db8::3,2001:db8::5-2001:db8::ff,2001:db8::101-2001:db8::251,2001:db8::255-2001:db8::ffff:ffff:ffff:fffe")
require.Equal(t, defaultPool.V4Available.String(), "10.0.0.1,10.0.0.3,10.0.0.5-10.0.0.99,10.0.0.101-10.0.0.251,10.0.0.255-10.0.255.254")
require.Equal(t, defaultPool.V6Available.String(), "2001:db8::1,2001:db8::3,2001:db8::5-2001:db8::ff,2001:db8::101-2001:db8::251,2001:db8::255-2001:db8::ffff:ffff:ffff:fffe")
require.Equal(t, defaultPool.V4Reserved.String(), "10.0.0.2,10.0.0.4,10.0.0.100,10.0.0.252-10.0.0.254")
require.Equal(t, defaultPool.V6Reserved.String(), "2001:db8::2,2001:db8::4,2001:db8::100,2001:db8::252-2001:db8::254")
require.Equal(t, defaultPool.V4Released.String(), "")
require.Equal(t, defaultPool.V6Released.String(), "")
require.Equal(t, defaultPool.V4Using.String(), "")
require.Equal(t, defaultPool.V6Using.String(), "")
// check dualstack valid pool
dualValidPoolName := "dualValidPool"
validDualIPs := []string{"10.0.0.30", "10.0.0.80", "2001:db8::30", "2001:db8::80"}
err = subnet.AddOrUpdateIPPool(dualValidPoolName, validDualIPs)
require.NoError(t, err)
require.NotNil(t, subnet.IPPools[dualValidPoolName])
_, ok := subnet.IPPools[dualValidPoolName]
require.True(t, ok)
require.Equal(t, 2, len(subnet.IPPools))
// remove dualValidPool
subnet.RemoveIPPool(dualValidPoolName)
require.Nil(t, subnet.IPPools[dualValidPoolName])
require.Equal(t, 1, len(subnet.IPPools))
// recheck default pool
defaultPool = subnet.IPPools[""]
require.NotNil(t, defaultPool)
require.NotNil(t, defaultPool.V4IPs)
require.NotNil(t, defaultPool.V6IPs)
require.NotNil(t, defaultPool.V4Free)
require.NotNil(t, defaultPool.V6Free)
require.NotNil(t, defaultPool.V4Available)
require.NotNil(t, defaultPool.V6Available)
require.NotNil(t, defaultPool.V4Reserved)
require.NotNil(t, defaultPool.V6Reserved)
require.NotNil(t, defaultPool.V4IPs)
require.NotNil(t, defaultPool.V4IPs)
require.NotNil(t, defaultPool.V4Released)
require.NotNil(t, defaultPool.V6Released)
require.NotNil(t, defaultPool.V4Using)
require.NotNil(t, defaultPool.V6Using)
require.Equal(t, defaultPool.V4IPs.String(), "10.0.0.1-10.0.0.29,10.0.0.31-10.0.0.79,10.0.0.81-10.0.255.254")
require.Equal(t, defaultPool.V6IPs.String(), "2001:db8::1-2001:db8::2f,2001:db8::31-2001:db8::7f,2001:db8::81-2001:db8::ffff:ffff:ffff:fffe")
require.Equal(t, defaultPool.V4Free.String(), "10.0.0.1,10.0.0.3,10.0.0.5-10.0.0.99,10.0.0.101-10.0.0.251,10.0.0.255-10.0.255.254")
require.Equal(t, defaultPool.V6Free.String(), "2001:db8::1,2001:db8::3,2001:db8::5-2001:db8::ff,2001:db8::101-2001:db8::251,2001:db8::255-2001:db8::ffff:ffff:ffff:fffe")
require.Equal(t, defaultPool.V4Available.String(), "10.0.0.1,10.0.0.3,10.0.0.5-10.0.0.99,10.0.0.101-10.0.0.251,10.0.0.255-10.0.255.254")
require.Equal(t, defaultPool.V6Available.String(), "2001:db8::1,2001:db8::3,2001:db8::5-2001:db8::ff,2001:db8::101-2001:db8::251,2001:db8::255-2001:db8::ffff:ffff:ffff:fffe")
require.Equal(t, defaultPool.V4Reserved.String(), "10.0.0.2,10.0.0.4,10.0.0.100,10.0.0.252-10.0.0.254")
require.Equal(t, defaultPool.V6Reserved.String(), "2001:db8::2,2001:db8::4,2001:db8::100,2001:db8::252-2001:db8::254")
require.Equal(t, defaultPool.V4Released.String(), "")
require.Equal(t, defaultPool.V6Released.String(), "")
require.Equal(t, defaultPool.V4Using.String(), "")
require.Equal(t, defaultPool.V6Using.String(), "")

// remove dualValidPool
subnet.RemoveIPPool(dualValidPoolName)
require.Nil(t, subnet.IPPools[dualValidPoolName])
require.Equal(t, 1, len(subnet.IPPools))
}

func TestIPPoolStatistics(t *testing.T) {
excludeIps := []string{
"10.0.0.2", "10.0.0.4", "10.0.0.100",
"10.0.0.252", "10.0.0.253", "10.0.0.254",
"2001:db8::2", "2001:db8::4", "2001:db8::100",
"2001:db8::252", "2001:db8::253", "2001:db8::254",
}
subnetName := "dualSubnet"
subnet, err := NewSubnet(subnetName, "10.0.0.0/16,2001:db8::/64", excludeIps)
require.NoError(t, err)

// check V4 valid pool
v4ValidPoolName := "v4ValidPool"
validV4IPs := []string{"10.0.0.20", "10.0.0.90", "10.0.0.170", "10.0.0.240", "10.0.0.250"}
err = subnet.AddOrUpdateIPPool(v4ValidPoolName, validV4IPs)
require.NoError(t, err)
v4a, v4u, v6a, v6u, v4as, v4us, v6as, v6us := subnet.IPPoolStatistics(v4ValidPoolName)
require.Equal(t, v4a.String(), "5")
require.Empty(t, v4u)
require.Empty(t, v6a)
require.Empty(t, v6u)
require.Equal(t, v4as, "10.0.0.20,10.0.0.90,10.0.0.170,10.0.0.240,10.0.0.250")
require.Empty(t, v4us)
require.Empty(t, v6as)
require.Empty(t, v6us)

// check V6 valid pool
v6ValidPoolName := "v6ValidPool"
validV6IPs := []string{"2001:db8::20", "2001:db8::90", "2001:db8::170", "2001:db8::240", "2001:db8::250"}
err = subnet.AddOrUpdateIPPool(v6ValidPoolName, validV6IPs)
require.NoError(t, err)
v4a, v4u, v6a, v6u, v4as, v4us, v6as, v6us = subnet.IPPoolStatistics(v6ValidPoolName)
require.Empty(t, v4a)
require.Empty(t, v4u)
require.Equal(t, v6a.String(), "5")
require.Empty(t, v6u)
require.Empty(t, v4as)
require.Empty(t, v4us)
require.Equal(t, v6as, "2001:db8::20,2001:db8::90,2001:db8::170,2001:db8::240,2001:db8::250")
require.Empty(t, v6us)

// check dualstack valid pool
dualValidPoolName := "dualValidPool"
validDualIPs := []string{"10.0.0.30", "10.0.0.80", "2001:db8::30", "2001:db8::80"}
err = subnet.AddOrUpdateIPPool(dualValidPoolName, validDualIPs)
require.NoError(t, err)
v4a, v4u, v6a, v6u, v4as, v4us, v6as, v6us = subnet.IPPoolStatistics(dualValidPoolName)
require.Equal(t, v4a.String(), "2")
require.Empty(t, v4u)
require.Equal(t, v6a.String(), "2")
require.Empty(t, v6u)
require.Equal(t, v4as, "10.0.0.30,10.0.0.80")
require.Empty(t, v4us)
require.Equal(t, v6as, "2001:db8::30,2001:db8::80")
require.Empty(t, v6us)

// check not exist pool
notExistPoolName := "notExistPool"
v4a, v4u, v6a, v6u, v4as, v4us, v6as, v6us = subnet.IPPoolStatistics(notExistPoolName)
require.Empty(t, v4a)
require.Empty(t, v4u)
require.Empty(t, v6a)
require.Empty(t, v6u)
require.Empty(t, v4as)
require.Empty(t, v4us)
require.Empty(t, v6as)
require.Empty(t, v6us)
}

0 comments on commit 6c90013

Please sign in to comment.