mirror of
https://github.com/netbirdio/netbird.git
synced 2026-09-21 06:09:07 +02:00
fixes + tests
Signed-off-by: Dmitri Dolguikh <dmitri.external@netbird.io>
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
insert into accounts (id) VALUES('account-1');
|
||||
insert into groups (id, account_id, name, resources, public_id) VALUES('group-one-resource-id','account-1','group-1-name', '[{"ID":"host-id-1","Type":"host"}]','group-one-resource-id-public');
|
||||
insert into groups (id, account_id, name, resources, public_id) VALUES('group-two-resources-id','account-1','group-2-name', '[{"ID":"subnet-id-1","Type":"subnet"}, {"ID":"host-id-2","Type":"host"}]','group-two-resources-id-public');
|
||||
insert into groups (id, account_id, name, resources, public_id) VALUES('group-no-resources-id','account-1','group-3-name', null,'group-no-resources-id-public');
|
||||
insert into group_peers (account_id, peer_id, group_id) VALUES('account-1','peer-id-1','group-one-resource-id');
|
||||
insert into group_peers (account_id, peer_id, group_id) VALUES('account-1','peer-id-2','group-two-resources-id');
|
||||
insert into group_peers (account_id, peer_id, group_id) VALUES('account-1','peer-id-3','group-two-resources-id');
|
||||
@@ -16,43 +16,24 @@ func TestGetGroups(t *testing.T) {
|
||||
s, err := networkmap_pgsql.NewPostgresqlStore(ctx, dsn)
|
||||
assert.NoError(t, err)
|
||||
|
||||
acctId := xid.New().String()
|
||||
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into accounts (id) VALUES($1)", acctId)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into groups (id, account_id, name, resources, public_id) VALUES('g1-test-group-id-1',$1,'test-group-1', '[{\"ID\":\"host-id-1\",\"Type\":\"host\"}]','public-id-1')", acctId)
|
||||
assert.NoError(t, err)
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into groups (id, account_id, name, resources, public_id) VALUES('g1-test-group-id-2',$1,'test-group-2', '[{\"ID\":\"subnet-id-1\",\"Type\":\"subnet\"}, {\"ID\":\"host-id-2\",\"Type\":\"host\"}]','public-id-2')", acctId)
|
||||
assert.NoError(t, err)
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into group_peers (peer_id, group_id) VALUES('peer-id-1','g1-test-group-id-1')")
|
||||
assert.NoError(t, err)
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into group_peers (peer_id, group_id) VALUES('peer-id-2','g1-test-group-id-2')")
|
||||
assert.NoError(t, err)
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into group_peers (peer_id, group_id) VALUES('peer-id-3','g1-test-group-id-2')")
|
||||
assert.NoError(t, err)
|
||||
|
||||
groups, resourceToGroupIdx, err := s.GetGroups(ctx, acctId)
|
||||
groups, resourceToGroupIdx, err := s.GetGroups(ctx, "account-1")
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t,
|
||||
groups,
|
||||
nmdata.Group{ID: "g1-test-group-id-1", Name: "test-group-1", PublicID: "public-id-1", Resources: []nmdata.Resource{{ID: "host-id-1", Type: "host"}}, Peers: []string{"peer-id-1"}},
|
||||
nmdata.Group{ID: "group-one-resource-id", Name: "group-1-name", PublicID: "group-one-resource-id-public", Resources: []nmdata.Resource{{ID: "host-id-1", Type: "host"}}, Peers: []string{"peer-id-1"}},
|
||||
)
|
||||
assert.NotNil(t, resourceToGroupIdx["host-id-1"]["g1-test-group-id-1"])
|
||||
assert.NotNil(t, resourceToGroupIdx["host-id-1"]["group-one-resource-id"])
|
||||
assert.Contains(t,
|
||||
groups,
|
||||
nmdata.Group{ID: "g1-test-group-id-2", Name: "test-group-2", PublicID: "public-id-2",
|
||||
nmdata.Group{ID: "group-two-resources-id", Name: "group-2-name", PublicID: "group-two-resources-id-public",
|
||||
Resources: []nmdata.Resource{{ID: "subnet-id-1", Type: "subnet"}, {ID: "host-id-2", Type: "host"}},
|
||||
Peers: []string{"peer-id-2", "peer-id-3"}},
|
||||
)
|
||||
assert.NotNil(t, resourceToGroupIdx["host-id-2"]["g1-test-group-id-2"])
|
||||
assert.NotNil(t, resourceToGroupIdx["subnet-id-1"]["g1-test-group-id-2"])
|
||||
assert.NotNil(t, resourceToGroupIdx["host-id-2"]["group-two-resources-id"])
|
||||
assert.NotNil(t, resourceToGroupIdx["subnet-id-1"]["group-two-resources-id"])
|
||||
assert.Contains(t,
|
||||
groups,
|
||||
nmdata.Group{ID: "group-no-resources-id", Name: "group-3-name", PublicID: "group-no-resources-id-public"})
|
||||
}
|
||||
|
||||
// Verify handling of empty fields in groups table
|
||||
|
||||
@@ -2,6 +2,7 @@ package networkmap_pgsql
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
@@ -15,10 +16,14 @@ import (
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
|
||||
networkmap_pgsql "github.com/netbirdio/netbird/management/internals/network_map_db/pgsql"
|
||||
gormstore "github.com/netbirdio/netbird/management/server/store"
|
||||
"github.com/netbirdio/netbird/management/server/testutil"
|
||||
)
|
||||
|
||||
//go:embed base_data.sql
|
||||
var baseData string
|
||||
|
||||
var dsn string
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
@@ -59,6 +64,18 @@ func TestMain(m *testing.M) {
|
||||
log.Fatalf("error running migrations %v", err)
|
||||
}
|
||||
|
||||
ctx := context.TODO()
|
||||
s, err := networkmap_pgsql.NewPostgresqlStore(ctx, dsn)
|
||||
if err != nil {
|
||||
log.Fatal("error creating postgres store %w", err)
|
||||
}
|
||||
|
||||
for _, query := range strings.Split(baseData, ";") {
|
||||
if _, err := s.Pool.Exec(ctx, query); err != nil {
|
||||
log.Fatalf("error initializing db: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
code := m.Run()
|
||||
|
||||
cleanup()
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
networkmap_pgsql "github.com/netbirdio/netbird/management/internals/network_map_db/pgsql"
|
||||
"github.com/netbirdio/netbird/shared/management/networkmap/nmdata"
|
||||
"github.com/rs/xid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -16,42 +15,21 @@ func TestGetNetworkRouters(t *testing.T) {
|
||||
s, err := networkmap_pgsql.NewPostgresqlStore(ctx, dsn)
|
||||
assert.NoError(t, err)
|
||||
|
||||
acctId := xid.New().String()
|
||||
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into accounts (id) VALUES($1)", acctId)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = s.Pool.Query(ctx,
|
||||
`insert into network_routers (id, account_id, public_id, peer, network_id, masquerade, metric, enabled, peer_groups)
|
||||
VALUES('test-nr-id-1',$1,'public-id-1','peer-id-1','network-id-1',TRUE,999,TRUE,'["nr-test-group-id-1"]')`,
|
||||
acctId)
|
||||
VALUES('test-nr-id-1','account-1','public-id-1','peer-id-1','network-id-1',TRUE,999,TRUE,'["group-one-resource-id"]')`)
|
||||
_, err = s.Pool.Query(ctx,
|
||||
`insert into network_routers (id, account_id, public_id, peer, network_id, masquerade, metric, enabled, peer_groups)
|
||||
VALUES('test-nr-id-2',$1,'public-id-2','','network-id-2',TRUE,333,TRUE,'["nr-test-group-id-1","nr-test-group-id-2"]')`, acctId)
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into groups (id, account_id, public_id) VALUES('nr-test-group-id-1',$1,'public-id-1')", acctId)
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into groups (id, account_id, public_id) VALUES('nr-test-group-id-2',$1,'public-id-2')", acctId)
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into group_peers (peer_id, group_id) VALUES('peer-id-11','nr-test-group-id-1')")
|
||||
assert.NoError(t, err)
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into group_peers (peer_id, group_id) VALUES('peer-id-22','nr-test-group-id-2')")
|
||||
assert.NoError(t, err)
|
||||
_, err = s.Pool.Query(ctx,
|
||||
"insert into group_peers (peer_id, group_id) VALUES('peer-id-33','nr-test-group-id-2')")
|
||||
assert.NoError(t, err)
|
||||
VALUES('test-nr-id-2','account-1','public-id-2','','network-id-2',TRUE,333,TRUE,'["group-two-resources-id","group-no-resources-id"]')`)
|
||||
|
||||
routers, err := s.GetNetworkRouters(ctx, acctId)
|
||||
routers, err := s.GetNetworkRouters(ctx, "account-1")
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, routers)
|
||||
|
||||
assert.Equal(t, routers["network-id-1"],
|
||||
map[string]*nmdata.NetworkRouter{"peer-id-1": {PublicID: "public-id-1", Masquerade: true, Metric: 999, Enabled: true, PeerGroups: []string{"peer-id-11"}}})
|
||||
map[string]*nmdata.NetworkRouter{"peer-id-1": {PublicID: "public-id-1", Masquerade: true, Metric: 999, Enabled: true, PeerGroups: []string{"group-one-resource-id"}}})
|
||||
assert.Equal(t, routers["network-id-2"],
|
||||
map[string]*nmdata.NetworkRouter{
|
||||
"peer-id-11": {PublicID: "public-id-2", Masquerade: true, Metric: 333, Enabled: true, PeerGroups: []string{"peer-id-11", "peer-id-22", "peer-id-33"}},
|
||||
"peer-id-22": {PublicID: "public-id-2", Masquerade: true, Metric: 333, Enabled: true, PeerGroups: []string{"peer-id-11", "peer-id-22", "peer-id-33"}},
|
||||
"peer-id-33": {PublicID: "public-id-2", Masquerade: true, Metric: 333, Enabled: true, PeerGroups: []string{"peer-id-11", "peer-id-22", "peer-id-33"}}})
|
||||
"peer-id-2": {PublicID: "public-id-2", Masquerade: true, Metric: 333, Enabled: true, PeerGroups: []string{"group-two-resources-id", "group-no-resources-id"}},
|
||||
"peer-id-3": {PublicID: "public-id-2", Masquerade: true, Metric: 333, Enabled: true, PeerGroups: []string{"group-two-resources-id", "group-no-resources-id"}}})
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ const (
|
||||
(
|
||||
select array_agg(group_peers.peer_id)
|
||||
from group_peers
|
||||
where group_peers.group_id = groups.id
|
||||
where group_peers.group_id = groups.id and group_peers.account_id=$1
|
||||
) as peers
|
||||
from groups where account_id=$1
|
||||
`
|
||||
|
||||
@@ -3,6 +3,7 @@ package networkmap_pgsql
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
@@ -13,11 +14,11 @@ import (
|
||||
|
||||
const (
|
||||
GetNetworkRouterQuery = `
|
||||
select public_id, peer, network_id, masquerade, metric, enabled,
|
||||
select public_id, peer, network_id, masquerade, metric, enabled, peer_groups,
|
||||
(
|
||||
select array_agg(group_peers.peer_id)
|
||||
from group_peers
|
||||
where group_peers.group_id in (select json_array_elements_text(peer_groups::json))
|
||||
where group_peers.account_id=$1 and group_peers.group_id in (select json_array_elements_text(peer_groups::json))
|
||||
) as peers_via_groups
|
||||
from network_routers
|
||||
where account_id=$1
|
||||
@@ -79,7 +80,8 @@ type networkrouter struct {
|
||||
PublicID sql.NullString
|
||||
NetworkID sql.NullString `nmap:"skip"`
|
||||
Peer sql.NullString `nmap:"skip"`
|
||||
PeersViaGroups []string `nmap:"map_to:PeerGroups"`
|
||||
PeerGroups json.RawMessage
|
||||
PeersViaGroups []string `nmap:"skip"`
|
||||
Masquerade sql.NullBool
|
||||
Metric sql.NullInt64
|
||||
Enabled sql.NullBool
|
||||
|
||||
Reference in New Issue
Block a user