internal/agent/ipalloc/ipalloc_test.go
Ref: Size: 1.5 KiB History
package ipalloc
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func itoa(i int) string { return strconv.Itoa(i) }
func TestAllocSkipsGatewayAndUsed(t *testing.T) {
ip, err := Alloc("10.77.1.0/24", []string{"10.77.1.2", "10.77.1.3"})
require.NoError(t, err)
assert.Equal(t, "10.77.1.4", ip, ".1 is the bridge gateway; .2/.3 used")
}
func TestAllocFirstVM(t *testing.T) {
ip, err := Alloc("10.77.1.0/24", nil)
require.NoError(t, err)
assert.Equal(t, "10.77.1.2", ip)
}
func TestAllocReservesSubnetBroadcastForNonSlash24(t *testing.T) {
// /26 => hosts .0(network) .1(gw) ... .63(broadcast). The old hardcoded-.255
// broadcast would have wrongly handed out .63 and stopped short of .255.
used := make([]string, 0, 60)
for i := 2; i <= 62; i++ { // fill .2..*.62, leaving only .63 (broadcast)
used = append(used, "10.0.0."+itoa(i))
}
_, err := Alloc("10.0.0.0/26", used)
assert.Error(t, err, ".63 is the /26 broadcast and must not be allocated")
// With .62 free it is allocatable (it is a valid host, not the broadcast).
ip, err := Alloc("10.0.0.0/26", used[:len(used)-1])
require.NoError(t, err)
assert.Equal(t, "10.0.0.62", ip)
}
func TestAllocExhausted(t *testing.T) {
used := make([]string, 0, 253)
for i := 2; i <= 254; i++ {
used = append(used, "10.77.1."+itoa(i))
}
_, err := Alloc("10.77.1.0/24", used)
assert.Error(t, err, "pool exhausted (quarantined VMs hold IPs — spec sizing note)")
}