github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 TestAccAzureRMStorageContainer_root(t *testing.T) { 62 var c storage.Container 63 64 ri := acctest.RandInt() 65 rs := strings.ToLower(acctest.RandString(11)) 66 config := fmt.Sprintf(testAccAzureRMStorageContainer_root, ri, rs) 67 68 resource.Test(t, resource.TestCase{ 69 PreCheck: func() { testAccPreCheck(t) }, 70 Providers: testAccProviders, 71 CheckDestroy: testCheckAzureRMStorageContainerDestroy, 72 Steps: []resource.TestStep{ 73 { 74 Config: config, 75 Check: resource.ComposeTestCheckFunc( 76 testCheckAzureRMStorageContainerExists("azurerm_storage_container.test", &c), 77 resource.TestCheckResourceAttr("azurerm_storage_container.test", "name", "$root"), 78 ), 79 }, 80 }, 81 }) 82 } 83 84 func testCheckAzureRMStorageContainerExists(name string, c *storage.Container) resource.TestCheckFunc { 85 return func(s *terraform.State) error { 86 87 rs, ok := s.RootModule().Resources[name] 88 if !ok { 89 return fmt.Errorf("Not found: %s", name) 90 } 91 92 name := rs.Primary.Attributes["name"] 93 storageAccountName := rs.Primary.Attributes["storage_account_name"] 94 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 95 if !hasResourceGroup { 96 return fmt.Errorf("Bad: no resource group found in state for storage container: %s", name) 97 } 98 99 armClient := testAccProvider.Meta().(*ArmClient) 100 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 101 if err != nil { 102 return err 103 } 104 if !accountExists { 105 return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName) 106 } 107 108 containers, err := blobClient.ListContainers(storage.ListContainersParameters{ 109 Prefix: name, 110 Timeout: 90, 111 }) 112 113 if len(containers.Containers) == 0 { 114 return fmt.Errorf("Bad: Storage Container %q (storage account: %q) does not exist", name, storageAccountName) 115 } 116 117 var found bool 118 for _, container := range containers.Containers { 119 if container.Name == name { 120 found = true 121 *c = container 122 } 123 } 124 125 if !found { 126 return fmt.Errorf("Bad: Storage Container %q (storage account: %q) does not exist", name, storageAccountName) 127 } 128 129 return nil 130 } 131 } 132 133 func testAccARMStorageContainerDisappears(name string, c *storage.Container) resource.TestCheckFunc { 134 return func(s *terraform.State) error { 135 rs, ok := s.RootModule().Resources[name] 136 if !ok { 137 return fmt.Errorf("Not found: %s", name) 138 } 139 140 armClient := testAccProvider.Meta().(*ArmClient) 141 142 storageAccountName := rs.Primary.Attributes["storage_account_name"] 143 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 144 if !hasResourceGroup { 145 return fmt.Errorf("Bad: no resource group found in state for storage container: %s", c.Name) 146 } 147 148 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 149 if err != nil { 150 return err 151 } 152 if !accountExists { 153 log.Printf("[INFO]Storage Account %q doesn't exist so the container won't exist", storageAccountName) 154 return nil 155 } 156 157 _, err = blobClient.DeleteContainerIfExists(c.Name) 158 if err != nil { 159 return err 160 } 161 162 return nil 163 } 164 } 165 166 func testCheckAzureRMStorageContainerDestroy(s *terraform.State) error { 167 for _, rs := range s.RootModule().Resources { 168 if rs.Type != "azurerm_storage_container" { 169 continue 170 } 171 172 name := rs.Primary.Attributes["name"] 173 storageAccountName := rs.Primary.Attributes["storage_account_name"] 174 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 175 if !hasResourceGroup { 176 return fmt.Errorf("Bad: no resource group found in state for storage container: %s", name) 177 } 178 179 armClient := testAccProvider.Meta().(*ArmClient) 180 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 181 if err != nil { 182 //If we can't get keys then the blob can't exist 183 return nil 184 } 185 if !accountExists { 186 return nil 187 } 188 189 containers, err := blobClient.ListContainers(storage.ListContainersParameters{ 190 Prefix: name, 191 Timeout: 90, 192 }) 193 194 if err != nil { 195 return nil 196 } 197 198 var found bool 199 for _, container := range containers.Containers { 200 if container.Name == name { 201 found = true 202 } 203 } 204 205 if found { 206 return fmt.Errorf("Bad: Storage Container %q (storage account: %q) still exist", name, storageAccountName) 207 } 208 } 209 210 return nil 211 } 212 213 func TestValidateArmStorageContainerName(t *testing.T) { 214 validNames := []string{ 215 "valid-name", 216 "valid02-name", 217 "$root", 218 } 219 for _, v := range validNames { 220 _, errors := validateArmStorageContainerName(v, "name") 221 if len(errors) != 0 { 222 t.Fatalf("%q should be a valid Storage Container Name: %q", v, errors) 223 } 224 } 225 226 invalidNames := []string{ 227 "InvalidName1", 228 "-invalidname1", 229 "invalid_name", 230 "invalid!", 231 "ww", 232 "$notroot", 233 strings.Repeat("w", 65), 234 } 235 for _, v := range invalidNames { 236 _, errors := validateArmStorageContainerName(v, "name") 237 if len(errors) == 0 { 238 t.Fatalf("%q should be an invalid Storage Container Name", v) 239 } 240 } 241 } 242 243 var testAccAzureRMStorageContainer_basic = ` 244 resource "azurerm_resource_group" "test" { 245 name = "acctestRG-%d" 246 location = "westus" 247 } 248 249 resource "azurerm_storage_account" "test" { 250 name = "acctestacc%s" 251 resource_group_name = "${azurerm_resource_group.test.name}" 252 location = "westus" 253 account_type = "Standard_LRS" 254 255 tags { 256 environment = "staging" 257 } 258 } 259 260 resource "azurerm_storage_container" "test" { 261 name = "vhds" 262 resource_group_name = "${azurerm_resource_group.test.name}" 263 storage_account_name = "${azurerm_storage_account.test.name}" 264 container_access_type = "private" 265 } 266 ` 267 268 var testAccAzureRMStorageContainer_root = ` 269 resource "azurerm_resource_group" "test" { 270 name = "acctestRG-%d" 271 location = "westus" 272 } 273 274 resource "azurerm_storage_account" "test" { 275 name = "acctestacc%s" 276 resource_group_name = "${azurerm_resource_group.test.name}" 277 location = "westus" 278 account_type = "Standard_LRS" 279 280 tags { 281 environment = "staging" 282 } 283 } 284 285 resource "azurerm_storage_container" "test" { 286 name = "$root" 287 resource_group_name = "${azurerm_resource_group.test.name}" 288 storage_account_name = "${azurerm_storage_account.test.name}" 289 container_access_type = "private" 290 } 291 `