credentials/google: stub out the oauth package in test (#5118)

This commit is contained in:
Easwar Swaminathan 2022-01-11 14:42:12 -08:00 committed by GitHub
parent 13c41bce4a
commit 9353ae3bb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View File

@ -50,7 +50,7 @@ func NewDefaultCredentialsWithOptions(opts DefaultCredentialsOptions) credential
ctx, cancel := context.WithTimeout(context.Background(), tokenRequestTimeout)
defer cancel()
var err error
opts.PerRPCCreds, err = oauth.NewApplicationDefault(ctx)
opts.PerRPCCreds, err = newADC(ctx)
if err != nil {
logger.Warningf("NewDefaultCredentialsWithOptions: failed to create application oauth: %v", err)
}
@ -112,6 +112,9 @@ var (
newALTS = func() credentials.TransportCredentials {
return alts.NewClientCreds(alts.DefaultClientOptions())
}
newADC = func(ctx context.Context) (credentials.PerRPCCredentials, error) {
return oauth.NewApplicationDefault(ctx)
}
)
// NewWithMode should make a copy of Bundle, and switch mode. Modifying the

View File

@ -65,17 +65,24 @@ var (
)
func overrideNewCredsFuncs() func() {
oldNewTLS := newTLS
origNewTLS := newTLS
newTLS = func() credentials.TransportCredentials {
return testTLS
}
oldNewALTS := newALTS
origNewALTS := newALTS
newALTS = func() credentials.TransportCredentials {
return testALTS
}
origNewADC := newADC
newADC = func(context.Context) (credentials.PerRPCCredentials, error) {
// We do not use perRPC creds in this test. It is safe to return nil here.
return nil, nil
}
return func() {
newTLS = oldNewTLS
newALTS = oldNewALTS
newTLS = origNewTLS
newALTS = origNewALTS
newADC = origNewADC
}
}