2021-05-12 08:55:36 +08:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
2021-10-05 01:40:21 +08:00
|
|
|
"strconv"
|
2021-11-21 21:40:19 +08:00
|
|
|
"time"
|
2021-07-25 23:59:48 +08:00
|
|
|
|
2021-05-12 08:55:36 +08:00
|
|
|
"gopkg.in/check.v1"
|
2022-01-16 21:16:59 +08:00
|
|
|
"inet.af/netaddr"
|
2021-05-12 08:55:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestGetMachine(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
_, err = app.GetMachine("test", "testmachine")
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
machine := &Machine{
|
2021-05-12 08:55:36 +08:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-05-12 08:55:36 +08:00
|
|
|
Registered: true,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-12 08:55:36 +08:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(machine)
|
2021-05-12 08:55:36 +08:00
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
machineFromDB, err := app.GetMachine("test", "testmachine")
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
_, err = machineFromDB.GetHostInfo()
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-07-17 06:14:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetMachineByID(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-05-12 08:55:36 +08:00
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
_, err = app.GetMachineByID(0)
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
machine := Machine{
|
2021-07-17 06:14:22 +08:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-07-17 06:14:22 +08:00
|
|
|
Registered: true,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-07-17 06:14:22 +08:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(&machine)
|
2021-07-17 06:14:22 +08:00
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
machineByID, err := app.GetMachineByID(0)
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
_, err = machineByID.GetHostInfo()
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestDeleteMachine(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
machine := Machine{
|
2021-07-17 06:14:22 +08:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-07-17 06:14:22 +08:00
|
|
|
Registered: true,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-07-17 06:14:22 +08:00
|
|
|
AuthKeyID: uint(1),
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
err = app.DeleteMachine(&machine)
|
2021-07-17 06:29:14 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
|
|
|
|
_, err = app.GetMachine(namespace.Name, "testmachine")
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestHardDeleteMachine(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
machine := Machine{
|
2021-07-17 06:14:22 +08:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine3",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-07-17 06:14:22 +08:00
|
|
|
Registered: true,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-07-17 06:14:22 +08:00
|
|
|
AuthKeyID: uint(1),
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
err = app.HardDeleteMachine(&machine)
|
2021-07-17 06:29:14 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
|
|
|
|
_, err = app.GetMachine(namespace.Name, "testmachine3")
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
2021-05-12 08:55:36 +08:00
|
|
|
}
|
2021-10-05 01:40:21 +08:00
|
|
|
|
|
|
|
func (s *Suite) TestGetDirectPeers(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-10-05 01:40:21 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-10-05 01:40:21 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
_, err = app.GetMachineByID(0)
|
2021-10-05 01:40:21 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
for index := 0; index <= 10; index++ {
|
|
|
|
machine := Machine{
|
|
|
|
ID: uint64(index),
|
|
|
|
MachineKey: "foo" + strconv.Itoa(index),
|
|
|
|
NodeKey: "bar" + strconv.Itoa(index),
|
|
|
|
DiscoKey: "faa" + strconv.Itoa(index),
|
|
|
|
Name: "testmachine" + strconv.Itoa(index),
|
|
|
|
NamespaceID: namespace.ID,
|
2021-10-05 01:40:21 +08:00
|
|
|
Registered: true,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-10-05 01:40:21 +08:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(&machine)
|
2021-10-05 01:40:21 +08:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
machine0ByID, err := app.GetMachineByID(0)
|
2021-10-05 01:40:21 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
_, err = machine0ByID.GetHostInfo()
|
2021-10-05 01:40:21 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
peersOfMachine0, err := app.getDirectPeers(machine0ByID)
|
2021-10-05 01:40:21 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
c.Assert(len(peersOfMachine0), check.Equals, 9)
|
|
|
|
c.Assert(peersOfMachine0[0].Name, check.Equals, "testmachine2")
|
|
|
|
c.Assert(peersOfMachine0[5].Name, check.Equals, "testmachine7")
|
|
|
|
c.Assert(peersOfMachine0[8].Name, check.Equals, "testmachine10")
|
2021-10-05 01:40:21 +08:00
|
|
|
}
|
2021-11-21 21:40:19 +08:00
|
|
|
|
|
|
|
func (s *Suite) TestExpireMachine(c *check.C) {
|
|
|
|
namespace, err := app.CreateNamespace("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = app.GetMachine("test", "testmachine")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
machine := &Machine{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
Expiry: &time.Time{},
|
|
|
|
}
|
|
|
|
app.db.Save(machine)
|
|
|
|
|
|
|
|
machineFromDB, err := app.GetMachine("test", "testmachine")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
c.Assert(machineFromDB.isExpired(), check.Equals, false)
|
|
|
|
|
|
|
|
app.ExpireMachine(machineFromDB)
|
|
|
|
|
|
|
|
c.Assert(machineFromDB.isExpired(), check.Equals, true)
|
|
|
|
}
|
2022-01-16 21:16:59 +08:00
|
|
|
|
|
|
|
func (s *Suite) TestSerdeAddressStrignSlice(c *check.C) {
|
|
|
|
input := MachineAddresses([]netaddr.IP{
|
|
|
|
netaddr.MustParseIP("192.0.2.1"),
|
|
|
|
netaddr.MustParseIP("2001:db8::1"),
|
|
|
|
})
|
|
|
|
serialized, err := input.Value()
|
|
|
|
c.Assert(err, check.IsNil)
|
2022-01-30 16:35:10 +08:00
|
|
|
if serial, ok := serialized.(string); ok {
|
|
|
|
c.Assert(serial, check.Equals, "192.0.2.1,2001:db8::1")
|
|
|
|
}
|
2022-01-16 21:16:59 +08:00
|
|
|
|
|
|
|
var deserialized MachineAddresses
|
|
|
|
err = deserialized.Scan(serialized)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
c.Assert(len(deserialized), check.Equals, len(input))
|
|
|
|
for i := range deserialized {
|
|
|
|
c.Assert(deserialized[i], check.Equals, input[i])
|
|
|
|
}
|
|
|
|
}
|