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