Commit 60779683 authored by Matt Butcher's avatar Matt Butcher

feat(engine): add recursive template resolution

parent bb8fca9b
......@@ -66,10 +66,7 @@ func (e *Engine) Render(chrt *chart.Chart, vals *chart.Config) (map[string]strin
}
// Render the charts
tmap := make(map[string]string, len(chrt.Templates))
for _, tpl := range chrt.Templates {
tmap[tpl.Name] = string(tpl.Data)
}
tmap := allTemplates(chrt)
return e.render(tmap, cvals)
}
......@@ -103,3 +100,17 @@ func (e *Engine) render(tpls map[string]string, v interface{}) (map[string]strin
return rendered, nil
}
// allTemplates returns all templates for a chart and its dependencies.
func allTemplates(c *chart.Chart) map[string]string {
templates := map[string]string{}
for _, child := range c.Dependencies {
for _, t := range child.Templates {
templates[t.Name] = string(t.Data)
}
}
for _, t := range c.Templates {
templates[t.Name] = string(t.Data)
}
return templates
}
......@@ -4,6 +4,8 @@ import (
"fmt"
"sync"
"testing"
"github.com/deis/tiller/pkg/proto/hapi/chart"
)
func TestEngine(t *testing.T) {
......@@ -80,3 +82,55 @@ func TestParallelRenderInternals(t *testing.T) {
}
wg.Wait()
}
func TestAllTemplates(t *testing.T) {
ch1 := &chart.Chart{
Templates: []*chart.Template{
{Name: "foo", Data: []byte("foo")},
{Name: "bar", Data: []byte("bar")},
},
Dependencies: []*chart.Chart{
{Templates: []*chart.Template{
{Name: "pinky", Data: []byte("pinky")},
{Name: "brain", Data: []byte("brain")},
}},
},
}
tpls := allTemplates(ch1)
if len(tpls) != 4 {
t.Errorf("Expected 4 charts, got %d", len(tpls))
}
}
func TestRenderDependency(t *testing.T) {
e := New()
deptpl := `{{define "myblock"}}World{{end}}`
toptpl := `Hello {{template "myblock"}}`
ch := &chart.Chart{
Templates: []*chart.Template{
{Name: "outer", Data: []byte(toptpl)},
},
Dependencies: []*chart.Chart{
{Templates: []*chart.Template{
{Name: "inner", Data: []byte(deptpl)},
}},
},
}
out, err := e.Render(ch, nil)
if err != nil {
t.Fatalf("failed to render chart: %s", err)
}
if len(out) != 2 {
t.Errorf("Expected 2, got %d", len(out))
}
expect := "Hello World"
if out["outer"] != expect {
t.Errorf("Expected %q, got %q", expect, out["outer"])
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment