Merge pull request #8202 from hashicorp/f-testing-value-is-set

testing: Add TestCheckResourceAttrSet helper
This commit is contained in:
James Nugent 2016-08-15 15:46:13 -05:00 committed by GitHub
commit cb1787c2f7
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()