github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_storage_container_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 "testing" 8 9 "github.com/Azure/azure-sdk-for-go/storage" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAzureRMStorageContainer_basic(t *testing.T) { 16 var c storage.Container 17 18 ri := acctest.RandInt() 19 rs := strings.ToLower(acctest.RandString(11)) 20 config := fmt.Sprintf(testAccAzureRMStorageContainer_basic, ri, rs) 21 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testCheckAzureRMStorageContainerDestroy, 26 Steps: []resource.TestStep{ 27 { 28 Config: config, 29 Check: resource.ComposeTestCheckFunc( 30 testCheckAzureRMStorageContainerExists("azurerm_storage_container.test", &c), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccAzureRMStorageContainer_disappears(t *testing.T) { 38 var c storage.Container 39 40 ri := acctest.RandInt() 41 rs := strings.ToLower(acctest.RandString(11)) 42 config := fmt.Sprintf(testAccAzureRMStorageContainer_basic, ri, rs) 43 44 resource.Test(t, resource.TestCase{ 45 PreCheck: func() { testAccPreCheck(t) }, 46 Providers: testAccProviders, 47 CheckDestroy: testCheckAzureRMStorageContainerDestroy, 48 Steps: []resource.TestStep{ 49 { 50 Config: config, 51 Check: resource.ComposeTestCheckFunc( 52 testCheckAzureRMStorageContainerExists("azurerm_storage_container.test", &c), 53 testAccARMStorageContainerDisappears("azurerm_storage_container.test", &c), 54 ), 55 ExpectNonEmptyPlan: true, 56 }, 57 }, 58 }) 59 } 60 61 func testCheckAzureRMStorageContainerExists(name string, c *storage.Container) resource.TestCheckFunc { 62 return func(s *terraform.State) error { 63 64 rs, ok := s.RootModule().Resources[name] 65 if !ok { 66 return fmt.Errorf("Not found: %s", name) 67 } 68 69 name := rs.Primary.Attributes["name"] 70 storageAccountName := rs.Primary.Attributes["storage_account_name"] 71 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 72 if !hasResourceGroup { 73 return fmt.Errorf("Bad: no resource group found in state for storage container: %s", name) 74 } 75 76 armClient := testAccProvider.Meta().(*ArmClient) 77 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 78 if err != nil { 79 return err 80 } 81 if !accountExists { 82 return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName) 83 } 84 85 containers, err := blobClient.ListContainers(storage.ListContainersParameters{ 86 Prefix: name, 87 Timeout: 90, 88 }) 89 90 if len(containers.Containers) == 0 { 91 return fmt.Errorf("Bad: Storage Container %q (storage account: %q) does not exist", name, storageAccountName) 92 } 93 94 var found bool 95 for _, container := range containers.Containers { 96 if container.Name == name { 97 found = true 98 *c = container 99 } 100 } 101 102 if !found { 103 return fmt.Errorf("Bad: Storage Container %q (storage account: %q) does not exist", name, storageAccountName) 104 } 105 106 return nil 107 } 108 } 109 110 func testAccARMStorageContainerDisappears(name string, c *storage.Container) resource.TestCheckFunc { 111 return func(s *terraform.State) error { 112 rs, ok := s.RootModule().Resources[name] 113 if !ok { 114 return fmt.Errorf("Not found: %s", name) 115 } 116 117 armClient := testAccProvider.Meta().(*ArmClient) 118 119 storageAccountName := rs.Primary.Attributes["storage_account_name"] 120 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 121 if !hasResourceGroup { 122 return fmt.Errorf("Bad: no resource group found in state for storage container: %s", c.Name) 123 } 124 125 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 126 if err != nil { 127 return err 128 } 129 if !accountExists { 130 log.Printf("[INFO]Storage Account %q doesn't exist so the container won't exist", storageAccountName) 131 return nil 132 } 133 134 _, err = blobClient.DeleteContainerIfExists(c.Name) 135 if err != nil { 136 return err 137 } 138 139 return nil 140 } 141 } 142 143 func testCheckAzureRMStorageContainerDestroy(s *terraform.State) error { 144 for _, rs := range s.RootModule().Resources { 145 if rs.Type != "azurerm_storage_container" { 146 continue 147 } 148 149 name := rs.Primary.Attributes["name"] 150 storageAccountName := rs.Primary.Attributes["storage_account_name"] 151 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 152 if !hasResourceGroup { 153 return fmt.Errorf("Bad: no resource group found in state for storage container: %s", name) 154 } 155 156 armClient := testAccProvider.Meta().(*ArmClient) 157 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 158 if err != nil { 159 //If we can't get keys then the blob can't exist 160 return nil 161 } 162 if !accountExists { 163 return nil 164 } 165 166 containers, err := blobClient.ListContainers(storage.ListContainersParameters{ 167 Prefix: name, 168 Timeout: 90, 169 }) 170 171 if err != nil { 172 return nil 173 } 174 175 var found bool 176 for _, container := range containers.Containers { 177 if container.Name == name { 178 found = true 179 } 180 } 181 182 if found { 183 return fmt.Errorf("Bad: Storage Container %q (storage account: %q) still exist", name, storageAccountName) 184 } 185 } 186 187 return nil 188 } 189 190 func TestValidateArmStorageContainerName(t *testing.T) { 191 validNames := []string{ 192 "valid-name", 193 "valid02-name", 194 } 195 for _, v := range validNames { 196 _, errors := validateArmStorageContainerName(v, "name") 197 if len(errors) != 0 { 198 t.Fatalf("%q should be a valid Storage Container Name: %q", v, errors) 199 } 200 } 201 202 invalidNames := []string{ 203 "InvalidName1", 204 "-invalidname1", 205 "invalid_name", 206 "invalid!", 207 "ww", 208 strings.Repeat("w", 65), 209 } 210 for _, v := range invalidNames { 211 _, errors := validateArmStorageContainerName(v, "name") 212 if len(errors) == 0 { 213 t.Fatalf("%q should be an invalid Storage Container Name", v) 214 } 215 } 216 } 217 218 var testAccAzureRMStorageContainer_basic = ` 219 resource "azurerm_resource_group" "test" { 220 name = "acctestRG-%d" 221 location = "westus" 222 } 223 224 resource "azurerm_storage_account" "test" { 225 name = "acctestacc%s" 226 resource_group_name = "${azurerm_resource_group.test.name}" 227 location = "westus" 228 account_type = "Standard_LRS" 229 230 tags { 231 environment = "staging" 232 } 233 } 234 235 resource "azurerm_storage_container" "test" { 236 name = "vhds" 237 resource_group_name = "${azurerm_resource_group.test.name}" 238 storage_account_name = "${azurerm_storage_account.test.name}" 239 container_access_type = "private" 240 } 241 `