|
1 | 1 | package test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
4 | 6 | "testing" |
5 | 7 |
|
| 8 | + "github.com/gruntwork-io/terratest/modules/random" |
6 | 9 | "github.com/gruntwork-io/terratest/modules/terraform" |
7 | 10 | "github.com/stretchr/testify/assert" |
8 | 11 | ) |
9 | 12 |
|
10 | 13 | func TestWafWebAclV2Logging(t *testing.T) { |
| 14 | + // Random generate a string for naming resources |
| 15 | + uniqueID := strings.ToLower(random.UniqueId()) |
| 16 | + resourceName := fmt.Sprintf("test%s", uniqueID) |
| 17 | + |
11 | 18 | // retryable errors in terraform testing. |
12 | 19 | terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ |
13 | 20 | TerraformDir: "../examples/wafv2-logging-configuration", |
14 | 21 | Upgrade: true, |
| 22 | + |
| 23 | + // Variables to pass using -var-file option |
| 24 | + Vars: map[string]interface{}{ |
| 25 | + "name_prefix": resourceName, |
| 26 | + }, |
15 | 27 | }) |
16 | 28 |
|
17 | 29 | // At the end of the test, run `terraform destroy` to clean up any resources that were created |
@@ -51,39 +63,39 @@ func TestWafWebAclV2Logging(t *testing.T) { |
51 | 63 | S3BucketId := terraform.Output(t, terraformOptions, "logging_s3_bucket_id") |
52 | 64 |
|
53 | 65 | // Verify we're getting back the outputs we expect |
54 | | - assert.Equal(t, WebAclName, "test-waf-setup") |
| 66 | + assert.Equal(t, "test"+uniqueID, WebAclName) |
55 | 67 | assert.Contains(t, WebAclArn, "arn:aws:wafv2:eu-west-1:") |
56 | | - assert.Contains(t, WebAclArn, "regional/webacl/test-waf-setup") |
57 | | - assert.Equal(t, WebAclVisConfigMetricName, "test-waf-setup-waf-main-metrics") |
58 | | - assert.Equal(t, WebAclCapacity, "950") |
59 | | - assert.Equal(t, WebAclRuleNames, "AWSManagedRulesCommonRuleSet-rule-1, AWSManagedRulesKnownBadInputsRuleSet-rule-2, AWSManagedRulesPHPRuleSet-rule-3") |
| 68 | + assert.Contains(t, WebAclArn, "regional/webacl/test"+uniqueID) |
| 69 | + assert.Equal(t, "test"+uniqueID+"-waf-setup-waf-main-metrics", WebAclVisConfigMetricName) |
| 70 | + assert.Equal(t, "950", WebAclCapacity) |
| 71 | + assert.Equal(t, "[AWSManagedRulesCommonRuleSet-rule-1 AWSManagedRulesKnownBadInputsRuleSet-rule-2 AWSManagedRulesPHPRuleSet-rule-3]", WebAclRuleNames) |
60 | 72 |
|
61 | 73 | assert.Contains(t, WebAclAssociationId, "arn:aws:wafv2:eu-west-1") |
62 | | - assert.Contains(t, WebAclAssociationId, "regional/webacl/test-waf-setup") |
| 74 | + assert.Contains(t, WebAclAssociationId, "regional/webacl/"+resourceName) |
63 | 75 | assert.Contains(t, WebAclAssociationResourceArn, "arn:aws:elasticloadbalancing:eu-west-1") |
64 | | - assert.Contains(t, WebAclAssociationResourceArn, "loadbalancer/app/alb-waf-example") |
| 76 | + assert.Contains(t, WebAclAssociationResourceArn, "loadbalancer/app/"+resourceName+"-alb-waf-example") |
65 | 77 | assert.Contains(t, WebAclAssociationAclArn, "arn:aws:wafv2:eu-west-1:") |
66 | | - assert.Contains(t, WebAclAssociationAclArn, "regional/webacl/test-waf-setup") |
| 78 | + assert.Contains(t, WebAclAssociationAclArn, "regional/webacl/"+resourceName) |
67 | 79 |
|
68 | 80 | assert.Contains(t, WebAclAssociationAlbListId, "arn:aws:wafv2:eu-west-1") |
69 | | - assert.Contains(t, WebAclAssociationAlbListId, "regional/webacl/test-waf-setup") |
| 81 | + assert.Contains(t, WebAclAssociationAlbListId, "regional/webacl/"+resourceName) |
70 | 82 | assert.Contains(t, WebAclAssociationAlbListResourceArn, "arn:aws:elasticloadbalancing:eu-west-1") |
71 | | - assert.Contains(t, WebAclAssociationAlbListResourceArn, "loadbalancer/app/alb-waf-example") |
| 83 | + assert.Contains(t, WebAclAssociationAlbListResourceArn, "loadbalancer/app/"+resourceName+"-alb-waf-example") |
72 | 84 | assert.Contains(t, WebAclAssociationAlbListAclArn, "arn:aws:wafv2:eu-west-1:") |
73 | | - assert.Contains(t, WebAclAssociationAlbListAclArn, "regional/webacl/test-waf-setup") |
| 85 | + assert.Contains(t, WebAclAssociationAlbListAclArn, "regional/webacl/"+resourceName) |
74 | 86 |
|
75 | 87 | assert.Contains(t, KinesisStreamArn, "arn:aws:firehose:eu-west-1") |
76 | 88 | assert.Contains(t, KinesisStreamArn, "deliverystream/aws-waf-logs-kinesis-firehose-test-stream") |
77 | 89 |
|
78 | 90 | assert.Contains(t, IamRoleArn, "arn:aws:iam::") |
79 | 91 | assert.Contains(t, IamRoleArn, "role/firehose-stream-test-role") |
80 | | - assert.Equal(t, IamRoleId, "firehose-stream-test-role") |
81 | | - assert.Equal(t, IamRoleName, "firehose-stream-test-role") |
| 92 | + assert.Equal(t, "firehose-stream-test-role", IamRoleId) |
| 93 | + assert.Equal(t, "firehose-stream-test-role", IamRoleName) |
82 | 94 |
|
83 | | - assert.Equal(t, IamRolePolicyId, "firehose-stream-test-role:firehose-role-custom-policy") |
84 | | - assert.Equal(t, IamRolePolicyName, "firehose-role-custom-policy") |
85 | | - assert.Equal(t, IamRolePolicyRole, "firehose-stream-test-role") |
| 95 | + assert.Equal(t, "firehose-stream-test-role:firehose-role-custom-policy", IamRolePolicyId) |
| 96 | + assert.Equal(t, "firehose-role-custom-policy", IamRolePolicyName) |
| 97 | + assert.Equal(t, "firehose-stream-test-role", IamRolePolicyRole) |
86 | 98 |
|
87 | | - assert.Equal(t, S3BucketArn, "arn:aws:s3:::aws-waf-firehose-stream-test-bucket") |
88 | | - assert.Equal(t, S3BucketId, "aws-waf-firehose-stream-test-bucket") |
| 99 | + assert.Equal(t, "arn:aws:s3:::test"+uniqueID+"-aws-waf-firehose-stream-test-bucket", S3BucketArn) |
| 100 | + assert.Equal(t, "test"+uniqueID+"-aws-waf-firehose-stream-test-bucket", S3BucketId) |
89 | 101 | } |
0 commit comments