2021-09-02 10:59:12 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
2021-10-17 16:29:30 -04:00
|
|
|
func CreateNodeNamespace(c *check.C, namespace, node, key, IP string) (*Namespace, *Machine) {
|
|
|
|
n1, err := h.CreateNamespace(namespace)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak1, err := h.CreatePreAuthKey(n1.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, node)
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
m1 := &Machine{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: key,
|
|
|
|
NodeKey: key,
|
|
|
|
DiscoKey: key,
|
|
|
|
Name: node,
|
|
|
|
NamespaceID: n1.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: IP,
|
|
|
|
AuthKeyID: uint(pak1.ID),
|
|
|
|
}
|
|
|
|
h.db.Save(m1)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, m1.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
return n1, m1
|
|
|
|
}
|
|
|
|
|
2021-09-02 10:59:12 -04:00
|
|
|
func (s *Suite) TestBasicSharedNodesInNamespace(c *check.C) {
|
|
|
|
n1, err := h.CreateNamespace("shared1")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
n2, err := h.CreateNamespace("shared2")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak1, err := h.CreatePreAuthKey(n1.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak2, err := h.CreatePreAuthKey(n2.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, "test_get_shared_nodes_1")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m1 := &Machine{
|
2021-09-02 10:59:12 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
Name: "test_get_shared_nodes_1",
|
|
|
|
NamespaceID: n1.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.1",
|
|
|
|
AuthKeyID: uint(pak1.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m1)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, m1.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m2 := &Machine{
|
2021-09-02 10:59:12 -04:00
|
|
|
ID: 1,
|
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_2",
|
|
|
|
NamespaceID: n2.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.2",
|
|
|
|
AuthKeyID: uint(pak2.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m2)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n2.Name, m2.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1s, err := h.getPeers(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-02 17:00:09 -04:00
|
|
|
c.Assert(len(p1s), check.Equals, 0)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
err = h.AddSharedMachineToNamespace(m2, n1)
|
2021-09-02 10:59:12 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1sAfter, err := h.getPeers(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-02 17:00:09 -04:00
|
|
|
c.Assert(len(p1sAfter), check.Equals, 1)
|
|
|
|
c.Assert(p1sAfter[0].ID, check.Equals, m2.ID)
|
2021-09-02 10:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestSameNamespace(c *check.C) {
|
|
|
|
n1, err := h.CreateNamespace("shared1")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
n2, err := h.CreateNamespace("shared2")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak1, err := h.CreatePreAuthKey(n1.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak2, err := h.CreatePreAuthKey(n2.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, "test_get_shared_nodes_1")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m1 := &Machine{
|
2021-09-02 10:59:12 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
Name: "test_get_shared_nodes_1",
|
|
|
|
NamespaceID: n1.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.1",
|
|
|
|
AuthKeyID: uint(pak1.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m1)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, m1.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m2 := &Machine{
|
2021-09-02 10:59:12 -04:00
|
|
|
ID: 1,
|
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_2",
|
|
|
|
NamespaceID: n2.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.2",
|
|
|
|
AuthKeyID: uint(pak2.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m2)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n2.Name, m2.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1s, err := h.getPeers(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-02 17:00:09 -04:00
|
|
|
c.Assert(len(p1s), check.Equals, 0)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
err = h.AddSharedMachineToNamespace(m1, n1)
|
2021-09-02 10:59:12 -04:00
|
|
|
c.Assert(err, check.Equals, errorSameNamespace)
|
|
|
|
}
|
|
|
|
|
2021-10-17 16:29:30 -04:00
|
|
|
func (s *Suite) TestUnshare(c *check.C) {
|
|
|
|
n1, m1 := CreateNodeNamespace(c, "shared1", "test_unshare_1", "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66", "100.64.0.1")
|
|
|
|
_, m2 := CreateNodeNamespace(c, "shared2", "test_unshare_2", "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863", "100.64.0.2")
|
|
|
|
|
|
|
|
p1s, err := h.getPeers(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(p1s), check.Equals, 0)
|
|
|
|
|
|
|
|
err = h.AddSharedMachineToNamespace(m2, n1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1s, err = h.getShared(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(p1s), check.Equals, 1)
|
|
|
|
|
|
|
|
err = h.RemoveSharedMachineFromNamespace(m2, n1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1s, err = h.getShared(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(p1s), check.Equals, 0)
|
|
|
|
|
|
|
|
err = h.RemoveSharedMachineFromNamespace(m2, n1)
|
|
|
|
c.Assert(err, check.Equals, errorMachineNotShared)
|
|
|
|
}
|
|
|
|
|
2021-09-02 10:59:12 -04:00
|
|
|
func (s *Suite) TestAlreadyShared(c *check.C) {
|
|
|
|
n1, err := h.CreateNamespace("shared1")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
n2, err := h.CreateNamespace("shared2")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak1, err := h.CreatePreAuthKey(n1.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak2, err := h.CreatePreAuthKey(n2.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, "test_get_shared_nodes_1")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m1 := &Machine{
|
2021-09-02 10:59:12 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
Name: "test_get_shared_nodes_1",
|
|
|
|
NamespaceID: n1.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.1",
|
|
|
|
AuthKeyID: uint(pak1.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m1)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, m1.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m2 := &Machine{
|
2021-09-02 10:59:12 -04:00
|
|
|
ID: 1,
|
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_2",
|
|
|
|
NamespaceID: n2.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.2",
|
|
|
|
AuthKeyID: uint(pak2.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m2)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n2.Name, m2.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1s, err := h.getPeers(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-02 17:00:09 -04:00
|
|
|
c.Assert(len(p1s), check.Equals, 0)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
err = h.AddSharedMachineToNamespace(m2, n1)
|
2021-09-02 10:59:12 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-02 17:00:09 -04:00
|
|
|
err = h.AddSharedMachineToNamespace(m2, n1)
|
2021-09-09 18:26:46 -04:00
|
|
|
c.Assert(err, check.Equals, errorMachineAlreadyShared)
|
2021-09-02 10:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestDoNotIncludeRoutesOnShared(c *check.C) {
|
|
|
|
n1, err := h.CreateNamespace("shared1")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
n2, err := h.CreateNamespace("shared2")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak1, err := h.CreatePreAuthKey(n1.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak2, err := h.CreatePreAuthKey(n2.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, "test_get_shared_nodes_1")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m1 := &Machine{
|
2021-09-02 10:59:12 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
Name: "test_get_shared_nodes_1",
|
|
|
|
NamespaceID: n1.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.1",
|
|
|
|
AuthKeyID: uint(pak1.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m1)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, m1.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m2 := &Machine{
|
2021-09-02 10:59:12 -04:00
|
|
|
ID: 1,
|
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_2",
|
|
|
|
NamespaceID: n2.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.2",
|
|
|
|
AuthKeyID: uint(pak2.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m2)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n2.Name, m2.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1s, err := h.getPeers(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-02 17:00:09 -04:00
|
|
|
c.Assert(len(p1s), check.Equals, 0)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
err = h.AddSharedMachineToNamespace(m2, n1)
|
2021-09-02 10:59:12 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1sAfter, err := h.getPeers(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-02 17:00:09 -04:00
|
|
|
c.Assert(len(p1sAfter), check.Equals, 1)
|
2021-10-04 13:40:21 -04:00
|
|
|
c.Assert(p1sAfter[0].Name, check.Equals, "test_get_shared_nodes_2")
|
2021-09-02 10:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestComplexSharingAcrossNamespaces(c *check.C) {
|
|
|
|
n1, err := h.CreateNamespace("shared1")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
n2, err := h.CreateNamespace("shared2")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
n3, err := h.CreateNamespace("shared3")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak1, err := h.CreatePreAuthKey(n1.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak2, err := h.CreatePreAuthKey(n2.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak3, err := h.CreatePreAuthKey(n3.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak4, err := h.CreatePreAuthKey(n1.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, "test_get_shared_nodes_1")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m1 := &Machine{
|
2021-10-13 14:48:50 -04:00
|
|
|
ID: 1,
|
2021-09-02 10:59:12 -04:00
|
|
|
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
Name: "test_get_shared_nodes_1",
|
|
|
|
NamespaceID: n1.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.1",
|
|
|
|
AuthKeyID: uint(pak1.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m1)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, m1.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m2 := &Machine{
|
2021-10-13 14:48:50 -04:00
|
|
|
ID: 2,
|
2021-09-02 10:59:12 -04:00
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_2",
|
|
|
|
NamespaceID: n2.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.2",
|
|
|
|
AuthKeyID: uint(pak2.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m2)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n2.Name, m2.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m3 := &Machine{
|
2021-10-13 14:48:50 -04:00
|
|
|
ID: 3,
|
2021-09-02 10:59:12 -04:00
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_3",
|
|
|
|
NamespaceID: n3.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.3",
|
|
|
|
AuthKeyID: uint(pak3.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m3)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n3.Name, m3.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
m4 := &Machine{
|
2021-10-13 14:48:50 -04:00
|
|
|
ID: 4,
|
2021-09-02 10:59:12 -04:00
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_4",
|
|
|
|
NamespaceID: n1.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.4",
|
|
|
|
AuthKeyID: uint(pak4.ID),
|
|
|
|
}
|
2021-10-02 17:00:09 -04:00
|
|
|
h.db.Save(m4)
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, m4.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1s, err := h.getPeers(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-13 14:56:32 -04:00
|
|
|
c.Assert(len(p1s), check.Equals, 1) // node1 can see node4
|
2021-10-04 13:40:21 -04:00
|
|
|
c.Assert(p1s[0].Name, check.Equals, "test_get_shared_nodes_4")
|
2021-09-02 10:59:12 -04:00
|
|
|
|
2021-10-02 17:00:09 -04:00
|
|
|
err = h.AddSharedMachineToNamespace(m2, n1)
|
2021-09-02 10:59:12 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1sAfter, err := h.getPeers(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-13 14:56:32 -04:00
|
|
|
c.Assert(len(p1sAfter), check.Equals, 2) // node1 can see node2 (shared) and node4 (same namespace)
|
2021-10-04 13:40:21 -04:00
|
|
|
c.Assert(p1sAfter[0].Name, check.Equals, "test_get_shared_nodes_2")
|
|
|
|
c.Assert(p1sAfter[1].Name, check.Equals, "test_get_shared_nodes_4")
|
|
|
|
|
|
|
|
node1shared, err := h.getShared(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-13 14:56:32 -04:00
|
|
|
c.Assert(len(node1shared), check.Equals, 1) // node1 can see node2 as shared
|
2021-10-04 13:40:21 -04:00
|
|
|
c.Assert(node1shared[0].Name, check.Equals, "test_get_shared_nodes_2")
|
2021-09-02 10:59:12 -04:00
|
|
|
|
|
|
|
pAlone, err := h.getPeers(m3)
|
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-13 14:56:32 -04:00
|
|
|
c.Assert(len(pAlone), check.Equals, 0) // node3 is alone
|
|
|
|
|
|
|
|
pSharedTo, err := h.getPeers(m2)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(pSharedTo), check.Equals, 2) // node2 should see node1 (sharedTo) and node4 (sharedTo), as is shared in namespace1
|
|
|
|
c.Assert(pSharedTo[0].Name, check.Equals, "test_get_shared_nodes_1")
|
|
|
|
c.Assert(pSharedTo[1].Name, check.Equals, "test_get_shared_nodes_4")
|
2021-09-02 10:59:12 -04:00
|
|
|
}
|
2021-10-10 17:55:18 -04:00
|
|
|
|
|
|
|
func (s *Suite) TestDeleteSharedMachine(c *check.C) {
|
|
|
|
n1, err := h.CreateNamespace("shared1")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
n2, err := h.CreateNamespace("shared2")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
n3, err := h.CreateNamespace("shared3")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak1n1, err := h.CreatePreAuthKey(n1.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak2n2, err := h.CreatePreAuthKey(n2.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak3n3, err := h.CreatePreAuthKey(n3.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak4n1, err := h.CreatePreAuthKey(n1.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, "test_get_shared_nodes_1")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
m1 := &Machine{
|
2021-10-13 14:48:50 -04:00
|
|
|
ID: 1,
|
2021-10-10 17:55:18 -04:00
|
|
|
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
Name: "test_get_shared_nodes_1",
|
|
|
|
NamespaceID: n1.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.1",
|
|
|
|
AuthKeyID: uint(pak1n1.ID),
|
|
|
|
}
|
|
|
|
h.db.Save(m1)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, m1.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
m2 := &Machine{
|
2021-10-13 14:48:50 -04:00
|
|
|
ID: 2,
|
2021-10-10 17:55:18 -04:00
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_2",
|
|
|
|
NamespaceID: n2.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.2",
|
|
|
|
AuthKeyID: uint(pak2n2.ID),
|
|
|
|
}
|
|
|
|
h.db.Save(m2)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n2.Name, m2.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
m3 := &Machine{
|
2021-10-13 14:48:50 -04:00
|
|
|
ID: 3,
|
2021-10-10 17:55:18 -04:00
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_3",
|
|
|
|
NamespaceID: n3.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.3",
|
|
|
|
AuthKeyID: uint(pak3n3.ID),
|
|
|
|
}
|
|
|
|
h.db.Save(m3)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n3.Name, m3.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
m4 := &Machine{
|
2021-10-13 14:48:50 -04:00
|
|
|
ID: 4,
|
2021-10-10 17:55:18 -04:00
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
Name: "test_get_shared_nodes_4",
|
|
|
|
NamespaceID: n1.ID,
|
|
|
|
Registered: true,
|
|
|
|
RegisterMethod: "authKey",
|
|
|
|
IPAddress: "100.64.0.4",
|
|
|
|
AuthKeyID: uint(pak4n1.ID),
|
|
|
|
}
|
|
|
|
h.db.Save(m4)
|
|
|
|
|
|
|
|
_, err = h.GetMachine(n1.Name, m4.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1s, err := h.getPeers(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(p1s), check.Equals, 1) // nodes 1 and 4
|
|
|
|
c.Assert(p1s[0].Name, check.Equals, "test_get_shared_nodes_4")
|
|
|
|
|
|
|
|
err = h.AddSharedMachineToNamespace(m2, n1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p1sAfter, err := h.getPeers(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(p1sAfter), check.Equals, 2) // nodes 1, 2, 4
|
|
|
|
c.Assert(p1sAfter[0].Name, check.Equals, "test_get_shared_nodes_2")
|
|
|
|
c.Assert(p1sAfter[1].Name, check.Equals, "test_get_shared_nodes_4")
|
|
|
|
|
|
|
|
node1shared, err := h.getShared(m1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(node1shared), check.Equals, 1) // nodes 1, 2, 4
|
|
|
|
c.Assert(node1shared[0].Name, check.Equals, "test_get_shared_nodes_2")
|
|
|
|
|
|
|
|
pAlone, err := h.getPeers(m3)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(pAlone), check.Equals, 0) // node 3 is alone
|
|
|
|
|
|
|
|
sharedMachines, err := h.ListSharedMachinesInNamespace(n1.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(*sharedMachines), check.Equals, 1)
|
|
|
|
|
|
|
|
err = h.DeleteMachine(m2)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
sharedMachines, err = h.ListSharedMachinesInNamespace(n1.Name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(*sharedMachines), check.Equals, 0)
|
|
|
|
}
|