Skip to content

Commit

Permalink
serverutils: add UseDatabase to TestTenantArgs
Browse files Browse the repository at this point in the history
UseDatabase is used in the changefeed tests to connect to a particular
database during the tests.

Release note: None
  • Loading branch information
stevendanna committed May 21, 2021
1 parent 71c977b commit 600b64b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 27 deletions.
5 changes: 5 additions & 0 deletions pkg/base/test_server_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,9 @@ type TestTenantArgs struct {
// ExternalIODirConfig is used to initialize the same-named
// field on the server.Config struct.
ExternalIODirConfig ExternalIODirConfig

// If set, this will be appended to the Postgres URL by functions that
// automatically open a connection to the server. That's equivalent to running
// SET DATABASE=foo, which works even if the database doesn't (yet) exist.
UseDatabase string
}
28 changes: 6 additions & 22 deletions pkg/ccl/changefeedccl/changefeed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,10 @@ func TestChangefeedTenants(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

ctx := context.Background()
defer changefeedbase.TestingSetDefaultFlushFrequency(testSinkFlushFrequency)()

kvServer, kvSQLdb, _ := serverutils.StartServer(t, base.TestServerArgs{
ExternalIODirConfig: base.ExternalIODirConfig{
DisableOutbound: true,
},
kvServer, kvSQLdb, cleanup := startTestServer(t, func(args *base.TestServerArgs) {
args.ExternalIODirConfig.DisableOutbound = true
})
defer kvServer.Stopper().Stop(ctx)
defer cleanup()

tenantArgs := base.TestTenantArgs{
// crdb_internal.create_tenant called by StartTenant
Expand All @@ -234,27 +229,16 @@ func TestChangefeedTenants(t *testing.T) {
ExternalIODirConfig: base.ExternalIODirConfig{
DisableOutbound: true,
},
UseDatabase: `d`,
}

tenantServer, tenantDB := serverutils.StartTenant(t, kvServer, tenantArgs)
tenantSQL := sqlutils.MakeSQLRunner(tenantDB)
// TODO(ssd): Cleanup this shared setup code once the refactor
// in #64693 is setttled.
tenantSQL.Exec(t, `SET CLUSTER SETTING kv.rangefeed.enabled = true`)
tenantSQL.Exec(t, `SET CLUSTER SETTING kv.closed_timestamp.target_duration = '100ms'`)
tenantSQL.Exec(t, `SET CLUSTER SETTING changefeed.experimental_poll_interval = '10ms'`)

// Database `d` is hardcoded in a number of places. Create it
// and create a new connection to that database.
tenantSQL.Exec(t, `CREATE DATABASE d`)
tenantSQL = sqlutils.MakeSQLRunner(
serverutils.OpenDBConn(t,
tenantServer.SQLAddr(), `d`, false /* insecure */, kvServer.Stopper()))
tenantSQL.Exec(t, `CREATE TABLE foo_in_tenant (pk INT PRIMARY KEY)`)
tenantSQL.Exec(t, serverSetupStatements)

tenantSQL.Exec(t, `CREATE TABLE foo_in_tenant (pk INT PRIMARY KEY)`)
t.Run("changefeed on non-tenant table fails", func(t *testing.T) {
kvSQL := sqlutils.MakeSQLRunner(kvSQLdb)
kvSQL.Exec(t, `CREATE DATABASE d`)
kvSQL.Exec(t, `CREATE TABLE d.foo (pk INT PRIMARY KEY)`)

tenantSQL.ExpectErr(t, `table "foo" does not exist`,
Expand Down
5 changes: 1 addition & 4 deletions pkg/ccl/changefeedccl/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ func sinklessTenantTestWithServerArgs(
ExternalIODirConfig: base.ExternalIODirConfig{
DisableOutbound: true,
},
UseDatabase: `d`,
}

tenantServer, tenantDB := serverutils.StartTenant(t, kvServer, tenantArgs)
Expand All @@ -361,10 +362,6 @@ func sinklessTenantTestWithServerArgs(
_, err := tenantDB.ExecContext(ctx, serverSetupStatements)
require.NoError(t, err)

// Database `d` is hardcoded in a number of
// places. Create a new connection to that database.
tenantDB = serverutils.OpenDBConn(t, tenantServer.SQLAddr(), `d`, false /* insecure */, kvServer.Stopper())

sink, cleanup := sqlutils.PGUrl(t, tenantServer.SQLAddr(), t.Name(), url.User(security.RootUser))
defer cleanup()

Expand Down
2 changes: 1 addition & 1 deletion pkg/testutils/serverutils/test_server_shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func StartTenant(
}

goDB := OpenDBConn(
t, tenant.SQLAddr(), "", false /* insecure */, stopper)
t, tenant.SQLAddr(), params.UseDatabase, false /* insecure */, stopper)
return tenant, goDB
}

Expand Down

0 comments on commit 600b64b

Please sign in to comment.