2023-09-04 11:37:39 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-09-04 11:37:39 +02:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-11-28 13:45:26 +01:00
|
|
|
|
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2024-09-16 15:47:03 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2023-09-04 11:37:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type MockStore struct {
|
|
|
|
Store
|
|
|
|
account *Account
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (s *MockStore) GetAllAccounts(_ context.Context) []*Account {
|
2023-09-04 11:37:39 +02:00
|
|
|
return []*Account{s.account}
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (s *MockStore) GetAccountByPeerID(_ context.Context, peerId string) (*Account, error) {
|
2023-09-04 11:37:39 +02:00
|
|
|
_, ok := s.account.Peers[peerId]
|
|
|
|
if ok {
|
|
|
|
return s.account, nil
|
|
|
|
}
|
|
|
|
|
2024-09-16 15:47:03 +02:00
|
|
|
return nil, status.NewPeerNotFoundError(peerId)
|
2023-09-04 11:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type MocAccountManager struct {
|
|
|
|
AccountManager
|
|
|
|
store *MockStore
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (a MocAccountManager) DeletePeer(_ context.Context, accountID, peerID, userID string) error {
|
2023-09-04 11:37:39 +02:00
|
|
|
delete(a.store.account.Peers, peerID)
|
2023-10-01 19:51:39 +02:00
|
|
|
return nil //nolint:nil
|
2023-09-04 11:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewManager(t *testing.T) {
|
|
|
|
startTime := time.Now()
|
|
|
|
timeNow = func() time.Time {
|
|
|
|
return startTime
|
|
|
|
}
|
|
|
|
|
|
|
|
store := &MockStore{}
|
|
|
|
am := MocAccountManager{
|
|
|
|
store: store,
|
|
|
|
}
|
|
|
|
|
|
|
|
numberOfPeers := 5
|
|
|
|
numberOfEphemeralPeers := 3
|
|
|
|
seedPeers(store, numberOfPeers, numberOfEphemeralPeers)
|
|
|
|
|
|
|
|
mgr := NewEphemeralManager(store, am)
|
2024-07-03 11:33:02 +02:00
|
|
|
mgr.loadEphemeralPeers(context.Background())
|
2023-09-04 11:37:39 +02:00
|
|
|
startTime = startTime.Add(ephemeralLifeTime + 1)
|
2024-07-03 11:33:02 +02:00
|
|
|
mgr.cleanup(context.Background())
|
2023-09-04 11:37:39 +02:00
|
|
|
|
|
|
|
if len(store.account.Peers) != numberOfPeers {
|
|
|
|
t.Errorf("failed to cleanup ephemeral peers, expected: %d, result: %d", numberOfPeers, len(store.account.Peers))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewManagerPeerConnected(t *testing.T) {
|
|
|
|
startTime := time.Now()
|
|
|
|
timeNow = func() time.Time {
|
|
|
|
return startTime
|
|
|
|
}
|
|
|
|
|
|
|
|
store := &MockStore{}
|
|
|
|
am := MocAccountManager{
|
|
|
|
store: store,
|
|
|
|
}
|
|
|
|
|
|
|
|
numberOfPeers := 5
|
|
|
|
numberOfEphemeralPeers := 3
|
|
|
|
seedPeers(store, numberOfPeers, numberOfEphemeralPeers)
|
|
|
|
|
|
|
|
mgr := NewEphemeralManager(store, am)
|
2024-07-03 11:33:02 +02:00
|
|
|
mgr.loadEphemeralPeers(context.Background())
|
|
|
|
mgr.OnPeerConnected(context.Background(), store.account.Peers["ephemeral_peer_0"])
|
2023-09-04 11:37:39 +02:00
|
|
|
|
|
|
|
startTime = startTime.Add(ephemeralLifeTime + 1)
|
2024-07-03 11:33:02 +02:00
|
|
|
mgr.cleanup(context.Background())
|
2023-09-04 11:37:39 +02:00
|
|
|
|
|
|
|
expected := numberOfPeers + 1
|
|
|
|
if len(store.account.Peers) != expected {
|
|
|
|
t.Errorf("failed to cleanup ephemeral peers, expected: %d, result: %d", expected, len(store.account.Peers))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewManagerPeerDisconnected(t *testing.T) {
|
|
|
|
startTime := time.Now()
|
|
|
|
timeNow = func() time.Time {
|
|
|
|
return startTime
|
|
|
|
}
|
|
|
|
|
|
|
|
store := &MockStore{}
|
|
|
|
am := MocAccountManager{
|
|
|
|
store: store,
|
|
|
|
}
|
|
|
|
|
|
|
|
numberOfPeers := 5
|
|
|
|
numberOfEphemeralPeers := 3
|
|
|
|
seedPeers(store, numberOfPeers, numberOfEphemeralPeers)
|
|
|
|
|
|
|
|
mgr := NewEphemeralManager(store, am)
|
2024-07-03 11:33:02 +02:00
|
|
|
mgr.loadEphemeralPeers(context.Background())
|
2023-09-04 11:37:39 +02:00
|
|
|
for _, v := range store.account.Peers {
|
2024-07-03 11:33:02 +02:00
|
|
|
mgr.OnPeerConnected(context.Background(), v)
|
2023-09-04 11:37:39 +02:00
|
|
|
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
mgr.OnPeerDisconnected(context.Background(), store.account.Peers["ephemeral_peer_0"])
|
2023-09-04 11:37:39 +02:00
|
|
|
|
|
|
|
startTime = startTime.Add(ephemeralLifeTime + 1)
|
2024-07-03 11:33:02 +02:00
|
|
|
mgr.cleanup(context.Background())
|
2023-09-04 11:37:39 +02:00
|
|
|
|
|
|
|
expected := numberOfPeers + numberOfEphemeralPeers - 1
|
|
|
|
if len(store.account.Peers) != expected {
|
|
|
|
t.Errorf("failed to cleanup ephemeral peers, expected: %d, result: %d", expected, len(store.account.Peers))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func seedPeers(store *MockStore, numberOfPeers int, numberOfEphemeralPeers int) {
|
2024-07-03 11:33:02 +02:00
|
|
|
store.account = newAccountWithId(context.Background(), "my account", "", "")
|
2023-09-04 11:37:39 +02:00
|
|
|
|
|
|
|
for i := 0; i < numberOfPeers; i++ {
|
|
|
|
peerId := fmt.Sprintf("peer_%d", i)
|
2023-11-28 13:45:26 +01:00
|
|
|
p := &nbpeer.Peer{
|
2023-09-04 11:37:39 +02:00
|
|
|
ID: peerId,
|
|
|
|
Ephemeral: false,
|
|
|
|
}
|
|
|
|
store.account.Peers[p.ID] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < numberOfEphemeralPeers; i++ {
|
|
|
|
peerId := fmt.Sprintf("ephemeral_peer_%d", i)
|
2023-11-28 13:45:26 +01:00
|
|
|
p := &nbpeer.Peer{
|
2023-09-04 11:37:39 +02:00
|
|
|
ID: peerId,
|
|
|
|
Ephemeral: true,
|
|
|
|
}
|
|
|
|
store.account.Peers[p.ID] = p
|
|
|
|
}
|
|
|
|
}
|