MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/cloud-sql-proxy / removeAuthEnvVar

Function removeAuthEnvVar

tests/connection_test.go:35–59  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

33// and then unsets GOOGLE_APPLICATION_CREDENTIALS. It returns a cleanup function
34// that restores the original setup.
35func 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
61func keyfile(t *testing.T) string {
62 path := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")

Callers 3

TestMySQLAuthenticationFunction · 0.85

Calls 2

ErrorfMethod · 0.65
TokenMethod · 0.45

Tested by

no test coverage detected