Skip to content

Commit

Permalink
Revert "Extra TestCheckFuncs to check attribute's string length (#893
Browse files Browse the repository at this point in the history
…)" (#896)

This reverts commit fe75bb1.
  • Loading branch information
Ivan De Marino authored Mar 8, 2022
1 parent db02baa commit b73052f
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 114 deletions.
3 changes: 0 additions & 3 deletions .changelog/893.txt

This file was deleted.

30 changes: 0 additions & 30 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,36 +1033,6 @@ 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: 0 additions & 81 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,87 +874,6 @@ 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)
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

0 comments on commit b73052f

Please sign in to comment.