Management - add serial to Network reflecting network updates (#179)

* chore: [management] - add account serial ID

* Fix concurrency on the client (#183)

* reworked peer connection establishment logic eliminating race conditions and deadlocks while running many peers

* chore: move serial to Network from Account

* feature: increment Network serial ID when adding/removing peers

* chore: extract network struct init to network.go

* chore: add serial test when adding peer to the account

* test: add ModificationID test on AddPeer and DeletePeer
This commit is contained in:
Mikhail Bragin
2022-01-14 14:34:27 +01:00
committed by GitHub
parent bafa71fc2e
commit 9d1ecbbfb2
6 changed files with 130 additions and 21 deletions

View File

@@ -146,6 +146,8 @@ func TestAccountManager_AddPeer(t *testing.T) {
t.Fatal(err)
}
serial := account.Network.Serial() //should be 0
var setupKey *SetupKey
for _, key := range account.SetupKeys {
setupKey = key
@@ -156,6 +158,11 @@ func TestAccountManager_AddPeer(t *testing.T) {
return
}
if account.Network.serial != 0 {
t.Errorf("expecting account network to have an initial serial=0")
return
}
key, err := wgtypes.GenerateKey()
if err != nil {
t.Fatal(err)
@@ -174,6 +181,12 @@ func TestAccountManager_AddPeer(t *testing.T) {
return
}
account, err = manager.GetAccount(account.Id)
if err != nil {
t.Fatal(err)
return
}
if peer.Key != expectedPeerKey {
t.Errorf("expecting just added peer to have key = %s, got %s", expectedPeerKey, peer.Key)
}
@@ -182,7 +195,64 @@ func TestAccountManager_AddPeer(t *testing.T) {
t.Errorf("expecting just added peer to have IP = %s, got %s", expectedPeerIP, peer.IP.String())
}
if account.Network.Serial() != 1 {
t.Errorf("expecting Network serial=%d to be incremented by 1 and be equal to %d when adding new peer to account", serial, account.Network.Serial())
}
}
func TestAccountManager_DeletePeer(t *testing.T) {
manager, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
account, err := manager.AddAccount("test_account", "account_creator")
if err != nil {
t.Fatal(err)
}
var setupKey *SetupKey
for _, key := range account.SetupKeys {
setupKey = key
}
key, err := wgtypes.GenerateKey()
if err != nil {
t.Fatal(err)
return
}
peerKey := key.PublicKey().String()
_, err = manager.AddPeer(setupKey.Key, Peer{
Key: peerKey,
Meta: PeerSystemMeta{},
Name: peerKey,
})
if err != nil {
t.Errorf("expecting peer to be added, got failure %v", err)
return
}
_, err = manager.DeletePeer(account.Id, peerKey)
if err != nil {
return
}
account, err = manager.GetAccount(account.Id)
if err != nil {
t.Fatal(err)
return
}
if account.Network.Serial() != 2 {
t.Errorf("expecting Network serial=%d to be incremented and be equal to 2 after adding and deleteing a peer", account.Network.Serial())
}
}
func createManager(t *testing.T) (*AccountManager, error) {
store, err := createStore(t)
if err != nil {