You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

142 line
4.6 KiB

  1. // Copyright 2014 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package testutil contains helper functions for writing tests.
  15. package testutil
  16. import (
  17. "context"
  18. "fmt"
  19. "io/ioutil"
  20. "log"
  21. "os"
  22. "golang.org/x/oauth2"
  23. "golang.org/x/oauth2/google"
  24. "golang.org/x/oauth2/jwt"
  25. )
  26. const (
  27. envProjID = "GCLOUD_TESTS_GOLANG_PROJECT_ID"
  28. envPrivateKey = "GCLOUD_TESTS_GOLANG_KEY"
  29. )
  30. // ProjID returns the project ID to use in integration tests, or the empty
  31. // string if none is configured.
  32. func ProjID() string {
  33. return os.Getenv(envProjID)
  34. }
  35. // Credentials returns the credentials to use in integration tests, or nil if
  36. // none is configured. It uses the standard environment variable for tests in
  37. // this repo.
  38. func Credentials(ctx context.Context, scopes ...string) *google.Credentials {
  39. return CredentialsEnv(ctx, envPrivateKey, scopes...)
  40. }
  41. // CredentialsEnv returns the credentials to use in integration tests, or nil
  42. // if none is configured. If the environment variable is unset, CredentialsEnv
  43. // will try to find 'Application Default Credentials'. Else, CredentialsEnv
  44. // will return nil. CredentialsEnv will log.Fatal if the token source is
  45. // specified but missing or invalid.
  46. func CredentialsEnv(ctx context.Context, envVar string, scopes ...string) *google.Credentials {
  47. key := os.Getenv(envVar)
  48. if key == "" { // Try for application default credentials.
  49. creds, err := google.FindDefaultCredentials(ctx, scopes...)
  50. if err != nil {
  51. log.Println("No 'Application Default Credentials' found.")
  52. return nil
  53. }
  54. return creds
  55. }
  56. data, err := ioutil.ReadFile(key)
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. creds, err := google.CredentialsFromJSON(ctx, data, scopes...)
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. return creds
  65. }
  66. // TokenSource returns the OAuth2 token source to use in integration tests,
  67. // or nil if none is configured. It uses the standard environment variable
  68. // for tests in this repo.
  69. func TokenSource(ctx context.Context, scopes ...string) oauth2.TokenSource {
  70. return TokenSourceEnv(ctx, envPrivateKey, scopes...)
  71. }
  72. // TokenSourceEnv returns the OAuth2 token source to use in integration tests. or nil
  73. // if none is configured. It tries to get credentials from the filename in the
  74. // environment variable envVar. If the environment variable is unset, TokenSourceEnv
  75. // will try to find 'Application Default Credentials'. Else, TokenSourceEnv will
  76. // return nil. TokenSourceEnv will log.Fatal if the token source is specified but
  77. // missing or invalid.
  78. func TokenSourceEnv(ctx context.Context, envVar string, scopes ...string) oauth2.TokenSource {
  79. key := os.Getenv(envVar)
  80. if key == "" { // Try for application default credentials.
  81. ts, err := google.DefaultTokenSource(ctx, scopes...)
  82. if err != nil {
  83. log.Println("No 'Application Default Credentials' found.")
  84. return nil
  85. }
  86. return ts
  87. }
  88. conf, err := jwtConfigFromFile(key, scopes)
  89. if err != nil {
  90. log.Fatal(err)
  91. }
  92. return conf.TokenSource(ctx)
  93. }
  94. // JWTConfig reads the JSON private key file whose name is in the default
  95. // environment variable, and returns the jwt.Config it contains. It ignores
  96. // scopes.
  97. // If the environment variable is empty, it returns (nil, nil).
  98. func JWTConfig() (*jwt.Config, error) {
  99. return jwtConfigFromFile(os.Getenv(envPrivateKey), nil)
  100. }
  101. // jwtConfigFromFile reads the given JSON private key file, and returns the
  102. // jwt.Config it contains.
  103. // If the filename is empty, it returns (nil, nil).
  104. func jwtConfigFromFile(filename string, scopes []string) (*jwt.Config, error) {
  105. if filename == "" {
  106. return nil, nil
  107. }
  108. jsonKey, err := ioutil.ReadFile(filename)
  109. if err != nil {
  110. return nil, fmt.Errorf("cannot read the JSON key file, err: %v", err)
  111. }
  112. conf, err := google.JWTConfigFromJSON(jsonKey, scopes...)
  113. if err != nil {
  114. return nil, fmt.Errorf("google.JWTConfigFromJSON: %v", err)
  115. }
  116. return conf, nil
  117. }
  118. // CanReplay reports whether an integration test can be run in replay mode.
  119. // The replay file must exist, and the GCLOUD_TESTS_GOLANG_ENABLE_REPLAY
  120. // environment variable must be non-empty.
  121. func CanReplay(replayFilename string) bool {
  122. if os.Getenv("GCLOUD_TESTS_GOLANG_ENABLE_REPLAY") == "" {
  123. return false
  124. }
  125. _, err := os.Stat(replayFilename)
  126. return err == nil
  127. }