terraform/builtin/providers/openstack/provider_test.go

231 lines
5.8 KiB
Go
Raw Normal View History

2015-01-04 23:27:54 +01:00
package openstack
import (
"fmt"
"io/ioutil"
2015-01-04 23:27:54 +01:00
"os"
"testing"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/helper/pathorcontents"
2015-01-04 23:27:54 +01:00
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
2015-01-27 06:36:08 +01:00
var (
OS_EXTGW_ID = os.Getenv("OS_EXTGW_ID")
OS_FLAVOR_ID = os.Getenv("OS_FLAVOR_ID")
OS_FLAVOR_NAME = os.Getenv("OS_FLAVOR_NAME")
OS_IMAGE_ID = os.Getenv("OS_IMAGE_ID")
OS_IMAGE_NAME = os.Getenv("OS_IMAGE_NAME")
OS_NETWORK_ID = os.Getenv("OS_NETWORK_ID")
OS_POOL_NAME = os.Getenv("OS_POOL_NAME")
OS_REGION_NAME = os.Getenv("OS_REGION_NAME")
2015-01-27 06:36:08 +01:00
)
2015-01-04 23:27:54 +01:00
var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider
func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"openstack": testAccProvider,
}
}
func testAccPreCheck(t *testing.T) {
2015-01-27 06:36:08 +01:00
v := os.Getenv("OS_AUTH_URL")
if v == "" {
2015-01-04 23:27:54 +01:00
t.Fatal("OS_AUTH_URL must be set for acceptance tests")
}
if OS_IMAGE_ID == "" || OS_IMAGE_NAME == "" {
t.Fatal("OS_IMAGE_ID and OS_IMAGE_NAME must be set for acceptance tests")
2015-01-04 23:27:54 +01:00
}
if OS_POOL_NAME == "" {
2015-02-09 13:27:30 +01:00
t.Fatal("OS_POOL_NAME must be set for acceptance tests")
}
if OS_FLAVOR_ID == "" && OS_FLAVOR_NAME == "" {
t.Fatal("OS_FLAVOR_ID or OS_FLAVOR_NAME must be set for acceptance tests")
2015-01-04 23:27:54 +01:00
}
if OS_NETWORK_ID == "" {
t.Fatal("OS_NETWORK_ID must be set for acceptance tests")
}
if OS_EXTGW_ID == "" {
t.Fatal("OS_EXTGW_ID must be set for acceptance tests")
}
2015-01-04 23:27:54 +01:00
}
func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
// Steps for configuring OpenStack with SSL validation are here:
// https://github.com/hashicorp/terraform/pull/6279#issuecomment-219020144
func TestAccProvider_caCertFile(t *testing.T) {
if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping OpenStack SSL test.")
}
if os.Getenv("OS_CACERT") == "" {
t.Skip("OS_CACERT is not set; skipping OpenStack CA test.")
}
p := Provider()
caFile, err := envVarFile("OS_CACERT")
if err != nil {
t.Fatal(err)
}
defer os.Remove(caFile)
raw := map[string]interface{}{
"cacert_file": caFile,
}
rawConfig, err := config.NewRawConfig(raw)
if err != nil {
t.Fatalf("err: %s", err)
}
err = p.Configure(terraform.NewResourceConfig(rawConfig))
if err != nil {
t.Fatalf("Unexpected err when specifying OpenStack CA by file: %s", err)
}
}
func TestAccProvider_caCertString(t *testing.T) {
if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping OpenStack SSL test.")
}
if os.Getenv("OS_CACERT") == "" {
t.Skip("OS_CACERT is not set; skipping OpenStack CA test.")
}
p := Provider()
caContents, err := envVarContents("OS_CACERT")
if err != nil {
t.Fatal(err)
}
raw := map[string]interface{}{
"cacert_file": caContents,
}
rawConfig, err := config.NewRawConfig(raw)
if err != nil {
t.Fatalf("err: %s", err)
}
err = p.Configure(terraform.NewResourceConfig(rawConfig))
if err != nil {
t.Fatalf("Unexpected err when specifying OpenStack CA by string: %s", err)
}
}
func TestAccProvider_clientCertFile(t *testing.T) {
if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping OpenStack SSL test.")
}
if os.Getenv("OS_CERT") == "" || os.Getenv("OS_KEY") == "" {
t.Skip("OS_CERT or OS_KEY is not set; skipping OpenStack client SSL auth test.")
}
p := Provider()
certFile, err := envVarFile("OS_CERT")
if err != nil {
t.Fatal(err)
}
defer os.Remove(certFile)
keyFile, err := envVarFile("OS_KEY")
if err != nil {
t.Fatal(err)
}
defer os.Remove(keyFile)
raw := map[string]interface{}{
"cert": certFile,
"key": keyFile,
}
rawConfig, err := config.NewRawConfig(raw)
if err != nil {
t.Fatalf("err: %s", err)
}
err = p.Configure(terraform.NewResourceConfig(rawConfig))
if err != nil {
t.Fatalf("Unexpected err when specifying OpenStack Client keypair by file: %s", err)
}
}
func TestAccProvider_clientCertString(t *testing.T) {
if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping OpenStack SSL test.")
}
if os.Getenv("OS_CERT") == "" || os.Getenv("OS_KEY") == "" {
t.Skip("OS_CERT or OS_KEY is not set; skipping OpenStack client SSL auth test.")
}
p := Provider()
certContents, err := envVarContents("OS_CERT")
if err != nil {
t.Fatal(err)
}
keyContents, err := envVarContents("OS_KEY")
if err != nil {
t.Fatal(err)
}
raw := map[string]interface{}{
"cert": certContents,
"key": keyContents,
}
rawConfig, err := config.NewRawConfig(raw)
if err != nil {
t.Fatalf("err: %s", err)
}
err = p.Configure(terraform.NewResourceConfig(rawConfig))
if err != nil {
t.Fatalf("Unexpected err when specifying OpenStack Client keypair by contents: %s", err)
}
}
func envVarContents(varName string) (string, error) {
contents, _, err := pathorcontents.Read(os.Getenv(varName))
if err != nil {
return "", fmt.Errorf("Error reading %s: %s", varName, err)
}
return contents, nil
}
func envVarFile(varName string) (string, error) {
contents, err := envVarContents(varName)
if err != nil {
return "", err
}
tmpFile, err := ioutil.TempFile("", varName)
if err != nil {
return "", fmt.Errorf("Error creating temp file: %s", err)
}
if _, err := tmpFile.Write([]byte(contents)); err != nil {
_ = os.Remove(tmpFile.Name())
return "", fmt.Errorf("Error writing temp file: %s", err)
}
if err := tmpFile.Close(); err != nil {
_ = os.Remove(tmpFile.Name())
return "", fmt.Errorf("Error closing temp file: %s", err)
}
return tmpFile.Name(), nil
}