2021-08-02 16:57:45 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gopkg.in/check.v1"
|
|
|
|
"inet.af/netaddr"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestGetAvailableIp(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
ip, err := app.getAvailableIP()
|
2021-08-02 16:57:45 -04:00
|
|
|
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-08-03 05:06:42 -04:00
|
|
|
expected := netaddr.MustParseIP("10.27.0.1")
|
2021-08-02 16:57:45 -04:00
|
|
|
|
|
|
|
c.Assert(ip.String(), check.Equals, expected.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetUsedIps(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
ip, err := app.getAvailableIP()
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
namespace, err := app.CreateNamespace("test_ip")
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetMachine("test", "testmachine")
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machine := Machine{
|
2021-08-02 16:57:45 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
2021-11-15 11:16:04 -05:00
|
|
|
NamespaceID: namespace.ID,
|
2021-08-02 16:57:45 -04:00
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
IPAddress: ip.String(),
|
|
|
|
}
|
2021-11-15 11:16:04 -05:00
|
|
|
app.db.Save(&machine)
|
2021-08-02 16:57:45 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
ips, err := app.getUsedIPs()
|
2021-08-02 16:57:45 -04:00
|
|
|
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-08-03 05:06:42 -04:00
|
|
|
expected := netaddr.MustParseIP("10.27.0.1")
|
2021-08-02 16:57:45 -04:00
|
|
|
|
|
|
|
c.Assert(ips[0], check.Equals, expected)
|
2021-08-02 17:39:18 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machine1, err := app.GetMachineByID(0)
|
2021-08-02 17:39:18 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(machine1.IPAddress, check.Equals, expected.String())
|
2021-08-02 16:57:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetMultiIp(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
namespace, err := app.CreateNamespace("test-ip-multi")
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
for index := 1; index <= 350; index++ {
|
|
|
|
ip, err := app.getAvailableIP()
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetMachine("test", "testmachine")
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machine := Machine{
|
|
|
|
ID: uint64(index),
|
2021-08-02 16:57:45 -04:00
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
2021-11-15 11:16:04 -05:00
|
|
|
NamespaceID: namespace.ID,
|
2021-08-02 16:57:45 -04:00
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
IPAddress: ip.String(),
|
|
|
|
}
|
2021-11-15 11:16:04 -05:00
|
|
|
app.db.Save(&machine)
|
2021-08-02 16:57:45 -04:00
|
|
|
}
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
ips, err := app.getUsedIPs()
|
2021-08-02 16:57:45 -04:00
|
|
|
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
c.Assert(len(ips), check.Equals, 350)
|
|
|
|
|
2021-08-03 05:06:42 -04:00
|
|
|
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"))
|
2021-08-02 16:57:45 -04:00
|
|
|
|
2021-08-02 17:39:18 -04:00
|
|
|
// Check that we can read back the IPs
|
2021-11-15 11:16:04 -05:00
|
|
|
machine1, err := app.GetMachineByID(1)
|
2021-08-02 17:39:18 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(
|
|
|
|
machine1.IPAddress,
|
|
|
|
check.Equals,
|
|
|
|
netaddr.MustParseIP("10.27.0.1").String(),
|
|
|
|
)
|
2021-08-02 17:39:18 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machine50, err := app.GetMachineByID(50)
|
2021-08-02 17:39:18 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(
|
|
|
|
machine50.IPAddress,
|
|
|
|
check.Equals,
|
|
|
|
netaddr.MustParseIP("10.27.0.50").String(),
|
|
|
|
)
|
2021-08-02 17:39:18 -04:00
|
|
|
|
2021-08-03 05:06:42 -04:00
|
|
|
expectedNextIP := netaddr.MustParseIP("10.27.1.97")
|
2021-11-15 11:16:04 -05:00
|
|
|
nextIP, err := app.getAvailableIP()
|
2021-08-02 16:57:45 -04:00
|
|
|
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 11:16:04 -05:00
|
|
|
nextIP2, err := app.getAvailableIP()
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
c.Assert(nextIP2.String(), check.Equals, expectedNextIP.String())
|
|
|
|
}
|
2021-08-03 04:26:28 -04:00
|
|
|
|
|
|
|
func (s *Suite) TestGetAvailableIpMachineWithoutIP(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
ip, err := app.getAvailableIP()
|
2021-08-03 04:26:28 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-08-03 05:06:42 -04:00
|
|
|
expected := netaddr.MustParseIP("10.27.0.1")
|
2021-08-03 04:26:28 -04:00
|
|
|
|
|
|
|
c.Assert(ip.String(), check.Equals, expected.String())
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
namespace, err := app.CreateNamespace("test_ip")
|
2021-08-03 04:26:28 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-08-03 04:26:28 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetMachine("test", "testmachine")
|
2021-08-03 04:26:28 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machine := Machine{
|
2021-08-03 04:26:28 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
2021-11-15 11:16:04 -05:00
|
|
|
NamespaceID: namespace.ID,
|
2021-08-03 04:26:28 -04:00
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 11:16:04 -05:00
|
|
|
app.db.Save(&machine)
|
2021-08-03 04:26:28 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
ip2, err := app.getAvailableIP()
|
2021-08-03 04:26:28 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
c.Assert(ip2.String(), check.Equals, expected.String())
|
|
|
|
}
|