From dbfdd04c7bf94b4d98ca07b2c867d3ce596b61c1 Mon Sep 17 00:00:00 2001 From: Dmitri Dolguikh Date: Tue, 4 Aug 2026 16:43:17 +0200 Subject: [PATCH] adding network-routers integration query test Signed-off-by: Dmitri Dolguikh --- .../network_map_db/pgsql/group_test.go | 55 +++++++++++++++++-- .../pgsql/network_router_test.go | 54 ++++++++++++++++++ 2 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 integration_tests/management/network_map_db/pgsql/network_router_test.go diff --git a/integration_tests/management/network_map_db/pgsql/group_test.go b/integration_tests/management/network_map_db/pgsql/group_test.go index 07d8ecf1a..1714761f8 100644 --- a/integration_tests/management/network_map_db/pgsql/group_test.go +++ b/integration_tests/management/network_map_db/pgsql/group_test.go @@ -6,6 +6,7 @@ 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" ) @@ -15,25 +16,67 @@ 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('account-id-1')") + "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('test-group-id-1','account-id-1','test-group-1', '[{\"ID\":\"host-id-1\",\"Type\":\"host\"}]','public-id-1')") + "insert into groups (id, account_id, name, resources, public_id) VALUES('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('test-group-id-2','account-id-1','test-group-2', '[{\"ID\":\"subnet-id-1\",\"Type\":\"subnet\"}, {\"ID\":\"host-id-2\",\"Type\":\"host\"}]','public-id-2')") + "insert into groups (id, account_id, name, resources, public_id) VALUES('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','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','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','test-group-id-2')") assert.NoError(t, err) - groups, err := s.GetGroups(ctx, "account-id-1") + groups, resourceToGroupIdx, err := s.GetGroups(ctx, acctId) assert.NoError(t, err) assert.Contains(t, groups, - nmdata.Group{Name: "test-group-1", PublicID: "public-id-1", Resources: []nmdata.Resource{{ID: "host-id-1", Type: "host"}}}, + nmdata.Group{ID: "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"}}, ) + assert.NotNil(t, resourceToGroupIdx["host-id-1"]["test-group-id-1"]) assert.Contains(t, groups, - nmdata.Group{Name: "test-group-2", PublicID: "public-id-2", Resources: []nmdata.Resource{{ID: "subnet-id-1", Type: "subnet"}, {ID: "host-id-2", Type: "host"}}}, + nmdata.Group{ID: "test-group-id-2", Name: "test-group-2", PublicID: "public-id-2", + 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"]["test-group-id-2"]) + assert.NotNil(t, resourceToGroupIdx["subnet-id-1"]["test-group-id-2"]) +} + +// Verify handling of empty fields in groups table +// Verify that group's PublicID gets populated on retrieval +// TODO (dmitri) PublicID should not be populated with delta updates, +// which require stable PublicIDs +func TestGetGroupsWithoutExpectedFields(t *testing.T) { + ctx := context.TODO() + + 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) VALUES('test-group-id-1',$1)", acctId) + assert.NoError(t, err) + + groups, _, err := s.GetGroups(ctx, acctId) + assert.NoError(t, err) + assert.Len(t, groups, 1) + assert.NotEmpty(t, groups[0].PublicID) } diff --git a/integration_tests/management/network_map_db/pgsql/network_router_test.go b/integration_tests/management/network_map_db/pgsql/network_router_test.go new file mode 100644 index 000000000..45622f319 --- /dev/null +++ b/integration_tests/management/network_map_db/pgsql/network_router_test.go @@ -0,0 +1,54 @@ +package networkmap_pgsql + +import ( + "context" + "testing" + + 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" +) + +func TestGetNetworkRouters(t *testing.T) { + ctx := context.TODO() + + 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) VALUES('test-nr-id-1',$1,'public-id-1','peer-id-1','network-id-1',TRUE,999,TRUE)", acctId) + _, err = s.Pool.Query(ctx, + "insert into network_routers (id, account_id, public_id, peer, network_id, masquerade, metric, enabled) VALUES('test-nr-id-2',$1,'public-id-2','peer-id-2','network-id-2',TRUE,333,TRUE)", acctId) + _, err = s.Pool.Query(ctx, + "insert into group_peers (peer_id, group_id) VALUES('peer-id-1','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','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','test-group-id-2')") + assert.NoError(t, err) + + groups, resourceToGroupIdx, err := s.GetGroups(ctx, acctId) + assert.NoError(t, err) + assert.Contains(t, + groups, + nmdata.Group{ID: "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"}}, + ) + assert.NotNil(t, resourceToGroupIdx["host-id-1"]["test-group-id-1"]) + assert.Contains(t, + groups, + nmdata.Group{ID: "test-group-id-2", Name: "test-group-2", PublicID: "public-id-2", + 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"]["test-group-id-2"]) + assert.NotNil(t, resourceToGroupIdx["subnet-id-1"]["test-group-id-2"]) +}