From ba574dc739654860fdeaec9d46056e8b1f996539 Mon Sep 17 00:00:00 2001 From: Dmitri Dolguikh Date: Wed, 5 Aug 2026 16:06:43 +0200 Subject: [PATCH] added tests for GetPrivateServicesViaPgxConnection and GetPrivateServicesViaPgxConnection Signed-off-by: Dmitri Dolguikh --- .../network_map_db/pgsql/service_test.go | 117 ++++++++++++++++++ .../network_map_db/pgsql/network_map_data.go | 4 +- .../internals/network_map_db/pgsql/service.go | 8 +- 3 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 integration_tests/management/network_map_db/pgsql/service_test.go diff --git a/integration_tests/management/network_map_db/pgsql/service_test.go b/integration_tests/management/network_map_db/pgsql/service_test.go new file mode 100644 index 000000000..a23fad55d --- /dev/null +++ b/integration_tests/management/network_map_db/pgsql/service_test.go @@ -0,0 +1,117 @@ +package networkmap_pgsql + +import ( + "context" + "database/sql" + "testing" + + networkmap_pgsql "github.com/netbirdio/netbird/management/internals/network_map_db/pgsql" + "github.com/stretchr/testify/assert" +) + +func TestGetPrivateServicesViaPgxConnection(t *testing.T) { + ctx := context.TODO() + + _, err := pgstore.Pool.Query(ctx, + `insert into services (id, account_id, enabled, private, access_groups, proxy_cluster, domain) + values('service-1','account-1',true,true,'["group-one-resource-id"]','test-1.com','test-2.com')`) + assert.NoError(t, err) + _, err = pgstore.Pool.Query(ctx, + `insert into services (id, account_id, enabled, private, access_groups, proxy_cluster, domain) + values('service-2','account-1',true,true,'["group-one-resource-id","group-two-resources-id"]','test-3.com','test-4.com')`) + assert.NoError(t, err) + _, err = pgstore.Pool.Query(ctx, + `insert into services (id, account_id, enabled, private, access_groups, proxy_cluster, domain) + values('service-3','account-1',null,null,null,null,null)`) + assert.NoError(t, err) + + services, err := networkmap_pgsql.GetPrivateServicesViaPgxConnection(ctx, conn(t, ctx), "account-1") + assert.NoError(t, err) + assert.Contains(t, services, networkmap_pgsql.Service{ + Enabled: sql.NullBool{Bool: true, Valid: true}, + Private: sql.NullBool{Bool: true, Valid: true}, + AccessGroups: []string{"group-one-resource-id"}, + ProxyCluster: sql.NullString{String: "test-1.com", Valid: true}, + Domain: sql.NullString{String: "test-2.com", Valid: true}, + }) + assert.Contains(t, services, networkmap_pgsql.Service{ + Enabled: sql.NullBool{Bool: true, Valid: true}, + Private: sql.NullBool{Bool: true, Valid: true}, + AccessGroups: []string{"group-one-resource-id", "group-two-resources-id"}, + ProxyCluster: sql.NullString{String: "test-3.com", Valid: true}, + Domain: sql.NullString{String: "test-4.com", Valid: true}, + }) + assert.Contains(t, services, networkmap_pgsql.Service{ + Enabled: sql.NullBool{Bool: false, Valid: false}, + Private: sql.NullBool{Bool: false, Valid: false}, + AccessGroups: []string{}, + ProxyCluster: sql.NullString{String: "", Valid: false}, + Domain: sql.NullString{String: "", Valid: false}, + }) +} + +func TestGetProxyTargetedDomainResourceIDsViaPgxConnection(t *testing.T) { + ctx := context.TODO() + + _, err := pgstore.Pool.Query(ctx, + `insert into services (id, account_id, enabled, terminated) + values('service-4','account-1',true,false)`) + assert.NoError(t, err) + _, err = pgstore.Pool.Query(ctx, + `insert into targets (target_id, account_id, service_id, enabled, target_type) + values('target-1','account-1','service-4',true,'domain')`) + // id shouldn't be returned as the taget_type is not "domain" + _, err = pgstore.Pool.Query(ctx, + `insert into targets (target_id, account_id, service_id, enabled, target_type) + values('target-2','account-1','service-4',true,'cluster')`) + assert.NoError(t, err) + // id shouldn't be included as the target is disabled + _, err = pgstore.Pool.Query(ctx, + `insert into targets (target_id, account_id, service_id, enabled, target_type) + values('target-3','account-1','service-4',false,'domain')`) + assert.NoError(t, err) + // id shouldn't be included as the service is disabled + _, err = pgstore.Pool.Query(ctx, + `insert into services (id, account_id, enabled, terminated) + values('service-5','account-1',false,false)`) + _, err = pgstore.Pool.Query(ctx, + `insert into targets (target_id, account_id, service_id, enabled, target_type) + values('target-4','account-1','service-5',false,'domain')`) + assert.NoError(t, err) + // id shouldn't be included as the service is terminated (explicitly) + _, err = pgstore.Pool.Query(ctx, + `insert into services (id, account_id, enabled, terminated) + values('service-6','account-1',true,true)`) + _, err = pgstore.Pool.Query(ctx, + `insert into targets (target_id, account_id, service_id, enabled, target_type) + values('target-5','account-1','service-6',true,'domain')`) + assert.NoError(t, err) + // id shouldn't be included as the service is terminated (implicitly) + _, err = pgstore.Pool.Query(ctx, + `insert into services (id, account_id, enabled, terminated) + values('service-7','account-1',true,null)`) + _, err = pgstore.Pool.Query(ctx, + `insert into targets (target_id, account_id, service_id, enabled, target_type) + values('target-6','account-1','service-7',true,'domain')`) + assert.NoError(t, err) + _, err = pgstore.Pool.Query(ctx, + `insert into services (id, account_id, enabled, terminated) + values('service-8','account-1',true,false)`) + _, err = pgstore.Pool.Query(ctx, + `insert into targets (target_id, account_id, service_id, enabled, target_type) + values('target-7','account-1','service-8',true,'domain')`) + assert.NoError(t, err) + // id shouldn't be returned as the taget_id is null + _, err = pgstore.Pool.Query(ctx, + `insert into targets (target_id, account_id, service_id, enabled, target_type) + values(null,'account-1','service-4',true,'cluster')`) + assert.NoError(t, err) + + servtargetedDomains, err := networkmap_pgsql.GetProxyTargetedDomainResourceIDsViaPgxConnection(ctx, conn(t, ctx), "account-1") + assert.NoError(t, err) + assert.Equal(t, servtargetedDomains, map[string]struct{}{ + "target-1": {}, + "target-6": {}, + "target-7": {}, + }) +} diff --git a/management/internals/network_map_db/pgsql/network_map_data.go b/management/internals/network_map_db/pgsql/network_map_data.go index 81246a8fd..f8ef30f2c 100644 --- a/management/internals/network_map_db/pgsql/network_map_data.go +++ b/management/internals/network_map_db/pgsql/network_map_data.go @@ -164,7 +164,7 @@ func toSliceOfPtrs[T any](all []T) []*T { return toret } -func serviceDomainZone(svc service, ds []Domain) string { +func serviceDomainZone(svc Service, ds []Domain) string { if domainFromSuffix(svc.Domain.String, svc.ProxyCluster.String) { return svc.ProxyCluster.String } @@ -189,7 +189,7 @@ func domainFromSuffix(domain, suffix string) bool { return domain == suffix || strings.HasSuffix(domain, "."+suffix) } -func buildPrivateServiceCandidates(svcs []service, domains []Domain, proxyPeersByCluster map[string][]*nmdata.Peer) []networkmap.PrivateServiceCandidate { +func buildPrivateServiceCandidates(svcs []Service, domains []Domain, proxyPeersByCluster map[string][]*nmdata.Peer) []networkmap.PrivateServiceCandidate { var out []networkmap.PrivateServiceCandidate if len(proxyPeersByCluster) == 0 { diff --git a/management/internals/network_map_db/pgsql/service.go b/management/internals/network_map_db/pgsql/service.go index add69a208..fce62a207 100644 --- a/management/internals/network_map_db/pgsql/service.go +++ b/management/internals/network_map_db/pgsql/service.go @@ -23,7 +23,7 @@ const ( ` ) -func (pg *PgStore) GetPrivateServices(ctx context.Context, accountId string) ([]service, error) { +func (pg *PgStore) GetPrivateServices(ctx context.Context, accountId string) ([]Service, error) { c, err := pg.Pool.Acquire(ctx) if err != nil { return nil, err @@ -31,13 +31,13 @@ func (pg *PgStore) GetPrivateServices(ctx context.Context, accountId string) ([] return GetPrivateServicesViaPgxConnection(ctx, c.Conn(), accountId) } -func GetPrivateServicesViaPgxConnection(ctx context.Context, conn *pgx.Conn, accountId string) ([]service, error) { +func GetPrivateServicesViaPgxConnection(ctx context.Context, conn *pgx.Conn, accountId string) ([]Service, error) { rows, err := conn.Query(ctx, GetServicesQuery, accountId) if err != nil { return nil, err } - return pgx.CollectRows(rows, pgx.RowToStructByName[service]) + return pgx.CollectRows(rows, pgx.RowToStructByName[Service]) } func GetProxyTargetedDomainResourceIDsViaPgxConnection(ctx context.Context, conn *pgx.Conn, accountId string) (map[string]struct{}, error) { @@ -58,7 +58,7 @@ func GetProxyTargetedDomainResourceIDsViaPgxConnection(ctx context.Context, conn return toret, nil } -type service struct { +type Service struct { Enabled sql.NullBool Private sql.NullBool AccessGroups []string