removeAuthEnvVar retrieves an OAuth2 token and a path to a service account key and then unsets GOOGLE_APPLICATION_CREDENTIALS. It returns a cleanup function that restores the original setup.
(t *testing.T)
| 33 | // and then unsets GOOGLE_APPLICATION_CREDENTIALS. It returns a cleanup function |
| 34 | // that restores the original setup. |
| 35 | func removeAuthEnvVar(t *testing.T) (*oauth2.Token, string, func()) { |
| 36 | ts, err := google.DefaultTokenSource(context.Background(), |
| 37 | "https://www.googleapis.com/auth/cloud-platform", |
| 38 | ) |
| 39 | if err != nil { |
| 40 | t.Errorf("failed to resolve token source: %v", err) |
| 41 | } |
| 42 | tok, err := ts.Token() |
| 43 | if err != nil { |
| 44 | t.Errorf("failed to get token: %v", err) |
| 45 | } |
| 46 | if *ipType != "public" { |
| 47 | return tok, "", func() {} |
| 48 | } |
| 49 | path, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS") |
| 50 | if !ok { |
| 51 | t.Fatalf("GOOGLE_APPLICATION_CREDENTIALS was not set in the environment") |
| 52 | } |
| 53 | if err := os.Unsetenv("GOOGLE_APPLICATION_CREDENTIALS"); err != nil { |
| 54 | t.Fatalf("failed to unset GOOGLE_APPLICATION_CREDENTIALS") |
| 55 | } |
| 56 | return tok, path, func() { |
| 57 | os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", path) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func keyfile(t *testing.T) string { |
| 62 | path := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") |
no test coverage detected