Skip to content

Commit

Permalink
Refactoring tests to use helper functions (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jun 13, 2022
1 parent da82249 commit 6eb39fa
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 124 deletions.
31 changes: 20 additions & 11 deletions internal/provider/resource_password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ func TestAccResourcePasswordBasic(t *testing.T) {
length = 12
}`,
Check: resource.ComposeTestCheckFunc(
testAccResourceStringCheck("random_password.basic", &customLens{
customLen: 12,
resource.TestCheckResourceAttrWith("random_password.basic", "result", func(result string) error {
if len(result) != 12 {
return fmt.Errorf("expected length 12, actual length %d", len(result))
}
return nil
}),
),
},
Expand Down Expand Up @@ -69,10 +72,13 @@ func TestAccResourcePasswordOverride(t *testing.T) {
numeric = false
}`,
Check: resource.ComposeTestCheckFunc(
testAccResourceStringCheck("random_password.override", &customLens{
customLen: 4,
resource.TestCheckResourceAttrWith("random_password.override", "result", func(result string) error {
if len(result) != 4 {
return fmt.Errorf("expected length 4, actual length %d", len(result))
}
return nil
}),
patternMatch("random_password.override", "!!!!"),
resource.TestCheckResourceAttr("random_password.override", "result", "!!!!"),
),
},
},
Expand Down Expand Up @@ -499,13 +505,16 @@ func TestAccResourcePasswordMin(t *testing.T) {
min_numeric = 4
}`,
Check: resource.ComposeTestCheckFunc(
testAccResourceStringCheck("random_password.min", &customLens{
customLen: 12,
resource.TestCheckResourceAttrWith("random_password.min", "result", func(result string) error {
if len(result) != 12 {
return fmt.Errorf("expected length 12, actual length %d", len(result))
}
return nil
}),
regexMatch("random_password.min", regexp.MustCompile(`([a-z])`), 2),
regexMatch("random_password.min", regexp.MustCompile(`([A-Z])`), 3),
regexMatch("random_password.min", regexp.MustCompile(`([0-9])`), 4),
regexMatch("random_password.min", regexp.MustCompile(`([!#@])`), 1),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([a-z].*){2,}`)),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([A-Z].*){3,}`)),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([0-9].*){4,}`)),
resource.TestMatchResourceAttr("random_password.min", "result", regexp.MustCompile(`([!#@])`)),
),
},
},
Expand Down
158 changes: 45 additions & 113 deletions internal/provider/resource_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

type customLens struct {
customLen int
}

func TestAccResourceString(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccResourceStringBasic,
Config: `resource "random_string" "basic" {
length = 12
}`,
Check: resource.ComposeTestCheckFunc(
testAccResourceStringCheck("random_string.basic", &customLens{
customLen: 12,
resource.TestCheckResourceAttrWith("random_string.basic", "result", func(result string) error {
if len(result) != 12 {
return fmt.Errorf("expected length 12, actual length %d", len(result))
}
return nil
}),
),
},
Expand All @@ -42,12 +42,21 @@ func TestAccResourceStringOverride(t *testing.T) {
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccResourceStringOverride,
Config: `resource "random_string" "override" {
length = 4
override_special = "!"
lower = false
upper = false
number = false
}`,
Check: resource.ComposeTestCheckFunc(
testAccResourceStringCheck("random_string.override", &customLens{
customLen: 4,
resource.TestCheckResourceAttrWith("random_string.override", "result", func(result string) error {
if len(result) != 4 {
return fmt.Errorf("expected length 12, actual length %d", len(result))
}
return nil
}),
patternMatch("random_string.override", "!!!!"),
resource.TestCheckResourceAttr("random_string.override", "result", "!!!!"),
),
},
},
Expand All @@ -60,15 +69,25 @@ func TestAccResourceStringMin(t *testing.T) {
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccResourceStringMin,
Config: `resource "random_string" "min" {
length = 12
override_special = "!#@"
min_lower = 2
min_upper = 3
min_special = 1
min_numeric = 4
}`,
Check: resource.ComposeTestCheckFunc(
testAccResourceStringCheck("random_string.min", &customLens{
customLen: 12,
resource.TestCheckResourceAttrWith("random_string.min", "result", func(result string) error {
if len(result) != 12 {
return fmt.Errorf("expected length 12, actual length %d", len(result))
}
return nil
}),
regexMatch("random_string.min", regexp.MustCompile(`([a-z])`), 2),
regexMatch("random_string.min", regexp.MustCompile(`([A-Z])`), 3),
regexMatch("random_string.min", regexp.MustCompile(`([0-9])`), 4),
regexMatch("random_string.min", regexp.MustCompile(`([!#@])`), 1),
resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([a-z].*){2,}`)),
resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([A-Z].*){3,}`)),
resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([0-9].*){4,}`)),
resource.TestMatchResourceAttr("random_string.min", "result", regexp.MustCompile(`([!#@].*)`)),
),
},
},
Expand Down Expand Up @@ -278,105 +297,18 @@ func TestAccResourceStringErrors(t *testing.T) {
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccResourceStringInvalidConfig,
Config: `resource "random_string" "invalid_length" {
length = 2
min_lower = 3
}`,
ExpectError: regexp.MustCompile(`.*Attribute "length" \(2\) cannot be less than min_upper \+ min_lower \+\nmin_numeric \+ min_special \(3\).`),
},
{
Config: testAccResourceStringLengthTooShortConfig,
Config: `resource "random_string" "invalid_length" {
length = 0
}`,
ExpectError: regexp.MustCompile(`.*Attribute "length" \(0\) must be at least 1`),
},
},
})
}

const (
testAccResourceStringBasic = `
resource "random_string" "basic" {
length = 12
}`
testAccResourceStringOverride = `
resource "random_string" "override" {
length = 4
override_special = "!"
lower = false
upper = false
number = false
}
`
testAccResourceStringMin = `
resource "random_string" "min" {
length = 12
override_special = "!#@"
min_lower = 2
min_upper = 3
min_special = 1
min_numeric = 4
}`
testAccResourceStringInvalidConfig = `
resource "random_string" "invalid_length" {
length = 2
min_lower = 3
}`
testAccResourceStringLengthTooShortConfig = `
resource "random_string" "invalid_length" {
length = 0
}`
)

func testAccResourceStringCheck(id string, want *customLens) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[id]
if !ok {
return fmt.Errorf("Not found: %s", id)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

customStr := rs.Primary.Attributes["result"]

if got, want := len(customStr), want.customLen; got != want {
return fmt.Errorf("custom string length is %d; want %d", got, want)
}

return nil
}
}

func regexMatch(id string, exp *regexp.Regexp, requiredMatches int) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[id]
if !ok {
return fmt.Errorf("Not found: %s", id)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

customStr := rs.Primary.Attributes["result"]

if matches := exp.FindAllStringSubmatchIndex(customStr, -1); len(matches) < requiredMatches {
return fmt.Errorf("custom string is %s; did not match %s", customStr, exp)
}

return nil
}
}
func patternMatch(id string, want string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[id]
if !ok {
return fmt.Errorf("Not found: %s", id)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
customStr := rs.Primary.Attributes["result"]

if got, want := customStr, want; got != want {
return fmt.Errorf("custom string is %s; want %s", got, want)
}

return nil
}
}

0 comments on commit 6eb39fa

Please sign in to comment.