github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/provider_test.go (about) 1 package google 2 3 import ( 4 "io/ioutil" 5 "os" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 var testAccProviders map[string]terraform.ResourceProvider 14 var testAccProvider *schema.Provider 15 16 func init() { 17 testAccProvider = Provider().(*schema.Provider) 18 testAccProviders = map[string]terraform.ResourceProvider{ 19 "google": testAccProvider, 20 } 21 } 22 23 func TestProvider(t *testing.T) { 24 if err := Provider().(*schema.Provider).InternalValidate(); err != nil { 25 t.Fatalf("err: %s", err) 26 } 27 } 28 29 func TestProvider_impl(t *testing.T) { 30 var _ terraform.ResourceProvider = Provider() 31 } 32 33 func testAccPreCheck(t *testing.T) { 34 if v := os.Getenv("GOOGLE_CREDENTIALS_FILE"); v != "" { 35 creds, err := ioutil.ReadFile(v) 36 if err != nil { 37 t.Fatalf("Error reading GOOGLE_CREDENTIALS_FILE path: %s", err) 38 } 39 os.Setenv("GOOGLE_CREDENTIALS", string(creds)) 40 } 41 42 multiEnvSearch := func(ks []string) string { 43 for _, k := range ks { 44 if v := os.Getenv(k); v != "" { 45 return v 46 } 47 } 48 return "" 49 } 50 51 creds := []string{ 52 "GOOGLE_CREDENTIALS", 53 "GOOGLE_CLOUD_KEYFILE_JSON", 54 "GCLOUD_KEYFILE_JSON", 55 } 56 if v := multiEnvSearch(creds); v == "" { 57 t.Fatalf("One of %s must be set for acceptance tests", strings.Join(creds, ", ")) 58 } 59 60 projs := []string{ 61 "GOOGLE_PROJECT", 62 "GCLOUD_PROJECT", 63 "CLOUDSDK_CORE_PROJECT", 64 } 65 if v := multiEnvSearch(projs); v == "" { 66 t.Fatalf("One of %s must be set for acceptance tests", strings.Join(projs, ", ")) 67 } 68 69 regs := []string{ 70 "GOOGLE_REGION", 71 "GCLOUD_REGION", 72 "CLOUDSDK_COMPUTE_REGION", 73 } 74 if v := multiEnvSearch(regs); v != "us-central1" { 75 t.Fatalf("One of %s must be set to us-central1 for acceptance tests", strings.Join(regs, ", ")) 76 } 77 78 if v := os.Getenv("GOOGLE_XPN_HOST_PROJECT"); v == "" { 79 t.Fatal("GOOGLE_XPN_HOST_PROJECT must be set for acceptance tests") 80 } 81 } 82 83 func TestProvider_getRegionFromZone(t *testing.T) { 84 expected := "us-central1" 85 actual := getRegionFromZone("us-central1-f") 86 if expected != actual { 87 t.Fatalf("Region (%s) did not match expected value: %s", actual, expected) 88 } 89 }