Skip to content

Commit

Permalink
Add update testcase in acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pdecat committed Jun 10, 2017
1 parent 0c95555 commit 8d02222
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 11 deletions.
2 changes: 1 addition & 1 deletion builtin/providers/google/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,4 @@ func resourceGCSBucketLifecycleRuleConditionHash(v interface{}) int {
}

return hashcode.String(buf.String())
}
}
135 changes: 125 additions & 10 deletions builtin/providers/google/resource_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func TestAccStorageBucket_lifecycleRules(t *testing.T) {
var bucket storage.Bucket
bucketName := fmt.Sprintf("tf-test-acc-bucket-%d", acctest.RandInt())

hash_action_0 := resourceGCSBucketLifecycleRuleActionHash(map[string]interface{}{"type": "SetStorageClass", "storage_class": "NEARLINE"})
hash_condition_0 := resourceGCSBucketLifecycleRuleConditionHash(map[string]interface{}{"age": 2, "created_before": "", "is_live": false, "number_of_newer_versions": 0})
hash_step0_lc0_action := resourceGCSBucketLifecycleRuleActionHash(map[string]interface{}{"type": "SetStorageClass", "storage_class": "NEARLINE"})
hash_step0_lc0_condition := resourceGCSBucketLifecycleRuleConditionHash(map[string]interface{}{"age": 2, "created_before": "", "is_live": false, "number_of_newer_versions": 0})

hash_action_1 := resourceGCSBucketLifecycleRuleActionHash(map[string]interface{}{"type": "Delete", "storage_class": ""})
hash_condition_1 := resourceGCSBucketLifecycleRuleConditionHash(map[string]interface{}{"age": 10, "created_before": "", "is_live": false, "number_of_newer_versions": 0})
hash_step0_lc1_action := resourceGCSBucketLifecycleRuleActionHash(map[string]interface{}{"type": "Delete", "storage_class": ""})
hash_step0_lc1_condition := resourceGCSBucketLifecycleRuleConditionHash(map[string]interface{}{"age": 10, "created_before": "", "is_live": false, "number_of_newer_versions": 0})

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -87,21 +87,21 @@ func TestAccStorageBucket_lifecycleRules(t *testing.T) {
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.0.action.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.0.action.%d.type", hash_action_0), "SetStorageClass"),
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.0.action.%d.type", hash_step0_lc0_action), "SetStorageClass"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.0.action.%d.storage_class", hash_action_0), "NEARLINE"),
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.0.action.%d.storage_class", hash_step0_lc0_action), "NEARLINE"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.0.condition.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.0.condition.%d.age", hash_condition_0), "2"),
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.0.condition.%d.age", hash_step0_lc0_condition), "2"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.1.action.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.1.action.%d.type", hash_action_1), "Delete"),
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.1.action.%d.type", hash_step0_lc1_action), "Delete"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.1.condition.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.1.condition.%d.age", hash_condition_1), "10"),
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.1.condition.%d.age", hash_step0_lc1_condition), "10"),
),
},
},
Expand Down Expand Up @@ -154,6 +154,15 @@ func TestAccStorageBucket_update(t *testing.T) {
var bucket storage.Bucket
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())

hash_step2_lc0_action := resourceGCSBucketLifecycleRuleActionHash(map[string]interface{}{"type": "Delete", "storage_class": ""})
hash_step2_lc0_condition := resourceGCSBucketLifecycleRuleConditionHash(map[string]interface{}{"age": 10, "created_before": "", "is_live": false, "number_of_newer_versions": 0})

hash_step3_lc0_action := resourceGCSBucketLifecycleRuleActionHash(map[string]interface{}{"type": "SetStorageClass", "storage_class": "NEARLINE"})
hash_step3_lc0_condition := resourceGCSBucketLifecycleRuleConditionHash(map[string]interface{}{"age": 2, "created_before": "", "is_live": false, "number_of_newer_versions": 0})

hash_step3_lc1_action := resourceGCSBucketLifecycleRuleActionHash(map[string]interface{}{"type": "Delete", "storage_class": ""})
hash_step3_lc1_condition := resourceGCSBucketLifecycleRuleConditionHash(map[string]interface{}{"age": 10, "created_before": "", "is_live": false, "number_of_newer_versions": 2})

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand Down Expand Up @@ -183,6 +192,64 @@ func TestAccStorageBucket_update(t *testing.T) {
"google_storage_bucket.bucket", "force_destroy", "true"),
),
},
resource.TestStep{
Config: testAccStorageBucket_customAttributes_withLifecycle1(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "predefined_acl", "publicReadWrite"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "location", "EU"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "force_destroy", "true"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.0.action.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.0.action.%d.type", hash_step2_lc0_action), "Delete"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.0.condition.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.0.condition.%d.age", hash_step2_lc0_condition), "10"),
),
},
resource.TestStep{
Config: testAccStorageBucket_customAttributes_withLifecycle2(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "predefined_acl", "publicReadWrite"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "location", "EU"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "force_destroy", "true"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.#", "2"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.0.action.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.0.action.%d.type", hash_step3_lc0_action), "SetStorageClass"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.0.action.%d.storage_class", hash_step3_lc0_action), "NEARLINE"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.0.condition.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.0.condition.%d.age", hash_step3_lc0_condition), "2"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.1.action.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.1.action.%d.type", hash_step3_lc1_action), "Delete"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.1.condition.#", "1"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.1.condition.%d.age", hash_step3_lc1_condition), "10"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", fmt.Sprintf("lifecycle_rule.1.condition.%d.number_of_newer_versions", hash_step3_lc1_condition), "2"),
),
},
},
})
}
Expand Down Expand Up @@ -392,6 +459,54 @@ resource "google_storage_bucket" "bucket" {
`, bucketName)
}

func testAccStorageBucket_customAttributes_withLifecycle1(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
predefined_acl = "publicReadWrite"
location = "EU"
force_destroy = "true"
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
}
}
}
`, bucketName)
}

func testAccStorageBucket_customAttributes_withLifecycle2(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
predefined_acl = "publicReadWrite"
location = "EU"
force_destroy = "true"
lifecycle_rule {
action {
type = "SetStorageClass"
storage_class = "NEARLINE"
}
condition {
age = 2
}
}
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
number_of_newer_versions = 2
}
}
}
`, bucketName)
}

func testAccStorageBucket_storageClass(bucketName, storageClass, location string) string {
var locationBlock string
if location != "" {
Expand Down Expand Up @@ -450,4 +565,4 @@ resource "google_storage_bucket" "bucket" {
}
}
`, bucketName)
}
}

0 comments on commit 8d02222

Please sign in to comment.