From b04a1cc7d41e5f5b2209ab28f3d369e4317ca94f Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Wed, 13 Apr 2016 11:55:27 -0600 Subject: [PATCH 1/2] fix(tiller): set up Tiller environment correctly And structure the tests better. --- cmd/tiller/environment/environment.go | 25 ++++++++++++++++++++++++- pkg/engine/engine_test.go | 4 ---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/cmd/tiller/environment/environment.go b/cmd/tiller/environment/environment.go index f7d863b6d..f651800c0 100644 --- a/cmd/tiller/environment/environment.go +++ b/cmd/tiller/environment/environment.go @@ -1,9 +1,14 @@ package environment import ( + "github.com/deis/tiller/pkg/engine" "github.com/deis/tiller/pkg/hapi" ) +const GoTplEngine = "gotpl" + +var DefaultEngine = GoTplEngine + // EngineYard maps engine names to engine implementations. type EngineYard map[string]Engine @@ -12,6 +17,20 @@ func (y EngineYard) Get(k string) (Engine, bool) { return e, ok } +// Default returns the default template engine. +// +// The default is specified by DefaultEngine. +// +// If the default template engine cannot be found, this panics. +func (y EngineYard) Default() Engine { + d, ok := y[DefaultEngine] + if !ok { + // This is a developer error! + panic("Default template engine does not exist") + } + return d +} + // Engine represents a template engine that can render templates. // // For some engines, "rendering" includes both compiling and executing. (Other @@ -55,5 +74,9 @@ type Environment struct { // New returns an environment initialized with the defaults. func New() *Environment { - return &Environment{} + e := engine.New() + var ey EngineYard = map[string]Engine{GoTplEngine: e} + return &Environment{ + EngineYard: ey, + } } diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index 613804ef3..b0a00794b 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -4,12 +4,8 @@ import ( "fmt" "sync" "testing" - - "github.com/deis/tiller/cmd/tiller/environment" ) -var _ environment.Engine = &Engine{} - func TestEngine(t *testing.T) { e := New() From b647e9a94d3a2d36aa22506e0f9b8208e097ff08 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Wed, 13 Apr 2016 11:58:24 -0600 Subject: [PATCH 2/2] fix(tiller): add server test --- cmd/tiller/server_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 cmd/tiller/server_test.go diff --git a/cmd/tiller/server_test.go b/cmd/tiller/server_test.go new file mode 100644 index 000000000..e018530ad --- /dev/null +++ b/cmd/tiller/server_test.go @@ -0,0 +1,32 @@ +package main + +import ( + "testing" + + "github.com/deis/tiller/cmd/tiller/environment" + "github.com/deis/tiller/pkg/engine" +) + +// These are canary tests to make sure that the default server actually +// fulfills its requirements. +var _ environment.Engine = &engine.Engine{} + +func TestNewServer(t *testing.T) { + defer func() { + if recover() != nil { + t.Fatalf("Panic trapped. Check EngineYard.Default()") + } + }() + s := newServer() + + // This will panic if it is not correct. + s.Environment.EngineYard.Default() + + e, ok := s.Environment.EngineYard.Get(environment.GoTplEngine) + if !ok { + t.Fatalf("Could not find GoTplEngine") + } + if e == nil { + t.Fatalf("Template engine GoTplEngine returned nil.") + } +}