post-processor/vagrant: more tests

This commit is contained in:
Mitchell Hashimoto 2013-12-19 14:04:45 -08:00
parent 7b6bbbf42f
commit 6518c92e25
2 changed files with 73 additions and 7 deletions

View File

@ -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 {

View File

@ -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)