Skip to content

Commit

Permalink
Add more test cases to prove that peers and shared peers work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
kradalby committed Oct 4, 2021
1 parent 2eb57e6 commit f6a7564
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
41 changes: 41 additions & 0 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package headscale

import (
"encoding/json"
"strconv"

"gopkg.in/check.v1"
)
Expand Down Expand Up @@ -116,3 +117,43 @@ func (s *Suite) TestHardDeleteMachine(c *check.C) {
_, err = h.GetMachine(n.Name, "testmachine3")
c.Assert(err, check.NotNil)
}

func (s *Suite) TestGetDirectPeers(c *check.C) {
n, err := h.CreateNamespace("test")
c.Assert(err, check.IsNil)

pak, err := h.CreatePreAuthKey(n.Name, false, false, nil)
c.Assert(err, check.IsNil)

_, err = h.GetMachineByID(0)
c.Assert(err, check.NotNil)

for i := 0; i <= 10; i++ {
m := Machine{
ID: uint64(i),
MachineKey: "foo" + strconv.Itoa(i),
NodeKey: "bar" + strconv.Itoa(i),
DiscoKey: "faa" + strconv.Itoa(i),
Name: "testmachine" + strconv.Itoa(i),
NamespaceID: n.ID,
Registered: true,
RegisterMethod: "authKey",
AuthKeyID: uint(pak.ID),
}
h.db.Save(&m)
}

m1, err := h.GetMachineByID(0)
c.Assert(err, check.IsNil)

_, err = m1.GetHostInfo()
c.Assert(err, check.IsNil)

peers, err := h.getDirectPeers(m1)
c.Assert(err, check.IsNil)

c.Assert(len(peers), check.Equals, 9)
c.Assert(peers[0].Name, check.Equals, "testmachine2")
c.Assert(peers[5].Name, check.Equals, "testmachine7")
c.Assert(peers[8].Name, check.Equals, "testmachine10")
}
9 changes: 9 additions & 0 deletions sharing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ func (s *Suite) TestDoNotIncludeRoutesOnShared(c *check.C) {
p1sAfter, err := h.getPeers(m1)
c.Assert(err, check.IsNil)
c.Assert(len(p1sAfter), check.Equals, 1)
c.Assert(p1sAfter[0].Name, check.Equals, "test_get_shared_nodes_2")
}

func (s *Suite) TestComplexSharingAcrossNamespaces(c *check.C) {
Expand Down Expand Up @@ -343,13 +344,21 @@ func (s *Suite) TestComplexSharingAcrossNamespaces(c *check.C) {
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)
Expand Down

0 comments on commit f6a7564

Please sign in to comment.