diff --git a/packer/artifact_test.go b/packer/artifact_test.go new file mode 100644 index 000000000..8051c6fe7 --- /dev/null +++ b/packer/artifact_test.go @@ -0,0 +1,19 @@ +package packer + +type TestArtifact struct{} + +func (*TestArtifact) BuilderId() string { + return "bid" +} + +func (*TestArtifact) Files() []string { + return []string{"a", "b"} +} + +func (*TestArtifact) Id() string { + return "id" +} + +func (*TestArtifact) String() string { + return "string" +} diff --git a/packer/build.go b/packer/build.go index 5d6ef5449..895969a0a 100644 --- a/packer/build.go +++ b/packer/build.go @@ -1,6 +1,7 @@ package packer import ( + "fmt" "log" "sync" ) @@ -151,9 +152,35 @@ func (b *coreBuild) Run(ui Ui, cache Cache) ([]Artifact, error) { hook := &DispatchHook{hooks} artifacts := make([]Artifact, 0, 1) - artifact, err := b.builder.Run(ui, hook, cache) - if artifact != nil { - artifacts = append(artifacts, artifact) + builderArtifact, err := b.builder.Run(ui, hook, cache) + if builderArtifact != nil { + artifacts = append(artifacts, builderArtifact) + } + + if err != nil { + return artifacts, err + } + + errors := make([]error, 0) + + // Run the post-processors +PostProcessorRunSeqLoop: + for _, ppSeq := range b.postProcessors { + artifact := builderArtifact + for _, corePP := range ppSeq { + var err error + artifact, err = corePP.processor.PostProcess(artifact) + if err != nil { + errors = append(errors, fmt.Errorf("Post-processor failed: %s", err)) + continue PostProcessorRunSeqLoop + } + + artifacts = append(artifacts, artifact) + } + } + + if len(errors) > 0 { + err = &MultiError{errors} } return artifacts, err diff --git a/packer/build_test.go b/packer/build_test.go index f06d4d613..4362359b7 100644 --- a/packer/build_test.go +++ b/packer/build_test.go @@ -107,7 +107,9 @@ func TestBuild_Run(t *testing.T) { build := testBuild() build.Prepare() - build.Run(ui, cache) + artifacts, err := build.Run(ui, cache) + assert.Nil(err, "should not error") + assert.Equal(len(artifacts), 2, "should have two artifacts") coreB := build.(*coreBuild) @@ -128,6 +130,10 @@ func TestBuild_Run(t *testing.T) { dispatchHook.Run(HookProvision, nil, nil, 42) prov := coreB.provisioners[0].provisioner.(*TestProvisioner) assert.True(prov.provCalled, "provision should be called") + + // Verify post-processor was run + pp := coreB.postProcessors[0][0].processor.(*TestPostProcessor) + assert.True(pp.ppCalled, "post processor should be called") } func TestBuild_RunBeforePrepare(t *testing.T) { diff --git a/packer/builder_test.go b/packer/builder_test.go index e3741d3c1..4ddb8d691 100644 --- a/packer/builder_test.go +++ b/packer/builder_test.go @@ -21,7 +21,7 @@ func (tb *TestBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) { tb.runHook = h tb.runUi = ui tb.runCache = c - return nil, nil + return new(TestArtifact), nil } func (tb *TestBuilder) Cancel() {