diff --git a/packer/artifact_mock.go b/packer/artifact_mock.go index ec88007f3..a59af5331 100644 --- a/packer/artifact_mock.go +++ b/packer/artifact_mock.go @@ -2,16 +2,26 @@ package packer // MockArtifact is an implementation of Artifact that can be used for tests. type MockArtifact struct { - IdValue string - DestroyCalled bool + BuilderIdValue string + FilesValue []string + IdValue string + DestroyCalled bool } -func (*MockArtifact) BuilderId() string { - return "bid" +func (a *MockArtifact) BuilderId() string { + if a.BuilderIdValue == "" { + return "bid" + } + + return a.BuilderIdValue } -func (*MockArtifact) Files() []string { - return []string{"a", "b"} +func (a *MockArtifact) Files() []string { + if a.FilesValue == nil { + return []string{"a", "b"} + } + + return a.FilesValue } func (a *MockArtifact) Id() string { diff --git a/post-processor/vagrant/post-processor_test.go b/post-processor/vagrant/post-processor_test.go index 9046b0cf0..552c7ba02 100644 --- a/post-processor/vagrant/post-processor_test.go +++ b/post-processor/vagrant/post-processor_test.go @@ -1,7 +1,10 @@ package vagrant import ( + "bytes" + "compress/flate" "github.com/mitchellh/packer/packer" + "strings" "testing" ) @@ -9,11 +12,53 @@ func testConfig() map[string]interface{} { return map[string]interface{}{} } +func testPP(t *testing.T) *PostProcessor { + var p PostProcessor + if err := p.Configure(testConfig()); err != nil { + t.Fatalf("err: %s", err) + } + + return &p +} + +func testUi() *packer.BasicUi { + return &packer.BasicUi{ + Reader: new(bytes.Buffer), + Writer: new(bytes.Buffer), + } +} + func TestPostProcessor_ImplementsPostProcessor(t *testing.T) { var _ packer.PostProcessor = new(PostProcessor) } -func TestBuilderPrepare_OutputPath(t *testing.T) { +func TestPostProcessorPrepare_compressionLevel(t *testing.T) { + var p PostProcessor + + // Default + c := testConfig() + delete(c, "compression_level") + if err := p.Configure(c); err != nil { + t.Fatalf("err: %s", err) + } + + if p.config.CompressionLevel != flate.DefaultCompression { + t.Fatalf("bad: %#v", p.config.CompressionLevel) + } + + // Set + c = testConfig() + c["compression_level"] = 7 + if err := p.Configure(c); err != nil { + t.Fatalf("err: %s", err) + } + + if p.config.CompressionLevel != 7 { + t.Fatalf("bad: %#v", p.config.CompressionLevel) + } +} + +func TestPostProcessorPrepare_outputPath(t *testing.T) { var p PostProcessor // Default @@ -32,6 +77,17 @@ func TestBuilderPrepare_OutputPath(t *testing.T) { } } +func TestPostProcessorPostProcess_badId(t *testing.T) { + artifact := &packer.MockArtifact{ + BuilderIdValue: "invalid.packer", + } + + _, _, err := testPP(t).PostProcess(testUi(), artifact) + if !strings.Contains(err.Error(), "artifact type") { + t.Fatalf("err: %s", err) + } +} + func TestProviderForName(t *testing.T) { if v, ok := providerForName("virtualbox").(*VBoxProvider); !ok { t.Fatalf("bad: %#v", v)