headscale/utils_test.go

164 lines
4.0 KiB
Go
Raw Permalink Normal View History

package headscale
import (
"gopkg.in/check.v1"
"inet.af/netaddr"
)
func (s *Suite) TestGetAvailableIp(c *check.C) {
2021-11-15 16:16:04 +00:00
ip, err := app.getAvailableIP()
c.Assert(err, check.IsNil)
expected := netaddr.MustParseIP("10.27.0.1")
c.Assert(ip.String(), check.Equals, expected.String())
}
func (s *Suite) TestGetUsedIps(c *check.C) {
2021-11-15 16:16:04 +00:00
ip, err := app.getAvailableIP()
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
namespace, err := app.CreateNamespace("test_ip")
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
_, err = app.GetMachine("test", "testmachine")
c.Assert(err, check.NotNil)
2021-11-15 16:16:04 +00:00
machine := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Name: "testmachine",
2021-11-15 16:16:04 +00:00
NamespaceID: namespace.ID,
Registered: true,
2021-11-18 08:49:55 +00:00
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
IPAddress: ip.String(),
}
2021-11-15 16:16:04 +00:00
app.db.Save(&machine)
2021-11-15 16:16:04 +00:00
ips, err := app.getUsedIPs()
c.Assert(err, check.IsNil)
expected := netaddr.MustParseIP("10.27.0.1")
c.Assert(ips[0], check.Equals, expected)
2021-11-15 16:16:04 +00:00
machine1, err := app.GetMachineByID(0)
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
c.Assert(machine1.IPAddress, check.Equals, expected.String())
}
func (s *Suite) TestGetMultiIp(c *check.C) {
2021-11-15 16:16:04 +00:00
namespace, err := app.CreateNamespace("test-ip-multi")
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
for index := 1; index <= 350; index++ {
ip, err := app.getAvailableIP()
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
_, err = app.GetMachine("test", "testmachine")
c.Assert(err, check.NotNil)
2021-11-15 16:16:04 +00:00
machine := Machine{
ID: uint64(index),
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Name: "testmachine",
2021-11-15 16:16:04 +00:00
NamespaceID: namespace.ID,
Registered: true,
2021-11-18 08:49:55 +00:00
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
IPAddress: ip.String(),
}
2021-11-15 16:16:04 +00:00
app.db.Save(&machine)
}
2021-11-15 16:16:04 +00:00
ips, err := app.getUsedIPs()
c.Assert(err, check.IsNil)
c.Assert(len(ips), check.Equals, 350)
c.Assert(ips[0], check.Equals, netaddr.MustParseIP("10.27.0.1"))
c.Assert(ips[9], check.Equals, netaddr.MustParseIP("10.27.0.10"))
c.Assert(ips[300], check.Equals, netaddr.MustParseIP("10.27.1.47"))
// Check that we can read back the IPs
2021-11-15 16:16:04 +00:00
machine1, err := app.GetMachineByID(1)
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
c.Assert(
machine1.IPAddress,
check.Equals,
netaddr.MustParseIP("10.27.0.1").String(),
)
2021-11-15 16:16:04 +00:00
machine50, err := app.GetMachineByID(50)
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
c.Assert(
machine50.IPAddress,
check.Equals,
netaddr.MustParseIP("10.27.0.50").String(),
)
expectedNextIP := netaddr.MustParseIP("10.27.1.97")
2021-11-15 16:16:04 +00:00
nextIP, err := app.getAvailableIP()
c.Assert(err, check.IsNil)
c.Assert(nextIP.String(), check.Equals, expectedNextIP.String())
// If we call get Available again, we should receive
// the same IP, as it has not been reserved.
2021-11-15 16:16:04 +00:00
nextIP2, err := app.getAvailableIP()
c.Assert(err, check.IsNil)
c.Assert(nextIP2.String(), check.Equals, expectedNextIP.String())
}
func (s *Suite) TestGetAvailableIpMachineWithoutIP(c *check.C) {
2021-11-15 16:16:04 +00:00
ip, err := app.getAvailableIP()
c.Assert(err, check.IsNil)
expected := netaddr.MustParseIP("10.27.0.1")
c.Assert(ip.String(), check.Equals, expected.String())
2021-11-15 16:16:04 +00:00
namespace, err := app.CreateNamespace("test_ip")
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
_, err = app.GetMachine("test", "testmachine")
c.Assert(err, check.NotNil)
2021-11-15 16:16:04 +00:00
machine := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Name: "testmachine",
2021-11-15 16:16:04 +00:00
NamespaceID: namespace.ID,
Registered: true,
2021-11-18 08:49:55 +00:00
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
}
2021-11-15 16:16:04 +00:00
app.db.Save(&machine)
2021-11-15 16:16:04 +00:00
ip2, err := app.getAvailableIP()
c.Assert(err, check.IsNil)
c.Assert(ip2.String(), check.Equals, expected.String())
}