github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/openstack/provider_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/config"
    10  	"github.com/hashicorp/terraform/helper/pathorcontents"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  var (
    16  	OS_EXTGW_ID    = os.Getenv("OS_EXTGW_ID")
    17  	OS_FLAVOR_ID   = os.Getenv("OS_FLAVOR_ID")
    18  	OS_FLAVOR_NAME = os.Getenv("OS_FLAVOR_NAME")
    19  	OS_IMAGE_ID    = os.Getenv("OS_IMAGE_ID")
    20  	OS_IMAGE_NAME  = os.Getenv("OS_IMAGE_NAME")
    21  	OS_NETWORK_ID  = os.Getenv("OS_NETWORK_ID")
    22  	OS_POOL_NAME   = os.Getenv("OS_POOL_NAME")
    23  	OS_REGION_NAME = os.Getenv("OS_REGION_NAME")
    24  )
    25  
    26  var testAccProviders map[string]terraform.ResourceProvider
    27  var testAccProvider *schema.Provider
    28  
    29  func init() {
    30  	testAccProvider = Provider().(*schema.Provider)
    31  	testAccProviders = map[string]terraform.ResourceProvider{
    32  		"openstack": testAccProvider,
    33  	}
    34  }
    35  
    36  func testAccPreCheck(t *testing.T) {
    37  	v := os.Getenv("OS_AUTH_URL")
    38  	if v == "" {
    39  		t.Fatal("OS_AUTH_URL must be set for acceptance tests")
    40  	}
    41  
    42  	if OS_IMAGE_ID == "" || OS_IMAGE_NAME == "" {
    43  		t.Fatal("OS_IMAGE_ID and OS_IMAGE_NAME must be set for acceptance tests")
    44  	}
    45  
    46  	if OS_POOL_NAME == "" {
    47  		t.Fatal("OS_POOL_NAME must be set for acceptance tests")
    48  	}
    49  
    50  	if OS_FLAVOR_ID == "" && OS_FLAVOR_NAME == "" {
    51  		t.Fatal("OS_FLAVOR_ID or OS_FLAVOR_NAME must be set for acceptance tests")
    52  	}
    53  
    54  	if OS_NETWORK_ID == "" {
    55  		t.Fatal("OS_NETWORK_ID must be set for acceptance tests")
    56  	}
    57  
    58  	if OS_EXTGW_ID == "" {
    59  		t.Fatal("OS_EXTGW_ID must be set for acceptance tests")
    60  	}
    61  }
    62  
    63  func testAccPreCheckAdminOnly(t *testing.T) {
    64  	v := os.Getenv("OS_USERNAME")
    65  	if v != "admin" {
    66  		t.Skip("Skipping test because it requires the admin user")
    67  	}
    68  }
    69  
    70  func TestProvider(t *testing.T) {
    71  	if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
    72  		t.Fatalf("err: %s", err)
    73  	}
    74  }
    75  
    76  func TestProvider_impl(t *testing.T) {
    77  	var _ terraform.ResourceProvider = Provider()
    78  }
    79  
    80  // Steps for configuring OpenStack with SSL validation are here:
    81  // https://github.com/hashicorp/terraform/pull/6279#issuecomment-219020144
    82  func TestAccProvider_caCertFile(t *testing.T) {
    83  	if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
    84  		t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping OpenStack SSL test.")
    85  	}
    86  	if os.Getenv("OS_CACERT") == "" {
    87  		t.Skip("OS_CACERT is not set; skipping OpenStack CA test.")
    88  	}
    89  
    90  	p := Provider()
    91  
    92  	caFile, err := envVarFile("OS_CACERT")
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	defer os.Remove(caFile)
    97  
    98  	raw := map[string]interface{}{
    99  		"cacert_file": caFile,
   100  	}
   101  	rawConfig, err := config.NewRawConfig(raw)
   102  	if err != nil {
   103  		t.Fatalf("err: %s", err)
   104  	}
   105  
   106  	err = p.Configure(terraform.NewResourceConfig(rawConfig))
   107  	if err != nil {
   108  		t.Fatalf("Unexpected err when specifying OpenStack CA by file: %s", err)
   109  	}
   110  }
   111  
   112  func TestAccProvider_caCertString(t *testing.T) {
   113  	if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
   114  		t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping OpenStack SSL test.")
   115  	}
   116  	if os.Getenv("OS_CACERT") == "" {
   117  		t.Skip("OS_CACERT is not set; skipping OpenStack CA test.")
   118  	}
   119  
   120  	p := Provider()
   121  
   122  	caContents, err := envVarContents("OS_CACERT")
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	raw := map[string]interface{}{
   127  		"cacert_file": caContents,
   128  	}
   129  	rawConfig, err := config.NewRawConfig(raw)
   130  	if err != nil {
   131  		t.Fatalf("err: %s", err)
   132  	}
   133  
   134  	err = p.Configure(terraform.NewResourceConfig(rawConfig))
   135  	if err != nil {
   136  		t.Fatalf("Unexpected err when specifying OpenStack CA by string: %s", err)
   137  	}
   138  }
   139  
   140  func TestAccProvider_clientCertFile(t *testing.T) {
   141  	if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
   142  		t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping OpenStack SSL test.")
   143  	}
   144  	if os.Getenv("OS_CERT") == "" || os.Getenv("OS_KEY") == "" {
   145  		t.Skip("OS_CERT or OS_KEY is not set; skipping OpenStack client SSL auth test.")
   146  	}
   147  
   148  	p := Provider()
   149  
   150  	certFile, err := envVarFile("OS_CERT")
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  	defer os.Remove(certFile)
   155  	keyFile, err := envVarFile("OS_KEY")
   156  	if err != nil {
   157  		t.Fatal(err)
   158  	}
   159  	defer os.Remove(keyFile)
   160  
   161  	raw := map[string]interface{}{
   162  		"cert": certFile,
   163  		"key":  keyFile,
   164  	}
   165  	rawConfig, err := config.NewRawConfig(raw)
   166  	if err != nil {
   167  		t.Fatalf("err: %s", err)
   168  	}
   169  
   170  	err = p.Configure(terraform.NewResourceConfig(rawConfig))
   171  	if err != nil {
   172  		t.Fatalf("Unexpected err when specifying OpenStack Client keypair by file: %s", err)
   173  	}
   174  }
   175  
   176  func TestAccProvider_clientCertString(t *testing.T) {
   177  	if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
   178  		t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping OpenStack SSL test.")
   179  	}
   180  	if os.Getenv("OS_CERT") == "" || os.Getenv("OS_KEY") == "" {
   181  		t.Skip("OS_CERT or OS_KEY is not set; skipping OpenStack client SSL auth test.")
   182  	}
   183  
   184  	p := Provider()
   185  
   186  	certContents, err := envVarContents("OS_CERT")
   187  	if err != nil {
   188  		t.Fatal(err)
   189  	}
   190  	keyContents, err := envVarContents("OS_KEY")
   191  	if err != nil {
   192  		t.Fatal(err)
   193  	}
   194  
   195  	raw := map[string]interface{}{
   196  		"cert": certContents,
   197  		"key":  keyContents,
   198  	}
   199  	rawConfig, err := config.NewRawConfig(raw)
   200  	if err != nil {
   201  		t.Fatalf("err: %s", err)
   202  	}
   203  
   204  	err = p.Configure(terraform.NewResourceConfig(rawConfig))
   205  	if err != nil {
   206  		t.Fatalf("Unexpected err when specifying OpenStack Client keypair by contents: %s", err)
   207  	}
   208  }
   209  
   210  func envVarContents(varName string) (string, error) {
   211  	contents, _, err := pathorcontents.Read(os.Getenv(varName))
   212  	if err != nil {
   213  		return "", fmt.Errorf("Error reading %s: %s", varName, err)
   214  	}
   215  	return contents, nil
   216  }
   217  
   218  func envVarFile(varName string) (string, error) {
   219  	contents, err := envVarContents(varName)
   220  	if err != nil {
   221  		return "", err
   222  	}
   223  
   224  	tmpFile, err := ioutil.TempFile("", varName)
   225  	if err != nil {
   226  		return "", fmt.Errorf("Error creating temp file: %s", err)
   227  	}
   228  	if _, err := tmpFile.Write([]byte(contents)); err != nil {
   229  		_ = os.Remove(tmpFile.Name())
   230  		return "", fmt.Errorf("Error writing temp file: %s", err)
   231  	}
   232  	if err := tmpFile.Close(); err != nil {
   233  		_ = os.Remove(tmpFile.Name())
   234  		return "", fmt.Errorf("Error closing temp file: %s", err)
   235  	}
   236  	return tmpFile.Name(), nil
   237  }