From eee22b32f6354675e90698e23780f3d15274ec8f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 18 Jun 2013 09:49:05 -0700 Subject: [PATCH] packer: Environment can look up post processors --- packer/environment.go | 32 ++++++++++++++++++++++---- packer/environment_test.go | 42 +++++++++++++++++++++++++++++++++++ packer/post_processor_test.go | 11 +++++++++ 3 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 packer/post_processor_test.go diff --git a/packer/environment.go b/packer/environment.go index 35a5e5ecc..b0ee2579f 100644 --- a/packer/environment.go +++ b/packer/environment.go @@ -19,6 +19,9 @@ type CommandFunc func(name string) (Command, error) // The function type used to lookup Hook implementations. type HookFunc func(name string) (Hook, error) +// The function type used to lookup PostProcessor implementations. +type PostProcessorFunc func(name string) (PostProcessor, error) + // The function type used to lookup Provisioner implementations. type ProvisionerFunc func(name string) (Provisioner, error) @@ -26,10 +29,11 @@ type ProvisionerFunc func(name string) (Provisioner, error) // pointers necessary to look up components of Packer such as builders, // commands, etc. type ComponentFinder struct { - Builder BuilderFunc - Command CommandFunc - Hook HookFunc - Provisioner ProvisionerFunc + Builder BuilderFunc + Command CommandFunc + Hook HookFunc + PostProcessor PostProcessorFunc + Provisioner ProvisionerFunc } // The environment interface provides access to the configuration and @@ -42,6 +46,7 @@ type Environment interface { Cache() Cache Cli([]string) (int, error) Hook(string) (Hook, error) + PostProcessor(string) (PostProcessor, error) Provisioner(string) (Provisioner, error) Ui() Ui } @@ -104,6 +109,10 @@ func NewEnvironment(config *EnvironmentConfig) (resultEnv Environment, err error env.components.Hook = func(string) (Hook, error) { return nil, nil } } + if env.components.PostProcessor == nil { + env.components.PostProcessor = func(string) (PostProcessor, error) { return nil, nil } + } + if env.components.Provisioner == nil { env.components.Provisioner = func(string) (Provisioner, error) { return nil, nil } } @@ -152,6 +161,21 @@ func (e *coreEnvironment) Hook(name string) (h Hook, err error) { return } +// Returns a PostProcessor for the given name that is registered with this +// environment. +func (e *coreEnvironment) PostProcessor(name string) (p PostProcessor, err error) { + p, err = e.components.PostProcessor(name) + if err != nil { + return + } + + if p == nil { + err = fmt.Errorf("No post processor found for name: %s", name) + } + + return +} + // Returns a provisioner for the given name that is registered with this // environment. func (e *coreEnvironment) Provisioner(name string) (p Provisioner, err error) { diff --git a/packer/environment_test.go b/packer/environment_test.go index be9e7d28f..822f9bce8 100644 --- a/packer/environment_test.go +++ b/packer/environment_test.go @@ -67,6 +67,7 @@ func TestEnvironment_NilComponents(t *testing.T) { env.Builder("foo") env.Cli([]string{"foo"}) env.Hook("foo") + env.PostProcessor("foo") env.Provisioner("foo") } @@ -246,6 +247,47 @@ func TestEnvironment_Hook_Error(t *testing.T) { assert.Nil(returned, "should be no hook") } +func TestEnvironment_PostProcessor(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + pp := &TestPostProcessor{} + pps := make(map[string]PostProcessor) + pps["foo"] = pp + + config := DefaultEnvironmentConfig() + config.Components.PostProcessor = func(n string) (PostProcessor, error) { return pps[n], nil } + + env, _ := NewEnvironment(config) + returned, err := env.PostProcessor("foo") + assert.Nil(err, "should be no error") + assert.Equal(returned, pp, "should return correct pp") +} + +func TestEnvironment_PostProcessor_NilError(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + config := DefaultEnvironmentConfig() + config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, nil } + + env, _ := NewEnvironment(config) + returned, err := env.PostProcessor("foo") + assert.NotNil(err, "should be an error") + assert.Nil(returned, "should be no pp") +} + +func TestEnvironment_PostProcessor_Error(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + config := DefaultEnvironmentConfig() + config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, errors.New("foo") } + + env, _ := NewEnvironment(config) + returned, err := env.PostProcessor("foo") + assert.NotNil(err, "should be an error") + assert.Equal(err.Error(), "foo", "should be correct error") + assert.Nil(returned, "should be no pp") +} + func TestEnvironmentProvisioner(t *testing.T) { assert := asserts.NewTestingAsserts(t, true) diff --git a/packer/post_processor_test.go b/packer/post_processor_test.go new file mode 100644 index 000000000..8fc5e551a --- /dev/null +++ b/packer/post_processor_test.go @@ -0,0 +1,11 @@ +package packer + +type TestPostProcessor struct{} + +func (*TestPostProcessor) Configure(interface{}) error { + return nil +} + +func (*TestPostProcessor) PostProcess(Artifact) (Artifact, error) { + return nil, nil +}