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.
This commit is contained in:
parent
745d83a995
commit
76bcac3031
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/hashicorp/terraform/config/lang"
|
||||
"github.com/hashicorp/terraform/config/lang/ast"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
)
|
||||
|
||||
func resource() *schema.Resource {
|
||||
|
@ -35,7 +36,6 @@ func resource() *schema.Resource {
|
|||
"rendered": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Default: nil,
|
||||
Description: "rendered template",
|
||||
},
|
||||
},
|
||||
|
@ -55,11 +55,18 @@ func Exists(d *schema.ResourceData, meta interface{}) (bool, error) {
|
|||
return false, nil
|
||||
}
|
||||
|
||||
var readfile func(string) ([]byte, error) = ioutil.ReadFile // testing hook
|
||||
|
||||
func eval(d *schema.ResourceData) error {
|
||||
filename := d.Get("filename").(string)
|
||||
vars := d.Get("vars").(map[string]interface{})
|
||||
|
||||
buf, err := ioutil.ReadFile(filename)
|
||||
path, err := homedir.Expand(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buf, err := readfile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
|
@ -75,6 +75,12 @@ type TestStep struct {
|
|||
|
||||
// Destroy will create a destroy plan if set to true.
|
||||
Destroy bool
|
||||
|
||||
// TransientResource indicates that resources created as part
|
||||
// of this test step are temporary and might be recreated anew
|
||||
// with every planning step. This should only be set for
|
||||
// pseudo-resources, like the null resource or templates.
|
||||
TransientResource bool
|
||||
}
|
||||
|
||||
// Test performs an acceptance test on a resource.
|
||||
|
@ -260,7 +266,7 @@ func testStep(
|
|||
if p, err := ctx.Plan(); err != nil {
|
||||
return state, fmt.Errorf("Error on second follow-up plan: %s", err)
|
||||
} else {
|
||||
if p.Diff != nil && !p.Diff.Empty() {
|
||||
if p.Diff != nil && !p.Diff.Empty() && !step.TransientResource {
|
||||
return state, fmt.Errorf(
|
||||
"After applying this step and refreshing, the plan was not empty:\n\n%s", p)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue