testing: Add TestCheckResourceAttrSet helper

This commit adds a TestCheckFunc which ensures that a value is set for a
given name/key combination. It is primarily useful for ensuring that
computed values are set where it is not possible to know the expected
value ahead of time.
This commit is contained in:
James Nugent 2016-08-15 15:42:25 -05:00
parent 0c8b243ce0
commit b681ab23d3
1 changed files with 25 additions and 0 deletions

View File

@ -520,6 +520,31 @@ func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
}
}
// TestCheckResourceAttrSet is a TestCheckFunc which ensures a value
// exists in state for the given name/key combination. It is useful when
// testing that computed values were set, when it is not possible to
// know ahead of time what the values will be.
func TestCheckResourceAttrSet(name, key string) TestCheckFunc {
return func(s *terraform.State) error {
ms := s.RootModule()
rs, ok := ms.Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
is := rs.Primary
if is == nil {
return fmt.Errorf("No primary instance: %s", name)
}
if val, ok := is.Attributes[key]; ok && val != "" {
return nil
}
return fmt.Errorf("%s: Attribute '%s' expected to be set", name, key)
}
}
func TestCheckResourceAttr(name, key, value string) TestCheckFunc {
return func(s *terraform.State) error {
ms := s.RootModule()