Skip to content

Fix pkg/v1/google/auth tests for arm64 #2085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions pkg/v1/google/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ 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
// of accessing tokens (`gcloud auth print-access-token` is discouraged).
//
// --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
Expand All @@ -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()
Expand Down
16 changes: 6 additions & 10 deletions pkg/v1/google/auth_test.go
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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{}
Expand Down
Loading