From b681ab23d3bc5decb7afa74910ada99e4fbafbb3 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Mon, 15 Aug 2016 15:42:25 -0500 Subject: [PATCH] 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. --- helper/resource/testing.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 326a46a63..885207704 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -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()