github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/google/provider_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  var testAccProviders map[string]terraform.ResourceProvider
    15  var testAccProvider *schema.Provider
    16  
    17  func init() {
    18  	testAccProvider = Provider().(*schema.Provider)
    19  	testAccProviders = map[string]terraform.ResourceProvider{
    20  		"google": testAccProvider,
    21  	}
    22  }
    23  
    24  func TestProvider(t *testing.T) {
    25  	if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
    26  		t.Fatalf("err: %s", err)
    27  	}
    28  }
    29  
    30  func TestProvider_impl(t *testing.T) {
    31  	var _ terraform.ResourceProvider = Provider()
    32  }
    33  
    34  func testAccPreCheck(t *testing.T) {
    35  	if v := os.Getenv("GOOGLE_CREDENTIALS_FILE"); v != "" {
    36  		creds, err := ioutil.ReadFile(v)
    37  		if err != nil {
    38  			t.Fatalf("Error reading GOOGLE_CREDENTIALS_FILE path: %s", err)
    39  		}
    40  		os.Setenv("GOOGLE_CREDENTIALS", string(creds))
    41  	}
    42  
    43  	multiEnvSearch := func(ks []string) string {
    44  		for _, k := range ks {
    45  			if v := os.Getenv(k); v != "" {
    46  				return v
    47  			}
    48  		}
    49  		return ""
    50  	}
    51  
    52  	creds := []string{
    53  		"GOOGLE_CREDENTIALS",
    54  		"GOOGLE_CLOUD_KEYFILE_JSON",
    55  		"GCLOUD_KEYFILE_JSON",
    56  	}
    57  	if v := multiEnvSearch(creds); v == "" {
    58  		t.Fatalf("One of %s must be set for acceptance tests", strings.Join(creds, ", "))
    59  	}
    60  
    61  	projs := []string{
    62  		"GOOGLE_PROJECT",
    63  		"GCLOUD_PROJECT",
    64  		"CLOUDSDK_CORE_PROJECT",
    65  	}
    66  	if v := multiEnvSearch(projs); v == "" {
    67  		t.Fatalf("One of %s must be set for acceptance tests", strings.Join(projs, ", "))
    68  	}
    69  
    70  	regs := []string{
    71  		"GOOGLE_REGION",
    72  		"GCLOUD_REGION",
    73  		"CLOUDSDK_COMPUTE_REGION",
    74  	}
    75  	if v := multiEnvSearch(regs); v != "us-central1" {
    76  		t.Fatalf("One of %s must be set to us-central1 for acceptance tests", strings.Join(regs, ", "))
    77  	}
    78  
    79  	if v := os.Getenv("GOOGLE_XPN_HOST_PROJECT"); v == "" {
    80  		t.Fatal("GOOGLE_XPN_HOST_PROJECT must be set for acceptance tests")
    81  	}
    82  }
    83  
    84  func TestProvider_getRegionFromZone(t *testing.T) {
    85  	expected := "us-central1"
    86  	actual := getRegionFromZone("us-central1-f")
    87  	if expected != actual {
    88  		t.Fatalf("Region (%s) did not match expected value: %s", actual, expected)
    89  	}
    90  }
    91  
    92  // getTestRegion has the same logic as the provider's getRegion, to be used in tests.
    93  func getTestRegion(is *terraform.InstanceState, config *Config) (string, error) {
    94  	if res, ok := is.Attributes["region"]; ok {
    95  		return res, nil
    96  	}
    97  	if config.Region != "" {
    98  		return config.Region, nil
    99  	}
   100  	return "", fmt.Errorf("%q: required field is not set", "region")
   101  }
   102  
   103  // getTestProject has the same logic as the provider's getProject, to be used in tests.
   104  func getTestProject(is *terraform.InstanceState, config *Config) (string, error) {
   105  	if res, ok := is.Attributes["project"]; ok {
   106  		return res, nil
   107  	}
   108  	if config.Project != "" {
   109  		return config.Project, nil
   110  	}
   111  	return "", fmt.Errorf("%q: required field is not set", "project")
   112  }