github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/azure/provider_test.go (about)

     1  package azure
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"math/rand"
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/hashicorp/terraform/config"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  	"github.com/hashicorp/terraform/terraform"
    15  	"github.com/mitchellh/go-homedir"
    16  )
    17  
    18  var testAccProviders map[string]terraform.ResourceProvider
    19  var testAccProvider *schema.Provider
    20  
    21  const (
    22  	testAccSecurityGroupName = "terraform-security-group"
    23  	testAccHostedServiceName = "terraform-testing-service"
    24  )
    25  
    26  // testAccStorageServiceName is used as the name for the Storage Service
    27  // created in all storage-related tests.
    28  // It is much more convenient to provide a Storage Service which
    29  // has been created beforehand as the creation of one takes a lot
    30  // and would greatly impede the multitude of tests which rely on one.
    31  // NOTE: the storage container should be located in `West US`.
    32  var testAccStorageServiceName = os.Getenv("AZURE_STORAGE")
    33  
    34  const testAccStorageContainerName = "terraform-testing-container"
    35  
    36  func init() {
    37  	testAccProvider = Provider().(*schema.Provider)
    38  	testAccProviders = map[string]terraform.ResourceProvider{
    39  		"azure": testAccProvider,
    40  	}
    41  }
    42  
    43  func TestProvider(t *testing.T) {
    44  	if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
    45  		t.Fatalf("err: %s", err)
    46  	}
    47  }
    48  
    49  func TestProvider_impl(t *testing.T) {
    50  	var _ terraform.ResourceProvider = Provider()
    51  }
    52  
    53  func testAccPreCheck(t *testing.T) {
    54  	if v := os.Getenv("AZURE_PUBLISH_SETTINGS"); v == "" {
    55  		subscriptionID := os.Getenv("AZURE_SUBSCRIPTION_ID")
    56  		certificate := os.Getenv("AZURE_CERTIFICATE")
    57  
    58  		if subscriptionID == "" || certificate == "" {
    59  			t.Fatal("either AZURE_PUBLISH_SETTINGS, or AZURE_SUBSCRIPTION_ID " +
    60  				"and AZURE_CERTIFICATE must be set for acceptance tests")
    61  		}
    62  	}
    63  
    64  	if v := os.Getenv("AZURE_STORAGE"); v == "" {
    65  		t.Fatal("AZURE_STORAGE must be set for acceptance tests")
    66  	}
    67  }
    68  
    69  func TestAzure_validateSettingsFile(t *testing.T) {
    70  	f, err := ioutil.TempFile("", "tf-test")
    71  	if err != nil {
    72  		t.Fatalf("Error creating temporary file in TestAzure_validateSettingsFile: %s", err)
    73  	}
    74  	defer os.Remove(f.Name())
    75  
    76  	fx, err := ioutil.TempFile("", "tf-test-xml")
    77  	if err != nil {
    78  		t.Fatalf("Error creating temporary file with XML in TestAzure_validateSettingsFile: %s", err)
    79  	}
    80  	defer os.Remove(fx.Name())
    81  	_, err = io.WriteString(fx, "<PublishData></PublishData>")
    82  	if err != nil {
    83  		t.Fatalf("Error writing XML File: %s", err)
    84  	}
    85  	fx.Close()
    86  
    87  	home, err := homedir.Dir()
    88  	if err != nil {
    89  		t.Fatalf("Error fetching homedir: %s", err)
    90  	}
    91  	fh, err := ioutil.TempFile(home, "tf-test-home")
    92  	if err != nil {
    93  		t.Fatalf("Error creating homedir-based temporary file: %s", err)
    94  	}
    95  	defer os.Remove(fh.Name())
    96  	_, err = io.WriteString(fh, "<PublishData></PublishData>")
    97  	if err != nil {
    98  		t.Fatalf("Error writing XML File: %s", err)
    99  	}
   100  	fh.Close()
   101  
   102  	r := strings.NewReplacer(home, "~")
   103  	homePath := r.Replace(fh.Name())
   104  
   105  	cases := []struct {
   106  		Input string // String of XML or a path to an XML file
   107  		W     int    // expected count of warnings
   108  		E     int    // expected count of errors
   109  	}{
   110  		{"test", 0, 1},
   111  		{f.Name(), 1, 1},
   112  		{fx.Name(), 1, 0},
   113  		{homePath, 1, 0},
   114  		{"<PublishData></PublishData>", 0, 0},
   115  	}
   116  
   117  	for _, tc := range cases {
   118  		w, e := validateSettingsFile(tc.Input, "")
   119  
   120  		if len(w) != tc.W {
   121  			t.Errorf("Error in TestAzureValidateSettingsFile: input: %s , warnings: %v, errors: %v", tc.Input, w, e)
   122  		}
   123  		if len(e) != tc.E {
   124  			t.Errorf("Error in TestAzureValidateSettingsFile: input: %s , warnings: %v, errors: %v", tc.Input, w, e)
   125  		}
   126  	}
   127  }
   128  
   129  func TestAzure_providerConfigure(t *testing.T) {
   130  	home, err := homedir.Dir()
   131  	if err != nil {
   132  		t.Fatalf("Error fetching homedir: %s", err)
   133  	}
   134  	fh, err := ioutil.TempFile(home, "tf-test-home")
   135  	if err != nil {
   136  		t.Fatalf("Error creating homedir-based temporary file: %s", err)
   137  	}
   138  	defer os.Remove(fh.Name())
   139  
   140  	_, err = io.WriteString(fh, testAzurePublishSettingsStr)
   141  	if err != nil {
   142  		t.Fatalf("err: %s", err)
   143  	}
   144  	fh.Close()
   145  
   146  	r := strings.NewReplacer(home, "~")
   147  	homePath := r.Replace(fh.Name())
   148  
   149  	cases := []struct {
   150  		SettingsFile string // String of XML or a path to an XML file
   151  		NilMeta      bool   // whether meta is expected to be nil
   152  	}{
   153  		{testAzurePublishSettingsStr, false},
   154  		{homePath, false},
   155  	}
   156  
   157  	for _, tc := range cases {
   158  		rp := Provider()
   159  		raw := map[string]interface{}{
   160  			"settings_file": tc.SettingsFile,
   161  		}
   162  
   163  		rawConfig, err := config.NewRawConfig(raw)
   164  		if err != nil {
   165  			t.Fatalf("err: %s", err)
   166  		}
   167  
   168  		err = rp.Configure(terraform.NewResourceConfig(rawConfig))
   169  		meta := rp.(*schema.Provider).Meta()
   170  		if (meta == nil) != tc.NilMeta {
   171  			t.Fatalf("expected NilMeta: %t, got meta: %#v, settings_file: %q",
   172  				tc.NilMeta, meta, tc.SettingsFile)
   173  		}
   174  	}
   175  }
   176  
   177  func genRandInt() int {
   178  	return rand.New(rand.NewSource(time.Now().UnixNano())).Int() % 100000
   179  }
   180  
   181  // testAzurePublishSettingsStr is a revoked publishsettings file
   182  const testAzurePublishSettingsStr = `
   183  <?xml version="1.0" encoding="utf-8"?>
   184  <PublishData>
   185    <PublishProfile
   186      SchemaVersion="2.0"
   187      PublishMethod="AzureServiceManagementAPI">
   188      <Subscription
   189        ServiceManagementUrl="https://management.core.windows.net"
   190        Id="a65bf94f-26b3-4fb1-9d50-6e27c6096df1"
   191        Name="terraform-testing"
   192        ManagementCertificate="MIIJ/AIBAzCCCbwGCSqGSIb3DQEHAaCCCa0EggmpMIIJpTCCBe4GCSqGSIb3DQEHAaCCBd8EggXbMIIF1zCCBdMGCyqGSIb3DQEMCgECoIIE7jCCBOowHAYKKoZIhvcNAQwBAzAOBAhqcsGLGr+LsQICB9AEggTImIsD3qDkT8IkH4qOlRanUFVQIWCUfXBf5U0QnXS/7N2a5fOeSou0dFuxXg81emaxecr8Myge9rBMHvLi2m4h9JIah6K/33hJhGu3nwiu+n7MzjwpjOfkc4tFMUqD1m/TAF32feq3hYqDjc3FLHrAXNIsrvaucmPipsfT/sq29xC6cWN1sUw6X43F18rqqDKyyGUuEMOJwK9s2Vir/oXlzl6bspVRJHCf0Yyo5+2GWhgcEWjzAOjIZCF7iciYj75aG3mUZjcJYT5DqUQyiyKD/LjWhiYkmHRioaCo4amyrCX92uFuZMIlHOk4LhU+UCyTn/dsvavdj8IH146u/5tUxOIsjP5hN3CcZS/TlMvX9W74uGr5BBs7EWvccUCrYyhmhFOl0YY2+99wob3VOUDSEF73VerYpFEM5POxFzjBj8K7NleB8lEuSjJXn9FbYVUpcZ/u1qhAYewFgf7KBWUTKPjGuf1b8nRVndSIaLyxSZOVbCfUtlAindZoBWjGzCa0opie1axZgouObFxHeo7ZJGjiO2q73YrZOqpPB0zOi/sycadHRKBp4O2Svz4WXBKqa2RV9oM4PYrRnH51cdFmCFqQ4eKGJCnc/Kzdwlt6ldMiCV6gsHTm44NcfPwZW5ivKZPG5aM2mad4rPpQiX+6dQz/ForKZj3WpwI+UIchc7fhwvKykCRpH/GLDBKVrjgWioKHcTDRiqOimCjLkJA+u4Qg/qHKkMOIyr75zfTEw7S9MTiYPSEnFJO60pt3rRrMU99N1Jw07Ld24SsmK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