Skip to content

Commit fe75bb1

Browse files
Ivan De Marinobookshelfdave
andauthored
Extra TestCheckFuncs to check attribute's string length (#893)
* enhancement: Adding helper functions in `helper/resources/testing.go` to check resource attribute string length * Proposing a `.gitignore` as I just stumbled into it's absence This is based on the official [GitHub templates](https://github.com/github/gitignore), plus something for Jetbrains products * Adding changelog txt * Update helper/resource/testing.go Co-authored-by: Dave Parfitt <[email protected]> * Update helper/resource/testing.go * Removing `.gitignore` and leaving it for another PR * Update helper/resource/testing.go Co-authored-by: Dave Parfitt <[email protected]>
1 parent 45133e6 commit fe75bb1

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

.changelog/893.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
helper/resource: Add `TestCheckFunc` helpers to check attribute's value length
3+
```

helper/resource/testing.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,36 @@ func testCheckResourceAttrPair(isFirst *terraform.InstanceState, nameFirst strin
10311031
return nil
10321032
}
10331033

1034+
// TestRangeLengthResourceAttr is a TestCheckFunc which checks that the length of the value
1035+
// in state for the given name/key is within an expected (closed) range.
1036+
func TestRangeLengthResourceAttr(name, key string, min, max int) TestCheckFunc {
1037+
return checkIfIndexesIntoTypeSet(key, func(s *terraform.State) error {
1038+
is, err := primaryInstanceState(s, name)
1039+
if err != nil {
1040+
return err
1041+
}
1042+
1043+
valLen := len(is.Attributes[key])
1044+
if valLen < min || valLen > max {
1045+
return fmt.Errorf(
1046+
"%s: Attribute '%s' length is %d, which is not within the expected closed range [%d, %d]",
1047+
name,
1048+
key,
1049+
valLen,
1050+
min,
1051+
max)
1052+
}
1053+
1054+
return nil
1055+
})
1056+
}
1057+
1058+
// TestMatchLengthResourceAttr is a TestCheckFunc which checks that the length of the value
1059+
// in state for the given name/key matches a specific length.
1060+
func TestMatchLengthResourceAttr(name, key string, length int) TestCheckFunc {
1061+
return TestRangeLengthResourceAttr(name, key, length, length)
1062+
}
1063+
10341064
// TestCheckOutput checks an output in the Terraform configuration
10351065
func TestCheckOutput(name, value string) TestCheckFunc {
10361066
return func(s *terraform.State) error {

helper/resource/testing_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,87 @@ func TestCheckResourceAttr_empty(t *testing.T) {
874874
}
875875
}
876876

877+
func TestTestRangeLengthResourceAttr(t *testing.T) {
878+
resName := "test_resource"
879+
resKey := "test_key"
880+
resVal := "test_value"
881+
882+
s := terraform.NewState()
883+
s.AddModuleState(&terraform.ModuleState{
884+
Path: []string{"root"},
885+
Resources: map[string]*terraform.ResourceState{
886+
resName: {
887+
Primary: &terraform.InstanceState{
888+
Attributes: map[string]string{
889+
resKey: resVal,
890+
},
891+
},
892+
},
893+
},
894+
})
895+
896+
t.Run("in_range", func(t *testing.T) {
897+
check := TestRangeLengthResourceAttr(resName, resKey, 2, 20)
898+
if err := check(s); err != nil {
899+
t.Fatal(err)
900+
}
901+
})
902+
t.Run("out_of_range", func(t *testing.T) {
903+
check := TestRangeLengthResourceAttr(resName, resKey, 20, 40)
904+
if err := check(s); err == nil {
905+
t.Fatal(fmt.Errorf("failed to detect attribute '%s.%s' value '%s' length is out of range",
906+
resName,
907+
resKey,
908+
resVal))
909+
}
910+
})
911+
}
912+
913+
func TestTestMatchLengthResourceAttr(t *testing.T) {
914+
resName := "test_resource"
915+
resKey := "test_key"
916+
resVal := "test_value"
917+
918+
s := terraform.NewState()
919+
s.AddModuleState(&terraform.ModuleState{
920+
Path: []string{"root"},
921+
Resources: map[string]*terraform.ResourceState{
922+
resName: {
923+
Primary: &terraform.InstanceState{
924+
Attributes: map[string]string{
925+
resKey: resVal,
926+
},
927+
},
928+
},
929+
},
930+
})
931+
932+
t.Run("matches_length", func(t *testing.T) {
933+
check := TestMatchLengthResourceAttr(resName, resKey, 10)
934+
if err := check(s); err != nil {
935+
t.Fatal(err)
936+
}
937+
})
938+
t.Run("too_short", func(t *testing.T) {
939+
check := TestMatchLengthResourceAttr(resName, resKey, 11)
940+
if err := check(s); err == nil {
941+
t.Fatal(fmt.Errorf("failed to detect attribute '%s.%s' value '%s' is too short",
942+
resName,
943+
resKey,
944+
resVal))
945+
}
946+
})
947+
t.Run("too_long", func(t *testing.T) {
948+
check := TestMatchLengthResourceAttr(resName, resKey, 2)
949+
if err := check(s); err == nil {
950+
t.Fatal(fmt.Errorf("failed to detect attribute '%s.%s' value '%s' is too long",
951+
resName,
952+
resKey,
953+
resVal))
954+
}
955+
})
956+
}
957+
877958
func TestCheckNoResourceAttr_empty(t *testing.T) {
878959
s := terraform.NewState()
879960
s.AddModuleState(&terraform.ModuleState{

0 commit comments

Comments
 (0)