From 32a2f9c602c131e456e0fcf66a467259cd5bf60c Mon Sep 17 00:00:00 2001 From: Jared Rodriguez Date: Wed, 9 Apr 2025 16:50:56 -0700 Subject: [PATCH] replace gcloud in binary calls in pkg/v1/google tests --- pkg/v1/google/auth.go | 13 ++++++++----- pkg/v1/google/auth_test.go | 16 ++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/pkg/v1/google/auth.go b/pkg/v1/google/auth.go index 4e64eda43..74174b7fd 100644 --- a/pkg/v1/google/auth.go +++ b/pkg/v1/google/auth.go @@ -30,8 +30,11 @@ import ( const cloudPlatformScope = "https://www.googleapis.com/auth/cloud-platform" -// GetGcloudCmd is exposed so we can test this. -var GetGcloudCmd = func(ctx context.Context) *exec.Cmd { +// gcloudBin is replaced in tests to mock detecting the gcloud binary. +var gcloudBin = "gcloud" + +// getGcloudCmd is replaced in tests to drive the gcloud mock. +var getGcloudCmd = func(ctx context.Context) *exec.Cmd { // This is odd, but basically what docker-credential-gcr does. // // config-helper is undocumented, but it's purportedly the only supported way @@ -39,7 +42,7 @@ var GetGcloudCmd = func(ctx context.Context) *exec.Cmd { // // --force-auth-refresh means we are getting a token that is valid for about // an hour (we reuse it until it's expired). - return exec.CommandContext(ctx, "gcloud", "config", "config-helper", "--force-auth-refresh", "--format=json(credential)") + return exec.CommandContext(ctx, gcloudBin, "config", "config-helper", "--force-auth-refresh", "--format=json(credential)") } // NewEnvAuthenticator returns an authn.Authenticator that generates access @@ -63,13 +66,13 @@ func NewEnvAuthenticator(ctx context.Context) (authn.Authenticator, error) { // NewGcloudAuthenticator returns an oauth2.TokenSource that generates access // tokens by shelling out to the gcloud sdk. func NewGcloudAuthenticator(ctx context.Context) (authn.Authenticator, error) { - if _, err := exec.LookPath("gcloud"); err != nil { + if _, err := exec.LookPath(gcloudBin); err != nil { // gcloud is not available, fall back to anonymous logs.Warn.Println("gcloud binary not found") return authn.Anonymous, nil } - ts := gcloudSource{ctx, GetGcloudCmd} + ts := gcloudSource{ctx, getGcloudCmd} // Attempt to fetch a token to ensure gcloud is installed and we can run it. token, err := ts.Token() diff --git a/pkg/v1/google/auth_test.go b/pkg/v1/google/auth_test.go index 3b949e9f8..435622849 100644 --- a/pkg/v1/google/auth_test.go +++ b/pkg/v1/google/auth_test.go @@ -1,6 +1,3 @@ -//go:build !arm64 -// +build !arm64 - // Copyright 2018 Google LLC All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -59,9 +56,8 @@ const ( // out the gcloud dependency of gcloudSource. The exec package does this, too. // // See: https://www.joeshaw.org/testing-with-os-exec-and-testmain/ -// -// TODO(#908): This doesn't work on arm64 or darwin for some reason. func TestMain(m *testing.M) { + gcloudBin = os.Args[0] switch os.Getenv("GO_TEST_MODE") { case "": // Normal test mode @@ -113,7 +109,7 @@ func TestGcloudErrors(t *testing.T) { for _, tc := range cases { t.Run(tc.env, func(t *testing.T) { - GetGcloudCmd = newGcloudCmdMock(tc.env) + getGcloudCmd = newGcloudCmdMock(tc.env) if _, err := NewGcloudAuthenticator(ctx); err == nil { t.Errorf("wanted error, got nil") @@ -130,7 +126,7 @@ func TestGcloudSuccess(t *testing.T) { var b bytes.Buffer logs.Debug.SetOutput(&b) - GetGcloudCmd = newGcloudCmdMock("success") + getGcloudCmd = newGcloudCmdMock("success") auth, err := NewGcloudAuthenticator(ctx) if err != nil { @@ -204,7 +200,7 @@ func TestKeychainGCRandAR(t *testing.T) { Keychain = &googleKeychain{} // Gcloud should succeed. - GetGcloudCmd = newGcloudCmdMock("success") + getGcloudCmd = newGcloudCmdMock("success") if auth, err := Keychain.Resolve(mustRegistry(tc.host)); err != nil { t.Errorf("expected success for %v, got: %v", tc.host, err) @@ -215,7 +211,7 @@ func TestKeychainGCRandAR(t *testing.T) { } // Make gcloud fail to test that caching works. - GetGcloudCmd = newGcloudCmdMock("badoutput") + getGcloudCmd = newGcloudCmdMock("badoutput") if auth, err := Keychain.Resolve(mustRegistry(tc.host)); err != nil { t.Errorf("expected success for %v, got: %v", tc.host, err) @@ -233,7 +229,7 @@ func TestKeychainError(t *testing.T) { t.Fatalf("unexpected err os.Setenv: %v", err) } - GetGcloudCmd = newGcloudCmdMock("badoutput") + getGcloudCmd = newGcloudCmdMock("badoutput") // Reset the keychain to ensure we don't cache earlier results. Keychain = &googleKeychain{}