Skip to content

Commit

Permalink
feat(spanner): add database roles (#5701)
Browse files Browse the repository at this point in the history
* feat(spanner): add database roles

* tests

* make tests pass

* respond to PR comments

* add test for ListDatabaseRoles

* skip emulator tests

* Add check for nil

Co-authored-by: rahul2393 <[email protected]>
Co-authored-by: rahul2393 <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2022
1 parent f2b1f1b commit 6bb95ef
Show file tree
Hide file tree
Showing 7 changed files with 690 additions and 6 deletions.
6 changes: 5 additions & 1 deletion spanner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ type ClientConfig struct {
// Recommended format: ``application-or-tool-ID/major.minor.version``.
UserAgent string

// DatabaseRole specifies the role to be assumed for all operations on the
// database by this client.
DatabaseRole string

// Logger is the logger to use for this client. If it is nil, all logging
// will be directed to the standard logger.
Logger *log.Logger
Expand Down Expand Up @@ -220,7 +224,7 @@ func NewClientWithConfig(ctx context.Context, database string, config ClientConf
config.incStep = DefaultSessionPoolConfig.incStep
}
// Create a session client.
sc := newSessionClient(pool, database, config.UserAgent, sessionLabels, metadata.Pairs(resourcePrefixHeader, database), config.Logger, config.CallOptions)
sc := newSessionClient(pool, database, config.UserAgent, sessionLabels, config.DatabaseRole, metadata.Pairs(resourcePrefixHeader, database), config.Logger, config.CallOptions)
// Create a session pool.
config.SessionPoolConfig.sessionLabels = sessionLabels
sp, err := newSessionPool(sc, config.SessionPoolConfig)
Expand Down
29 changes: 29 additions & 0 deletions spanner/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,35 @@ func TestClient_ReadWriteTransaction_DoNotLeakSessionOnPanic(t *testing.T) {
}
}

func TestClient_SessionContainsDatabaseRole(t *testing.T) {
// Make sure that there is always only one session in the pool.
sc := SessionPoolConfig{
MinOpened: 1,
MaxOpened: 1,
}
server, client, teardown := setupMockedTestServerWithConfig(t, ClientConfig{SessionPoolConfig: sc, DatabaseRole: "test"})
defer teardown()

// Wait until all sessions have been created, so we know that those requests will not interfere with the test.
sp := client.idleSessions
waitFor(t, func() error {
sp.mu.Lock()
defer sp.mu.Unlock()
if uint64(sp.idleList.Len()) != 1 {
return fmt.Errorf("num open sessions mismatch.\nGot: %d\nWant: %d", sp.numOpened, sp.MinOpened)
}
return nil
})

resp, err := server.TestSpanner.GetSession(context.Background(), &sppb.GetSessionRequest{Name: client.idleSessions.idleList.Front().Value.(*session).id})
if err != nil {
t.Fatalf("Failed to get session unexpectedly: %v", err)
}
if g, w := resp.CreatorRole, "test"; g != w {
t.Fatalf("database role mismatch.\nGot: %v\nWant: %v", g, w)
}
}

func TestClient_SessionNotFound(t *testing.T) {
// Ensure we always have at least one session in the pool.
sc := SessionPoolConfig{
Expand Down
Loading

0 comments on commit 6bb95ef

Please sign in to comment.