Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra TestCheckFuncs to check attribute's string length #893

Merged
merged 7 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/893.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
helper/resource: Add `TestCheckFunc` helpers to check attribute's value length
```
30 changes: 30 additions & 0 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,36 @@ func testCheckResourceAttrPair(isFirst *terraform.InstanceState, nameFirst strin
return nil
}

// TestRangeLengthResourceAttr is a TestCheckFunc which checks that the length of the value
// in state for the given name/key is within an expected (closed) range.
func TestRangeLengthResourceAttr(name, key string, min, max int) TestCheckFunc {
return checkIfIndexesIntoTypeSet(key, func(s *terraform.State) error {
is, err := primaryInstanceState(s, name)
if err != nil {
return err
}

valLen := len(is.Attributes[key])
if valLen < min || valLen > max {
return fmt.Errorf(
"%s: Attribute '%s' length is %d, which is not within the expected closed range [%d, %d]",
name,
key,
valLen,
min,
max)
}

return nil
})
}

// TestMatchLengthResourceAttr is a TestCheckFunc which checks that the length of the value
// in state for the given name/key matches a specific length.
func TestMatchLengthResourceAttr(name, key string, length int) TestCheckFunc {
return TestRangeLengthResourceAttr(name, key, length, length)
}

// TestCheckOutput checks an output in the Terraform configuration
func TestCheckOutput(name, value string) TestCheckFunc {
return func(s *terraform.State) error {
Expand Down
81 changes: 81 additions & 0 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,87 @@ func TestCheckResourceAttr_empty(t *testing.T) {
}
}

func TestTestRangeLengthResourceAttr(t *testing.T) {
resName := "test_resource"
resKey := "test_key"
resVal := "test_value"

s := terraform.NewState()
s.AddModuleState(&terraform.ModuleState{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
resName: {
Primary: &terraform.InstanceState{
Attributes: map[string]string{
resKey: resVal,
},
},
},
},
})

t.Run("in_range", func(t *testing.T) {
check := TestRangeLengthResourceAttr(resName, resKey, 2, 20)
if err := check(s); err != nil {
t.Fatal(err)
}
})
t.Run("out_of_range", func(t *testing.T) {
check := TestRangeLengthResourceAttr(resName, resKey, 20, 40)
if err := check(s); err == nil {
t.Fatal(fmt.Errorf("failed to detect attribute '%s.%s' value '%s' length is out of range",
resName,
resKey,
resVal))
}
})
}

func TestTestMatchLengthResourceAttr(t *testing.T) {
resName := "test_resource"
resKey := "test_key"
resVal := "test_value"

s := terraform.NewState()
s.AddModuleState(&terraform.ModuleState{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
resName: {
Primary: &terraform.InstanceState{
Attributes: map[string]string{
resKey: resVal,
},
},
},
},
})

t.Run("matches_length", func(t *testing.T) {
check := TestMatchLengthResourceAttr(resName, resKey, 10)
bookshelfdave marked this conversation as resolved.
Show resolved Hide resolved
if err := check(s); err != nil {
t.Fatal(err)
}
})
t.Run("too_short", func(t *testing.T) {
check := TestMatchLengthResourceAttr(resName, resKey, 11)
if err := check(s); err == nil {
t.Fatal(fmt.Errorf("failed to detect attribute '%s.%s' value '%s' is too short",
resName,
resKey,
resVal))
}
})
t.Run("too_long", func(t *testing.T) {
check := TestMatchLengthResourceAttr(resName, resKey, 2)
if err := check(s); err == nil {
t.Fatal(fmt.Errorf("failed to detect attribute '%s.%s' value '%s' is too long",
resName,
resKey,
resVal))
}
})
}

func TestCheckNoResourceAttr_empty(t *testing.T) {
s := terraform.NewState()
s.AddModuleState(&terraform.ModuleState{
Expand Down