This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from GoogleCloudPlatform/release
container-builder-local now updates metadata with the correct Service Account
- Loading branch information
Showing
15 changed files
with
391 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,22 +20,86 @@ import ( | |
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type mockRunner struct { | ||
mu sync.Mutex | ||
t *testing.T | ||
testCaseName string | ||
commands []string | ||
projectID string | ||
configHelper string | ||
} | ||
|
||
func newMockRunner(t *testing.T, projectID string) *mockRunner { | ||
return &mockRunner{ | ||
t: t, | ||
projectID: projectID, | ||
const ( | ||
projectID = "my-project-id" | ||
projectNum = 1234 | ||
// validConfig is what `gcloud config config-helper --format=json` spits out | ||
validConfig = `{ | ||
"configuration": { | ||
"active_configuration": "default", | ||
"properties": { | ||
"compute": { | ||
"region": "us-central1", | ||
"zone": "us-central1-f" | ||
}, | ||
"core": { | ||
"account": "[email protected]", | ||
"disable_usage_reporting": "False", | ||
"project": "my-project-id" | ||
} | ||
} | ||
}, | ||
"credential": { | ||
"access_token": "my-token", | ||
"token_expiry": "2100-08-24T23:46:12Z" | ||
}, | ||
"sentinels": { | ||
"config_sentinel": "/home/someone/.config/gcloud/config_sentinel" | ||
} | ||
} | ||
` | ||
cfgNoToken = `{ | ||
"configuration": { | ||
"properties": { | ||
"core": { | ||
"account": "[email protected]" | ||
} | ||
} | ||
} | ||
}` | ||
cfgNoAcct = `{ | ||
"credential": { | ||
"access_token": "my-token" | ||
} | ||
}` | ||
cfgNoExpiration = `{ | ||
"configuration": { | ||
"properties": { | ||
"core": { | ||
"account": "[email protected]" | ||
} | ||
} | ||
}, | ||
"credential": { | ||
"access_token": "my-token" | ||
} | ||
}` | ||
cfgExpired = `{ | ||
"configuration": { | ||
"properties": { | ||
"core": { | ||
"account": "[email protected]" | ||
} | ||
} | ||
}, | ||
"credential": { | ||
"access_token": "my-token", | ||
"token_expiry": "1999-01-01T00:00:00Z" | ||
} | ||
} | ||
` | ||
) | ||
|
||
// startsWith returns true iff arr startsWith parts. | ||
func startsWith(arr []string, parts ...string) bool { | ||
|
@@ -57,10 +121,10 @@ func (r *mockRunner) Run(args []string, in io.Reader, out, err io.Writer, _ stri | |
|
||
if startsWith(args, "gcloud", "config", "list") { | ||
fmt.Fprintln(out, r.projectID) | ||
} else if startsWith(args, "gcloud", "projects", "describe") { | ||
fmt.Fprintln(out, "1234") | ||
} else if startsWith(args, "gcloud", "config", "config-helper", "--format=value(credential.access_token)") { | ||
fmt.Fprintln(out, "my-token") | ||
} else if startsWith(args, "gcloud", "projects", "describe") && r.projectID == projectID { | ||
fmt.Fprintln(out, projectNum) | ||
} else if startsWith(args, "gcloud", "config", "config-helper", "--format=json") { | ||
fmt.Fprintln(out, r.configHelper) | ||
} | ||
|
||
return nil | ||
|
@@ -79,45 +143,95 @@ func (r *mockRunner) Clean() error { | |
} | ||
|
||
func TestAccessToken(t *testing.T) { | ||
r := newMockRunner(t, "") | ||
// Happy case. | ||
r := &mockRunner{configHelper: validConfig} | ||
token, err := AccessToken(r) | ||
if err != nil { | ||
t.Errorf("AccessToken failed: %v", err) | ||
} | ||
if token != "my-token" { | ||
t.Errorf("AccessToken failed returning the token; got %s, want %s", token, "my-token") | ||
if token.AccessToken != "my-token" { | ||
t.Errorf("AccessToken failed returning the token; got %q, want %q", token.AccessToken, "my-token") | ||
} | ||
if token.Email != "[email protected]" { | ||
t.Errorf("AccessToken failed returning the email; got %q, want %q", token.Email, "[email protected]") | ||
} | ||
got := strings.Join(r.commands, "\n") | ||
want := "gcloud config config-helper --format=value(credential.access_token)" | ||
want := "gcloud config config-helper --format=json" | ||
if got != want { | ||
t.Errorf("Commands didn't match!\n===Want:\n%s\n===Got:\n%s", want, got) | ||
} | ||
|
||
// We'll look for this error below... | ||
_, errParseTime := time.Parse(time.RFC3339, "") | ||
|
||
// Unhappy cases | ||
for _, tc := range []struct { | ||
desc string | ||
json string | ||
want error | ||
}{{ | ||
desc: "empty json", | ||
json: "{}", | ||
want: errTokenNotFound, | ||
}, { | ||
desc: "no token", | ||
json: cfgNoToken, | ||
want: errTokenNotFound, | ||
}, { | ||
desc: "no account", | ||
json: cfgNoAcct, | ||
want: errAcctNotFound, | ||
}, { | ||
desc: "no expiration", | ||
json: cfgNoExpiration, | ||
want: errParseTime, | ||
}, { | ||
desc: "expired", | ||
json: cfgExpired, | ||
want: errTokenExpired, | ||
}} { | ||
r.configHelper = tc.json | ||
token, err = AccessToken(r) | ||
if err.Error() != tc.want.Error() { | ||
t.Errorf("%s: got %v; want %v", tc.desc, err, tc.want) | ||
} | ||
if token != nil { | ||
t.Errorf("%s: got unexpected token %v", tc.desc, token) | ||
} | ||
} | ||
} | ||
|
||
func TestProjectInfo(t *testing.T) { | ||
r := newMockRunner(t, "my-project-id") | ||
r := &mockRunner{projectID: projectID} | ||
projectInfo, err := ProjectInfo(r) | ||
if err != nil { | ||
t.Errorf("ProjectInfo failed: %v", err) | ||
} | ||
if projectInfo.ProjectID != "my-project-id" { | ||
t.Errorf("ProjectInfo failed returning the projectID; got %s, want %s", projectInfo.ProjectID, "my-project-id") | ||
if projectInfo.ProjectID != projectID { | ||
t.Errorf("ProjectInfo failed returning the projectID; got %s, want %s", projectInfo.ProjectID, projectID) | ||
} | ||
if projectInfo.ProjectNum != 1234 { | ||
t.Errorf("ProjectInfo failed returning the projectNum; got %d, want %d", projectInfo.ProjectNum, 1234) | ||
if projectInfo.ProjectNum != projectNum { | ||
t.Errorf("ProjectInfo failed returning the projectNum; got %d, want %d", projectInfo.ProjectNum, projectNum) | ||
} | ||
|
||
got := strings.Join(r.commands, "\n") | ||
want := strings.Join([]string{`gcloud config list --format value(core.project)`, | ||
`gcloud projects describe my-project-id --format value(projectNumber)`}, "\n") | ||
fmt.Sprintf(`gcloud projects describe %s --format value(projectNumber)`, projectID)}, "\n") | ||
if got != want { | ||
t.Errorf("Commands didn't match!\n===Want:\n%s\n===Got:\n%s", want, got) | ||
} | ||
} | ||
|
||
func TestProjectInfoError(t *testing.T) { | ||
r := newMockRunner(t, "") | ||
r := &mockRunner{} | ||
_, err := ProjectInfo(r) | ||
if err == nil { | ||
t.Errorf("ProjectInfo should fail when no projectId set in gcloud") | ||
} | ||
|
||
r.projectID = "some-other-project" | ||
_, err = ProjectInfo(r) | ||
if err == nil { | ||
t.Errorf("ProjectInfo should fail when no projectNum available from gcloud") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
steps: | ||
- name: 'gcr.io/cloud-builders/gcloud' | ||
args: [ "auth", "list" ] |
Oops, something went wrong.