2021-05-09 11:12:05 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gopkg.in/check.v1"
|
2021-11-13 14:01:05 -05:00
|
|
|
"gorm.io/gorm"
|
2022-01-16 08:16:59 -05:00
|
|
|
"inet.af/netaddr"
|
2021-05-09 11:12:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestCreateAndDestroyNamespace(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-05-09 11:12:05 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(namespace.Name, check.Equals, "test")
|
2021-05-09 11:12:05 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
namespaces, err := app.ListNamespaces()
|
2021-05-09 11:12:05 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(len(namespaces), check.Equals, 1)
|
2021-05-09 11:12:05 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
err = app.DestroyNamespace("test")
|
2021-05-09 11:12:05 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetNamespace("test")
|
2021-05-09 11:12:05 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestDestroyNamespaceErrors(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
err := app.DestroyNamespace("test")
|
2021-11-15 11:33:16 -05:00
|
|
|
c.Assert(err, check.Equals, errNamespaceNotFound)
|
2021-05-09 11:12:05 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-05-09 11:12:05 -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-05-09 11:12:05 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
err = app.DestroyNamespace("test")
|
2021-11-13 14:01:05 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
result := app.db.Preload("Namespace").First(&pak, "key = ?", pak.Key)
|
2021-11-13 14:01:05 -05:00
|
|
|
// destroying a namespace also deletes all associated preauthkeys
|
|
|
|
c.Assert(result.Error, check.Equals, gorm.ErrRecordNotFound)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
namespace, err = app.CreateNamespace("test")
|
2021-11-13 14:01:05 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
pak, err = app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-11-13 14:01:05 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machine := Machine{
|
2021-05-09 11:12:05 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testmachine",
|
2021-11-15 11:16:04 -05:00
|
|
|
NamespaceID: namespace.ID,
|
2021-05-09 11:12:05 -04:00
|
|
|
Registered: true,
|
2021-11-18 03:49:55 -05:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-09 11:12:05 -04:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 11:16:04 -05:00
|
|
|
app.db.Save(&machine)
|
2021-05-09 11:12:05 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
err = app.DestroyNamespace("test")
|
2021-11-15 11:33:16 -05:00
|
|
|
c.Assert(err, check.Equals, errNamespaceNotEmptyOfNodes)
|
2021-05-09 11:12:05 -04:00
|
|
|
}
|
2021-10-17 17:58:09 -04:00
|
|
|
|
2021-10-16 11:20:06 -04:00
|
|
|
func (s *Suite) TestRenameNamespace(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
namespaceTest, err := app.CreateNamespace("test")
|
2021-10-16 11:20:06 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(namespaceTest.Name, check.Equals, "test")
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
namespaces, err := app.ListNamespaces()
|
2021-10-16 11:20:06 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(len(namespaces), check.Equals, 1)
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
err = app.RenameNamespace("test", "test_renamed")
|
2021-10-16 11:20:06 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetNamespace("test")
|
2021-11-15 11:33:16 -05:00
|
|
|
c.Assert(err, check.Equals, errNamespaceNotFound)
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetNamespace("test_renamed")
|
2021-10-16 11:20:06 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
err = app.RenameNamespace("test_does_not_exit", "test")
|
2021-11-15 11:33:16 -05:00
|
|
|
c.Assert(err, check.Equals, errNamespaceNotFound)
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
namespaceTest2, err := app.CreateNamespace("test2")
|
2021-10-16 11:20:06 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(namespaceTest2.Name, check.Equals, "test2")
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
err = app.RenameNamespace("test2", "test_renamed")
|
2021-11-15 11:33:16 -05:00
|
|
|
c.Assert(err, check.Equals, errNamespaceExists)
|
2021-10-16 11:20:06 -04:00
|
|
|
}
|
2021-10-17 17:59:44 -04:00
|
|
|
|
2021-10-17 17:58:09 -04:00
|
|
|
func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) {
|
2021-11-15 11:16:04 -05:00
|
|
|
namespaceShared1, err := app.CreateNamespace("shared1")
|
2021-10-17 17:58:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
namespaceShared2, err := app.CreateNamespace("shared2")
|
2021-10-17 17:58:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
namespaceShared3, err := app.CreateNamespace("shared3")
|
2021-10-17 17:58:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
preAuthKeyShared1, err := app.CreatePreAuthKey(
|
|
|
|
namespaceShared1.Name,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
)
|
2021-10-17 17:58:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
preAuthKeyShared2, err := app.CreatePreAuthKey(
|
|
|
|
namespaceShared2.Name,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
)
|
2021-10-17 17:58:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
preAuthKeyShared3, err := app.CreatePreAuthKey(
|
|
|
|
namespaceShared3.Name,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
)
|
2021-10-17 17:58:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
preAuthKey2Shared1, err := app.CreatePreAuthKey(
|
|
|
|
namespaceShared1.Name,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
)
|
2021-10-17 17:58:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetMachine(namespaceShared1.Name, "test_get_shared_nodes_1")
|
2021-10-17 17:58:09 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machineInShared1 := &Machine{
|
2021-10-17 17:58:09 -04:00
|
|
|
ID: 1,
|
|
|
|
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
Name: "test_get_shared_nodes_1",
|
2021-11-15 11:16:04 -05:00
|
|
|
NamespaceID: namespaceShared1.ID,
|
|
|
|
Namespace: *namespaceShared1,
|
2021-10-17 17:58:09 -04:00
|
|
|
Registered: true,
|
2021-11-18 03:49:55 -05:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-01-16 08:16:59 -05:00
|
|
|
IPAddresses: []netaddr.IP{netaddr.MustParseIP("100.64.0.1")},
|
2021-11-15 11:16:04 -05:00
|
|
|
AuthKeyID: uint(preAuthKeyShared1.ID),
|
2021-10-17 17:58:09 -04:00
|
|
|
}
|
2021-11-15 11:16:04 -05:00
|
|
|
app.db.Save(machineInShared1)
|
2021-10-17 17:58:09 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetMachine(namespaceShared1.Name, machineInShared1.Name)
|
2021-10-17 17:58:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machineInShared2 := &Machine{
|
2021-10-17 17:58:09 -04:00
|
|
|
ID: 2,
|
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_2",
|
2021-11-15 11:16:04 -05:00
|
|
|
NamespaceID: namespaceShared2.ID,
|
|
|
|
Namespace: *namespaceShared2,
|
2021-10-17 17:58:09 -04:00
|
|
|
Registered: true,
|
2021-11-18 03:49:55 -05:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-01-16 08:16:59 -05:00
|
|
|
IPAddresses: []netaddr.IP{netaddr.MustParseIP("100.64.0.2")},
|
2021-11-15 11:16:04 -05:00
|
|
|
AuthKeyID: uint(preAuthKeyShared2.ID),
|
2021-10-17 17:58:09 -04:00
|
|
|
}
|
2021-11-15 11:16:04 -05:00
|
|
|
app.db.Save(machineInShared2)
|
2021-10-17 17:58:09 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetMachine(namespaceShared2.Name, machineInShared2.Name)
|
2021-10-17 17:58:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machineInShared3 := &Machine{
|
2021-10-17 17:58:09 -04:00
|
|
|
ID: 3,
|
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_3",
|
2021-11-15 11:16:04 -05:00
|
|
|
NamespaceID: namespaceShared3.ID,
|
|
|
|
Namespace: *namespaceShared3,
|
2021-10-17 17:58:09 -04:00
|
|
|
Registered: true,
|
2021-11-18 03:49:55 -05:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-01-16 08:16:59 -05:00
|
|
|
IPAddresses: []netaddr.IP{netaddr.MustParseIP("100.64.0.3")},
|
2021-11-15 11:16:04 -05:00
|
|
|
AuthKeyID: uint(preAuthKeyShared3.ID),
|
2021-10-17 17:58:09 -04:00
|
|
|
}
|
2021-11-15 11:16:04 -05:00
|
|
|
app.db.Save(machineInShared3)
|
2021-10-17 17:58:09 -04:00
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
_, err = app.GetMachine(namespaceShared3.Name, machineInShared3.Name)
|
2021-10-17 17:58:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machine2InShared1 := &Machine{
|
2021-10-17 17:58:09 -04:00
|
|
|
ID: 4,
|
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_4",
|
2021-11-15 11:16:04 -05:00
|
|
|
NamespaceID: namespaceShared1.ID,
|
|
|
|
Namespace: *namespaceShared1,
|
2021-10-17 17:58:09 -04:00
|
|
|
Registered: true,
|
2021-11-18 03:49:55 -05:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-01-16 08:16:59 -05:00
|
|
|
IPAddresses: []netaddr.IP{netaddr.MustParseIP("100.64.0.4")},
|
2021-11-15 11:16:04 -05:00
|
|
|
AuthKeyID: uint(preAuthKey2Shared1.ID),
|
2021-10-17 17:58:09 -04:00
|
|
|
}
|
2021-11-15 11:16:04 -05:00
|
|
|
app.db.Save(machine2InShared1)
|
2021-10-17 17:58:09 -04:00
|
|
|
|
2022-02-21 18:10:20 -05:00
|
|
|
// TODO: Remove comment out when we have all nodes available to every node
|
|
|
|
// peersOfMachine1InShared1, err := app.getPeers(machineInShared1)
|
|
|
|
// c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// userProfiles := getMapResponseUserProfiles(
|
|
|
|
// *machineInShared1,
|
|
|
|
// peersOfMachine1InShared1,
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// log.Trace().Msgf("userProfiles %#v", userProfiles)
|
|
|
|
// c.Assert(len(userProfiles), check.Equals, 2)
|
|
|
|
//
|
|
|
|
// found := false
|
|
|
|
// for _, userProfiles := range userProfiles {
|
|
|
|
// if userProfiles.DisplayName == namespaceShared1.Name {
|
|
|
|
// found = true
|
|
|
|
//
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// c.Assert(found, check.Equals, true)
|
|
|
|
//
|
|
|
|
// found = false
|
|
|
|
// for _, userProfile := range userProfiles {
|
|
|
|
// if userProfile.DisplayName == namespaceShared2.Name {
|
|
|
|
// found = true
|
|
|
|
//
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// c.Assert(found, check.Equals, true)
|
2021-10-17 17:58:09 -04:00
|
|
|
}
|