Skip to content

Commit 91976b5

Browse files
committed
testserver: new methods SQLConnForUser and SQLConnForUserE
This deprecates the following pattern: ```go testUserURL, cleanupFn := sqlutils.PGUrl(t, s.AdvSQLAddr(), "dbname", "username") defer cleanupFn() testUserDb, err := gosql.Open("postgres", testUserURL.String()) require.NoError(t, err) defer testUserDb.Close() ``` Which can now be replaced by: ``` testUserDb := s.ApplicationLayer().SQLConnForUser(t, "username", "dbname") ``` Release note: None
1 parent a32665f commit 91976b5

File tree

50 files changed

+499
-748
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+499
-748
lines changed

pkg/ccl/auditloggingccl/audit_logging_test.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ package auditloggingccl
1010

1111
import (
1212
"context"
13-
gosql "database/sql"
1413
"fmt"
1514
"math"
16-
"net/url"
1715
"regexp"
1816
"strings"
1917
"testing"
@@ -121,13 +119,7 @@ func TestSingleRoleAuditLogging(t *testing.T) {
121119
rootRunner := sqlutils.MakeSQLRunner(sqlDB)
122120
defer s.Stopper().Stop(context.Background())
123121

124-
testUserURL, cleanupFn := sqlutils.PGUrl(t,
125-
s.ApplicationLayer().AdvSQLAddr(), t.Name(), url.User(username.TestUser))
126-
defer cleanupFn()
127-
128-
testUserDb, err := gosql.Open("postgres", testUserURL.String())
129-
require.NoError(t, err)
130-
defer testUserDb.Close()
122+
testUserDb := s.ApplicationLayer().SQLConnForUser(t, username.TestUser, "")
131123
testRunner := sqlutils.MakeSQLRunner(testUserDb)
132124

133125
// Dummy table/user used by tests.
@@ -250,16 +242,10 @@ func TestMultiRoleAuditLogging(t *testing.T) {
250242
defer cleanup()
251243

252244
s, sqlDB, _ := serverutils.StartServer(t, base.TestServerArgs{})
253-
rootRunner := sqlutils.MakeSQLRunner(sqlDB)
254245
defer s.Stopper().Stop(context.Background())
246+
rootRunner := sqlutils.MakeSQLRunner(sqlDB)
255247

256-
testUserURL, cleanupFn := sqlutils.PGUrl(t,
257-
s.ApplicationLayer().AdvSQLAddr(), t.Name(), url.User(username.TestUser))
258-
defer cleanupFn()
259-
260-
testUserDb, err := gosql.Open("postgres", testUserURL.String())
261-
require.NoError(t, err)
262-
defer testUserDb.Close()
248+
testUserDb := s.ApplicationLayer().SQLConnForUser(t, username.TestUser, "")
263249
testRunner := sqlutils.MakeSQLRunner(testUserDb)
264250

265251
// Dummy table/user used by tests.
@@ -374,13 +360,7 @@ func TestReducedAuditConfig(t *testing.T) {
374360
return nil
375361
})
376362

377-
testUserURL, cleanupFn := sqlutils.PGUrl(t,
378-
s.ApplicationLayer().AdvSQLAddr(), t.Name(), url.User(username.TestUser))
379-
defer cleanupFn()
380-
381-
testUserDb, err := gosql.Open("postgres", testUserURL.String())
382-
require.NoError(t, err)
383-
defer testUserDb.Close()
363+
testUserDb := s.ApplicationLayer().SQLConnForUser(t, username.TestUser, "")
384364
testRunner := sqlutils.MakeSQLRunner(testUserDb)
385365

386366
// Set a cluster configuration.
@@ -436,9 +416,7 @@ func TestReducedAuditConfig(t *testing.T) {
436416
}
437417

438418
// Open 2nd connection for the test user.
439-
testUserDb2, err := gosql.Open("postgres", testUserURL.String())
440-
require.NoError(t, err)
441-
defer testUserDb2.Close()
419+
testUserDb2 := s.ApplicationLayer().SQLConnForUser(t, username.TestUser, "")
442420
testRunner2 := sqlutils.MakeSQLRunner(testUserDb2)
443421

444422
// Run a query on the new connection. The new connection will cause the reduced audit config to be re-computed.

pkg/ccl/backupccl/create_scheduled_backup_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ package backupccl
1010

1111
import (
1212
"context"
13-
gosql "database/sql"
1413
"fmt"
15-
"net/url"
1614
"regexp"
1715
"sort"
1816
"strconv"
@@ -970,18 +968,9 @@ func TestCreateBackupScheduleRequiresAdminRole(t *testing.T) {
970968
defer cleanup()
971969

972970
th.sqlDB.Exec(t, `CREATE USER testuser`)
973-
pgURL, cleanupFunc := sqlutils.PGUrl(
974-
t, th.server.ApplicationLayer().AdvSQLAddr(),
975-
"TestCreateSchedule-testuser", url.User("testuser"),
976-
)
977-
defer cleanupFunc()
978-
testuser, err := gosql.Open("postgres", pgURL.String())
979-
require.NoError(t, err)
980-
defer func() {
981-
require.NoError(t, testuser.Close())
982-
}()
971+
testuser := th.server.ApplicationLayer().SQLConnForUser(t, "testuser", "")
983972

984-
_, err = testuser.Exec("CREATE SCHEDULE FOR BACKUP INTO 'somewhere' RECURRING '@daily'")
973+
_, err := testuser.Exec("CREATE SCHEDULE FOR BACKUP INTO 'somewhere' RECURRING '@daily'")
985974
require.Error(t, err)
986975
}
987976

pkg/ccl/backupccl/show_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package backupccl
1010

1111
import (
1212
"context"
13-
gosql "database/sql"
1413
"fmt"
1514
"net/url"
1615
"os"
@@ -681,14 +680,7 @@ func TestShowBackupPrivileges(t *testing.T) {
681680
sqlDB.Exec(t, `CREATE USER testuser`)
682681
sqlDB.Exec(t, `CREATE TABLE privs (a INT)`)
683682

684-
pgURL, cleanup := sqlutils.PGUrl(t, srv.ApplicationLayer().AdvSQLAddr(),
685-
"TestShowBackupPrivileges-testuser", url.User("testuser"))
686-
defer cleanup()
687-
testuser, err := gosql.Open("postgres", pgURL.String())
688-
require.NoError(t, err)
689-
defer func() {
690-
require.NoError(t, testuser.Close())
691-
}()
683+
testuser := srv.ApplicationLayer().SQLConnForUser(t, "testuser", "")
692684

693685
// Make an initial backup.
694686
const full = localFoo + "/full"
@@ -698,7 +690,7 @@ func TestShowBackupPrivileges(t *testing.T) {
698690
// Make a second full backup using non into syntax.
699691
sqlDB.Exec(t, `BACKUP TO $1;`, full)
700692

701-
_, err = testuser.Exec(`SHOW BACKUPS IN $1`, full)
693+
_, err := testuser.Exec(`SHOW BACKUPS IN $1`, full)
702694
require.True(t, testutils.IsError(err,
703695
"only users with the admin role or the EXTERNALIOIMPLICITACCESS system privilege are allowed to access the specified nodelocal URI"))
704696

pkg/ccl/backupccl/tenant_backup_nemesis_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func TestTenantBackupNemesis(t *testing.T) {
179179
g := ctxgroup.WithContext(ctx)
180180
g.GoCtx(func(ctx context.Context) error {
181181
pgURL, cleanupGoDB, err := sqlutils.PGUrlE(
182-
tenant10.SQLAddr(), "workload-worker" /* prefix */, url.User(username.RootUser))
182+
tenant10.AdvSQLAddr(), "workload-worker" /* prefix */, url.User(username.RootUser))
183183
if err != nil {
184184
return err
185185
}

pkg/ccl/cloudccl/cloudprivilege/privileges_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ package cloudprivilege
1010

1111
import (
1212
"context"
13-
gosql "database/sql"
1413
"fmt"
15-
"net/url"
1614
"testing"
1715

1816
"github.com/cockroachdb/cockroach/pkg/base"
@@ -44,14 +42,7 @@ func TestURIRequiresAdminOrPrivilege(t *testing.T) {
4442
rootDB := sqlutils.MakeSQLRunner(conn)
4543

4644
rootDB.Exec(t, `CREATE USER testuser`)
47-
pgURL, cleanupFunc := sqlutils.PGUrl(
48-
t, tc.ApplicationLayer(0).AdvSQLAddr(), "TestURIRequiresAdminRole-testuser",
49-
url.User("testuser"),
50-
)
51-
defer cleanupFunc()
52-
testuser, err := gosql.Open("postgres", pgURL.String())
53-
require.NoError(t, err)
54-
defer testuser.Close()
45+
testuser := tc.ApplicationLayer(0).SQLConnForUser(t, username.TestUser, "")
5546
rootDB.Exec(t, `CREATE TABLE foo (id INT)`)
5647

5748
// Grant SELECT so that EXPORT fails when checking URI privileges.

pkg/ccl/kvccl/kvfollowerreadsccl/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ go_test(
6262
"//pkg/rpc",
6363
"//pkg/security/securityassets",
6464
"//pkg/security/securitytest",
65-
"//pkg/security/username",
6665
"//pkg/server",
6766
"//pkg/settings/cluster",
6867
"//pkg/sql",

pkg/ccl/kvccl/kvfollowerreadsccl/followerreads_test.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
gosql "database/sql"
1414
"fmt"
1515
"math"
16-
"net/url"
1716
"strings"
1817
"testing"
1918
"time"
@@ -31,7 +30,6 @@ import (
3130
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock"
3231
"github.com/cockroachdb/cockroach/pkg/roachpb"
3332
"github.com/cockroachdb/cockroach/pkg/rpc"
34-
"github.com/cockroachdb/cockroach/pkg/security/username"
3533
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
3634
"github.com/cockroachdb/cockroach/pkg/sql"
3735
"github.com/cockroachdb/cockroach/pkg/sql/physicalplan/replicaoracle"
@@ -971,23 +969,22 @@ func TestSecondaryTenantFollowerReadsRouting(t *testing.T) {
971969
// in KV so we must ensure they're not redacted.
972970
systemSQL.Exec(t, `SET CLUSTER SETTING server.secondary_tenants.redact_trace.enabled = 'false'`)
973971

972+
dbs := make([]*gosql.DB, numNodes)
973+
for i := 0; i < numNodes; i++ {
974+
dbs[i] = tenants[i].SQLConn(t, "")
975+
}
976+
974977
// Wait until all tenant servers are aware of the setting override.
975978
testutils.SucceedsSoon(t, func() error {
976979
settingNames := []string{
977980
"kv.closed_timestamp.target_duration", "kv.closed_timestamp.side_transport_interval", "kv.closed_timestamp.propagation_slack",
978981
}
979982
for _, settingName := range settingNames {
980983
for i := 0; i < numNodes; i++ {
981-
pgURL, cleanup := sqlutils.PGUrl(t, tenants[i].SQLAddr(), "Tenant", url.User(username.RootUser))
982-
defer cleanup()
983-
db, err := gosql.Open("postgres", pgURL.String())
984-
if err != nil {
985-
t.Fatal(err)
986-
}
987-
defer db.Close()
984+
db := dbs[i]
988985

989986
var val string
990-
err = db.QueryRow(
987+
err := db.QueryRow(
991988
fmt.Sprintf("SHOW CLUSTER SETTING %s", settingName),
992989
).Scan(&val)
993990
require.NoError(t, err)
@@ -1003,13 +1000,7 @@ func TestSecondaryTenantFollowerReadsRouting(t *testing.T) {
10031000
return nil
10041001
})
10051002

1006-
pgURL, cleanupPGUrl := sqlutils.PGUrl(
1007-
t, tenants[3].SQLAddr(), "Tenant", url.User(username.RootUser),
1008-
)
1009-
defer cleanupPGUrl()
1010-
tenantSQLDB, err := gosql.Open("postgres", pgURL.String())
1011-
require.NoError(t, err)
1012-
defer tenantSQLDB.Close()
1003+
tenantSQLDB := dbs[3]
10131004
tenantSQL := sqlutils.MakeSQLRunner(tenantSQLDB)
10141005

10151006
tenantSQL.Exec(t, `CREATE DATABASE t`)

pkg/ccl/kvccl/kvtenantccl/upgradeccl/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ go_test(
1616
"//pkg/roachpb",
1717
"//pkg/security/securityassets",
1818
"//pkg/security/securitytest",
19-
"//pkg/security/username",
2019
"//pkg/server",
2120
"//pkg/settings/cluster",
2221
"//pkg/spanconfig",

0 commit comments

Comments
 (0)