Skip to content

Commit 32a2f9c

Browse files
committed
replace gcloud in binary calls in pkg/v1/google tests
1 parent 098045d commit 32a2f9c

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

pkg/v1/google/auth.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ import (
3030

3131
const cloudPlatformScope = "https://www.googleapis.com/auth/cloud-platform"
3232

33-
// GetGcloudCmd is exposed so we can test this.
34-
var GetGcloudCmd = func(ctx context.Context) *exec.Cmd {
33+
// gcloudBin is replaced in tests to mock detecting the gcloud binary.
34+
var gcloudBin = "gcloud"
35+
36+
// getGcloudCmd is replaced in tests to drive the gcloud mock.
37+
var getGcloudCmd = func(ctx context.Context) *exec.Cmd {
3538
// This is odd, but basically what docker-credential-gcr does.
3639
//
3740
// config-helper is undocumented, but it's purportedly the only supported way
3841
// of accessing tokens (`gcloud auth print-access-token` is discouraged).
3942
//
4043
// --force-auth-refresh means we are getting a token that is valid for about
4144
// an hour (we reuse it until it's expired).
42-
return exec.CommandContext(ctx, "gcloud", "config", "config-helper", "--force-auth-refresh", "--format=json(credential)")
45+
return exec.CommandContext(ctx, gcloudBin, "config", "config-helper", "--force-auth-refresh", "--format=json(credential)")
4346
}
4447

4548
// NewEnvAuthenticator returns an authn.Authenticator that generates access
@@ -63,13 +66,13 @@ func NewEnvAuthenticator(ctx context.Context) (authn.Authenticator, error) {
6366
// NewGcloudAuthenticator returns an oauth2.TokenSource that generates access
6467
// tokens by shelling out to the gcloud sdk.
6568
func NewGcloudAuthenticator(ctx context.Context) (authn.Authenticator, error) {
66-
if _, err := exec.LookPath("gcloud"); err != nil {
69+
if _, err := exec.LookPath(gcloudBin); err != nil {
6770
// gcloud is not available, fall back to anonymous
6871
logs.Warn.Println("gcloud binary not found")
6972
return authn.Anonymous, nil
7073
}
7174

72-
ts := gcloudSource{ctx, GetGcloudCmd}
75+
ts := gcloudSource{ctx, getGcloudCmd}
7376

7477
// Attempt to fetch a token to ensure gcloud is installed and we can run it.
7578
token, err := ts.Token()

pkg/v1/google/auth_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build !arm64
2-
// +build !arm64
3-
41
// Copyright 2018 Google LLC All Rights Reserved.
52
//
63
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -59,9 +56,8 @@ const (
5956
// out the gcloud dependency of gcloudSource. The exec package does this, too.
6057
//
6158
// See: https://www.joeshaw.org/testing-with-os-exec-and-testmain/
62-
//
63-
// TODO(#908): This doesn't work on arm64 or darwin for some reason.
6459
func TestMain(m *testing.M) {
60+
gcloudBin = os.Args[0]
6561
switch os.Getenv("GO_TEST_MODE") {
6662
case "":
6763
// Normal test mode
@@ -113,7 +109,7 @@ func TestGcloudErrors(t *testing.T) {
113109

114110
for _, tc := range cases {
115111
t.Run(tc.env, func(t *testing.T) {
116-
GetGcloudCmd = newGcloudCmdMock(tc.env)
112+
getGcloudCmd = newGcloudCmdMock(tc.env)
117113

118114
if _, err := NewGcloudAuthenticator(ctx); err == nil {
119115
t.Errorf("wanted error, got nil")
@@ -130,7 +126,7 @@ func TestGcloudSuccess(t *testing.T) {
130126
var b bytes.Buffer
131127
logs.Debug.SetOutput(&b)
132128

133-
GetGcloudCmd = newGcloudCmdMock("success")
129+
getGcloudCmd = newGcloudCmdMock("success")
134130

135131
auth, err := NewGcloudAuthenticator(ctx)
136132
if err != nil {
@@ -204,7 +200,7 @@ func TestKeychainGCRandAR(t *testing.T) {
204200
Keychain = &googleKeychain{}
205201

206202
// Gcloud should succeed.
207-
GetGcloudCmd = newGcloudCmdMock("success")
203+
getGcloudCmd = newGcloudCmdMock("success")
208204

209205
if auth, err := Keychain.Resolve(mustRegistry(tc.host)); err != nil {
210206
t.Errorf("expected success for %v, got: %v", tc.host, err)
@@ -215,7 +211,7 @@ func TestKeychainGCRandAR(t *testing.T) {
215211
}
216212

217213
// Make gcloud fail to test that caching works.
218-
GetGcloudCmd = newGcloudCmdMock("badoutput")
214+
getGcloudCmd = newGcloudCmdMock("badoutput")
219215

220216
if auth, err := Keychain.Resolve(mustRegistry(tc.host)); err != nil {
221217
t.Errorf("expected success for %v, got: %v", tc.host, err)
@@ -233,7 +229,7 @@ func TestKeychainError(t *testing.T) {
233229
t.Fatalf("unexpected err os.Setenv: %v", err)
234230
}
235231

236-
GetGcloudCmd = newGcloudCmdMock("badoutput")
232+
getGcloudCmd = newGcloudCmdMock("badoutput")
237233

238234
// Reset the keychain to ensure we don't cache earlier results.
239235
Keychain = &googleKeychain{}

0 commit comments

Comments
 (0)