From 316e82337f8f1944960574929cd78e27fa7a1d44 Mon Sep 17 00:00:00 2001 From: Dmitri Dolguikh Date: Wed, 5 Aug 2026 18:09:28 +0200 Subject: [PATCH] cleanup query execution in tests Signed-off-by: Dmitri Dolguikh --- .../pgsql/account_settings_test.go | 3 +- .../network_map_db/pgsql/domain_test.go | 9 ++-- .../network_map_db/pgsql/group_test.go | 14 +++--- .../network_map_db/pgsql/main_test.go | 6 +++ .../network_map_db/pgsql/nameserver_test.go | 9 ++-- .../pgsql/network_resource_test.go | 9 ++-- .../pgsql/network_router_test.go | 6 +-- .../network_map_db/pgsql/networks_test.go | 6 +-- .../network_map_db/pgsql/posture_test.go | 9 ++-- .../network_map_db/pgsql/route_test.go | 9 ++-- .../network_map_db/pgsql/service_test.go | 43 +++++++------------ .../network_map_db/pgsql/user_test.go | 26 +++++------ .../internals/network_map_db/db_store.go | 5 ++- 13 files changed, 60 insertions(+), 94 deletions(-) diff --git a/integration_tests/management/network_map_db/pgsql/account_settings_test.go b/integration_tests/management/network_map_db/pgsql/account_settings_test.go index 53d485dc3..9ef51405f 100644 --- a/integration_tests/management/network_map_db/pgsql/account_settings_test.go +++ b/integration_tests/management/network_map_db/pgsql/account_settings_test.go @@ -13,12 +13,11 @@ import ( func TestGetAccountSettings(t *testing.T) { ctx := context.TODO() - _, err := pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into accounts (id, settings_peer_login_expiration_enabled, settings_peer_login_expiration, settings_peer_inactivity_expiration_enabled, settings_peer_inactivity_expiration, settings_dns_domain, settings_ipv6_enabled_groups, settings_routing_peer_dns_resolution_enabled, settings_lazy_connection_enabled, settings_auto_update_version, settings_auto_update_always, settings_metrics_push_enabled) values('account-3',null,null,null,null,null,null,null,null,null,null,null)`) - assert.NoError(t, err) accountSettings, err := networkmap_pgsql.GetAccountSettingsViaPgxConnection(ctx, conn(t, ctx), "account-1") assert.NoError(t, err) diff --git a/integration_tests/management/network_map_db/pgsql/domain_test.go b/integration_tests/management/network_map_db/pgsql/domain_test.go index afb8645e0..3c0948224 100644 --- a/integration_tests/management/network_map_db/pgsql/domain_test.go +++ b/integration_tests/management/network_map_db/pgsql/domain_test.go @@ -12,18 +12,15 @@ import ( func TestGetDomains(t *testing.T) { ctx := context.TODO() - _, err := pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into domains (id, account_id, domain, target_cluster) VALUES('domain-1','account-1','test-1.com','target-1.cluster.local')`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into domains (id, account_id, domain, target_cluster) VALUES('domain-2','account-1','test-2.com','target-2.cluster.local')`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into domains (id, account_id, domain, target_cluster) VALUES('domain-3','account-1',null,null)`) - assert.NoError(t, err) domains, err := pgstore.GetDomains(ctx, "account-1") assert.NoError(t, err) 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 c56859634..19a8d810d 100644 --- a/integration_tests/management/network_map_db/pgsql/group_test.go +++ b/integration_tests/management/network_map_db/pgsql/group_test.go @@ -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" ) @@ -46,17 +45,14 @@ func TestGetGroupsWithoutExpectedFields(t *testing.T) { s, err := networkmap_pgsql.NewPostgresqlStore(ctx, dsn) assert.NoError(t, err) - acctId := xid.New().String() + execQuery(t, ctx, + "insert into accounts (id) VALUES('random-id')") - _, err = s.Pool.Exec(ctx, - "insert into accounts (id) VALUES($1)", acctId) + execQuery(t, ctx, + "insert into groups (id, account_id) VALUES('g2-test-group-id-1','random-id')") assert.NoError(t, err) - _, err = s.Pool.Exec(ctx, - "insert into groups (id, account_id) VALUES('g2-test-group-id-1',$1)", acctId) - assert.NoError(t, err) - - groups, _, err := s.GetGroups(ctx, acctId) + groups, _, err := s.GetGroups(ctx, "random-id") 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/main_test.go b/integration_tests/management/network_map_db/pgsql/main_test.go index c14abb72f..f2457b0a8 100644 --- a/integration_tests/management/network_map_db/pgsql/main_test.go +++ b/integration_tests/management/network_map_db/pgsql/main_test.go @@ -147,3 +147,9 @@ func conn(t *testing.T, ctx context.Context) *pgx.Conn { assert.NoError(t, err) return c.Conn() } + +func execQuery(t *testing.T, ctx context.Context, q string) { + t.Helper() + _, err := pgstore.Pool.Exec(ctx, q) + assert.NoError(t, err) +} diff --git a/integration_tests/management/network_map_db/pgsql/nameserver_test.go b/integration_tests/management/network_map_db/pgsql/nameserver_test.go index d2879719f..868ad8f0b 100644 --- a/integration_tests/management/network_map_db/pgsql/nameserver_test.go +++ b/integration_tests/management/network_map_db/pgsql/nameserver_test.go @@ -13,18 +13,15 @@ import ( func TestGetNameServerGroups(t *testing.T) { ctx := context.TODO() - _, err := pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into name_server_groups (id, public_id, name, description, name_servers, groups, domains, enabled, search_domains_enabled, "primary", account_id) VALUES('nsgroup-1','nsgroup-1-public','nsgroup-1','nsgroup-1','[{"IP":"192.168.31.2","NSType":1,"Port":53}]','["group-one-resource-id"]','["test-1.com"]',TRUE,FALSE,TRUE,'account-1')`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into name_server_groups (id, public_id, name, description, name_servers, groups, domains, enabled, search_domains_enabled,"primary",account_id) VALUES('nsgroup-2','nsgroup-2-public','nsgroup-2','nsgroup-2','[{"IP":"192.168.32.3","NSType":1,"Port":53}]','["group-one-resource-id","group-no-resources-id"]','["test-1.com","test-2.com"]',TRUE,FALSE,TRUE,'account-1')`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into name_server_groups (id, public_id, name, description, name_servers, groups, domains, enabled, search_domains_enabled,"primary",account_id) VALUES('nsgroup-3','nsgroup-3-public',null,null,null,null,null,TRUE,FALSE,FALSE,'account-1')`) - assert.NoError(t, err) nsgroups, err := networkmap_pgsql.GetNameServerGroupsViaPgxConnection(ctx, conn(t, ctx), "account-1") assert.NoError(t, err) diff --git a/integration_tests/management/network_map_db/pgsql/network_resource_test.go b/integration_tests/management/network_map_db/pgsql/network_resource_test.go index 7356831e5..018d55700 100644 --- a/integration_tests/management/network_map_db/pgsql/network_resource_test.go +++ b/integration_tests/management/network_map_db/pgsql/network_resource_test.go @@ -16,18 +16,15 @@ func TestGetNetworkResources(t *testing.T) { s, err := networkmap_pgsql.NewPostgresqlStore(ctx, dsn) assert.NoError(t, err) - _, err = s.Pool.Exec(ctx, + execQuery(t, ctx, `insert into network_resources (id, account_id, network_id, public_id, name, description, type, domain, prefix, enabled) VALUES('net-resource-1','account-1','network-1','net-resource-public-1','network-resource-1','network-resource-1','subnet','','"10.0.0.0/16"',TRUE)`) - assert.NoError(t, err) - _, err = s.Pool.Exec(ctx, + execQuery(t, ctx, `insert into network_resources (id, account_id, network_id, public_id, name, description, type, domain, prefix, enabled) VALUES('net-resource-2','account-1','network-2','net-resource-public-2','network-resource-2','network-resource-2','domain','test.com','',TRUE)`) - assert.NoError(t, err) - _, err = s.Pool.Exec(ctx, + execQuery(t, ctx, `insert into network_resources (id, account_id, network_id, public_id, name, description, type, domain, prefix, enabled) VALUES('net-resource-3','account-1','network-3','net-resource-public-3','network-resource-3','network-resource-3','host','','"10.0.0.1/32"',TRUE)`) - assert.NoError(t, err) resources, err := s.GetNetworkResources(ctx, "account-1") assert.NoError(t, err) 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 index 1f555bb58..86e6b3674 100644 --- a/integration_tests/management/network_map_db/pgsql/network_router_test.go +++ b/integration_tests/management/network_map_db/pgsql/network_router_test.go @@ -15,14 +15,12 @@ func TestGetNetworkRouters(t *testing.T) { s, err := networkmap_pgsql.NewPostgresqlStore(ctx, dsn) assert.NoError(t, err) - _, err = s.Pool.Exec(ctx, + execQuery(t, ctx, `insert into network_routers (id, account_id, public_id, peer, network_id, masquerade, metric, enabled, peer_groups) VALUES('test-nr-id-1','account-1','public-id-1','peer-id-1','network-id-1',TRUE,999,TRUE,'["group-one-resource-id"]')`) - assert.NoError(t, err) - _, err = s.Pool.Exec(ctx, + execQuery(t, ctx, `insert into network_routers (id, account_id, public_id, peer, network_id, masquerade, metric, enabled, peer_groups) VALUES('test-nr-id-2','account-1','public-id-2','','network-id-2',TRUE,333,TRUE,'["group-two-resources-id","group-no-resources-id"]')`) - assert.NoError(t, err) routers, err := s.GetNetworkRouters(ctx, "account-1") assert.NoError(t, err) diff --git a/integration_tests/management/network_map_db/pgsql/networks_test.go b/integration_tests/management/network_map_db/pgsql/networks_test.go index 4cf6a27cd..4e94d7b8e 100644 --- a/integration_tests/management/network_map_db/pgsql/networks_test.go +++ b/integration_tests/management/network_map_db/pgsql/networks_test.go @@ -11,12 +11,10 @@ import ( func TestGetNetworks(t *testing.T) { ctx := context.TODO() - _, err := pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into networks (id, account_id, public_id) VALUES('network-1','account-1','network-1-public')`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into networks (id, account_id, public_id) VALUES('network-2','account-1','network-2-public')`) - assert.NoError(t, err) networksIdx, err := networkmap_pgsql.GetNetworkXIDToPublicIdMapViaPgxConnection(ctx, conn(t, ctx), "account-1") assert.NoError(t, err) diff --git a/integration_tests/management/network_map_db/pgsql/posture_test.go b/integration_tests/management/network_map_db/pgsql/posture_test.go index 4de8f64ef..10d81fd5f 100644 --- a/integration_tests/management/network_map_db/pgsql/posture_test.go +++ b/integration_tests/management/network_map_db/pgsql/posture_test.go @@ -13,27 +13,24 @@ import ( func TestGetPostureChecks(t *testing.T) { ctx := context.TODO() - _, err := pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into posture_checks (id, account_id, public_id, checks) VALUES('posturecheck-1','account-1','posturecheck-1-public', '{"NBVersionCheck":{"MinVersion":"0.25.0"}, "OSVersionCheck":{"Darwin":{"MinVersion":"12.0"}}, "GeoLocationCheck":{"Locations":[{"CountryCode":"FI","CityName":""}],"Action":"allow"}, "PeerNetworkRangeCheck":{"Action":"deny","Ranges":["192.168.0.1/24"]}}')`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into posture_checks (id, account_id, public_id, checks) VALUES('posturecheck-2','account-1','posturecheck-2-public', '{"NBVersionCheck":{"MinVersion":"0.25.0"}, "OSVersionCheck":{"Android":{"MinVersion":"0"}}, "GeoLocationCheck":{"Locations":[{"CountryCode":"US","CityName":"Harker Heights"}],"Action":"allow"}, "PeerNetworkRangeCheck":{"Action":"allow","Ranges":["0.0.0.0/0"]}}')`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into posture_checks (id, account_id, public_id, checks) VALUES('posturecheck-3','account-1','posturecheck-3-public', null)`) - assert.NoError(t, err) postureChecks, idToPublicIDIdx, err := networkmap_pgsql.GetPostureChecksViaPgxConnection(ctx, conn(t, ctx), "account-1") assert.NoError(t, err) diff --git a/integration_tests/management/network_map_db/pgsql/route_test.go b/integration_tests/management/network_map_db/pgsql/route_test.go index 6a7287122..6d2e72009 100644 --- a/integration_tests/management/network_map_db/pgsql/route_test.go +++ b/integration_tests/management/network_map_db/pgsql/route_test.go @@ -14,29 +14,26 @@ import ( func TestGetRoutes(t *testing.T) { ctx := context.TODO() - _, err := pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into routes (id, account_id, public_id, network, domains, keep_route, net_id, description, peer, peer_groups, network_type, masquerade, metric, enabled, groups, access_control_groups, skip_auto_apply) VALUES('route-1','account-1','route-1-public','"172.0.0.0/16"','["test-1.com"]',true,'route-1-net-id','route-1', 'peer-id-1','["group-one-resource-id"]',1,true,9999,true, '["group-one-resource-id"]','["group-one-resource-id"]',false)`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into routes (id, account_id, public_id, network, domains, keep_route, net_id, description, peer, peer_groups, network_type, masquerade, metric, enabled, groups, access_control_groups, skip_auto_apply) VALUES('route-2','account-1','route-2-public','"172.10.0.0/16"','["test-1.com","test-2.com"]',true,'route-2-net-id','route-2', 'peer-id-2','["group-two-resources-id"]',1,true,9999,true, '["group-two-resources-id"]','["group-two-resources-id"]',false)`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into routes (id, account_id, public_id, network, domains, keep_route, net_id, description, peer, peer_groups, network_type, masquerade, metric, enabled, groups, access_control_groups, skip_auto_apply) VALUES('route-3','account-1','route-3-public',null,null,null,null,'route-3', null,null,null,null,null,null,null,null,null)`) - assert.NoError(t, err) routes, err := networkmap_pgsql.GetRoutesViaPgxConnection(ctx, conn(t, ctx), "account-1") assert.NoError(t, err) diff --git a/integration_tests/management/network_map_db/pgsql/service_test.go b/integration_tests/management/network_map_db/pgsql/service_test.go index 7292187a7..e81336158 100644 --- a/integration_tests/management/network_map_db/pgsql/service_test.go +++ b/integration_tests/management/network_map_db/pgsql/service_test.go @@ -17,11 +17,11 @@ func TestGetPrivateServicesViaPgxConnection(t *testing.T) { `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.Exec(ctx, + execQuery(t, 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.Exec(ctx, + execQuery(t, 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) @@ -54,64 +54,51 @@ func TestGetPrivateServicesViaPgxConnection(t *testing.T) { func TestGetProxyTargetedDomainResourceIDsViaPgxConnection(t *testing.T) { ctx := context.TODO() - _, err := pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into services (id, account_id, enabled, terminated) values('service-4','account-1',true,false)`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, ctx, `insert into targets (target_id, account_id, service_id, enabled, target_type) values('target-1','account-1','service-4',true,'domain')`) - assert.NoError(t, err) // id shouldn't be returned as the taget_type is not "domain" - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, 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.Exec(ctx, + execQuery(t, 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.Exec(ctx, + execQuery(t, ctx, `insert into services (id, account_id, enabled, terminated) values('service-5','account-1',false,false)`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, 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.Exec(ctx, + execQuery(t, ctx, `insert into services (id, account_id, enabled, terminated) values('service-6','account-1',true,true)`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, 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.Exec(ctx, + execQuery(t, ctx, `insert into services (id, account_id, enabled, terminated) values('service-7','account-1',true,null)`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, 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.Exec(ctx, + execQuery(t, ctx, `insert into services (id, account_id, enabled, terminated) values('service-8','account-1',true,false)`) - assert.NoError(t, err) - _, err = pgstore.Pool.Exec(ctx, + execQuery(t, 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.Exec(ctx, + execQuery(t, 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) diff --git a/integration_tests/management/network_map_db/pgsql/user_test.go b/integration_tests/management/network_map_db/pgsql/user_test.go index 12883e511..ee7a09f24 100644 --- a/integration_tests/management/network_map_db/pgsql/user_test.go +++ b/integration_tests/management/network_map_db/pgsql/user_test.go @@ -11,38 +11,32 @@ import ( func TestGetAllowedUsers(t *testing.T) { ctx := context.TODO() - _, err := pgstore.Pool.Query(ctx, + execQuery(t, ctx, `insert into users (id, name, account_id, auto_groups, blocked, is_service_user) VALUES('user-1','user-1','account-1','["group-one-resource-id"]',false,false)`) - assert.NoError(t, err) - _, err = pgstore.Pool.Query(ctx, + execQuery(t, ctx, `insert into users (id, name, account_id, auto_groups, blocked, is_service_user) VALUES('user-2','user-2','account-1','["group-one-resource-id","group-two-resources-id"]',false,false)`) - assert.NoError(t, err) - _, err = pgstore.Pool.Query(ctx, + execQuery(t, ctx, `insert into users (id, name, account_id, auto_groups, blocked, is_service_user) VALUES('user-3','user-3','account-1','["group-two-resources-id"]',false,false)`) // shouldn't be included as it's blocked - _, err = pgstore.Pool.Query(ctx, + execQuery(t, ctx, `insert into users (id, name, account_id, auto_groups, blocked, is_service_user) - VALUES('user-3','user-3','account-1','["group-two-resources-id"]',true,false)`) - assert.NoError(t, err) + VALUES('user-4','user-4','account-1','["group-two-resources-id"]',true,false)`) // shouldn't be included as it's a service_user - _, err = pgstore.Pool.Query(ctx, + execQuery(t, ctx, `insert into users (id, name, account_id, auto_groups, blocked, is_service_user) - VALUES('user-3','user-3','account-1','["group-two-resources-id"]',false,true)`) - _, err = pgstore.Pool.Query(ctx, + VALUES('user-5','user-5','account-1','["group-two-resources-id"]',false,true)`) + execQuery(t, ctx, `insert into groups (id, name, account_id) VALUES('all-group-1','All','account-1')`) - assert.NoError(t, err) - _, err = pgstore.Pool.Query(ctx, + execQuery(t, ctx, `insert into groups (id, name, account_id) VALUES('all-group-2','All','account-1')`) - assert.NoError(t, err) - _, err = pgstore.Pool.Query(ctx, + execQuery(t, ctx, `insert into groups (id, name, account_id) VALUES('all-group-3','All','account-1')`) - assert.NoError(t, err) userIdx, groupIdToUserIds, err := networkmap_pgsql.GetAllowedUsersViaPgxConnection(ctx, conn(t, ctx), "account-1") assert.NoError(t, err) diff --git a/management/internals/network_map_db/db_store.go b/management/internals/network_map_db/db_store.go index 0dfa88e25..634b5725f 100644 --- a/management/internals/network_map_db/db_store.go +++ b/management/internals/network_map_db/db_store.go @@ -138,12 +138,15 @@ func FromSqlTypesToSharedTypes(src reflect.Value, dst reflect.Value) error { } case "json.RawMessage": s := srcField.Interface().(json.RawMessage) + if len(s) == 0 { + continue + } if err := json.Unmarshal(s, dstField.Addr().Interface()); err != nil { return err } case "[]string": if srcField.IsNil() { - return nil + continue } dstv := reflect.MakeSlice(dstField.Type(), srcField.Len(), srcField.Cap()) reflect.Copy(dstv, srcField)