From c9748b8e70a1f8fa43276d40089cae3ceefc64d0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 26 Jun 2013 19:09:24 -0700 Subject: [PATCH] post-processor/vagrant: the proper post-processor is actually run --- post-processor/vagrant/aws_test.go | 2 +- post-processor/vagrant/post-processor.go | 37 ++++++++++++++++++- post-processor/vagrant/post-processor_test.go | 29 +++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 post-processor/vagrant/post-processor_test.go diff --git a/post-processor/vagrant/aws_test.go b/post-processor/vagrant/aws_test.go index d7d5ce696..7c7d6fcc3 100644 --- a/post-processor/vagrant/aws_test.go +++ b/post-processor/vagrant/aws_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -func TestPostProcessor_ImplementsPostProcessor(t *testing.T) { +func TestAWSBoxPostProcessor_ImplementsPostProcessor(t *testing.T) { var raw interface{} raw = &AWSBoxPostProcessor{} if _, ok := raw.(packer.PostProcessor); !ok { diff --git a/post-processor/vagrant/post-processor.go b/post-processor/vagrant/post-processor.go index 2bcddb8fd..420973d0b 100644 --- a/post-processor/vagrant/post-processor.go +++ b/post-processor/vagrant/post-processor.go @@ -4,6 +4,8 @@ package vagrant import ( + "fmt" + "github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/packer" ) @@ -11,16 +13,47 @@ var builtins = map[string]string{ "mitchellh.amazonebs": "aws", } -type Config struct{} +type Config struct{ + OutputPath string `mapstructure:"output"` +} type PostProcessor struct { config Config } func (p *PostProcessor) Configure(raw interface{}) error { + err := mapstructure.Decode(raw, &p.config) + if err != nil { + return err + } + + if p.config.OutputPath == "" { + return fmt.Errorf("`output` must be specified.") + } + return nil } func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, error) { - return nil, nil + ppName, ok := builtins[artifact.BuilderId()] + if !ok { + return nil, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId()) + } + + // Get the actual PostProcessor implementation for this type + var pp packer.PostProcessor + switch ppName { + case "aws": + pp = new(AWSBoxPostProcessor) + default: + return nil, fmt.Errorf("Vagrant box post-processor not found: %s", ppName) + } + + // Prepare and run the post-processor + config := map[string]string{"output": p.config.OutputPath} + if err := pp.Configure(config); err != nil { + return nil, err + } + + return pp.PostProcess(ui, artifact) } diff --git a/post-processor/vagrant/post-processor_test.go b/post-processor/vagrant/post-processor_test.go new file mode 100644 index 000000000..da37b7b04 --- /dev/null +++ b/post-processor/vagrant/post-processor_test.go @@ -0,0 +1,29 @@ +package vagrant + +import ( + "github.com/mitchellh/packer/packer" + "testing" +) + +func testConfig() map[string]interface{} { + return map[string]interface{}{} +} + +func TestPostProcessor_ImplementsPostProcessor(t *testing.T) { + var raw interface{} + raw = &PostProcessor{} + if _, ok := raw.(packer.PostProcessor); !ok { + t.Fatalf("AWS PostProcessor should be a PostProcessor") + } +} + +func TestBuilderPrepare_OutputPath(t *testing.T) { + var p PostProcessor + + c := testConfig() + delete(c, "output") + err := p.Configure(c) + if err == nil { + t.Fatalf("configure should fail") + } +}