-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
providers/template: add tests, address review comments
Do directory expansion on filenames. Add basic acceptance tests. Code coverage is 72.5%. Uncovered code is uninteresting and/or impossible error cases. Note that this required adding a knob to helper/resource.TestStep to allow transient resources.
- Loading branch information
Showing
3 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package template | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
r "github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
var testProviders = map[string]terraform.ResourceProvider{ | ||
"template": Provider(), | ||
} | ||
|
||
func TestTemplateRendering(t *testing.T) { | ||
var cases = []struct { | ||
vars string | ||
template string | ||
want string | ||
}{ | ||
{`{}`, `ABC`, `ABC`}, | ||
{`{a="foo"}`, `${a}`, `foo`}, | ||
{`{a="hello"}`, `${replace(a, "ello", "i")}`, `hi`}, | ||
{`{}`, `${1+2+3}`, `6`}, | ||
} | ||
|
||
for _, tt := range cases { | ||
r.Test(t, r.TestCase{ | ||
PreCheck: func() { | ||
readfile = func(string) ([]byte, error) { | ||
return []byte(tt.template), nil | ||
} | ||
}, | ||
Providers: testProviders, | ||
Steps: []r.TestStep{ | ||
r.TestStep{ | ||
Config: ` | ||
resource "template_file" "t0" { | ||
filename = "mock" | ||
vars = ` + tt.vars + ` | ||
} | ||
output "rendered" { | ||
value = "${template_file.t0.rendered}" | ||
} | ||
`, | ||
Check: func(s *terraform.State) error { | ||
got := s.RootModule().Outputs["rendered"] | ||
if tt.want != got { | ||
return fmt.Errorf("template:\n%s\nvars:\n%s\ngot:\n%s\nwant:\n%s\n", tt.template, tt.vars, got, tt.want) | ||
} | ||
return nil | ||
}, | ||
TransientResource: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters