Skip to content

Commit

Permalink
Add test to update resource
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartleeks committed Oct 2, 2019
1 parent 77b5dec commit 81ac702
Showing 1 changed file with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions azurerm/resource_arm_storage_management_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,113 @@ func TestAccAzureRMStorageManagementPolicy_multipleRule(t *testing.T) {
})
}

func TestAccAzureRMStorageManagementPolicy_updateMultipleRule(t *testing.T) {
resourceName := "azurerm_storage_management_policy.testpolicy"
ri := tf.AccRandTimeInt()
rs := acctest.RandString(4)
location := testLocation()
config1 := testAccAzureRMStorageManagementPolicy_multipleRule(ri, rs, location)
config2 := testAccAzureRMStorageManagementPolicy_multipleRuleUpdated(ri, rs, location)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMStorageAccountManagementPolicyDestroy(),
Steps: []resource.TestStep{
{
Config: config1,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountManagementPolicyExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "2"),

// Rule1
resource.TestCheckResourceAttr(resourceName, "rule.0.name", "rule1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "rule.0.filters.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.filters.0.prefix_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.filters.0.prefix_match.3439697764", "container1/prefix1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.filters.0.blob_types.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.filters.0.blob_types.1068358194", "blockBlob"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.base_blob.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.base_blob.0.tier_to_cool_after_days_since_modification_greater_than", "10"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.base_blob.0.tier_to_archive_after_days_since_modification_greater_than", "50"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.base_blob.0.delete_after_days_since_modification_greater_than", "100"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.snapshot.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.snapshot.0.delete_after_days_since_creation_greater_than", "30"),

// Rule2
resource.TestCheckResourceAttr(resourceName, "rule.1.name", "rule2"),
resource.TestCheckResourceAttr(resourceName, "rule.1.enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.0.prefix_match.#", "2"),
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.0.prefix_match.4102595489", "container2/prefix1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.0.prefix_match.1837232667", "container2/prefix2"),
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.0.blob_types.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.0.blob_types.1068358194", "blockBlob"),
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.base_blob.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.base_blob.0.tier_to_cool_after_days_since_modification_greater_than", "11"),
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.base_blob.0.tier_to_archive_after_days_since_modification_greater_than", "51"),
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.base_blob.0.delete_after_days_since_modification_greater_than", "101"),
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.snapshot.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.snapshot.0.delete_after_days_since_creation_greater_than", "31"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: config2,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountManagementPolicyExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "2"),

// Rule1
resource.TestCheckResourceAttr(resourceName, "rule.0.name", "rule1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "rule.0.filters.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.filters.0.prefix_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.filters.0.prefix_match.3439697764", "container1/prefix1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.filters.0.blob_types.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.filters.0.blob_types.1068358194", "blockBlob"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.base_blob.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.base_blob.0.tier_to_cool_after_days_since_modification_greater_than", "10"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.base_blob.0.tier_to_archive_after_days_since_modification_greater_than", "50"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.base_blob.0.delete_after_days_since_modification_greater_than", "100"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.snapshot.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.actions.0.snapshot.0.delete_after_days_since_creation_greater_than", "30"),

// Rule2
resource.TestCheckResourceAttr(resourceName, "rule.1.name", "rule2"),
resource.TestCheckResourceAttr(resourceName, "rule.1.enabled", "true"), // check updated
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.0.prefix_match.#", "2"),
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.0.prefix_match.4102595489", "container2/prefix1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.0.prefix_match.1837232667", "container2/prefix2"),
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.0.blob_types.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.filters.0.blob_types.1068358194", "blockBlob"),
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.base_blob.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.base_blob.0.tier_to_cool_after_days_since_modification_greater_than", "12"), // check updated
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.base_blob.0.tier_to_archive_after_days_since_modification_greater_than", "52"), // check updated
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.base_blob.0.delete_after_days_since_modification_greater_than", "102"), // check updated
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.snapshot.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.1.actions.0.snapshot.0.delete_after_days_since_creation_greater_than", "32"), // check updated
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckAzureRMStorageAccountManagementPolicyDestroy() resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
Expand Down Expand Up @@ -277,3 +384,63 @@ resource "azurerm_storage_management_policy" "testpolicy" {
}
`, rInt, location, rString)
}

func testAccAzureRMStorageManagementPolicy_multipleRuleUpdated(rInt int, rString string, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "testrg" {
name = "acctestAzureRMSA-%d"
location = "%s"
}
resource "azurerm_storage_account" "testsa" {
name = "unlikely23exst2acct%s"
resource_group_name = "${azurerm_resource_group.testrg.name}"
location = "${azurerm_resource_group.testrg.location}"
account_tier = "Standard"
account_replication_type = "LRS"
account_kind = "BlobStorage"
}
resource "azurerm_storage_management_policy" "testpolicy" {
storage_account_id = "${azurerm_storage_account.testsa.id}"
rule {
name = "rule1"
enabled = true
filters {
prefix_match = [ "container1/prefix1" ]
blob_types = [ "blockBlob" ]
}
actions {
base_blob {
tier_to_cool_after_days_since_modification_greater_than = 10
tier_to_archive_after_days_since_modification_greater_than = 50
delete_after_days_since_modification_greater_than = 100
}
snapshot {
delete_after_days_since_creation_greater_than = 30
}
}
}
rule {
name = "rule2"
enabled = true
filters {
prefix_match = [ "container2/prefix1", "container2/prefix2" ]
blob_types = [ "blockBlob" ]
}
actions {
base_blob {
tier_to_cool_after_days_since_modification_greater_than = 12
tier_to_archive_after_days_since_modification_greater_than = 52
delete_after_days_since_modification_greater_than = 102
}
snapshot {
delete_after_days_since_creation_greater_than = 32
}
}
}
}
`, rInt, location, rString)
}

0 comments on commit 81ac702

Please sign in to comment.