github.com/anuaimi/terraform@v0.6.4-0.20150904235404-2bf9aec61da8/builtin/providers/azure/provider_test.go (about) 1 package azure 2 3 import ( 4 "io" 5 "io/ioutil" 6 "log" 7 "os" 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 const ( 18 testAccSecurityGroupName = "terraform-security-group" 19 testAccHostedServiceName = "terraform-testing-service" 20 ) 21 22 // testAccStorageServiceName is used as the name for the Storage Service 23 // created in all storage-related tests. 24 // It is much more convenient to provide a Storage Service which 25 // has been created beforehand as the creation of one takes a lot 26 // and would greatly impede the multitude of tests which rely on one. 27 // NOTE: the storage container should be located in `West US`. 28 var testAccStorageServiceName = os.Getenv("AZURE_STORAGE") 29 30 const testAccStorageContainerName = "terraform-testing-container" 31 32 func init() { 33 testAccProvider = Provider().(*schema.Provider) 34 testAccProviders = map[string]terraform.ResourceProvider{ 35 "azure": testAccProvider, 36 } 37 } 38 39 func TestProvider(t *testing.T) { 40 if err := Provider().(*schema.Provider).InternalValidate(); err != nil { 41 t.Fatalf("err: %s", err) 42 } 43 } 44 45 func TestProvider_impl(t *testing.T) { 46 var _ terraform.ResourceProvider = Provider() 47 } 48 49 func testAccPreCheck(t *testing.T) { 50 if v := os.Getenv("AZURE_SETTINGS_FILE"); v == "" { 51 subscriptionID := os.Getenv("AZURE_SUBSCRIPTION_ID") 52 certificate := os.Getenv("AZURE_CERTIFICATE") 53 54 if subscriptionID == "" || certificate == "" { 55 t.Fatal("either AZURE_SETTINGS_FILE, or AZURE_SUBSCRIPTION_ID " + 56 "and AZURE_CERTIFICATE must be set for acceptance tests") 57 } 58 } 59 60 if v := os.Getenv("AZURE_STORAGE"); v == "" { 61 t.Fatal("AZURE_STORAGE must be set for acceptance tests") 62 } 63 } 64 65 func TestAzure_validateSettingsFile(t *testing.T) { 66 f, err := ioutil.TempFile("", "tf-test") 67 if err != nil { 68 t.Fatalf("Error creating temporary file in TestAzure_validateSettingsFile: %s", err) 69 } 70 71 fx, err := ioutil.TempFile("", "tf-test-xml") 72 if err != nil { 73 t.Fatalf("Error creating temporary file with XML in TestAzure_validateSettingsFile: %s", err) 74 } 75 76 _, err = io.WriteString(fx, "<PublishData></PublishData>") 77 if err != nil { 78 t.Fatalf("Error writing XML File: %s", err) 79 } 80 81 log.Printf("fx name: %s", fx.Name()) 82 fx.Close() 83 84 cases := []struct { 85 Input string // String of XML or a path to an XML file 86 W int // expected count of warnings 87 E int // expected count of errors 88 }{ 89 {"test", 1, 1}, 90 {f.Name(), 1, 0}, 91 {fx.Name(), 1, 0}, 92 {"<PublishData></PublishData>", 0, 0}, 93 } 94 95 for _, tc := range cases { 96 w, e := validateSettingsFile(tc.Input, "") 97 98 if len(w) != tc.W { 99 t.Errorf("Error in TestAzureValidateSettingsFile: input: %s , warnings: %#v, errors: %#v", tc.Input, w, e) 100 } 101 if len(e) != tc.E { 102 t.Errorf("Error in TestAzureValidateSettingsFile: input: %s , warnings: %#v, errors: %#v", tc.Input, w, e) 103 } 104 } 105 } 106 107 func TestAzure_isFile(t *testing.T) { 108 f, err := ioutil.TempFile("", "tf-test-file") 109 if err != nil { 110 t.Fatalf("Error creating temporary file with XML in TestAzure_isFile: %s", err) 111 } 112 cases := []struct { 113 Input string // String path to file 114 B bool // expected true/false 115 E bool // expect error 116 }{ 117 {"test", false, true}, 118 {f.Name(), true, false}, 119 } 120 121 for _, tc := range cases { 122 x, y := isFile(tc.Input) 123 if tc.B != x { 124 t.Errorf("Error in TestAzure_isFile: input: %s , returned: %#v, expected: %#v", tc.Input, x, tc.B) 125 } 126 127 if tc.E != (y != nil) { 128 t.Errorf("Error in TestAzure_isFile: input: %s , returned: %#v, expected: %#v", tc.Input, y, tc.E) 129 } 130 } 131 }