mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-29 18:33:05 +00:00
106 lines
2.4 KiB
Go
106 lines
2.4 KiB
Go
|
package headscale
|
||
|
|
||
|
import (
|
||
|
"gopkg.in/check.v1"
|
||
|
"inet.af/netaddr"
|
||
|
)
|
||
|
|
||
|
func (s *Suite) TestGetAvailableIp(c *check.C) {
|
||
|
ip, err := h.getAvailableIP()
|
||
|
|
||
|
c.Assert(err, check.IsNil)
|
||
|
|
||
|
expected := netaddr.MustParseIP("10.27.0.0")
|
||
|
|
||
|
c.Assert(ip.String(), check.Equals, expected.String())
|
||
|
}
|
||
|
|
||
|
func (s *Suite) TestGetUsedIps(c *check.C) {
|
||
|
ip, err := h.getAvailableIP()
|
||
|
c.Assert(err, check.IsNil)
|
||
|
|
||
|
n, err := h.CreateNamespace("test_ip")
|
||
|
c.Assert(err, check.IsNil)
|
||
|
|
||
|
pak, err := h.CreatePreAuthKey(n.Name, false, false, nil)
|
||
|
c.Assert(err, check.IsNil)
|
||
|
|
||
|
_, err = h.GetMachine("test", "testmachine")
|
||
|
c.Assert(err, check.NotNil)
|
||
|
|
||
|
m := Machine{
|
||
|
ID: 0,
|
||
|
MachineKey: "foo",
|
||
|
NodeKey: "bar",
|
||
|
DiscoKey: "faa",
|
||
|
Name: "testmachine",
|
||
|
NamespaceID: n.ID,
|
||
|
Registered: true,
|
||
|
RegisterMethod: "authKey",
|
||
|
AuthKeyID: uint(pak.ID),
|
||
|
IPAddress: ip.String(),
|
||
|
}
|
||
|
h.db.Save(&m)
|
||
|
|
||
|
ips, err := h.getUsedIPs()
|
||
|
|
||
|
c.Assert(err, check.IsNil)
|
||
|
|
||
|
expected := netaddr.MustParseIP("10.27.0.0")
|
||
|
|
||
|
c.Assert(ips[0], check.Equals, expected)
|
||
|
}
|
||
|
|
||
|
func (s *Suite) TestGetMultiIp(c *check.C) {
|
||
|
n, err := h.CreateNamespace("test-ip-multi")
|
||
|
c.Assert(err, check.IsNil)
|
||
|
|
||
|
for i := 1; i <= 350; i++ {
|
||
|
ip, err := h.getAvailableIP()
|
||
|
c.Assert(err, check.IsNil)
|
||
|
|
||
|
pak, err := h.CreatePreAuthKey(n.Name, false, false, nil)
|
||
|
c.Assert(err, check.IsNil)
|
||
|
|
||
|
_, err = h.GetMachine("test", "testmachine")
|
||
|
c.Assert(err, check.NotNil)
|
||
|
|
||
|
m := Machine{
|
||
|
ID: 0,
|
||
|
MachineKey: "foo",
|
||
|
NodeKey: "bar",
|
||
|
DiscoKey: "faa",
|
||
|
Name: "testmachine",
|
||
|
NamespaceID: n.ID,
|
||
|
Registered: true,
|
||
|
RegisterMethod: "authKey",
|
||
|
AuthKeyID: uint(pak.ID),
|
||
|
IPAddress: ip.String(),
|
||
|
}
|
||
|
h.db.Save(&m)
|
||
|
}
|
||
|
|
||
|
ips, err := h.getUsedIPs()
|
||
|
|
||
|
c.Assert(err, check.IsNil)
|
||
|
|
||
|
c.Assert(len(ips), check.Equals, 350)
|
||
|
|
||
|
c.Assert(ips[0], check.Equals, netaddr.MustParseIP("10.27.0.0"))
|
||
|
c.Assert(ips[9], check.Equals, netaddr.MustParseIP("10.27.0.9"))
|
||
|
c.Assert(ips[300], check.Equals, netaddr.MustParseIP("10.27.1.44"))
|
||
|
|
||
|
expectedNextIP := netaddr.MustParseIP("10.27.1.94")
|
||
|
nextIP, err := h.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.
|
||
|
nextIP2, err := h.getAvailableIP()
|
||
|
c.Assert(err, check.IsNil)
|
||
|
|
||
|
c.Assert(nextIP2.String(), check.Equals, expectedNextIP.String())
|
||
|
}
|