Skip to content

Commit 917e6f7

Browse files
committed
chore: refactor and add test
1 parent c73d3b1 commit 917e6f7

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

pkg/skaffold/gcp/auth.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type tokenSource struct {
7171
}
7272

7373
func (ts tokenSource) Token() (*oauth2.Token, error) {
74+
// the command return a json object containing id_token, access_token, token_expiry
7475
cmd := exec.Command("gcloud", "auth", "print-identity-token", "--format=json")
7576
var body bytes.Buffer
7677
cmd.Stdout = &body
@@ -85,20 +86,27 @@ func (ts tokenSource) Token() (*oauth2.Token, error) {
8586
return &oauth2.Token{AccessToken: t.AccessToken, Expiry: t.TokenExpiry}, nil
8687
}
8788

88-
func activeUserCredentials(ctx context.Context) (*google.Credentials, error) {
89+
func activeUserCredentialsOnce() (*google.Credentials, error) {
8990
credsOnce.Do(func() {
90-
var ts tokenSource
91-
t, err := ts.Token()
91+
c, err := activeUserCredentials()
9292
if err != nil {
9393
log.Entry(context.TODO()).Infof("unable to retrieve gcloud access token: %v", err)
9494
log.Entry(context.TODO()).Info("falling back to application default credentials")
9595
credsErr = fmt.Errorf("retrieving gcloud access token: %w", err)
9696
return
9797
}
98-
99-
c := &google.Credentials{TokenSource: oauth2.ReuseTokenSource(t, ts)}
10098
creds = c
10199
})
102100

103101
return creds, credsErr
104102
}
103+
104+
func activeUserCredentials() (*google.Credentials, error) {
105+
var ts tokenSource
106+
t, err := ts.Token()
107+
if err != nil {
108+
return nil, err
109+
}
110+
c := &google.Credentials{TokenSource: oauth2.ReuseTokenSource(t, ts)}
111+
return c, nil
112+
}

pkg/skaffold/gcp/auth_test.go

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ limitations under the License.
2020
package gcp
2121

2222
import (
23-
"context"
2423
"fmt"
2524
"testing"
2625

2726
"github.com/docker/cli/cli/config/configfile"
2827

28+
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/util"
2929
"github.com/GoogleContainerTools/skaffold/v2/testutil"
3030
)
3131

@@ -114,11 +114,45 @@ func TestAutoConfigureGCRCredentialHelper(t *testing.T) {
114114
}
115115
}
116116

117-
func TestTesting(t *testing.T) {
118-
credentials, err := activeUserCredentials(context.TODO())
119-
if err != nil {
120-
fmt.Println(err)
121-
fmt.Println("err")
117+
func TestActiveUserCredentials(t *testing.T) {
118+
output := `{
119+
"access_token": "access_token_value",
120+
"id_token": "id_token_value",
121+
"token_expiry": "2023-03-23T23:10:40Z"
122+
}`
123+
124+
tests := []struct {
125+
name string
126+
mockCommand util.Command
127+
shouldErr bool
128+
expected string
129+
}{
130+
{
131+
name: "get credential succeed",
132+
mockCommand: testutil.CmdRunWithOutput("gcloud auth print-identity-token --format=json", output).
133+
AndRunWithOutput("gcloud auth print-identity-token --format=json", output),
134+
expected: "access_token_value",
135+
},
136+
{
137+
name: "command error, get error",
138+
mockCommand: testutil.CmdRunErr("gcloud auth print-identity-token --format=json", fmt.Errorf("command exited with non-zero code")),
139+
shouldErr: true,
140+
},
141+
{
142+
name: "invalid json, get error",
143+
mockCommand: testutil.CmdRunWithOutput("gcloud auth print-identity-token --format=json", "{{{"),
144+
shouldErr: true,
145+
},
146+
}
147+
for _, test := range tests {
148+
testutil.Run(t, test.name, func(t *testutil.T) {
149+
t.Override(&util.DefaultExecCommand, test.mockCommand)
150+
out, err := activeUserCredentials()
151+
t.CheckError(test.shouldErr, err)
152+
if err == nil {
153+
token, _ := out.TokenSource.Token()
154+
t.CheckDeepEqual(test.expected, token.AccessToken)
155+
}
156+
})
122157
}
123-
fmt.Println(credentials.TokenSource.Token())
124158
}

pkg/skaffold/gcp/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func ClientOptions(ctx context.Context) []option.ClientOption {
3131
option.WithUserAgent(version.UserAgent()),
3232
}
3333

34-
creds, cErr := activeUserCredentials(ctx)
34+
creds, cErr := activeUserCredentialsOnce()
3535
if cErr == nil && creds != nil {
3636
options = append(options, option.WithCredentials(creds))
3737
}

0 commit comments

Comments
 (0)