|
| 1 | +# Default AWS provider configuration |
| 2 | +mock_provider "aws" { |
| 3 | +} |
| 4 | + |
| 5 | +# Default required test variables |
| 6 | +variables { |
| 7 | + bucket_name = "test-bucket" |
| 8 | + kms_key_arn = "arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012" |
| 9 | + readonly_iam_role_arns = [] |
| 10 | + readwrite_iam_role_arns = [] |
| 11 | + backup_enabled = false |
| 12 | +} |
| 13 | + |
| 14 | +# Test 1 |
| 15 | +run "verify_valid_lifecycle_rules" { |
| 16 | + command = plan |
| 17 | + |
| 18 | + variables { |
| 19 | + lifecycle_rules = [ |
| 20 | + { |
| 21 | + id = "log" |
| 22 | + enabled = true |
| 23 | + filter = { |
| 24 | + tags = { |
| 25 | + some = "value" |
| 26 | + another = "value2" |
| 27 | + } |
| 28 | + } |
| 29 | + transition = [ |
| 30 | + { |
| 31 | + days = 30 |
| 32 | + storage_class = "ONEZONE_IA" |
| 33 | + }, |
| 34 | + { |
| 35 | + days = 60 |
| 36 | + storage_class = "GLACIER" |
| 37 | + } |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + id = "log1" |
| 42 | + enabled = true |
| 43 | + abort_incomplete_multipart_upload_days = 7 |
| 44 | + noncurrent_version_transition = [ |
| 45 | + { |
| 46 | + days = 30 |
| 47 | + storage_class = "STANDARD_IA" |
| 48 | + } |
| 49 | + ] |
| 50 | + noncurrent_version_expiration = { |
| 51 | + days = 300 |
| 52 | + } |
| 53 | + }, |
| 54 | + { |
| 55 | + id = "expire_all_objects" |
| 56 | + status = "Enabled" |
| 57 | + expiration = { |
| 58 | + days = 7 |
| 59 | + } |
| 60 | + noncurrent_version_expiration = { |
| 61 | + noncurrent_days = 3 |
| 62 | + } |
| 63 | + abort_incomplete_multipart_upload_days = 1 |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + |
| 68 | + assert { |
| 69 | + condition = length(var.lifecycle_rules) == 3 |
| 70 | + error_message = "Expected 3 lifecycle rules" |
| 71 | + } |
| 72 | + |
| 73 | + assert { |
| 74 | + condition = alltrue([ |
| 75 | + for rule in var.lifecycle_rules : contains(keys(rule), "id") |
| 76 | + ]) |
| 77 | + error_message = "All rules must have an id" |
| 78 | + } |
| 79 | + |
| 80 | + assert { |
| 81 | + condition = alltrue([ |
| 82 | + for rule in var.lifecycle_rules : |
| 83 | + anytrue([contains(keys(rule), "enabled"), contains(keys(rule), "status")]) |
| 84 | + ]) |
| 85 | + error_message = "All rules must have either enabled or status field" |
| 86 | + } |
| 87 | + |
| 88 | + assert { |
| 89 | + condition = alltrue([ |
| 90 | + for rule in var.lifecycle_rules : |
| 91 | + anytrue([ |
| 92 | + !contains(keys(rule), "abort_incomplete_multipart_upload_days"), |
| 93 | + can(tonumber(rule.abort_incomplete_multipart_upload_days)) |
| 94 | + ]) |
| 95 | + ]) |
| 96 | + error_message = "abort_incomplete_multipart_upload_days must be a number" |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +# Test 2 |
| 101 | +run "fail_invalid_lifecycle_rules" { |
| 102 | + command = plan |
| 103 | + |
| 104 | + variables { |
| 105 | + lifecycle_rules = [ |
| 106 | + { |
| 107 | + id = "log1" |
| 108 | + enabled = true |
| 109 | + abort_incomplete_multipart_upload = { |
| 110 | + days_after_initiation = "1" |
| 111 | + } |
| 112 | + noncurrent_version_transition = [ |
| 113 | + { |
| 114 | + days = 30 |
| 115 | + storage_class = "STANDARD_IA" |
| 116 | + } |
| 117 | + ] |
| 118 | + noncurrent_version_expiration = { |
| 119 | + days = 300 |
| 120 | + } |
| 121 | + } |
| 122 | + ] |
| 123 | + } |
| 124 | + |
| 125 | + expect_failures = [ |
| 126 | + var.lifecycle_rules |
| 127 | + ] |
| 128 | + |
| 129 | + assert { |
| 130 | + condition = !alltrue([ |
| 131 | + for rule in var.lifecycle_rules : ( |
| 132 | + contains(keys(rule), "id") && |
| 133 | + (contains(keys(rule), "enabled") || contains(keys(rule), "status")) && |
| 134 | + alltrue([ |
| 135 | + for key in keys(rule) : contains([ |
| 136 | + "id", |
| 137 | + "enabled", |
| 138 | + "status", |
| 139 | + "filter", |
| 140 | + "abort_incomplete_multipart_upload_days", |
| 141 | + "expiration", |
| 142 | + "transition", |
| 143 | + "noncurrent_version_expiration", |
| 144 | + "noncurrent_version_transition" |
| 145 | + ], key) |
| 146 | + ]) |
| 147 | + ) |
| 148 | + ]) |
| 149 | + error_message = "Each lifecycle rule must contain 'id' and either 'enabled' or 'status', and may contain: 'filter', 'abort_incomplete_multipart_upload_days', 'expiration', 'transition', 'noncurrent_version_expiration', or 'noncurrent_version_transition'." |
| 150 | + } |
| 151 | +} |
0 commit comments