Skip to content

Commit 16fff5c

Browse files
committed
sql: audit usages of SystemSQLCodec in tests
One of the common modifications we need to make for tests to work with secondary tenants is using the right `keys.SQLCodec` (as opposed to the one belonging to the system tenant). This commit audits all tests in `pkg/sql` directory to ensure that the right codec is passed. Note that tests still might need further modifications. Also note that tests that don't start a test server / a test cluster weren't touched (since they don't benefit from the tenant randomization behavior). Release note: None
1 parent 613f230 commit 16fff5c

31 files changed

+486
-438
lines changed

pkg/server/server_systemlog_gc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package server
88
import (
99
"context"
1010
"fmt"
11-
math_rand "math/rand"
11+
"math/rand"
1212
"time"
1313

1414
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
@@ -288,5 +288,5 @@ func startSystemLogsGC(ctx context.Context, sqlServer *SQLServer) error {
288288
// jitteredInterval returns a randomly jittered (+/-25%) duration
289289
// from the interval.
290290
func jitteredInterval(interval time.Duration) time.Duration {
291-
return time.Duration(float64(interval) * (0.75 + 0.5*math_rand.Float64()))
291+
return time.Duration(float64(interval) * (0.75 + 0.5*rand.Float64()))
292292
}

pkg/sql/catalog/bootstrap/splits_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func TestInitialSplitPoints(t *testing.T) {
5151

5252
ctx := context.Background()
5353
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{
54+
ServerArgs: base.TestServerArgs{
55+
DefaultTestTenant: base.TestControlsTenantsExplicitly,
56+
},
5457
ReplicationMode: base.ReplicationManual,
5558
})
5659
defer tc.Stopper().Stop(ctx)

pkg/sql/check_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/cockroachdb/cockroach/pkg/jobs"
1616
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
1717
"github.com/cockroachdb/cockroach/pkg/jobs/jobstest"
18-
"github.com/cockroachdb/cockroach/pkg/keys"
1918
"github.com/cockroachdb/cockroach/pkg/kv"
2019
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
2120
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
@@ -42,12 +41,12 @@ func TestValidateTTLScheduledJobs(t *testing.T) {
4241

4342
testCases := []struct {
4443
desc string
45-
setup func(t *testing.T, sqlDB *gosql.DB, kvDB *kv.DB, s serverutils.TestServerInterface, tableDesc *tabledesc.Mutable, scheduleID jobspb.ScheduleID)
44+
setup func(t *testing.T, sqlDB *gosql.DB, kvDB *kv.DB, s serverutils.ApplicationLayerInterface, tableDesc *tabledesc.Mutable, scheduleID jobspb.ScheduleID)
4645
expectedErrRe func(tableID descpb.ID, scheduleID jobspb.ScheduleID) string
4746
}{
4847
{
4948
desc: "not pointing at a valid scheduled job",
50-
setup: func(t *testing.T, sqlDB *gosql.DB, kvDB *kv.DB, s serverutils.TestServerInterface, tableDesc *tabledesc.Mutable, scheduleID jobspb.ScheduleID) {
49+
setup: func(t *testing.T, sqlDB *gosql.DB, kvDB *kv.DB, s serverutils.ApplicationLayerInterface, tableDesc *tabledesc.Mutable, scheduleID jobspb.ScheduleID) {
5150
require.NoError(t, sqltestutils.TestingDescsTxn(ctx, s, func(ctx context.Context, txn isql.Txn, col *descs.Collection) (err error) {
5251
// We need the collection to read the descriptor from storage for
5352
// the subsequent write to succeed.
@@ -66,7 +65,7 @@ func TestValidateTTLScheduledJobs(t *testing.T) {
6665
},
6766
{
6867
desc: "scheduled job points at an different table",
69-
setup: func(t *testing.T, sqlDB *gosql.DB, kvDB *kv.DB, s serverutils.TestServerInterface, tableDesc *tabledesc.Mutable, scheduleID jobspb.ScheduleID) {
68+
setup: func(t *testing.T, sqlDB *gosql.DB, kvDB *kv.DB, s serverutils.ApplicationLayerInterface, tableDesc *tabledesc.Mutable, scheduleID jobspb.ScheduleID) {
7069
db := s.InternalDB().(isql.DB)
7170
require.NoError(t, db.Txn(ctx, func(ctx context.Context, txn isql.Txn) error {
7271
schedules := jobs.ScheduledJobTxn(txn)
@@ -103,13 +102,14 @@ func TestValidateTTLScheduledJobs(t *testing.T) {
103102

104103
for _, tc := range testCases {
105104
t.Run(tc.desc, func(t *testing.T) {
106-
s, sqlDB, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
107-
defer s.Stopper().Stop(ctx)
105+
srv, sqlDB, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
106+
defer srv.Stopper().Stop(ctx)
107+
s := srv.ApplicationLayer()
108108

109109
_, err := sqlDB.Exec(`CREATE TABLE t () WITH (ttl_expire_after = '10 mins')`)
110110
require.NoError(t, err)
111111

112-
tableDesc := desctestutils.TestingGetMutableExistingTableDescriptor(kvDB, keys.SystemSQLCodec, "defaultdb", "t")
112+
tableDesc := desctestutils.TestingGetMutableExistingTableDescriptor(kvDB, s.Codec(), "defaultdb", "t")
113113
require.NotNil(t, tableDesc.GetRowLevelTTL())
114114
scheduleID := tableDesc.GetRowLevelTTL().ScheduleID
115115

pkg/sql/conn_executor_internal_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ func startConnExecutor(
274274
) {
275275
// A lot of boilerplate for creating a connExecutor.
276276
stopper := stop.NewStopper()
277+
codec := keys.SystemSQLCodec
277278
clock := hlc.NewClockForTesting(nil)
278279
factory := kv.MakeMockTxnSenderFactory(
279280
func(context.Context, *roachpb.Transaction, *kvpb.BatchRequest,
@@ -297,7 +298,7 @@ func startConnExecutor(
297298
})
298299
// This pool should never be Stop()ed because, if the test is failing, memory
299300
// is not properly released.
300-
collectionFactory := descs.NewBareBonesCollectionFactory(st, keys.SystemSQLCodec)
301+
collectionFactory := descs.NewBareBonesCollectionFactory(st, codec)
301302
registry := stmtdiagnostics.NewRegistry(nil, st)
302303
cfg := &ExecutorConfig{
303304
AmbientCtx: ambientCtx,
@@ -313,7 +314,7 @@ func startConnExecutor(
313314
NodeID: nodeID,
314315
LogicalClusterID: func() uuid.UUID { return uuid.UUID{} },
315316
},
316-
Codec: keys.SystemSQLCodec,
317+
Codec: codec,
317318
DistSQLPlanner: NewDistSQLPlanner(
318319
ctx, st, 1, /* sqlInstanceID */
319320
nil, /* rpcCtx */
@@ -339,7 +340,7 @@ func startConnExecutor(
339340
nil, /* connHealthCheckerSystem */
340341
nil, /* instanceConnHealthChecker */
341342
nil, /* sqlInstanceDialer */
342-
keys.SystemSQLCodec,
343+
codec,
343344
nil, /* sqlAddressResolver */
344345
clock,
345346
),

pkg/sql/crdb_internal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,8 +1595,8 @@ func TestVirtualPTSTableDeprecated(t *testing.T) {
15951595
Mode: ptpb.PROTECT_AFTER,
15961596
DeprecatedSpans: []roachpb.Span{
15971597
{
1598-
Key: keys.SystemSQLCodec.TablePrefix(42),
1599-
EndKey: keys.SystemSQLCodec.TablePrefix(42).PrefixEnd(),
1598+
Key: s.Codec().TablePrefix(42),
1599+
EndKey: s.Codec().TablePrefix(42).PrefixEnd(),
16001600
},
16011601
},
16021602
MetaType: "foo",

pkg/sql/database_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ func TestDatabaseHasChildSchemas(t *testing.T) {
4242
defer log.Scope(t).Close(t)
4343

4444
ctx := context.Background()
45-
s, sqlDB, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
46-
defer s.Stopper().Stop(ctx)
45+
srv, sqlDB, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
46+
defer srv.Stopper().Stop(ctx)
47+
s := srv.ApplicationLayer()
4748

4849
// Create a database and schema.
4950
if _, err := sqlDB.Exec(`
@@ -55,7 +56,7 @@ CREATE SCHEMA sc;
5556
}
5657

5758
// Now get the database descriptor from disk.
58-
db := desctestutils.TestingGetDatabaseDescriptor(kvDB, keys.SystemSQLCodec, "d")
59+
db := desctestutils.TestingGetDatabaseDescriptor(kvDB, s.Codec(), "d")
5960
if db.GetSchemaID("sc") == descpb.InvalidID {
6061
t.Fatal("expected to find child schema sc in db")
6162
}
@@ -65,7 +66,7 @@ CREATE SCHEMA sc;
6566
t.Fatal(err)
6667
}
6768

68-
db = desctestutils.TestingGetDatabaseDescriptor(kvDB, keys.SystemSQLCodec, "d")
69+
db = desctestutils.TestingGetDatabaseDescriptor(kvDB, s.Codec(), "d")
6970
if db.GetSchemaID("sc2") == descpb.InvalidID {
7071
t.Fatal("expected to find child schema sc2 in db")
7172
}

pkg/sql/distsql_physical_planner_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,15 +1372,16 @@ func TestPartitionSpans(t *testing.T) {
13721372
// We need a mock Gossip to contain addresses for the nodes. Otherwise the
13731373
// DistSQLPlanner will not plan flows on them.
13741374
ctx := context.Background()
1375-
s := serverutils.StartServerOnly(t, base.TestServerArgs{
1375+
srv := serverutils.StartServerOnly(t, base.TestServerArgs{
13761376
Knobs: base.TestingKnobs{
13771377
DistSQL: &execinfra.TestingKnobs{
13781378
MinimumNumberOfGatewayPartitions: 1,
13791379
},
13801380
},
13811381
})
1382-
defer s.Stopper().Stop(ctx)
1383-
mockGossip := gossip.NewTest(roachpb.NodeID(1), s.Stopper(), metric.NewRegistry())
1382+
defer srv.Stopper().Stop(ctx)
1383+
s := srv.ApplicationLayer()
1384+
mockGossip := gossip.NewTest(roachpb.NodeID(1), srv.Stopper(), metric.NewRegistry())
13841385
var nodeDescs []*roachpb.NodeDescriptor
13851386
mockInstances := make(mockAddressResolver)
13861387
for i := 1; i <= 10; i++ {
@@ -1459,7 +1460,7 @@ func TestPartitionSpans(t *testing.T) {
14591460
TestingKnobs: execinfra.TestingKnobs{MinimumNumberOfGatewayPartitions: 1},
14601461
},
14611462
},
1462-
codec: keys.SystemSQLCodec,
1463+
codec: s.Codec(),
14631464
nodeDescs: mockGossip,
14641465
}
14651466

@@ -1468,7 +1469,7 @@ func TestPartitionSpans(t *testing.T) {
14681469
require.NoError(t, locFilter.Set(tc.locFilter))
14691470
}
14701471
evalCtx := &eval.Context{
1471-
Codec: keys.SystemSQLCodec,
1472+
Codec: s.Codec(),
14721473
SessionDataStack: sessiondata.NewStack(&sessiondata.SessionData{
14731474
SessionData: sessiondatapb.SessionData{
14741475
DistsqlPlanGatewayBias: 2,
@@ -1761,6 +1762,7 @@ func TestPartitionSpansSkipsNodesNotInGossip(t *testing.T) {
17611762

17621763
stopper := stop.NewStopper()
17631764
defer stopper.Stop(context.Background())
1765+
codec := keys.SystemSQLCodec
17641766

17651767
mockGossip := gossip.NewTest(roachpb.NodeID(1), stopper, metric.NewRegistry())
17661768
var nodeDescs []*roachpb.NodeDescriptor
@@ -1815,14 +1817,14 @@ func TestPartitionSpansSkipsNodesNotInGossip(t *testing.T) {
18151817
return true
18161818
},
18171819
},
1818-
codec: keys.SystemSQLCodec,
1820+
codec: codec,
18191821
}
18201822

18211823
ctx := context.Background()
18221824
// This test is specific to gossip-based planning.
18231825
useGossipPlanning.Override(ctx, &st.SV, true)
18241826
planCtx := dsp.NewPlanningCtx(
1825-
ctx, &extendedEvalContext{Context: eval.Context{Codec: keys.SystemSQLCodec, Settings: st}},
1827+
ctx, &extendedEvalContext{Context: eval.Context{Codec: codec, Settings: st}},
18261828
nil /* planner */, nil /* txn */, FullDistribution,
18271829
)
18281830
partitions, err := dsp.PartitionSpans(ctx, planCtx, roachpb.Spans{span}, PartitionSpansBoundDefault)

pkg/sql/distsql_plan_backfill_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/cockroachdb/cockroach/pkg/base"
14-
"github.com/cockroachdb/cockroach/pkg/keys"
1514
"github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils"
1615
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
1716
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
@@ -55,35 +54,35 @@ func TestDistBackfill(t *testing.T) {
5554
},
5655
})
5756
defer tc.Stopper().Stop(context.Background())
58-
cdb := tc.Server(0).DB()
57+
s := tc.ApplicationLayer(0)
58+
conn, cdb := s.SQLConn(t, serverutils.DBName("test")), s.DB()
5959

6060
sqlutils.CreateTable(
61-
t, tc.ServerConn(0), "numtosquare", "x INT PRIMARY KEY, xsquared INT",
61+
t, conn, "numtosquare", "x INT PRIMARY KEY, xsquared INT",
6262
n,
6363
sqlutils.ToRowFn(sqlutils.RowIdxFn, func(row int) tree.Datum {
6464
return tree.NewDInt(tree.DInt(row * row))
6565
}),
6666
)
6767

6868
sqlutils.CreateTable(
69-
t, tc.ServerConn(0), "numtostr", "y INT PRIMARY KEY, str STRING",
69+
t, conn, "numtostr", "y INT PRIMARY KEY, str STRING",
7070
n*n,
7171
sqlutils.ToRowFn(sqlutils.RowIdxFn, sqlutils.RowEnglishFn),
7272
)
7373
// Split the table into multiple ranges.
74-
descNumToStr := desctestutils.TestingGetPublicTableDescriptor(cdb, keys.SystemSQLCodec, "test", "numtostr")
74+
descNumToStr := desctestutils.TestingGetPublicTableDescriptor(cdb, s.Codec(), "test", "numtostr")
7575
var sps []serverutils.SplitPoint
7676
// for i := 1; i <= numNodes-1; i++ {
7777
for i := numNodes - 1; i > 0; i-- {
7878
sps = append(sps, serverutils.SplitPoint{TargetNodeIdx: i, Vals: []interface{}{n * n / numNodes * i}})
7979
}
8080
tc.SplitTable(t, descNumToStr, sps)
8181

82-
db := tc.ServerConn(0)
83-
db.SetMaxOpenConns(1)
84-
r := sqlutils.MakeSQLRunner(db)
82+
conn.SetMaxOpenConns(1)
83+
r := sqlutils.MakeSQLRunner(conn)
8584
r.Exec(t, "SET DISTSQL = OFF")
86-
if _, err := tc.ServerConn(0).Exec(`CREATE INDEX foo ON numtostr (str)`); err != nil {
85+
if _, err := conn.Exec(`CREATE INDEX foo ON numtostr (str)`); err != nil {
8786
t.Fatal(err)
8887
}
8988
r.Exec(t, "SET DISTSQL = ALWAYS")

0 commit comments

Comments
 (0)