Fix tests

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
bcmmbaga
2024-11-26 20:15:23 +03:00
parent accada3311
commit de99624610
15 changed files with 1057 additions and 1008 deletions

View File

@@ -68,20 +68,27 @@ func TestSqlite_SaveAccount_Large(t *testing.T) {
func runLargeTest(t *testing.T, store Store) {
t.Helper()
account := newAccountWithId(context.Background(), "account_id", "testuser", "")
groupALL, err := account.GetGroupAll()
if err != nil {
t.Fatal(err)
}
accountID := "account_id"
err := newAccountWithId(context.Background(), store, accountID, "testuser", "")
require.NoError(t, err, "failed to create account")
groupAll, err := store.GetGroupByName(context.Background(), LockingStrengthShare, accountID, "All")
require.NoError(t, err, "failed to get group All")
setupKey, _ := GenerateDefaultSetupKey()
account.SetupKeys[setupKey.Key] = setupKey
setupKey.AccountID = accountID
err = store.SaveSetupKey(context.Background(), LockingStrengthUpdate, setupKey)
assert.NoError(t, err, "failed to save setup key")
const numPerAccount = 6000
for n := 0; n < numPerAccount; n++ {
netIP := randomIPv4()
peerID := fmt.Sprintf("%s-peer-%d", account.Id, n)
peerID := fmt.Sprintf("%s-peer-%d", accountID, n)
peer := &nbpeer.Peer{
ID: peerID,
AccountID: accountID,
Key: peerID,
IP: netIP,
Name: peerID,
@@ -90,16 +97,21 @@ func runLargeTest(t *testing.T, store Store) {
Status: &nbpeer.PeerStatus{Connected: false, LastSeen: time.Now()},
SSHEnabled: false,
}
account.Peers[peerID] = peer
group, _ := account.GetGroupAll()
group.Peers = append(group.Peers, peerID)
user := &User{
Id: fmt.Sprintf("%s-user-%d", account.Id, n),
AccountID: account.Id,
}
account.Users[user.Id] = user
err = store.AddPeerToAccount(context.Background(), peer)
require.NoError(t, err, "failed to add peer")
err = store.AddPeerToAllGroup(context.Background(), accountID, peerID)
require.NoError(t, err, "failed to add peer to all group")
err = store.SaveUser(context.Background(), LockingStrengthUpdate, &User{
Id: fmt.Sprintf("%s-user-%d", accountID, n),
AccountID: accountID,
})
require.NoError(t, err, "failed to save user")
route := &route2.Route{
ID: route2.ID(fmt.Sprintf("network-id-%d", n)),
AccountID: accountID,
Description: "base route",
NetID: route2.NetID(fmt.Sprintf("network-id-%d", n)),
Network: netip.MustParsePrefix(netIP.String() + "/24"),
@@ -107,22 +119,24 @@ func runLargeTest(t *testing.T, store Store) {
Metric: 9999,
Masquerade: false,
Enabled: true,
Groups: []string{groupALL.ID},
Groups: []string{groupAll.ID},
}
account.Routes[route.ID] = route
err = store.SaveRoute(context.Background(), LockingStrengthUpdate, route)
require.NoError(t, err, "failed to save route")
group = &nbgroup.Group{
group := &nbgroup.Group{
ID: fmt.Sprintf("group-id-%d", n),
AccountID: account.Id,
AccountID: accountID,
Name: fmt.Sprintf("group-id-%d", n),
Issued: "api",
Peers: nil,
}
account.Groups[group.ID] = group
err = store.SaveGroup(context.Background(), LockingStrengthUpdate, group)
require.NoError(t, err, "failed to save group")
nameserver := &nbdns.NameServerGroup{
ID: fmt.Sprintf("nameserver-id-%d", n),
AccountID: account.Id,
AccountID: accountID,
Name: fmt.Sprintf("nameserver-id-%d", n),
Description: "",
NameServers: []nbdns.NameServer{{IP: netip.MustParseAddr(netIP.String()), NSType: nbdns.UDPNameServerType}},
@@ -132,20 +146,20 @@ func runLargeTest(t *testing.T, store Store) {
Enabled: false,
SearchDomainsEnabled: false,
}
account.NameServerGroups[nameserver.ID] = nameserver
err = store.SaveNameServerGroup(context.Background(), LockingStrengthUpdate, nameserver)
require.NoError(t, err, "failed to save nameserver group")
setupKey, _ := GenerateDefaultSetupKey()
account.SetupKeys[setupKey.Key] = setupKey
setupKey, _ = GenerateDefaultSetupKey()
setupKey.AccountID = accountID
err = store.SaveSetupKey(context.Background(), LockingStrengthUpdate, setupKey)
require.NoError(t, err, "failed to save setup key")
}
err = store.SaveAccount(context.Background(), account)
totalAccounts, err := store.GetTotalAccounts(context.Background())
require.NoError(t, err)
require.Equal(t, int64(1), totalAccounts, "expected 1 account")
if len(store.GetAllAccounts(context.Background())) != 1 {
t.Errorf("expecting 1 Accounts to be stored after SaveAccount()")
}
a, err := store.GetAccount(context.Background(), account.Id)
a, err := store.GetAccount(context.Background(), accountID)
if a == nil {
t.Errorf("expecting Account to be stored after SaveAccount(): %v", err)
}
@@ -213,41 +227,53 @@ func TestSqlite_SaveAccount(t *testing.T) {
t.Setenv("NETBIRD_STORE_ENGINE", string(SqliteStoreEngine))
store, cleanUp, err := NewTestStoreFromSQL(context.Background(), "", t.TempDir())
t.Cleanup(cleanUp)
assert.NoError(t, err)
require.NoError(t, err)
accountID := "account_id"
err = newAccountWithId(context.Background(), store, accountID, "testuser", "")
require.NoError(t, err, "failed to create account")
account := newAccountWithId(context.Background(), "account_id", "testuser", "")
setupKey, _ := GenerateDefaultSetupKey()
account.SetupKeys[setupKey.Key] = setupKey
account.Peers["testpeer"] = &nbpeer.Peer{
Key: "peerkey",
IP: net.IP{127, 0, 0, 1},
Meta: nbpeer.PeerSystemMeta{},
Name: "peer name",
Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()},
}
setupKey.AccountID = accountID
err = store.SaveSetupKey(context.Background(), LockingStrengthUpdate, setupKey)
require.NoError(t, err, "failed to save setup key")
err = store.SaveAccount(context.Background(), account)
require.NoError(t, err)
err = store.AddPeerToAccount(context.Background(), &nbpeer.Peer{
ID: "testpeer",
Key: "peerkey",
IP: net.IP{127, 0, 0, 1},
AccountID: accountID,
Meta: nbpeer.PeerSystemMeta{},
Name: "peer name",
Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()},
})
require.NoError(t, err, "failed to save peer")
accountID2 := "account_id2"
err = newAccountWithId(context.Background(), store, accountID2, "testuser2", "")
require.NoError(t, err, "failed to create account")
account2 := newAccountWithId(context.Background(), "account_id2", "testuser2", "")
setupKey, _ = GenerateDefaultSetupKey()
account2.SetupKeys[setupKey.Key] = setupKey
account2.Peers["testpeer2"] = &nbpeer.Peer{
Key: "peerkey2",
IP: net.IP{127, 0, 0, 2},
Meta: nbpeer.PeerSystemMeta{},
Name: "peer name 2",
Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()},
}
setupKey.AccountID = accountID2
err = store.SaveSetupKey(context.Background(), LockingStrengthUpdate, setupKey)
require.NoError(t, err, "failed to save setup key")
err = store.SaveAccount(context.Background(), account2)
require.NoError(t, err)
err = store.AddPeerToAccount(context.Background(), &nbpeer.Peer{
ID: "testpeer2",
Key: "peerkey2",
AccountID: accountID2,
IP: net.IP{127, 0, 0, 2},
Meta: nbpeer.PeerSystemMeta{},
Name: "peer name 2",
Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()},
})
require.NoError(t, err, "failed to save peer")
if len(store.GetAllAccounts(context.Background())) != 2 {
t.Errorf("expecting 2 Accounts to be stored after SaveAccount()")
}
a, err := store.GetAccount(context.Background(), account.Id)
a, err := store.GetAccount(context.Background(), accountID)
if a == nil {
t.Errorf("expecting Account to be stored after SaveAccount(): %v", err)
}
@@ -295,7 +321,12 @@ func TestSqlite_DeleteAccount(t *testing.T) {
Name: "test token",
}}
account := newAccountWithId(context.Background(), "account_id", testUserID, "")
err = newAccountWithId(context.Background(), store, "account_id", testUserID, "")
require.NoError(t, err, "failed to create account")
account, err := store.GetAccount(context.Background(), "account_id")
require.NoError(t, err, "failed to get account")
setupKey, _ := GenerateDefaultSetupKey()
account.SetupKeys[setupKey.Key] = setupKey
account.Peers["testpeer"] = &nbpeer.Peer{
@@ -685,19 +716,29 @@ func newSqliteStore(t *testing.T) *SqlStore {
}
func newAccount(store Store, id int) error {
str := fmt.Sprintf("%s-%d", uuid.New().String(), id)
account := newAccountWithId(context.Background(), str, str+"-testuser", "example.com")
accountID := fmt.Sprintf("%s-%d", uuid.New().String(), id)
userID := accountID + "-testuser"
err := newAccountWithId(context.Background(), store, accountID, userID, "example.com")
if err != nil {
return err
}
setupKey, _ := GenerateDefaultSetupKey()
account.SetupKeys[setupKey.Key] = setupKey
account.Peers["p"+str] = &nbpeer.Peer{
Key: "peerkey" + str,
setupKey.AccountID = accountID
err = store.SaveSetupKey(context.Background(), LockingStrengthUpdate, setupKey)
if err != nil {
return err
}
return store.SavePeer(context.Background(), LockingStrengthUpdate, accountID, &nbpeer.Peer{
ID: "p" + accountID,
Key: accountID + "-peerkey",
IP: net.IP{127, 0, 0, 1},
Meta: nbpeer.PeerSystemMeta{},
Name: "peer name",
Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()},
}
return store.SaveAccount(context.Background(), account)
})
}
func TestPostgresql_NewStore(t *testing.T) {
@@ -725,39 +766,53 @@ func TestPostgresql_SaveAccount(t *testing.T) {
t.Cleanup(cleanUp)
assert.NoError(t, err)
account := newAccountWithId(context.Background(), "account_id", "testuser", "")
accountID := "account_id"
err = newAccountWithId(context.Background(), store, accountID, "testuser", "")
require.NoError(t, err, "failed to create account")
setupKey, _ := GenerateDefaultSetupKey()
account.SetupKeys[setupKey.Key] = setupKey
account.Peers["testpeer"] = &nbpeer.Peer{
Key: "peerkey",
IP: net.IP{127, 0, 0, 1},
Meta: nbpeer.PeerSystemMeta{},
Name: "peer name",
Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()},
}
setupKey.AccountID = accountID
err = store.SaveSetupKey(context.Background(), LockingStrengthUpdate, setupKey)
require.NoError(t, err, "failed to save setup key")
err = store.SaveAccount(context.Background(), account)
require.NoError(t, err)
err = store.AddPeerToAccount(context.Background(), &nbpeer.Peer{
ID: "testpeer",
Key: "peerkey",
IP: net.IP{127, 0, 0, 1},
AccountID: accountID,
Meta: nbpeer.PeerSystemMeta{},
Name: "peer name",
Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()},
})
require.NoError(t, err, "failed to save peer")
accountID2 := "account_id2"
err = newAccountWithId(context.Background(), store, accountID2, "testuser2", "")
require.NoError(t, err, "failed to create account")
account2 := newAccountWithId(context.Background(), "account_id2", "testuser2", "")
setupKey, _ = GenerateDefaultSetupKey()
account2.SetupKeys[setupKey.Key] = setupKey
account2.Peers["testpeer2"] = &nbpeer.Peer{
Key: "peerkey2",
IP: net.IP{127, 0, 0, 2},
Meta: nbpeer.PeerSystemMeta{},
Name: "peer name 2",
Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()},
}
setupKey.AccountID = accountID2
err = store.SaveSetupKey(context.Background(), LockingStrengthUpdate, setupKey)
require.NoError(t, err, "failed to save setup key")
err = store.SaveAccount(context.Background(), account2)
require.NoError(t, err)
err = store.AddPeerToAccount(context.Background(), &nbpeer.Peer{
ID: "testpeer2",
Key: "peerkey2",
AccountID: accountID2,
IP: net.IP{127, 0, 0, 2},
Meta: nbpeer.PeerSystemMeta{},
Name: "peer name 2",
Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()},
})
require.NoError(t, err, "failed to save peer")
if len(store.GetAllAccounts(context.Background())) != 2 {
t.Errorf("expecting 2 Accounts to be stored after SaveAccount()")
}
totalAccounts, err := store.GetTotalAccounts(context.Background())
require.NoError(t, err, "failed to get total accounts")
require.Equal(t, int64(2), totalAccounts, "expecting 2 Accounts to be stored after SaveAccount()")
a, err := store.GetAccount(context.Background(), account.Id)
a, err := store.GetAccount(context.Background(), accountID)
if a == nil {
t.Errorf("expecting Account to be stored after SaveAccount(): %v", err)
}
@@ -798,31 +853,41 @@ func TestPostgresql_DeleteAccount(t *testing.T) {
t.Cleanup(cleanUp)
assert.NoError(t, err)
accountID := "account_id"
testUserID := "testuser"
user := NewAdminUser(testUserID)
user.PATs = map[string]*PersonalAccessToken{"testtoken": {
ID: "testtoken",
Name: "test token",
}}
account := newAccountWithId(context.Background(), "account_id", testUserID, "")
err = newAccountWithId(context.Background(), store, accountID, testUserID, "")
require.NoError(t, err, "failed to create account")
setupKey, _ := GenerateDefaultSetupKey()
account.SetupKeys[setupKey.Key] = setupKey
account.Peers["testpeer"] = &nbpeer.Peer{
Key: "peerkey",
IP: net.IP{127, 0, 0, 1},
Meta: nbpeer.PeerSystemMeta{},
Name: "peer name",
Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()},
}
account.Users[testUserID] = user
setupKey.AccountID = accountID
err = store.SaveSetupKey(context.Background(), LockingStrengthUpdate, setupKey)
require.NoError(t, err, "failed to save setup key")
err = store.SaveAccount(context.Background(), account)
require.NoError(t, err)
err = store.AddPeerToAccount(context.Background(), &nbpeer.Peer{
ID: "testingpeer",
AccountID: accountID,
Key: "peerkey",
IP: net.IP{127, 0, 0, 1},
Meta: nbpeer.PeerSystemMeta{},
Name: "peer name",
Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()},
})
require.NoError(t, err, "failed to save peer")
if len(store.GetAllAccounts(context.Background())) != 1 {
t.Errorf("expecting 1 Accounts to be stored after SaveAccount()")
}
err = store.SavePAT(context.Background(), LockingStrengthUpdate, &PersonalAccessToken{
ID: "testtoken",
UserID: testUserID,
Name: "test token",
})
require.NoError(t, err, "failed to save personal access token")
totalAccounts, err := store.GetTotalAccounts(context.Background())
require.NoError(t, err, "failed to get total accounts")
require.Equal(t, int64(1), totalAccounts, "expecting 1 Accounts to be stored after SaveAccount()")
account, err := store.GetAccount(context.Background(), accountID)
require.NoError(t, err, "failed to get account")
err = store.DeleteAccount(context.Background(), account)
require.NoError(t, err)
@@ -1172,7 +1237,7 @@ func TestSqlStore_UpdateAccountDomainAttributes(t *testing.T) {
domain := "example.com"
category := "public"
IsDomainPrimaryAccount := false
err = store.UpdateAccountDomainAttributes(context.Background(), accountID, domain, category, IsDomainPrimaryAccount)
err = store.UpdateAccountDomainAttributes(context.Background(), LockingStrengthUpdate, accountID, domain, category, &IsDomainPrimaryAccount)
require.NoError(t, err)
account, err := store.GetAccount(context.Background(), accountID)
require.NoError(t, err)
@@ -1186,7 +1251,7 @@ func TestSqlStore_UpdateAccountDomainAttributes(t *testing.T) {
domain := "test.com"
category := "private"
IsDomainPrimaryAccount := true
err = store.UpdateAccountDomainAttributes(context.Background(), accountID, domain, category, IsDomainPrimaryAccount)
err = store.UpdateAccountDomainAttributes(context.Background(), LockingStrengthUpdate, accountID, domain, category, &IsDomainPrimaryAccount)
require.NoError(t, err)
account, err := store.GetAccount(context.Background(), accountID)
require.NoError(t, err)
@@ -1200,7 +1265,7 @@ func TestSqlStore_UpdateAccountDomainAttributes(t *testing.T) {
domain := "test.com"
category := "private"
IsDomainPrimaryAccount := true
err = store.UpdateAccountDomainAttributes(context.Background(), "non-existing-account-id", domain, category, IsDomainPrimaryAccount)
err = store.UpdateAccountDomainAttributes(context.Background(), LockingStrengthUpdate, "non-existing-account-id", domain, category, &IsDomainPrimaryAccount)
require.Error(t, err)
})
@@ -2668,7 +2733,7 @@ func TestSqlStore_GetTotalAccounts(t *testing.T) {
t.Cleanup(cleanup)
require.NoError(t, err)
totalAccounts, err := store.GetTotalAccounts(context.Background(), LockingStrengthShare)
totalAccounts, err := store.GetTotalAccounts(context.Background())
require.NoError(t, err)
require.Equal(t, int64(1), totalAccounts)
}