github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 reference := blobClient.GetContainerReference(c.Name) 158 options := &storage.DeleteContainerOptions{} 159 _, err = reference.DeleteIfExists(options) 160 if err != nil { 161 return err 162 } 163 164 return nil 165 } 166 } 167 168 func testCheckAzureRMStorageContainerDestroy(s *terraform.State) error { 169 for _, rs := range s.RootModule().Resources { 170 if rs.Type != "azurerm_storage_container" { 171 continue 172 } 173 174 name := rs.Primary.Attributes["name"] 175 storageAccountName := rs.Primary.Attributes["storage_account_name"] 176 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 177 if !hasResourceGroup { 178 return fmt.Errorf("Bad: no resource group found in state for storage container: %s", name) 179 } 180 181 armClient := testAccProvider.Meta().(*ArmClient) 182 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 183 if err != nil { 184 //If we can't get keys then the blob can't exist 185 return nil 186 } 187 if !accountExists { 188 return nil 189 } 190 191 containers, err := blobClient.ListContainers(storage.ListContainersParameters{ 192 Prefix: name, 193 Timeout: 90, 194 }) 195 196 if err != nil { 197 return nil 198 } 199 200 var found bool 201 for _, container := range containers.Containers { 202 if container.Name == name { 203 found = true 204 } 205 } 206 207 if found { 208 return fmt.Errorf("Bad: Storage Container %q (storage account: %q) still exist", name, storageAccountName) 209 } 210 } 211 212 return nil 213 } 214 215 func TestValidateArmStorageContainerName(t *testing.T) { 216 validNames := []string{ 217 "valid-name", 218 "valid02-name", 219 "$root", 220 } 221 for _, v := range validNames { 222 _, errors := validateArmStorageContainerName(v, "name") 223 if len(errors) != 0 { 224 t.Fatalf("%q should be a valid Storage Container Name: %q", v, errors) 225 } 226 } 227 228 invalidNames := []string{ 229 "InvalidName1", 230 "-invalidname1", 231 "invalid_name", 232 "invalid!", 233 "ww", 234 "$notroot", 235 strings.Repeat("w", 65), 236 } 237 for _, v := range invalidNames { 238 _, errors := validateArmStorageContainerName(v, "name") 239 if len(errors) == 0 { 240 t.Fatalf("%q should be an invalid Storage Container Name", v) 241 } 242 } 243 } 244 245 var testAccAzureRMStorageContainer_basic = ` 246 resource "azurerm_resource_group" "test" { 247 name = "acctestRG-%d" 248 location = "westus" 249 } 250 251 resource "azurerm_storage_account" "test" { 252 name = "acctestacc%s" 253 resource_group_name = "${azurerm_resource_group.test.name}" 254 location = "westus" 255 account_type = "Standard_LRS" 256 257 tags { 258 environment = "staging" 259 } 260 } 261 262 resource "azurerm_storage_container" "test" { 263 name = "vhds" 264 resource_group_name = "${azurerm_resource_group.test.name}" 265 storage_account_name = "${azurerm_storage_account.test.name}" 266 container_access_type = "private" 267 } 268 ` 269 270 var testAccAzureRMStorageContainer_root = ` 271 resource "azurerm_resource_group" "test" { 272 name = "acctestRG-%d" 273 location = "westus" 274 } 275 276 resource "azurerm_storage_account" "test" { 277 name = "acctestacc%s" 278 resource_group_name = "${azurerm_resource_group.test.name}" 279 location = "westus" 280 account_type = "Standard_LRS" 281 282 tags { 283 environment = "staging" 284 } 285 } 286 287 resource "azurerm_storage_container" "test" { 288 name = "$root" 289 resource_group_name = "${azurerm_resource_group.test.name}" 290 storage_account_name = "${azurerm_storage_account.test.name}" 291 container_access_type = "private" 292 } 293 `