(t *testing.T, bucketDir string)
| 69 | } |
| 70 | |
| 71 | func testStorage(t *testing.T, bucketDir string) { |
| 72 | if *bucket == "" && *configFile == "" { |
| 73 | t.Skip("Skipping test without --bucket or --config flag") |
| 74 | } |
| 75 | var refreshToken string |
| 76 | if *configFile != "" { |
| 77 | data, err := os.ReadFile(*configFile) |
| 78 | if err != nil { |
| 79 | t.Fatalf("Error reading config file %v: %v", *configFile, err) |
| 80 | } |
| 81 | var conf Config |
| 82 | if err := json.Unmarshal(data, &conf); err != nil { |
| 83 | t.Fatalf("Error decoding config file %v: %v", *configFile, err) |
| 84 | } |
| 85 | *clientID = conf.Auth.ClientID |
| 86 | *clientSecret = conf.Auth.ClientSecret |
| 87 | refreshToken = conf.Auth.RefreshToken |
| 88 | *bucket = conf.Bucket |
| 89 | } |
| 90 | if *bucket == "" { |
| 91 | t.Fatal("bucket not provided in config file or as a flag.") |
| 92 | } |
| 93 | if *clientID == "" { |
| 94 | if *clientSecret == "" { |
| 95 | // Assume auto if neither the the clientID and clientSecret are specified |
| 96 | // A service account key should be specified using the GOOGLE_APPLICATION_CREDENTIALS environment variable. |
| 97 | *clientID = "auto" |
| 98 | } else { |
| 99 | t.Fatal("client ID and client secret must be specified together. Obtain from https://console.developers.google.com/ > Project > APIs & Auth > Credentials. Should be a 'native' or 'Installed application'") |
| 100 | } |
| 101 | } |
| 102 | if *configFile == "" { |
| 103 | config := &oauth2.Config{ |
| 104 | Scopes: []string{storage.ScopeReadWrite}, |
| 105 | Endpoint: google.Endpoint, |
| 106 | ClientID: *clientID, |
| 107 | ClientSecret: *clientSecret, |
| 108 | RedirectURL: oauthutil.TitleBarRedirectURL, |
| 109 | } |
| 110 | if !metadata.OnGCE() && *clientID != "auto" { |
| 111 | token, err := oauth2.ReuseTokenSource(nil, |
| 112 | &oauthutil.TokenSource{ |
| 113 | Config: config, |
| 114 | CacheFile: *tokenCache, |
| 115 | AuthCode: func() string { |
| 116 | if *authCode == "" { |
| 117 | t.Skipf("Re-run using --auth_code= with the value obtained from %s", |
| 118 | config.AuthCodeURL("", oauth2.AccessTypeOffline, oauth2.ApprovalForce)) |
| 119 | return "" |
| 120 | } |
| 121 | return *authCode |
| 122 | }, |
| 123 | }).Token() |
| 124 | if err != nil { |
| 125 | t.Fatalf("could not acquire token: %v", err) |
| 126 | } |
| 127 | refreshToken = token.RefreshToken |
| 128 | } |
no test coverage detected