headscale/machine_test.go

167 lines
4.3 KiB
Go
Raw Normal View History

2021-05-11 18:55:36 -06:00
package headscale
import (
"encoding/json"
"strconv"
2021-05-11 18:55:36 -06:00
"gopkg.in/check.v1"
)
func (s *Suite) TestGetMachine(c *check.C) {
2021-11-15 09:16:04 -07:00
namespace, err := app.CreateNamespace("test")
2021-05-11 18:55:36 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
2021-05-11 18:55:36 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
_, err = app.GetMachine("test", "testmachine")
2021-05-11 18:55:36 -06:00
c.Assert(err, check.NotNil)
2021-11-15 09:16:04 -07:00
machine := &Machine{
2021-05-11 18:55:36 -06:00
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Name: "testmachine",
2021-11-15 09:16:04 -07:00
NamespaceID: namespace.ID,
2021-05-11 18:55:36 -06:00
Registered: true,
2021-11-18 01:49:55 -07:00
RegisterMethod: RegisterMethodAuthKey,
2021-05-11 18:55:36 -06:00
AuthKeyID: uint(pak.ID),
}
2021-11-15 09:16:04 -07:00
app.db.Save(machine)
2021-05-11 18:55:36 -06:00
2021-11-15 09:16:04 -07:00
machineFromDB, err := app.GetMachine("test", "testmachine")
2021-05-11 18:55:36 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
_, err = machineFromDB.GetHostInfo()
2021-05-11 18:55:36 -06:00
c.Assert(err, check.IsNil)
2021-07-16 16:14:22 -06:00
}
func (s *Suite) TestGetMachineByID(c *check.C) {
2021-11-15 09:16:04 -07:00
namespace, err := app.CreateNamespace("test")
2021-07-16 16:14:22 -06:00
c.Assert(err, check.IsNil)
2021-05-11 18:55:36 -06:00
2021-11-15 09:16:04 -07:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
2021-07-16 16:14:22 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
_, err = app.GetMachineByID(0)
2021-07-16 16:14:22 -06:00
c.Assert(err, check.NotNil)
2021-11-15 09:16:04 -07:00
machine := Machine{
2021-07-16 16:14:22 -06:00
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Name: "testmachine",
2021-11-15 09:16:04 -07:00
NamespaceID: namespace.ID,
2021-07-16 16:14:22 -06:00
Registered: true,
2021-11-18 01:49:55 -07:00
RegisterMethod: RegisterMethodAuthKey,
2021-07-16 16:14:22 -06:00
AuthKeyID: uint(pak.ID),
}
2021-11-15 09:16:04 -07:00
app.db.Save(&machine)
2021-07-16 16:14:22 -06:00
2021-11-15 09:16:04 -07:00
machineByID, err := app.GetMachineByID(0)
2021-07-16 16:14:22 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
_, err = machineByID.GetHostInfo()
2021-07-16 16:14:22 -06:00
c.Assert(err, check.IsNil)
}
func (s *Suite) TestDeleteMachine(c *check.C) {
2021-11-15 09:16:04 -07:00
namespace, err := app.CreateNamespace("test")
2021-07-16 16:14:22 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
machine := Machine{
2021-07-16 16:14:22 -06:00
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Name: "testmachine",
2021-11-15 09:16:04 -07:00
NamespaceID: namespace.ID,
2021-07-16 16:14:22 -06:00
Registered: true,
2021-11-18 01:49:55 -07:00
RegisterMethod: RegisterMethodAuthKey,
2021-07-16 16:14:22 -06:00
AuthKeyID: uint(1),
}
2021-11-15 09:16:04 -07:00
app.db.Save(&machine)
err = app.DeleteMachine(&machine)
2021-07-16 16:29:14 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
namespacesPendingUpdates, err := app.getValue("namespaces_pending_updates")
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
names := []string{}
2021-11-15 09:16:04 -07:00
err = json.Unmarshal([]byte(namespacesPendingUpdates), &names)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
c.Assert(names, check.DeepEquals, []string{namespace.Name})
app.checkForNamespacesPendingUpdates()
namespacesPendingUpdates, _ = app.getValue("namespaces_pending_updates")
c.Assert(namespacesPendingUpdates, check.Equals, "")
_, err = app.GetMachine(namespace.Name, "testmachine")
2021-07-16 16:14:22 -06:00
c.Assert(err, check.NotNil)
}
func (s *Suite) TestHardDeleteMachine(c *check.C) {
2021-11-15 09:16:04 -07:00
namespace, err := app.CreateNamespace("test")
2021-07-16 16:14:22 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
machine := Machine{
2021-07-16 16:14:22 -06:00
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Name: "testmachine3",
2021-11-15 09:16:04 -07:00
NamespaceID: namespace.ID,
2021-07-16 16:14:22 -06:00
Registered: true,
2021-11-18 01:49:55 -07:00
RegisterMethod: RegisterMethodAuthKey,
2021-07-16 16:14:22 -06:00
AuthKeyID: uint(1),
}
2021-11-15 09:16:04 -07:00
app.db.Save(&machine)
err = app.HardDeleteMachine(&machine)
2021-07-16 16:29:14 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
_, err = app.GetMachine(namespace.Name, "testmachine3")
2021-07-16 16:14:22 -06:00
c.Assert(err, check.NotNil)
2021-05-11 18:55:36 -06:00
}
func (s *Suite) TestGetDirectPeers(c *check.C) {
2021-11-15 09:16:04 -07:00
namespace, err := app.CreateNamespace("test")
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
_, err = app.GetMachineByID(0)
c.Assert(err, check.NotNil)
2021-11-15 09:16:04 -07: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,
Registered: true,
2021-11-18 01:49:55 -07:00
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
}
2021-11-15 09:16:04 -07:00
app.db.Save(&machine)
}
2021-11-15 09:16:04 -07:00
machine0ByID, err := app.GetMachineByID(0)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
_, err = machine0ByID.GetHostInfo()
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
peersOfMachine0, err := app.getDirectPeers(machine0ByID)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07: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")
}