headscale/hscontrol/db/addresses_test.go

198 lines
5.2 KiB
Go
Raw Normal View History

package db
import (
2022-09-01 16:05:18 -06:00
"net/netip"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
2022-09-01 16:05:18 -06:00
"go4.org/netipx"
"gopkg.in/check.v1"
)
func (s *Suite) TestGetAvailableIp(c *check.C) {
ips, err := db.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-09-01 16:05:18 -06:00
expected := netip.MustParseAddr("10.27.0.1")
2022-01-16 06:16:59 -07:00
c.Assert(len(ips), check.Equals, 1)
c.Assert(ips[0].String(), check.Equals, expected.String())
}
func (s *Suite) TestGetUsedIps(c *check.C) {
ips, err := db.getAvailableIPs()
c.Assert(err, check.IsNil)
user, err := db.CreateUser("test-ip")
c.Assert(err, check.IsNil)
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
_, err = db.GetMachine("test", "testmachine")
c.Assert(err, check.NotNil)
machine := types.Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "testmachine",
UserID: user.ID,
RegisterMethod: util.RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
2022-01-16 06:16:59 -07:00
IPAddresses: ips,
}
db.db.Save(&machine)
usedIps, err := db.getUsedIPs()
c.Assert(err, check.IsNil)
2022-09-01 16:05:18 -06:00
expected := netip.MustParseAddr("10.27.0.1")
expectedIPSetBuilder := netipx.IPSetBuilder{}
expectedIPSetBuilder.Add(expected)
expectedIPSet, _ := expectedIPSetBuilder.IPSet()
c.Assert(usedIps.Equal(expectedIPSet), check.Equals, true)
c.Assert(usedIps.Contains(expected), check.Equals, true)
machine1, err := db.GetMachineByID(0)
c.Assert(err, check.IsNil)
2022-01-16 06:16:59 -07:00
c.Assert(len(machine1.IPAddresses), check.Equals, 1)
c.Assert(machine1.IPAddresses[0], check.Equals, expected)
c.Assert(channelUpdates, check.Equals, int32(0))
}
func (s *Suite) TestGetMultiIp(c *check.C) {
user, err := db.CreateUser("test-ip-multi")
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
for index := 1; index <= 350; index++ {
db.ipAllocationMutex.Lock()
ips, err := db.getAvailableIPs()
c.Assert(err, check.IsNil)
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
_, err = db.GetMachine("test", "testmachine")
c.Assert(err, check.NotNil)
machine := types.Machine{
2021-11-15 09:16:04 -07:00
ID: uint64(index),
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "testmachine",
UserID: user.ID,
RegisterMethod: util.RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
2022-01-16 06:16:59 -07:00
IPAddresses: ips,
}
db.db.Save(&machine)
db.ipAllocationMutex.Unlock()
}
usedIps, err := db.getUsedIPs()
c.Assert(err, check.IsNil)
2022-09-01 16:05:18 -06:00
expected0 := netip.MustParseAddr("10.27.0.1")
expected9 := netip.MustParseAddr("10.27.0.10")
expected300 := netip.MustParseAddr("10.27.0.45")
2022-09-01 16:05:18 -06:00
notExpectedIPSetBuilder := netipx.IPSetBuilder{}
notExpectedIPSetBuilder.Add(expected0)
notExpectedIPSetBuilder.Add(expected9)
notExpectedIPSetBuilder.Add(expected300)
notExpectedIPSet, err := notExpectedIPSetBuilder.IPSet()
c.Assert(err, check.IsNil)
// We actually expect it to be a lot larger
c.Assert(usedIps.Equal(notExpectedIPSet), check.Equals, false)
c.Assert(usedIps.Contains(expected0), check.Equals, true)
c.Assert(usedIps.Contains(expected9), check.Equals, true)
c.Assert(usedIps.Contains(expected300), check.Equals, true)
// Check that we can read back the IPs
machine1, err := db.GetMachineByID(1)
c.Assert(err, check.IsNil)
2022-01-16 06:16:59 -07:00
c.Assert(len(machine1.IPAddresses), check.Equals, 1)
2021-11-15 09:16:04 -07:00
c.Assert(
2022-01-16 06:16:59 -07:00
machine1.IPAddresses[0],
2021-11-15 09:16:04 -07:00
check.Equals,
2022-09-01 16:05:18 -06:00
netip.MustParseAddr("10.27.0.1"),
2021-11-15 09:16:04 -07:00
)
machine50, err := db.GetMachineByID(50)
c.Assert(err, check.IsNil)
2022-01-16 06:16:59 -07:00
c.Assert(len(machine50.IPAddresses), check.Equals, 1)
2021-11-15 09:16:04 -07:00
c.Assert(
2022-01-16 06:16:59 -07:00
machine50.IPAddresses[0],
2021-11-15 09:16:04 -07:00
check.Equals,
2022-09-01 16:05:18 -06:00
netip.MustParseAddr("10.27.0.50"),
2021-11-15 09:16:04 -07:00
)
2022-09-01 16:05:18 -06:00
expectedNextIP := netip.MustParseAddr("10.27.1.95")
nextIP, err := db.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-01-16 06:16:59 -07:00
c.Assert(len(nextIP), check.Equals, 1)
c.Assert(nextIP[0].String(), check.Equals, expectedNextIP.String())
// If we call get Available again, we should receive
// the same IP, as it has not been reserved.
nextIP2, err := db.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-01-16 06:16:59 -07:00
c.Assert(len(nextIP2), check.Equals, 1)
c.Assert(nextIP2[0].String(), check.Equals, expectedNextIP.String())
c.Assert(channelUpdates, check.Equals, int32(0))
}
func (s *Suite) TestGetAvailableIpMachineWithoutIP(c *check.C) {
ips, err := db.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-09-03 15:46:14 -06:00
expected := netip.MustParseAddr("10.27.0.1")
2022-01-16 06:16:59 -07:00
c.Assert(len(ips), check.Equals, 1)
c.Assert(ips[0].String(), check.Equals, expected.String())
user, err := db.CreateUser("test-ip")
c.Assert(err, check.IsNil)
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
_, err = db.GetMachine("test", "testmachine")
c.Assert(err, check.NotNil)
machine := types.Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "testmachine",
UserID: user.ID,
RegisterMethod: util.RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
}
db.db.Save(&machine)
ips2, err := db.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-01-16 06:16:59 -07:00
c.Assert(len(ips2), check.Equals, 1)
c.Assert(ips2[0].String(), check.Equals, expected.String())
c.Assert(channelUpdates, check.Equals, int32(0))
}