1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00
juanfont.headscale/utils_test.go

202 lines
5.2 KiB
Go
Raw Normal View History

package headscale
import (
2022-09-02 00:05:18 +02:00
"net/netip"
"go4.org/netipx"
"gopkg.in/check.v1"
)
func (s *Suite) TestGetAvailableIp(c *check.C) {
2022-01-16 14:16:59 +01:00
ips, err := app.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-09-02 00:05:18 +02:00
expected := netip.MustParseAddr("10.27.0.1")
2022-01-16 14:16:59 +01:00
c.Assert(len(ips), check.Equals, 1)
c.Assert(ips[0].String(), check.Equals, expected.String())
}
func (s *Suite) TestGetUsedIps(c *check.C) {
2022-01-16 14:16:59 +01:00
ips, err := app.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-02-23 11:38:20 +01:00
namespace, err := app.CreateNamespace("test-ip")
c.Assert(err, check.IsNil)
2022-08-25 12:12:41 +02:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine("test", "testmachine")
c.Assert(err, check.NotNil)
2021-11-15 17:16:04 +01:00
machine := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "testmachine",
2021-11-15 17:16:04 +01:00
NamespaceID: namespace.ID,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
2022-01-16 14:16:59 +01:00
IPAddresses: ips,
}
2021-11-15 17:16:04 +01:00
app.db.Save(&machine)
2022-01-16 14:16:59 +01:00
usedIps, err := app.getUsedIPs()
c.Assert(err, check.IsNil)
2022-09-02 00:05:18 +02: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)
2021-11-15 17:16:04 +01:00
machine1, err := app.GetMachineByID(0)
c.Assert(err, check.IsNil)
2022-01-16 14:16:59 +01:00
c.Assert(len(machine1.IPAddresses), check.Equals, 1)
c.Assert(machine1.IPAddresses[0], check.Equals, expected)
}
func (s *Suite) TestGetMultiIp(c *check.C) {
2021-11-15 17:16:04 +01:00
namespace, err := app.CreateNamespace("test-ip-multi")
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
for index := 1; index <= 350; index++ {
app.ipAllocationMutex.Lock()
2022-01-16 14:16:59 +01:00
ips, err := app.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-08-25 12:12:41 +02:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine("test", "testmachine")
c.Assert(err, check.NotNil)
2021-11-15 17:16:04 +01:00
machine := Machine{
ID: uint64(index),
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "testmachine",
2021-11-15 17:16:04 +01:00
NamespaceID: namespace.ID,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
2022-01-16 14:16:59 +01:00
IPAddresses: ips,
}
2021-11-15 17:16:04 +01:00
app.db.Save(&machine)
app.ipAllocationMutex.Unlock()
}
2022-01-16 14:16:59 +01:00
usedIps, err := app.getUsedIPs()
c.Assert(err, check.IsNil)
2022-09-02 00:05:18 +02:00
expected0 := netip.MustParseAddr("10.27.0.1")
expected9 := netip.MustParseAddr("10.27.0.10")
expected300 := netip.MustParseAddr("10.27.0.45")
2022-09-02 00:05:18 +02: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
2021-11-15 17:16:04 +01:00
machine1, err := app.GetMachineByID(1)
c.Assert(err, check.IsNil)
2022-01-16 14:16:59 +01:00
c.Assert(len(machine1.IPAddresses), check.Equals, 1)
2021-11-15 17:16:04 +01:00
c.Assert(
2022-01-16 14:16:59 +01:00
machine1.IPAddresses[0],
2021-11-15 17:16:04 +01:00
check.Equals,
2022-09-02 00:05:18 +02:00
netip.MustParseAddr("10.27.0.1"),
2021-11-15 17:16:04 +01:00
)
2021-11-15 17:16:04 +01:00
machine50, err := app.GetMachineByID(50)
c.Assert(err, check.IsNil)
2022-01-16 14:16:59 +01:00
c.Assert(len(machine50.IPAddresses), check.Equals, 1)
2021-11-15 17:16:04 +01:00
c.Assert(
2022-01-16 14:16:59 +01:00
machine50.IPAddresses[0],
2021-11-15 17:16:04 +01:00
check.Equals,
2022-09-02 00:05:18 +02:00
netip.MustParseAddr("10.27.0.50"),
2021-11-15 17:16:04 +01:00
)
2022-09-02 00:05:18 +02:00
expectedNextIP := netip.MustParseAddr("10.27.1.95")
2022-01-16 14:16:59 +01:00
nextIP, err := app.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-01-16 14:16:59 +01: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.
2022-01-16 14:16:59 +01:00
nextIP2, err := app.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-01-16 14:16:59 +01:00
c.Assert(len(nextIP2), check.Equals, 1)
c.Assert(nextIP2[0].String(), check.Equals, expectedNextIP.String())
}
func (s *Suite) TestGetAvailableIpMachineWithoutIP(c *check.C) {
2022-01-16 14:16:59 +01:00
ips, err := app.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-09-03 23:46:14 +02:00
expected := netip.MustParseAddr("10.27.0.1")
2022-01-16 14:16:59 +01:00
c.Assert(len(ips), check.Equals, 1)
c.Assert(ips[0].String(), check.Equals, expected.String())
2022-02-23 11:38:20 +01:00
namespace, err := app.CreateNamespace("test-ip")
c.Assert(err, check.IsNil)
2022-08-25 12:12:41 +02:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine("test", "testmachine")
c.Assert(err, check.NotNil)
2021-11-15 17:16:04 +01:00
machine := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "testmachine",
2021-11-15 17:16:04 +01:00
NamespaceID: namespace.ID,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(&machine)
2022-01-16 14:16:59 +01:00
ips2, err := app.getAvailableIPs()
c.Assert(err, check.IsNil)
2022-01-16 14:16:59 +01:00
c.Assert(len(ips2), check.Equals, 1)
c.Assert(ips2[0].String(), check.Equals, expected.String())
}
func (s *Suite) TestGenerateRandomStringDNSSafe(c *check.C) {
for i := 0; i < 100000; i++ {
str, err := GenerateRandomStringDNSSafe(8)
if err != nil {
c.Error(err)
}
if len(str) != 8 {
c.Error("invalid length", len(str), str)
}
}
}