Skip to content

Commit

Permalink
sql: adjust more tests to work with test tenant
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
yuzefovich committed Jul 21, 2023
1 parent e3b74b0 commit c899661
Show file tree
Hide file tree
Showing 21 changed files with 183 additions and 129 deletions.
6 changes: 2 additions & 4 deletions pkg/sql/alter_column_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils"
"github.com/cockroachdb/cockroach/pkg/sql/tests"
Expand Down Expand Up @@ -257,8 +256,7 @@ func TestAlterColumnTypeFailureRollback(t *testing.T) {
defer log.Scope(t).Close(t)

ctx := context.Background()
params, _ := tests.CreateTestServerParams()
s, db, kvDB := serverutils.StartServer(t, params)
s, db, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
sqlDB := sqlutils.MakeSQLRunner(db)
defer s.Stopper().Stop(ctx)

Expand All @@ -272,7 +270,7 @@ func TestAlterColumnTypeFailureRollback(t *testing.T) {

// Ensure that the add column and column swap mutations are cleaned up.
testutils.SucceedsSoon(t, func() error {
desc := desctestutils.TestingGetPublicTableDescriptor(kvDB, keys.SystemSQLCodec, "t", "test")
desc := desctestutils.TestingGetPublicTableDescriptor(kvDB, s.TenantOrServer().Codec(), "t", "test")
if len(desc.AllMutations()) != 0 {
return errors.New("expected no mutations on TableDescriptor")
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/sql/ambiguous_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord"
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
Expand Down Expand Up @@ -171,7 +170,7 @@ func TestAmbiguousCommit(t *testing.T) {
}

tableID := sqlutils.QueryTableID(t, sqlDB, "test", "public", "t")
tableStartKey.Store(keys.SystemSQLCodec.TablePrefix(tableID))
tableStartKey.Store(tc.TenantOrServer(0).Codec().TablePrefix(tableID))

// Wait for new table to split & replication.
if err := tc.WaitForSplitAndInitialization(tableStartKey.Load().(roachpb.Key)); err != nil {
Expand Down
19 changes: 10 additions & 9 deletions pkg/sql/backfill_num_ranges_in_span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
Expand Down Expand Up @@ -84,13 +84,14 @@ func TestNumRangesInSpanContainedBy(t *testing.T) {
},
}

tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
s, _, kvDB := serverutils.StartServer(t, base.TestServerArgs{
DefaultTestTenant: base.TestIsForStuffThatShouldWorkWithSecondaryTenantsButDoesntYet(107376),
})
ctx := context.Background()
defer tc.Stopper().Stop(ctx)
defer s.Stopper().Stop(ctx)

scratchKey := tc.ScratchRange(t)
scratchKey, err := s.ScratchRange()
require.NoError(t, err)
mkKey := func(prefix roachpb.Key, k string) roachpb.Key {
return append(prefix[:len(prefix):len(prefix)], k...)
}
Expand All @@ -106,8 +107,7 @@ func TestNumRangesInSpanContainedBy(t *testing.T) {
EndKey: mkEndKey(prefix, sp[1]),
}
}
db := tc.Server(0).DB()
dsp := tc.Server(0).ExecutorConfig().(sql.ExecutorConfig).DistSQLPlanner
dsp := s.TenantOrServer().ExecutorConfig().(sql.ExecutorConfig).DistSQLPlanner
spanString := func(sp span) string {
return sp[0] + "-" + sp[1]
}
Expand All @@ -121,7 +121,8 @@ func TestNumRangesInSpanContainedBy(t *testing.T) {
run := func(t *testing.T, c testCase) {
prefix := encoding.EncodeStringAscending(scratchKey, t.Name())
for _, split := range c.splits {
tc.SplitRangeOrFatal(t, mkKey(prefix, split))
_, _, err = s.SplitRange(mkKey(prefix, split))
require.NoError(t, err)
}
outerSpan := mkSpan(prefix, c.outer)
for _, sc := range c.subtests {
Expand All @@ -130,7 +131,7 @@ func TestNumRangesInSpanContainedBy(t *testing.T) {
for _, sp := range sc.subSpans {
spans = append(spans, mkSpan(prefix, sp))
}
total, contained, err := sql.NumRangesInSpanContainedBy(ctx, db, dsp, outerSpan, spans)
total, contained, err := sql.NumRangesInSpanContainedBy(ctx, kvDB, dsp, outerSpan, spans)
require.NoError(t, err)
require.Equal(t, c.total, total)
require.Equal(t, sc.contained, contained)
Expand Down
21 changes: 12 additions & 9 deletions pkg/sql/conn_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,12 @@ func TestErrorOnRollback(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

// We can't get the tableID programmatically here.
// The table id can be retrieved by doing.
// CREATE DATABASE test;
// CREATE TABLE test.t();
// SELECT id FROM system.namespace WHERE name = 't' AND "parentID" != 1
const targetKeyStringFmt string = "/Table/%d/1/1/0"
getTargetKey := func(s serverutils.TestServerInterface, tableID uint32) string {
if s.StartedDefaultTestTenant() {
return fmt.Sprintf("/Tenant/%d/Table/%d/1/1/0", serverutils.TestTenantID().ToUint64(), tableID)
}
return fmt.Sprintf("/Table/%d/1/1/0", tableID)
}
var targetKeyString atomic.Value
targetKeyString.Store("")
var injectedErr int64
Expand Down Expand Up @@ -363,7 +363,7 @@ CREATE DATABASE t;
CREATE TABLE t.test (k INT PRIMARY KEY, v TEXT);
`)
tableID := sqlutils.QueryTableID(t, sqlDB, "t", "public", "test")
targetKeyString.Store(fmt.Sprintf(targetKeyStringFmt, tableID))
targetKeyString.Store(getTargetKey(s, tableID))

tx, err := sqlDB.Begin()
if err != nil {
Expand Down Expand Up @@ -774,6 +774,7 @@ func TestRetriableErrorDuringPrepare(t *testing.T) {
func TestRetriableErrorDuringUpgradedTransaction(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

var retryCount int64
const numToRetry = 2 // only fail on the first two attempts
filter := newDynamicRequestFilter()
Expand All @@ -785,6 +786,7 @@ func TestRetriableErrorDuringUpgradedTransaction(t *testing.T) {
},
})
defer s.Stopper().Stop(context.Background())
codec := s.TenantOrServer().Codec()

conn, err := sqlDB.Conn(context.Background())
require.NoError(t, err)
Expand All @@ -803,7 +805,7 @@ func TestRetriableErrorDuringUpgradedTransaction(t *testing.T) {
}
if req, ok := ba.GetArg(kvpb.ConditionalPut); ok {
put := req.(*kvpb.ConditionalPutRequest)
_, tableID, err := keys.SystemSQLCodec.DecodeTablePrefix(put.Key)
_, tableID, err := codec.DecodeTablePrefix(put.Key)
if err != nil || tableID != fooTableId {
return nil
}
Expand Down Expand Up @@ -843,6 +845,7 @@ func TestErrorDuringPrepareInExplicitTransactionPropagates(t *testing.T) {
},
})
defer s.Stopper().Stop(ctx)
codec := s.TenantOrServer().Codec()

testDB := sqlutils.MakeSQLRunner(sqlDB)
testDB.Exec(t, "CREATE TABLE foo (i INT PRIMARY KEY)")
Expand Down Expand Up @@ -880,7 +883,7 @@ func TestErrorDuringPrepareInExplicitTransactionPropagates(t *testing.T) {
}
if req, ok := ba.GetArg(kvpb.Get); ok {
get := req.(*kvpb.GetRequest)
_, tableID, err := keys.SystemSQLCodec.DecodeTablePrefix(get.Key)
_, tableID, err := codec.DecodeTablePrefix(get.Key)
if err != nil || tableID != keys.NamespaceTableID {
err = nil
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/crdb_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@ func TestInternalSystemJobsAccess(t *testing.T) {
// do not disable background job creation nor job adoption. This is because creating
// users requires jobs to be created and run. Thus, this test only creates jobs of type
// jobspb.TypeImport and overrides the import resumer.
registry := s.JobRegistry().(*jobs.Registry)
registry := s.TenantOrServer().JobRegistry().(*jobs.Registry)
registry.TestingWrapResumerConstructor(jobspb.TypeImport, func(r jobs.Resumer) jobs.Resumer {
return &fakeResumer{}
})
Expand All @@ -1519,7 +1519,7 @@ func TestInternalSystemJobsAccess(t *testing.T) {
pgURL := url.URL{
Scheme: "postgres",
User: url.UserPassword(user, "test"),
Host: s.SQLAddr(),
Host: s.ServingSQLAddr(),
}
db2, err := gosql.Open("postgres", pgURL.String())
assert.NoError(t, err)
Expand Down
41 changes: 35 additions & 6 deletions pkg/sql/create_as_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ import (
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/stretchr/testify/require"
)

// TestCreateAsVTable verifies that all vtables can be used as the source of
// CREATE TABLE AS and CREATE MATERIALIZED VIEW AS.
func TestCreateAsVTable(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

ctx := context.Background()
testCluster := serverutils.StartNewTestCluster(t, 1, base.TestClusterArgs{})
defer testCluster.Stopper().Stop(ctx)
sqlRunner := sqlutils.MakeSQLRunner(testCluster.ServerConn(0))
s, db, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)
sqlRunner := sqlutils.MakeSQLRunner(db)
var p parser.Parser

i := 0
Expand Down Expand Up @@ -74,6 +76,27 @@ func TestCreateAsVTable(t *testing.T) {
}

fqName := name.FQString()
if s.StartedDefaultTestTenant() {
// Some of the virtual tables are currently only available in
// the system tenant.
// TODO(yuzefovich): update this list when #54252 is addressed.
onlySystemTenant := map[string]struct{}{
`"".crdb_internal.gossip_alerts`: {},
`"".crdb_internal.gossip_liveness`: {},
`"".crdb_internal.gossip_nodes`: {},
`"".crdb_internal.kv_flow_controller`: {},
`"".crdb_internal.kv_flow_control_handles`: {},
`"".crdb_internal.kv_flow_token_deductions`: {},
`"".crdb_internal.kv_node_status`: {},
`"".crdb_internal.kv_node_liveness`: {},
`"".crdb_internal.kv_store_status`: {},
`"".crdb_internal.node_tenant_capabilities_cache`: {},
`"".crdb_internal.tenant_usage_details`: {},
}
if _, ok := onlySystemTenant[fqName]; ok {
continue
}
}
// Filter by trace_id to prevent error when selecting from
// crdb_internal.cluster_inflight_traces:
// "pq: a trace_id value needs to be specified".
Expand Down Expand Up @@ -101,6 +124,7 @@ func TestCreateAsVTable(t *testing.T) {

func TestCreateAsShow(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

testCases := []struct {
sql string
Expand Down Expand Up @@ -267,16 +291,21 @@ func TestCreateAsShow(t *testing.T) {
}

ctx := context.Background()
testCluster := serverutils.StartNewTestCluster(t, 1, base.TestClusterArgs{})
defer testCluster.Stopper().Stop(ctx)
sqlRunner := sqlutils.MakeSQLRunner(testCluster.ServerConn(0))
s, db, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)
sqlRunner := sqlutils.MakeSQLRunner(db)

for i, testCase := range testCases {
t.Run(testCase.sql, func(t *testing.T) {
if testCase.skip {
return
}
if testCase.setup != "" {
if s.StartedDefaultTestTenant() && strings.Contains(testCase.setup, "create_tenant") {
// Only the system tenant has the ability to create other
// tenants.
return
}
sqlRunner.Exec(t, testCase.setup)
}
createTableStmt := fmt.Sprintf(
Expand Down
24 changes: 13 additions & 11 deletions pkg/sql/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descidgen"
Expand Down Expand Up @@ -83,7 +84,8 @@ func createTestTable(
// of values.
func verifyTables(
t *testing.T,
tc *testcluster.TestCluster,
kvDB *kv.DB,
codec keys.SQLCodec,
completed chan int,
expectedNumOfTables int,
descIDStart descpb.ID,
Expand All @@ -95,8 +97,7 @@ func verifyTables(
for id := range completed {
count++
tableName := fmt.Sprintf("table_%d", id)
kvDB := tc.Servers[count%tc.NumServers()].DB()
tableDesc := desctestutils.TestingGetPublicTableDescriptor(kvDB, keys.SystemSQLCodec, "test", tableName)
tableDesc := desctestutils.TestingGetPublicTableDescriptor(kvDB, codec, "test", tableName)
if tableDesc.GetID() < descIDStart {
t.Fatalf(
"table %s's ID %d is too small. Expected >= %d",
Expand All @@ -123,12 +124,11 @@ func verifyTables(

// Check that no extra descriptors have been written in the range
// descIDStart..maxID.
kvDB := tc.Servers[0].DB()
for id := descIDStart; id < maxID; id++ {
if _, ok := tableIDs[id]; ok {
continue
}
descKey := catalogkeys.MakeDescMetadataKey(keys.SystemSQLCodec, id)
descKey := catalogkeys.MakeDescMetadataKey(codec, id)
desc := &descpb.Descriptor{}
if err := kvDB.GetProto(context.Background(), descKey, desc); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -157,8 +157,8 @@ func TestParallelCreateTables(t *testing.T) {
t.Fatal(err)
}
// Get the id descriptor generator count.
s := tc.Servers[0]
idgen := descidgen.NewGenerator(s.ClusterSettings(), keys.SystemSQLCodec, s.DB())
s := tc.Servers[0].TenantOrServer()
idgen := descidgen.NewGenerator(s.ClusterSettings(), s.Codec(), s.DB())
descIDStart, err := idgen.PeekNextUniqueDescID(context.Background())
if err != nil {
t.Fatal(err)
Expand All @@ -185,7 +185,8 @@ func TestParallelCreateTables(t *testing.T) {

verifyTables(
t,
tc,
s.DB(),
s.Codec(),
completed,
numberOfTables,
descIDStart,
Expand All @@ -211,8 +212,8 @@ func TestParallelCreateConflictingTables(t *testing.T) {
}

// Get the id descriptor generator count.
s := tc.Servers[0]
idgen := descidgen.NewGenerator(s.ClusterSettings(), keys.SystemSQLCodec, s.DB())
s := tc.Servers[0].TenantOrServer()
idgen := descidgen.NewGenerator(s.ClusterSettings(), s.Codec(), s.DB())
descIDStart, err := idgen.PeekNextUniqueDescID(context.Background())
if err != nil {
t.Fatal(err)
Expand All @@ -239,7 +240,8 @@ func TestParallelCreateConflictingTables(t *testing.T) {

verifyTables(
t,
tc,
s.DB(),
s.Codec(),
completed,
1, /* expectedNumOfTables */
descIDStart,
Expand Down
10 changes: 5 additions & 5 deletions pkg/sql/distsql_plan_changefeed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"sort"
"testing"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
Expand Down Expand Up @@ -492,9 +493,9 @@ func TestCdcExpressionExecution(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

params, _ := tests.CreateTestServerParams()
s, db, kvDB := serverutils.StartServer(t, params)
s, db, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(context.Background())
tt := s.TenantOrServer()

sqlDB := sqlutils.MakeSQLRunner(db)
sqlDB.Exec(t, `CREATE TABLE foo (
Expand All @@ -506,11 +507,10 @@ FAMILY main(a,b,c),
FAMILY extra (extra)
)`)

fooDesc := desctestutils.TestingGetTableDescriptor(
kvDB, keys.SystemSQLCodec, "defaultdb", "public", "foo")
fooDesc := desctestutils.TestingGetPublicTableDescriptor(kvDB, tt.Codec(), "defaultdb", "foo")

ctx := context.Background()
execCfg := s.ExecutorConfig().(ExecutorConfig)
execCfg := tt.ExecutorConfig().(ExecutorConfig)
sd := NewInternalSessionData(ctx, execCfg.Settings, "test")
sd.Database = "defaultdb"
p, cleanup := NewInternalPlanner("test", kv.NewTxn(ctx, kvDB, s.NodeID()),
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/distsql_running_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ func TestDistSQLReceiverDrainsMeta(t *testing.T) {
Insecure: true,
}})
defer tc.Stopper().Stop(ctx)
SecondaryTenantSplitAtEnabled.Override(ctx, &tc.Server(0).TenantOrServer().ClusterSettings().SV, true)

// Create a table with 30 rows, split them into 3 ranges with each node
// having one.
Expand Down
Loading

0 comments on commit c899661

Please sign in to comment.