Skip to content

Commit

Permalink
wrtie acceptence tests for:
Browse files Browse the repository at this point in the history
* aws_waf_rate_based_rule
* aws_waf_rule_group
* aws_waf_rule

refactored tests a bit to not repeat resource name
  • Loading branch information
DrFaust92 authored and Ilia Lazebnik committed Oct 10, 2019
1 parent 00176bf commit 4597385
Show file tree
Hide file tree
Showing 3 changed files with 345 additions and 84 deletions.
152 changes: 115 additions & 37 deletions aws/resource_aws_waf_rate_based_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
func TestAccAWSWafRateBasedRule_basic(t *testing.T) {
var v waf.RateBasedRule
wafRuleName := fmt.Sprintf("wafrule%s", acctest.RandString(5))
resourceName := "aws_waf_rate_based_rule.wafrule"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Providers: testAccProviders,
Expand All @@ -25,13 +27,10 @@ func TestAccAWSWafRateBasedRule_basic(t *testing.T) {
{
Config: testAccAWSWafRateBasedRuleConfig(wafRuleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRateBasedRuleExists("aws_waf_rate_based_rule.wafrule", &v),
resource.TestCheckResourceAttr(
"aws_waf_rate_based_rule.wafrule", "name", wafRuleName),
resource.TestCheckResourceAttr(
"aws_waf_rate_based_rule.wafrule", "predicates.#", "1"),
resource.TestCheckResourceAttr(
"aws_waf_rate_based_rule.wafrule", "metric_name", wafRuleName),
testAccCheckAWSWafRateBasedRuleExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "name", wafRuleName),
resource.TestCheckResourceAttr(resourceName, "predicates.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metric_name", wafRuleName),
),
},
},
Expand All @@ -42,6 +41,7 @@ func TestAccAWSWafRateBasedRule_changeNameForceNew(t *testing.T) {
var before, after waf.RateBasedRule
wafRuleName := fmt.Sprintf("wafrule%s", acctest.RandString(5))
wafRuleNewName := fmt.Sprintf("wafrulenew%s", acctest.RandString(5))
resourceName := "aws_waf_rate_based_rule.wafrule"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -51,25 +51,19 @@ func TestAccAWSWafRateBasedRule_changeNameForceNew(t *testing.T) {
{
Config: testAccAWSWafRateBasedRuleConfig(wafRuleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRateBasedRuleExists("aws_waf_rate_based_rule.wafrule", &before),
resource.TestCheckResourceAttr(
"aws_waf_rate_based_rule.wafrule", "name", wafRuleName),
resource.TestCheckResourceAttr(
"aws_waf_rate_based_rule.wafrule", "predicates.#", "1"),
resource.TestCheckResourceAttr(
"aws_waf_rate_based_rule.wafrule", "metric_name", wafRuleName),
testAccCheckAWSWafRateBasedRuleExists(resourceName, &before),
resource.TestCheckResourceAttr(resourceName, "name", wafRuleName),
resource.TestCheckResourceAttr(resourceName, "predicates.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metric_name", wafRuleName),
),
},
{
Config: testAccAWSWafRateBasedRuleConfigChangeName(wafRuleNewName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRateBasedRuleExists("aws_waf_rate_based_rule.wafrule", &after),
resource.TestCheckResourceAttr(
"aws_waf_rate_based_rule.wafrule", "name", wafRuleNewName),
resource.TestCheckResourceAttr(
"aws_waf_rate_based_rule.wafrule", "predicates.#", "1"),
resource.TestCheckResourceAttr(
"aws_waf_rate_based_rule.wafrule", "metric_name", wafRuleNewName),
testAccCheckAWSWafRateBasedRuleExists(resourceName, &after),
resource.TestCheckResourceAttr(resourceName, "name", wafRuleNewName),
resource.TestCheckResourceAttr(resourceName, "predicates.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metric_name", wafRuleNewName),
),
},
},
Expand All @@ -79,6 +73,8 @@ func TestAccAWSWafRateBasedRule_changeNameForceNew(t *testing.T) {
func TestAccAWSWafRateBasedRule_disappears(t *testing.T) {
var v waf.RateBasedRule
wafRuleName := fmt.Sprintf("wafrule%s", acctest.RandString(5))
resourceName := "aws_waf_rate_based_rule.wafrule"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Providers: testAccProviders,
Expand All @@ -87,7 +83,7 @@ func TestAccAWSWafRateBasedRule_disappears(t *testing.T) {
{
Config: testAccAWSWafRateBasedRuleConfig(wafRuleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRateBasedRuleExists("aws_waf_rate_based_rule.wafrule", &v),
testAccCheckAWSWafRateBasedRuleExists(resourceName, &v),
testAccCheckAWSWafRateBasedRuleDisappears(&v),
),
ExpectNonEmptyPlan: true,
Expand All @@ -103,6 +99,7 @@ func TestAccAWSWafRateBasedRule_changePredicates(t *testing.T) {
var before, after waf.RateBasedRule
var idx int
ruleName := fmt.Sprintf("wafrule%s", acctest.RandString(5))
resourceName := "aws_waf_rate_based_rule.wafrule"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -113,30 +110,83 @@ func TestAccAWSWafRateBasedRule_changePredicates(t *testing.T) {
Config: testAccAWSWafRateBasedRuleConfig(ruleName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafIPSetExists("aws_waf_ipset.ipset", &ipset),
testAccCheckAWSWafRateBasedRuleExists("aws_waf_rate_based_rule.wafrule", &before),
resource.TestCheckResourceAttr("aws_waf_rate_based_rule.wafrule", "name", ruleName),
resource.TestCheckResourceAttr("aws_waf_rate_based_rule.wafrule", "predicates.#", "1"),
testAccCheckAWSWafRateBasedRuleExists(resourceName, &before),
resource.TestCheckResourceAttr(resourceName, "name", ruleName),
resource.TestCheckResourceAttr(resourceName, "predicates.#", "1"),
computeWafRateBasedRulePredicateWithIpSet(&ipset, false, "IPMatch", &idx),
testCheckResourceAttrWithIndexesAddr("aws_waf_rate_based_rule.wafrule", "predicates.%d.negated", &idx, "false"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rate_based_rule.wafrule", "predicates.%d.type", &idx, "IPMatch"),
testCheckResourceAttrWithIndexesAddr(resourceName, "predicates.%d.negated", &idx, "false"),
testCheckResourceAttrWithIndexesAddr(resourceName, "predicates.%d.type", &idx, "IPMatch"),
),
},
{
Config: testAccAWSWafRateBasedRuleConfig_changePredicates(ruleName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafByteMatchSetExists("aws_waf_byte_match_set.set", &byteMatchSet),
testAccCheckAWSWafRateBasedRuleExists("aws_waf_rate_based_rule.wafrule", &after),
resource.TestCheckResourceAttr("aws_waf_rate_based_rule.wafrule", "name", ruleName),
resource.TestCheckResourceAttr("aws_waf_rate_based_rule.wafrule", "predicates.#", "1"),
testAccCheckAWSWafRateBasedRuleExists(resourceName, &after),
resource.TestCheckResourceAttr(resourceName, "name", ruleName),
resource.TestCheckResourceAttr(resourceName, "predicates.#", "1"),
computeWafRateBasedRulePredicateWithByteMatchSet(&byteMatchSet, true, "ByteMatch", &idx),
testCheckResourceAttrWithIndexesAddr("aws_waf_rate_based_rule.wafrule", "predicates.%d.negated", &idx, "true"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rate_based_rule.wafrule", "predicates.%d.type", &idx, "ByteMatch"),
testCheckResourceAttrWithIndexesAddr(resourceName, "predicates.%d.negated", &idx, "true"),
testCheckResourceAttrWithIndexesAddr(resourceName, "predicates.%d.type", &idx, "ByteMatch"),
),
},
},
})
}

func TestAccAWSWafRateBasedRule_Tags(t *testing.T) {
var rule waf.RateBasedRule
ruleName := fmt.Sprintf("wafrule%s", acctest.RandString(5))
resourceName := "aws_waf_rate_based_rule.wafrule"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSWafWebAclDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSWafRateBasedRuleConfigTags1(ruleName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRateBasedRuleExists(resourceName, &rule),
resource.TestCheckResourceAttr(resourceName, "predicates.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metric_name", ruleName),
resource.TestCheckResourceAttr(resourceName, "name", ruleName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
Config: testAccAWSWafRateBasedRuleConfigTags2(ruleName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRateBasedRuleExists(resourceName, &rule),
resource.TestCheckResourceAttr(resourceName, "predicates.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metric_name", ruleName),
resource.TestCheckResourceAttr(resourceName, "name", ruleName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSWafRateBasedRuleConfigTags1(ruleName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRateBasedRuleExists(resourceName, &rule),
resource.TestCheckResourceAttr(resourceName, "predicates.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metric_name", ruleName),
resource.TestCheckResourceAttr(resourceName, "name", ruleName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

// computeWafRateBasedRulePredicateWithIpSet calculates index
// which isn't static because dataId is generated as part of the test
func computeWafRateBasedRulePredicateWithIpSet(ipSet *waf.IPSet, negated bool, pType string, idx *int) resource.TestCheckFunc {
Expand Down Expand Up @@ -178,6 +228,7 @@ func computeWafRateBasedRulePredicateWithByteMatchSet(set *waf.ByteMatchSet, neg
func TestAccAWSWafRateBasedRule_noPredicates(t *testing.T) {
var rule waf.RateBasedRule
ruleName := fmt.Sprintf("wafrule%s", acctest.RandString(5))
resourceName := "aws_waf_rate_based_rule.wafrule"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -187,11 +238,9 @@ func TestAccAWSWafRateBasedRule_noPredicates(t *testing.T) {
{
Config: testAccAWSWafRateBasedRuleConfig_noPredicates(ruleName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRateBasedRuleExists("aws_waf_rate_based_rule.wafrule", &rule),
resource.TestCheckResourceAttr(
"aws_waf_rate_based_rule.wafrule", "name", ruleName),
resource.TestCheckResourceAttr(
"aws_waf_rate_based_rule.wafrule", "predicates.#", "0"),
testAccCheckAWSWafRateBasedRuleExists(resourceName, &rule),
resource.TestCheckResourceAttr(resourceName, "name", ruleName),
resource.TestCheckResourceAttr(resourceName, "predicates.#", "0"),
),
},
},
Expand Down Expand Up @@ -407,3 +456,32 @@ resource "aws_waf_rate_based_rule" "wafrule" {
}
`, name, name)
}

func testAccAWSWafRateBasedRuleConfigTags1(rName, tag1Key, tag1Value string) string {
return fmt.Sprintf(`
resource "aws_waf_rate_based_rule" "wafrule" {
name = "%s"
metric_name = "%s"
rate_key = "IP"
rate_limit = 2000
tags = {
%q = %q
}
}
`, rName, rName, tag1Key, tag1Value)
}

func testAccAWSWafRateBasedRuleConfigTags2(rName, tag1Key, tag1Value, tag2Key, tag2Value string) string {
return fmt.Sprintf(`
resource "aws_waf_rate_based_rule" "wafrule" {
name = "%s"
metric_name = "%s"
rate_key = "IP"
rate_limit = 2000
tags = {
%q = %q
%q = %q
}
}
`, rName, rName, tag1Key, tag1Value, tag2Key, tag2Value)
}
83 changes: 79 additions & 4 deletions aws/resource_aws_waf_rule_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,56 @@ func computeWafActivatedRuleWithRuleId(rule *waf.Rule, actionType string, priori
}
}

func TestAccAWSWafRuleGroup_Tags(t *testing.T) {
var group waf.RuleGroup
groupName := fmt.Sprintf("test%s", acctest.RandString(5))
resourceName := "aws_waf_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSWafWebAclDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSWafRuleGroupConfigTags1(groupName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRuleGroupExists(resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "name", groupName),
resource.TestCheckResourceAttr(resourceName, "activated_rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
Config: testAccAWSWafRuleGroupConfigTags2(groupName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRuleGroupExists(resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "name", groupName),
resource.TestCheckResourceAttr(resourceName, "activated_rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSWafRuleGroupConfigTags1(groupName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRuleGroupExists(resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "name", groupName),
resource.TestCheckResourceAttr(resourceName, "activated_rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSWafRuleGroup_noActivatedRules(t *testing.T) {
var group waf.RuleGroup
groupName := fmt.Sprintf("test%s", acctest.RandString(5))
Expand All @@ -264,10 +314,8 @@ func TestAccAWSWafRuleGroup_noActivatedRules(t *testing.T) {
Config: testAccAWSWafRuleGroupConfig_noActivatedRules(groupName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRuleGroupExists(resourceName, &group),
resource.TestCheckResourceAttr(
resourceName, "name", groupName),
resource.TestCheckResourceAttr(
resourceName, "activated_rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "name", groupName),
resource.TestCheckResourceAttr(resourceName, "activated_rule.#", "0"),
),
},
},
Expand Down Expand Up @@ -458,3 +506,30 @@ resource "aws_waf_rule_group" "test" {
}
`, groupName)
}

func testAccAWSWafRuleGroupConfigTags1(gName, tag1Key, tag1Value string) string {
return fmt.Sprintf(`
resource "aws_waf_rule_group" "test" {
name = "%[1]s"
metric_name = "%[1]s"
tags = {
%q = %q
}
}
`, gName, tag1Key, tag1Value)
}

func testAccAWSWafRuleGroupConfigTags2(gName, tag1Key, tag1Value, tag2Key, tag2Value string) string {
return fmt.Sprintf(`
resource "aws_waf_rule_group" "test" {
name = "%[1]s"
metric_name = "%[1]s"
tags = {
%q = %q
%q = %q
}
}
`, gName, tag1Key, tag1Value, tag2Key, tag2Value)
}
Loading

0 comments on commit 4597385

Please sign in to comment.