@@ -12,11 +12,11 @@ import (
1212 "github.com/google/uuid"
1313 "github.com/stretchr/testify/assert"
1414 "github.com/stretchr/testify/require"
15+ "golang.org/x/oauth2"
16+ "golang.org/x/oauth2/google"
1517
1618 "github.com/argoproj/argo-cd/v2/util/cert"
1719 "github.com/argoproj/argo-cd/v2/util/io"
18- "golang.org/x/oauth2"
19- "golang.org/x/oauth2/google"
2020)
2121
2222type cred struct {
@@ -251,12 +251,14 @@ const invalidJSON = `{
251251`
252252
253253func TestNewGoogleCloudCreds (t * testing.T ) {
254- googleCloudCreds := NewGoogleCloudCreds (gcpServiceAccountKeyJSON )
254+ store := & memoryCredsStore {creds : make (map [string ]cred )}
255+ googleCloudCreds := NewGoogleCloudCreds (gcpServiceAccountKeyJSON , store )
255256 assert .NotNil (t , googleCloudCreds )
256257}
257258
258259func TestNewGoogleCloudCreds_invalidJSON (t * testing.T ) {
259- googleCloudCreds := NewGoogleCloudCreds (invalidJSON )
260+ store := & memoryCredsStore {creds : make (map [string ]cred )}
261+ googleCloudCreds := NewGoogleCloudCreds (invalidJSON , store )
260262 assert .Nil (t , googleCloudCreds .creds )
261263
262264 token , err := googleCloudCreds .getAccessToken ()
@@ -273,17 +275,25 @@ func TestNewGoogleCloudCreds_invalidJSON(t *testing.T) {
273275 assert .NotNil (t , err )
274276}
275277
276- func TestGoogleCloudCreds_Environ (t * testing.T ) {
278+ func TestGoogleCloudCreds_Environ_cleanup (t * testing.T ) {
279+ store := & memoryCredsStore {creds : make (map [string ]cred )}
277280 staticToken := & oauth2.Token {AccessToken : "token" }
278281 googleCloudCreds := GoogleCloudCreds {& google.Credentials {
279282 ProjectID : "my-google-project" ,
280283 TokenSource : oauth2 .StaticTokenSource (staticToken ),
281284 JSON : []byte (gcpServiceAccountKeyJSON ),
282- }}
285+ }, store }
283286
284287 closer , env , err := googleCloudCreds .Environ ()
285288 assert .NoError (t , err )
286- defer func () { _ = closer .Close () }()
287-
288- assert .Equal (t , []string {"GIT_ASKPASS=git-ask-pass.sh" , "GIT_USERNAME=argocd-service-account@my-google-project.iam.gserviceaccount.com" , "GIT_PASSWORD=token" }, env )
289+ var nonce string
290+ for _ , envVar := range env {
291+ if strings .HasPrefix (envVar , ASKPASS_NONCE_ENV ) {
292+ nonce = envVar [len (ASKPASS_NONCE_ENV )+ 1 :]
293+ break
294+ }
295+ }
296+ assert .Contains (t , store .creds , nonce )
297+ io .Close (closer )
298+ assert .NotContains (t , store .creds , nonce )
289299}
0 commit comments