diff --git a/management/server/account_test.go b/management/server/account_test.go index 0b2992760db..be719381f1e 100644 --- a/management/server/account_test.go +++ b/management/server/account_test.go @@ -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 { @@ -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") @@ -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) @@ -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, "") @@ -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" @@ -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" @@ -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" @@ -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" @@ -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") @@ -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 { @@ -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" @@ -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 { @@ -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" @@ -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") @@ -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") @@ -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") @@ -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") @@ -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") @@ -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) { diff --git a/management/server/event_test.go b/management/server/event_test.go index 401c80759c6..9098fc8320b 100644 --- a/management/server/event_test.go +++ b/management/server/event_test.go @@ -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" diff --git a/management/server/file_store_test.go b/management/server/file_store_test.go index d53298d8fa3..11571b0be6d 100644 --- a/management/server/file_store_test.go +++ b/management/server/file_store_test.go @@ -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") @@ -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() @@ -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 @@ -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{ @@ -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") diff --git a/management/server/group_test.go b/management/server/group_test.go index 35e9b2170eb..0474409f15c 100644 --- a/management/server/group_test.go +++ b/management/server/group_test.go @@ -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 { @@ -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 { diff --git a/management/server/peer_test.go b/management/server/peer_test.go index 6063cc2a742..d8d4c9acb01 100644 --- a/management/server/peer_test.go +++ b/management/server/peer_test.go @@ -67,11 +67,12 @@ func TestPeer_LoginExpired(t *testing.T) { } func TestAccountManager_GetNetworkMap(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" @@ -139,11 +140,12 @@ func TestAccountManager_GetNetworkMap(t *testing.T) { func TestAccountManager_GetNetworkMapWithPolicy(t *testing.T) { // TODO: disable until we start use policy again t.Skip() - manager, err := createManager(t) + manager, cleanUp, err := createManager(t) if err != nil { t.Fatal(err) return } + defer cleanUp() expectedID := "test_account" userID := "account_creator" @@ -316,11 +318,12 @@ func TestAccountManager_GetNetworkMapWithPolicy(t *testing.T) { } func TestAccountManager_GetPeerNetwork(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" @@ -377,11 +380,12 @@ func TestAccountManager_GetPeerNetwork(t *testing.T) { } func TestDefaultAccountManager_GetPeer(t *testing.T) { - manager, err := createManager(t) + manager, cleanUp, err := createManager(t) if err != nil { t.Fatal(err) return } + defer cleanUp() // account with an admin and a regular user accountID := "test_account" @@ -564,11 +568,12 @@ func TestDefaultAccountManager_GetPeers(t *testing.T) { } for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { - manager, err := createManager(t) + manager, cleanUp, err := createManager(t) if err != nil { t.Fatal(err) return } + defer cleanUp() // account with an admin and a regular user accountID := "test_account" diff --git a/management/server/posture_checks_test.go b/management/server/posture_checks_test.go index a65cb8c53e6..bf55f72c108 100644 --- a/management/server/posture_checks_test.go +++ b/management/server/posture_checks_test.go @@ -3,8 +3,9 @@ package server import ( "testing" - "github.com/netbirdio/netbird/management/server/posture" "github.com/stretchr/testify/assert" + + "github.com/netbirdio/netbird/management/server/posture" ) const ( @@ -15,10 +16,11 @@ const ( ) func TestDefaultAccountManager_PostureCheck(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 := initTestPostureChecksAccount(am) if err != nil { diff --git a/management/server/setupkey_test.go b/management/server/setupkey_test.go index 43edabbd6e2..962f9cba262 100644 --- a/management/server/setupkey_test.go +++ b/management/server/setupkey_test.go @@ -14,10 +14,11 @@ import ( ) func TestDefaultAccountManager_SaveSetupKey(t *testing.T) { - manager, err := createManager(t) + manager, cleanUp, err := createManager(t) if err != nil { t.Fatal(err) } + defer cleanUp() userID := "testingUser" account, err := manager.GetOrCreateAccountByUser(userID, "") @@ -72,10 +73,11 @@ func TestDefaultAccountManager_SaveSetupKey(t *testing.T) { } func TestDefaultAccountManager_CreateSetupKey(t *testing.T) { - manager, err := createManager(t) + manager, cleanUp, err := createManager(t) if err != nil { t.Fatal(err) } + defer cleanUp() userID := "testingUser" account, err := manager.GetOrCreateAccountByUser(userID, "") @@ -168,10 +170,11 @@ func TestDefaultAccountManager_CreateSetupKey(t *testing.T) { } func TestGetSetupKeys(t *testing.T) { - manager, err := createManager(t) + manager, cleanUp, err := createManager(t) if err != nil { t.Fatal(err) } + defer cleanUp() userID := "testingUser" account, err := manager.GetOrCreateAccountByUser(userID, "") diff --git a/management/server/user_test.go b/management/server/user_test.go index c92f87e6c7f..c518bf6b7be 100644 --- a/management/server/user_test.go +++ b/management/server/user_test.go @@ -39,6 +39,7 @@ const ( func TestUser_CreatePAT_ForSameUser(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") err := store.SaveAccount(account) @@ -76,6 +77,7 @@ func TestUser_CreatePAT_ForSameUser(t *testing.T) { func TestUser_CreatePAT_ForDifferentUser(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") account.Users[mockTargetUserId] = &User{ Id: mockTargetUserId, @@ -97,6 +99,7 @@ func TestUser_CreatePAT_ForDifferentUser(t *testing.T) { func TestUser_CreatePAT_ForServiceUser(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") account.Users[mockTargetUserId] = &User{ Id: mockTargetUserId, @@ -122,6 +125,7 @@ func TestUser_CreatePAT_ForServiceUser(t *testing.T) { func TestUser_CreatePAT_WithWrongExpiration(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") err := store.SaveAccount(account) @@ -140,6 +144,7 @@ func TestUser_CreatePAT_WithWrongExpiration(t *testing.T) { func TestUser_CreatePAT_WithEmptyName(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") err := store.SaveAccount(account) @@ -158,6 +163,7 @@ func TestUser_CreatePAT_WithEmptyName(t *testing.T) { func TestUser_DeletePAT(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") account.Users[mockUserID] = &User{ Id: mockUserID, @@ -190,6 +196,7 @@ func TestUser_DeletePAT(t *testing.T) { func TestUser_GetPAT(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") account.Users[mockUserID] = &User{ Id: mockUserID, @@ -221,6 +228,7 @@ func TestUser_GetPAT(t *testing.T) { func TestUser_GetAllPATs(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") account.Users[mockUserID] = &User{ Id: mockUserID, @@ -322,6 +330,7 @@ func validateStruct(s interface{}) (err error) { func TestUser_CreateServiceUser(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") err := store.SaveAccount(account) @@ -359,6 +368,7 @@ func TestUser_CreateServiceUser(t *testing.T) { func TestUser_CreateUser_ServiceUser(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") err := store.SaveAccount(account) @@ -397,6 +407,7 @@ func TestUser_CreateUser_ServiceUser(t *testing.T) { func TestUser_CreateUser_RegularUser(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") err := store.SaveAccount(account) @@ -421,6 +432,7 @@ func TestUser_CreateUser_RegularUser(t *testing.T) { func TestUser_InviteNewUser(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") err := store.SaveAccount(account) @@ -549,6 +561,7 @@ func TestUser_DeleteUser_ServiceUser(t *testing.T) { func TestUser_DeleteUser_SelfDelete(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") err := store.SaveAccount(account) @@ -569,6 +582,7 @@ func TestUser_DeleteUser_SelfDelete(t *testing.T) { func TestUser_DeleteUser_regularUser(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") targetId := "user2" @@ -650,6 +664,7 @@ func TestUser_DeleteUser_regularUser(t *testing.T) { func TestDefaultAccountManager_GetUser(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") err := store.SaveAccount(account) @@ -678,6 +693,7 @@ func TestDefaultAccountManager_GetUser(t *testing.T) { func TestDefaultAccountManager_ListUsers(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") account.Users["normal_user1"] = NewRegularUser("normal_user1") account.Users["normal_user2"] = NewRegularUser("normal_user2") @@ -790,6 +806,7 @@ func TestDefaultAccountManager_ListUsers_DashboardPermissions(t *testing.T) { func TestDefaultAccountManager_ExternalCache(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") externalUser := &User{ Id: "externalUser", @@ -853,6 +870,7 @@ func TestUser_IsAdmin(t *testing.T) { func TestUser_GetUsersFromAccount_ForAdmin(t *testing.T) { store := newStore(t) + defer store.Close() account := newAccountWithId(mockAccountID, mockUserID, "") account.Users[mockServiceUserID] = &User{ Id: mockServiceUserID, @@ -880,6 +898,8 @@ func TestUser_GetUsersFromAccount_ForAdmin(t *testing.T) { func TestUser_GetUsersFromAccount_ForUser(t *testing.T) { store := newStore(t) + defer store.Close() + account := newAccountWithId(mockAccountID, mockUserID, "") account.Users[mockServiceUserID] = &User{ Id: mockServiceUserID, @@ -907,11 +927,12 @@ func TestUser_GetUsersFromAccount_ForUser(t *testing.T) { } func TestDefaultAccountManager_SaveUser(t *testing.T) { - manager, err := createManager(t) + manager, cleanUp, err := createManager(t) if err != nil { t.Fatal(err) return } + defer cleanUp() regularUserID := "regularUser" serviceUserID := "serviceUser"