mirror of
https://github.com/juanfont/headscale.git
synced 2026-10-27 00:36:49 +09:00
130 lines
3.2 KiB
Go
130 lines
3.2 KiB
Go
package servertest_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/servertest"
|
|
"github.com/stretchr/testify/assert"
|
|
"tailscale.com/types/netmap"
|
|
)
|
|
|
|
// TestConsistency verifies that all nodes converge to the same
|
|
// view of the network and that no updates are lost during various
|
|
// operations.
|
|
func TestConsistency(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("all_nodes_converge", func(t *testing.T) {
|
|
t.Parallel()
|
|
h := servertest.NewHarness(t, 5)
|
|
servertest.AssertMeshComplete(t, h.Clients())
|
|
servertest.AssertConsistentState(t, h.Clients())
|
|
servertest.AssertSymmetricVisibility(t, h.Clients())
|
|
})
|
|
|
|
t.Run("self_node_has_correct_hostname", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
h := servertest.NewHarness(t, 3)
|
|
for _, c := range h.Clients() {
|
|
assert.Equal(t, c.Name, c.SelfName(),
|
|
"client %s self name should match", c.Name)
|
|
}
|
|
})
|
|
|
|
t.Run("update_count_positive", func(t *testing.T) {
|
|
t.Parallel()
|
|
h := servertest.NewHarness(t, 3)
|
|
// After mesh formation, each client should have received
|
|
// at least one update.
|
|
for _, c := range h.Clients() {
|
|
assert.Positive(t, c.UpdateCount(),
|
|
"client %s should have received at least one update", c.Name)
|
|
}
|
|
})
|
|
|
|
t.Run("new_node_visible_to_all", func(t *testing.T) {
|
|
t.Parallel()
|
|
h := servertest.NewHarness(t, 3)
|
|
|
|
newClient := h.AddClient(t)
|
|
h.WaitForMeshComplete(t, 10*time.Second)
|
|
|
|
// Verify every original client sees the new node.
|
|
for _, c := range h.Clients() {
|
|
if c == newClient {
|
|
continue
|
|
}
|
|
|
|
_, found := c.PeerByName(newClient.Name)
|
|
assert.True(t, found,
|
|
"client %s should see new client %s", c.Name, newClient.Name)
|
|
}
|
|
|
|
// And the new node sees all others.
|
|
for _, c := range h.Clients() {
|
|
if c == newClient {
|
|
continue
|
|
}
|
|
|
|
_, found := newClient.PeerByName(c.Name)
|
|
assert.True(t, found,
|
|
"new client %s should see %s", newClient.Name, c.Name)
|
|
}
|
|
})
|
|
|
|
t.Run("interleaved_join_and_leave", func(t *testing.T) {
|
|
t.Parallel()
|
|
h := servertest.NewHarness(t, 5)
|
|
|
|
// Disconnect 2 nodes.
|
|
h.Client(0).Disconnect(t)
|
|
h.Client(1).Disconnect(t)
|
|
|
|
// Add 3 new nodes while 2 are disconnected.
|
|
c5 := h.AddClient(t)
|
|
c6 := h.AddClient(t)
|
|
c7 := h.AddClient(t)
|
|
|
|
// Disconnected peers can satisfy a count before the new peers arrive.
|
|
// Wait for each connected identity before comparing the mesh.
|
|
connected := h.ConnectedClients()
|
|
for _, c := range connected {
|
|
c.WaitForCondition(t, "all connected peers visible", 30*time.Second,
|
|
func(nm *netmap.NetworkMap) bool {
|
|
seen := make(map[string]bool, len(nm.Peers))
|
|
for _, peer := range nm.Peers {
|
|
if hi := peer.Hostinfo(); hi.Valid() {
|
|
seen[hi.Hostname()] = true
|
|
}
|
|
}
|
|
|
|
for _, other := range connected {
|
|
if other != c && !seen[other.Name] {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
})
|
|
}
|
|
|
|
// Verify the new nodes can see each other.
|
|
for _, a := range []*servertest.TestClient{c5, c6, c7} {
|
|
for _, b := range []*servertest.TestClient{c5, c6, c7} {
|
|
if a == b {
|
|
continue
|
|
}
|
|
|
|
_, found := a.PeerByName(b.Name)
|
|
assert.True(t, found,
|
|
"new client %s should see %s", a.Name, b.Name)
|
|
}
|
|
}
|
|
|
|
// Verify all connected clients see each other (consistent state).
|
|
servertest.AssertConsistentState(t, connected)
|
|
})
|
|
}
|