Skip to content

Commit

Permalink
close store connection after tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcmmbaga committed May 9, 2024
1 parent be2a773 commit 0e99d34
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 35 deletions.
70 changes: 49 additions & 21 deletions management/server/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,12 @@ func TestNewAccount(t *testing.T) {
}

func TestAccountManager_GetOrCreateAccountByUser(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
defer cleanUp()

account, err := manager.GetOrCreateAccountByUser(userID, "")
if err != nil {
Expand Down Expand Up @@ -627,8 +628,9 @@ func TestDefaultAccountManager_GetAccountFromToken(t *testing.T) {

for _, testCase := range []test{testCase1, testCase2, testCase3, testCase4, testCase5, testCase6, testCase7} {
t.Run(testCase.name, func(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
require.NoError(t, err, "unable to create account manager")
defer cleanUp()

initAccount, err := manager.GetAccountByUserOrAccountID(testCase.inputInitUserParams.UserId, testCase.inputInitUserParams.AccountId, testCase.inputInitUserParams.Domain)
require.NoError(t, err, "create init user failed")
Expand Down Expand Up @@ -662,8 +664,9 @@ func TestDefaultAccountManager_GetGroupsFromTheToken(t *testing.T) {
domain := "test.domain"

initAccount := newAccountWithId("", userId, domain)
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
require.NoError(t, err, "unable to create account manager")
defer cleanUp()

accountID := initAccount.Id
acc, err := manager.GetAccountByUserOrAccountID(userId, accountID, domain)
Expand Down Expand Up @@ -800,11 +803,12 @@ func TestDefaultAccountManager_MarkPATUsed(t *testing.T) {
}

func TestAccountManager_PrivateAccount(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
defer cleanUp()

userId := "test_user"
account, err := manager.GetOrCreateAccountByUser(userId, "")
Expand All @@ -826,11 +830,12 @@ func TestAccountManager_PrivateAccount(t *testing.T) {
}

func TestAccountManager_SetOrUpdateDomain(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
defer cleanUp()

userId := "test_user"
domain := "hotmail.com"
Expand Down Expand Up @@ -863,11 +868,12 @@ func TestAccountManager_SetOrUpdateDomain(t *testing.T) {
}

func TestAccountManager_GetAccountByUserOrAccountId(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
defer cleanUp()

userId := "test_user"

Expand Down Expand Up @@ -901,11 +907,12 @@ func createAccount(am *DefaultAccountManager, accountID, userID, domain string)
}

func TestAccountManager_GetAccount(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
defer cleanUp()

expectedId := "test_account"
userId := "account_creator"
Expand Down Expand Up @@ -939,11 +946,12 @@ func TestAccountManager_GetAccount(t *testing.T) {
}

func TestAccountManager_DeleteAccount(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
defer cleanUp()

expectedId := "test_account"
userId := "account_creator"
Expand All @@ -964,11 +972,12 @@ func TestAccountManager_DeleteAccount(t *testing.T) {
}

func TestAccountManager_AddPeer(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
defer cleanUp()

userID := "testingUser"
account, err := createAccount(manager, "test_account", userID, "netbird.cloud")
Expand Down Expand Up @@ -1039,11 +1048,12 @@ func TestAccountManager_AddPeer(t *testing.T) {
}

func TestAccountManager_AddPeerWithUserID(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
defer cleanUp()

account, err := manager.GetOrCreateAccountByUser(userID, "netbird.cloud")
if err != nil {
Expand Down Expand Up @@ -1108,11 +1118,12 @@ func TestAccountManager_AddPeerWithUserID(t *testing.T) {
}

func TestAccountManager_NetworkUpdates(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
defer cleanUp()

userID := "account_creator"

Expand Down Expand Up @@ -1289,11 +1300,13 @@ func TestAccountManager_NetworkUpdates(t *testing.T) {
}

func TestAccountManager_DeletePeer(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
defer cleanUp()

userID := "account_creator"
account, err := createAccount(manager, "test_account", userID, "netbird.cloud")
if err != nil {
Expand Down Expand Up @@ -1370,10 +1383,11 @@ func getEvent(t *testing.T, accountID string, manager AccountManager, eventType
}

func TestGetUsersFromAccount(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
t.Fatal(err)
}
defer cleanUp()

users := map[string]*User{"1": {Id: "1", Role: UserRoleOwner}, "2": {Id: "2", Role: "user"}, "3": {Id: "3", Role: "user"}}
accountId := "test_account_id"
Expand Down Expand Up @@ -1641,8 +1655,9 @@ func hasNilField(x interface{}) error {
}

func TestDefaultAccountManager_DefaultAccountSettings(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
require.NoError(t, err, "unable to create account manager")
defer cleanUp()

account, err := manager.GetAccountByUserOrAccountID(userID, "", "")
require.NoError(t, err, "unable to create an account")
Expand All @@ -1653,8 +1668,10 @@ func TestDefaultAccountManager_DefaultAccountSettings(t *testing.T) {
}

func TestDefaultAccountManager_UpdatePeer_PeerLoginExpiration(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
require.NoError(t, err, "unable to create account manager")
defer cleanUp()

_, err = manager.GetAccountByUserOrAccountID(userID, "", "")
require.NoError(t, err, "unable to create an account")

Expand Down Expand Up @@ -1705,8 +1722,10 @@ func TestDefaultAccountManager_UpdatePeer_PeerLoginExpiration(t *testing.T) {
}

func TestDefaultAccountManager_MarkPeerConnected_PeerLoginExpiration(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
require.NoError(t, err, "unable to create account manager")
defer cleanUp()

account, err := manager.GetAccountByUserOrAccountID(userID, "", "")
require.NoError(t, err, "unable to create an account")

Expand Down Expand Up @@ -1748,8 +1767,10 @@ func TestDefaultAccountManager_MarkPeerConnected_PeerLoginExpiration(t *testing.
}

func TestDefaultAccountManager_UpdateAccountSettings_PeerLoginExpiration(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
require.NoError(t, err, "unable to create account manager")
defer cleanUp()

_, err = manager.GetAccountByUserOrAccountID(userID, "", "")
require.NoError(t, err, "unable to create an account")

Expand Down Expand Up @@ -1803,8 +1824,9 @@ func TestDefaultAccountManager_UpdateAccountSettings_PeerLoginExpiration(t *test
}

func TestDefaultAccountManager_UpdateAccountSettings(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
require.NoError(t, err, "unable to create account manager")
defer cleanUp()

account, err := manager.GetAccountByUserOrAccountID(userID, "", "")
require.NoError(t, err, "unable to create an account")
Expand Down Expand Up @@ -2265,14 +2287,20 @@ func TestAccount_UserGroupsRemoveFromPeers(t *testing.T) {
})
}

func createManager(t *testing.T) (*DefaultAccountManager, error) {
func createManager(t *testing.T) (*DefaultAccountManager, func(), error) {
t.Helper()
store, err := createStore(t)
if err != nil {
return nil, err
return nil, nil, err
}
eventStore := &activity.InMemoryEventStore{}
return BuildManager(store, NewPeersUpdateManager(nil), nil, "", "netbird.cloud", eventStore, nil, false, MocIntegratedValidator{})

manager, err := BuildManager(store, NewPeersUpdateManager(nil), nil, "", "netbird.cloud", eventStore, nil, false, MocIntegratedValidator{})
if err != nil {
return nil, nil, err
}

return manager, func() { _ = store.Close() }, nil
}

func createStore(t *testing.T) (Store, error) {
Expand Down
3 changes: 2 additions & 1 deletion management/server/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ func generateAndStoreEvents(t *testing.T, manager *DefaultAccountManager, typ ac
}

func TestDefaultAccountManager_GetEvents(t *testing.T) {
manager, err := createManager(t)
manager, cleanUp, err := createManager(t)
if err != nil {
return
}
defer cleanUp()

accountID := "accountID"

Expand Down
6 changes: 6 additions & 0 deletions management/server/file_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestStalePeerIndices(t *testing.T) {

func TestNewStore(t *testing.T) {
store := newStore(t)
defer store.Close()

if store.Accounts == nil || len(store.Accounts) != 0 {
t.Errorf("expected to create a new empty Accounts map when creating a new FileStore")
Expand Down Expand Up @@ -87,6 +88,7 @@ func TestNewStore(t *testing.T) {

func TestSaveAccount(t *testing.T) {
store := newStore(t)
defer store.Close()

account := newAccountWithId("account_id", "testuser", "")
setupKey := GenerateDefaultSetupKey()
Expand Down Expand Up @@ -135,6 +137,8 @@ func TestDeleteAccount(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer store.Close()

var account *Account
for _, a := range store.Accounts {
account = a
Expand Down Expand Up @@ -179,6 +183,7 @@ func TestDeleteAccount(t *testing.T) {

func TestStore(t *testing.T) {
store := newStore(t)
defer store.Close()

account := newAccountWithId("account_id", "testuser", "")
account.Peers["testpeer"] = &nbpeer.Peer{
Expand Down Expand Up @@ -436,6 +441,7 @@ func TestFileStore_GetTokenIDByHashedToken(t *testing.T) {

func TestFileStore_DeleteHashedPAT2TokenIDIndex(t *testing.T) {
store := newStore(t)
defer store.Close()
store.HashedPAT2TokenID["someHashedToken"] = "someTokenId"

err := store.DeleteHashedPAT2TokenIDIndex("someHashedToken")
Expand Down
6 changes: 4 additions & 2 deletions management/server/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ const (
)

func TestDefaultAccountManager_CreateGroup(t *testing.T) {
am, err := createManager(t)
am, cleanUp, err := createManager(t)
if err != nil {
t.Error("failed to create account manager")
}
defer cleanUp()

account, err := initTestGroupAccount(am)
if err != nil {
Expand Down Expand Up @@ -50,10 +51,11 @@ func TestDefaultAccountManager_CreateGroup(t *testing.T) {
}

func TestDefaultAccountManager_DeleteGroup(t *testing.T) {
am, err := createManager(t)
am, cleanUp, err := createManager(t)
if err != nil {
t.Error("failed to create account manager")
}
defer cleanUp()

account, err := initTestGroupAccount(am)
if err != nil {
Expand Down
Loading

0 comments on commit 0e99d34

Please sign in to comment.